#include "SocialTestSupport.hpp" #include "screens/notifications/NotificationRoute.hpp" #include "screens/notifications/NotificationsModel.hpp" #include "screens/notifications/UrlSyntax.hpp" using namespace dp; using namespace dp::test; namespace { Notification note(const std::string& id, std::optional type, bool read = false, bool actor = true) { Notification n; n.data.id = id; n.data.type = type; n.data.message = "m " + id; n.data.read = read; n.data.relatedId = "rel-" + id; n.data.createdAt = DateCodec::now(); if (actor) n.actor = MockFixtures::alice(); return n; } } // namespace DP_TEST(social_notification_route_priority_order) { using K = NotificationRoute::Kind; auto r = NotificationRouting::route(note("1", NotificationType::Message), "/posts/ignored"); CHECK(r && r->kind == K::Conversation && r->user.username == "alice"); // message without actor falls through to the redirect r = NotificationRouting::route(note("2", NotificationType::Message, false, false), "/posts/p-slug"); CHECK(r && r->kind == K::Post && r->post.slug == "p-slug"); r = NotificationRouting::route(note("3", NotificationType::Badge), "/posts/x"); CHECK(r && r->kind == K::OwnProfile); r = NotificationRouting::route(note("4", NotificationType::Issue), "/posts/x"); CHECK(r && r->kind == K::Web && r->url == "https://devplace.net/issues/rel-4"); r = NotificationRouting::route(note("5", NotificationType::Comment), "/profile/bob"); CHECK(!r); // no posts segment, no navigation r = NotificationRouting::route(note("6", std::nullopt), "/posts/s"); CHECK(r && r->kind == K::Post); // unknown type goes through the redirect } DP_TEST(social_notification_redirect_parsing_slug_and_comment) { auto d = NotificationRouting::postDestination("https://devplace.net/posts/my-slug"); CHECK(d && d->slug == "my-slug" && !d->scrollToCommentId); d = NotificationRouting::postDestination("/posts/s#comment-abc"); CHECK(d && d->slug == "s" && d->scrollToCommentId == std::optional("comment:abc")); d = NotificationRouting::postDestination("/posts/s#comment_abc"); CHECK(d && d->scrollToCommentId == std::optional("comment:abc")); d = NotificationRouting::postDestination("/posts/s#comment:abc"); CHECK(d && d->scrollToCommentId == std::optional("comment:abc")); d = NotificationRouting::postDestination("/posts/s#commentabc"); CHECK(d && d->scrollToCommentId == std::optional("comment:abc")); d = NotificationRouting::postDestination("/posts/s#comment-"); // empty uid after the prefix CHECK(d && !d->scrollToCommentId); d = NotificationRouting::postDestination("/posts/s?comment_id=q1"); CHECK(d && d->scrollToCommentId == std::optional("comment:q1")); d = NotificationRouting::postDestination("/posts/s?x=1&commentId=q2"); CHECK(d && d->scrollToCommentId == std::optional("comment:q2")); d = NotificationRouting::postDestination("/posts/s?comment="); // empty value CHECK(d && !d->scrollToCommentId); d = NotificationRouting::postDestination("/posts/s/comments/c9"); CHECK(d && d->scrollToCommentId == std::optional("comment:c9")); d = NotificationRouting::postDestination("/posts/s#other?comment=q3"); CHECK(d && !d->scrollToCommentId); // '?' after '#' belongs to the fragment CHECK(!NotificationRouting::postDestination("/posts/")); CHECK(!NotificationRouting::postDestination("/posts")); CHECK(!NotificationRouting::postDestination("not a url /posts/x")); } DP_TEST(social_url_syntax_validity) { CHECK(UrlSyntax::valid("https://github.com/u")); CHECK(!UrlSyntax::valid("")); CHECK(!UrlSyntax::valid("has space")); CHECK_EQ(UrlSyntax::percentDecode("a%20b%2"), std::string("a b%2")); } DP_TEST(social_notification_merge_groups_dedupe_and_cursor) { Notifications a, b; a.groups = {{"Today", {note("1", NotificationType::Comment), note("2", NotificationType::Reply)}}}; b.groups = {{"Today", {note("2", NotificationType::Reply), note("3", NotificationType::Vote)}}, {"Last week", {note("4", NotificationType::Mention), note("1", NotificationType::Comment)}}}; b.nextCursor = DateCodec::now(); std::vector dup; Notifications m = NotificationsModel::merged(a, b, &dup); CHECK_EQ(m.groups.size(), static_cast(2)); CHECK_EQ(m.groups[0].entries.size(), static_cast(3)); // 1, 2, 3 CHECK_EQ(m.groups[1].entries.size(), static_cast(1)); // 4 CHECK_EQ(dup.size(), static_cast(2)); CHECK(m.nextCursor.has_value()); // a group that only holds duplicates disappears Notifications c; c.groups = {{"Dupes", {note("1", NotificationType::Comment)}}}; Notifications m2 = NotificationsModel::merged(a, c); CHECK_EQ(m2.groups.size(), static_cast(1)); CHECK(!m2.nextCursor.has_value()); } DP_TEST(social_notification_apply_read) { Notifications a; a.groups = {{"Today", {note("1", NotificationType::Comment), note("2", NotificationType::Reply, true)}}}; Notifications r = NotificationsModel::applyingRead(a, {"1"}); CHECK(r.groups[0].entries[0].data.read && r.groups[0].entries[1].data.read); CHECK(!a.groups[0].entries[0].data.read); } DP_TEST(social_notification_model_load_open_and_mark_all_read) { SignedInMock mock; AlertLog log; NotificationsModel model(mock.api(), log.sink()); bool done = false; model.load([&] { done = true; }); CHECK(pumpUntil([&] { return done; })); CHECK(model.notifications().has_value()); CHECK(!model.groups().empty()); const Notification first = model.groups()[0].entries[0]; std::optional route; bool opened = false; model.open(first, [&](std::optional r) { route = r; opened = true; }); CHECK(pumpUntil([&] { return opened; })); CHECK(log.alerts.empty()); model.markAllRead(); for (const auto& g : model.groups()) { for (const auto& e : g.entries) CHECK(e.data.read); } CHECK(pumpUntil([&] { return !model.isBusy(); })); } DP_TEST(social_notification_logged_out_clears_list) { SignedInMock mock; AlertLog log; NotificationsModel model(mock.api(), log.sink()); bool done = false; model.load([&] { done = true; }); CHECK(pumpUntil([&] { return done; })); AppState::instance().clear(); done = false; model.load([&] { done = true; }); CHECK(done); CHECK(!model.notifications().has_value()); }