#include "TestHarness.hpp" #include "TestPump.hpp" #include "TestTempDir.hpp" #include "api/mock/MockDevPlaceApi.hpp" #include "api/mock/MockFixtures.hpp" #include "screens/AppContext.hpp" #include "screens/components/MentionSuggestions.hpp" #include "store/AppState.hpp" #include "store/SessionStore.hpp" using namespace dp; namespace { struct ComposeMock { ComposeMock() { static test::TempDir dir; SessionStore::instance().rebind(dir.path()); MockDevPlaceApi::shared().setDelayScale(0.0); AppContext::setApi(&MockDevPlaceApi::shared()); AppState::instance().clear(); } }; } // namespace DP_TEST(compose_mention_token_detection) { auto t = MentionToken::active("hello @ali", std::nullopt); CHECK(t.has_value()); CHECK_EQ(t->query, std::string("ali")); CHECK_EQ(t->begin, static_cast(6)); CHECK_EQ(t->end, static_cast(10)); CHECK(!MentionToken::active("mail@ali", std::nullopt).has_value()); // '@' not at a word start CHECK(!MentionToken::active("@ali bob", std::nullopt).has_value()); // caret after a space auto start = MentionToken::active("@", std::nullopt); CHECK(start.has_value()); CHECK_EQ(start->query, std::string()); CHECK(MentionToken::active("a\n@x_1", std::nullopt).has_value()); // whitespace includes newline CHECK(!MentionToken::active("@bob!", std::nullopt).has_value()); // '!' is not a mention character CHECK(!MentionToken::active("", std::nullopt).has_value()); } DP_TEST(compose_mention_token_replaces_through_following_characters) { auto t = MentionToken::active("hi @al|ice more", 6); // caret after "@al", token continues over "|"? no: '|' stops CHECK(t.has_value()); CHECK_EQ(t->query, std::string("al")); CHECK_EQ(t->end, static_cast(6)); auto u = MentionToken::active("hi @alice more", 6); CHECK(u.has_value()); CHECK_EQ(u->query, std::string("al")); CHECK_EQ(u->end, static_cast(9)); // replaces the rest of the word } DP_TEST(compose_mention_complete_inserts_username_and_space) { auto c = MentionSuggestions::complete("hi @al", std::nullopt, "alice"); CHECK(c.has_value()); CHECK_EQ(c->text, std::string("hi @alice ")); CHECK_EQ(c->caret, 10); auto mid = MentionSuggestions::complete("hi @al more", 6, "alice"); CHECK(mid.has_value()); CHECK_EQ(mid->text, std::string("hi @alice more")); // followed by whitespace: no extra space CHECK_EQ(mid->caret, 10); CHECK(!MentionSuggestions::complete("no token", std::nullopt, "alice").has_value()); } DP_TEST(compose_mention_empty_query_shows_participants_without_current_user) { ComposeMock m; auto& api = MockDevPlaceApi::shared(); bool done = false; api.logIn("a@b.c", "pw", [&](Result) { done = true; }); CHECK(test::pumpUntil([&] { return done; })); UserSearch participants = MockFixtures::userSearch(); CHECK(participants.results.size() >= 2); participants.results.push_back({AppState::instance().currentUser()->id, "me", std::nullopt}); std::size_t others = 0; for (const auto& r : participants.results) others += AppState::instance().isCurrentUser(r.id) ? 0 : 1; MentionSuggestions s; s.update("hello @", std::nullopt, participants.results); CHECK(s.isActive()); CHECK_EQ(s.suggestions().size(), others); // current user filtered CHECK(s.isShowing()); CHECK(s.listHeight() >= 50 && s.listHeight() <= 164); s.update("hello", std::nullopt, participants.results); CHECK(!s.isActive()); CHECK(s.suggestions().empty()); CHECK_EQ(s.listHeight(), 0.0f); } DP_TEST(compose_mention_query_debounces_then_searches) { ComposeMock m; auto& api = MockDevPlaceApi::shared(); bool done = false; api.logIn("a@b.c", "pw", [&](Result) { done = true; }); CHECK(test::pumpUntil([&] { return done; })); MentionSuggestions s; s.update("@nu", std::nullopt, {}); CHECK(s.isActive()); CHECK(s.suggestions().empty()); // debounce: nothing yet CHECK(test::pumpUntil([&] { return !s.suggestions().empty(); }, 3.0)); for (const auto& r : s.suggestions()) CHECK(r.username.find("nu") != std::string::npos); s.dismiss(); CHECK(!s.isActive()); CHECK(s.suggestions().empty()); }