#include "ModelSamples.hpp" #include "TestHarness.hpp" #include "model/CommentTree.hpp" #include "model/Feed.hpp" #include "model/PostDetail.hpp" using namespace dp; using dp::test::Samples; DP_TEST(reactions_toggling_adds_and_removes) { Reactions r; r = r.toggling("a"); CHECK_EQ(r.mine.size(), static_cast(1)); CHECK_EQ(r.counts.at("a"), 1); r = r.toggling("b"); r = r.toggling("a"); // remove, count hits 0 -> entry dropped CHECK_EQ(r.counts.count("a"), static_cast(0)); CHECK_EQ(r.mine.size(), static_cast(1)); CHECK_EQ(r.mine[0], std::string("b")); Reactions many; many.mine = {"x"}; many.counts = {{"x", 3}}; many = many.toggling("x"); CHECK_EQ(many.counts.at("x"), 2); CHECK(many.mine.empty()); } DP_TEST(comment_vote_update_moves_counters_recursively) { Comment root = Comment::fromJson(Json::parse(Samples::comment("c1", "[" + Samples::comment("c2") + "]"))); // sample comments: my_vote up, up=3 down=1 auto updated = CommentTree::updatingVote({root}, "c2", Vote(Vote::Down)); CHECK(updated[0].myVote == Vote::Up); // parent untouched CHECK(updated[0].children[0].myVote == Vote::Down); CHECK_EQ(updated[0].children[0].votes.up, 2); CHECK_EQ(updated[0].children[0].votes.down, 2); auto cleared = CommentTree::updatingVote({root}, "c1", Vote(Vote::None)); CHECK_EQ(cleared[0].votes.up, 2); CHECK_EQ(cleared[0].votes.down, 1); CHECK_EQ(cleared[0].children.size(), static_cast(1)); auto reacted = CommentTree::updatingReaction({root}, "c2", "fire"); CHECK(reacted[0].children[0].reactions.mine.empty()); // fire was mine: toggled off CHECK_EQ(reacted[0].children[0].reactions.counts.at("fire"), 1); CHECK_EQ(reacted[0].reactions.counts.at("fire"), 2); } DP_TEST(post_feed_and_detail_replacements) { Feed feed = Feed::fromJson(Json::parse(Samples::feed())); Post p = feed.posts[0].replacingReactions(feed.posts[0].reactions.toggling("new")); CHECK_EQ(p.reactions.counts.at("new"), 1); CHECK_EQ(p.data.id, feed.posts[0].data.id); Post q = p.replacingRecentComments({}); CHECK(q.recentComments.empty()); Feed f2 = feed.replacingPosts({q}); CHECK_EQ(f2.posts.size(), static_cast(1)); CHECK_EQ(f2.totalMembers, feed.totalMembers); PostDetail d = PostDetail::fromJson(Json::parse(Samples::postDetail())); PostDetail d2 = d.with(Vote(Vote::None), 3); CHECK(d2.myVote == Vote::None); CHECK_EQ(d2.starCount, 3); CHECK_EQ(d2.comments.size(), d.comments.size()); PostDetail d3 = d.with(std::nullopt, std::nullopt, std::vector{}); CHECK(d3.comments.empty()); CHECK_EQ(d3.starCount, 9); }