#include #include #include #include "TestHarness.hpp" #include "TestTempDir.hpp" #include "store/SessionStore.hpp" #include "store/SettingsStore.hpp" using namespace dp; namespace { void writeFile(const std::string& path, const std::string& text) { std::ofstream out(path, std::ios::binary); out << text; } } // namespace DP_TEST(settings_defaults_and_persistence) { test::TempDir dir; { SettingsStore s(dir.path()); CHECK(s.appearance() == AppearanceMode::Dark); CHECK(s.showFeedAttachments() && s.showFeedComments() && s.showReportButtons()); CHECK(s.recentEmojis().empty()); CHECK_EQ(s.draftPostTitle(), std::string("")); const auto v0 = s.version(); s.setAppearance(AppearanceMode::Light); s.setShowFeedComments(false); s.setDraftPostTitle("title"); s.setDraftPostMessage("body \xF0\x9F\x98\x84"); s.setDraftReplyMessage("reply"); CHECK_EQ(s.version(), v0 + 5); } SettingsStore again(dir.path()); CHECK(again.appearance() == AppearanceMode::Light); CHECK(again.showFeedAttachments()); CHECK(!again.showFeedComments()); CHECK_EQ(again.draftPostTitle(), std::string("title")); CHECK_EQ(again.draftPostMessage(), std::string("body \xF0\x9F\x98\x84")); CHECK_EQ(again.draftReplyMessage(), std::string("reply")); CHECK(!std::filesystem::exists(dir.path() + "settings.json.tmp")); // temp renamed away } DP_TEST(settings_recent_emojis_move_to_front_and_cap_30) { test::TempDir dir; SettingsStore s(dir.path()); for (int i = 0; i < 40; ++i) s.recordEmojiPick("e" + std::to_string(i)); CHECK_EQ(s.recentEmojis().size(), static_cast(30)); CHECK_EQ(s.recentEmojis()[0], std::string("e39")); s.recordEmojiPick("e20"); CHECK_EQ(s.recentEmojis()[0], std::string("e20")); CHECK_EQ(s.recentEmojis()[1], std::string("e39")); CHECK_EQ(s.recentEmojis().size(), static_cast(30)); SettingsStore again(dir.path()); CHECK_EQ(again.recentEmojis()[0], std::string("e20")); } DP_TEST(settings_bad_files_fall_back_loudly) { test::TempDir dir; writeFile(dir.path() + "settings.json", "{not json"); SettingsStore a(dir.path()); CHECK_EQ(a.rejectedLoads(), 1u); CHECK(a.appearance() == AppearanceMode::Dark); writeFile(dir.path() + "settings.json", std::string(2 * 1024 * 1024, 'x')); SettingsStore b(dir.path()); CHECK_EQ(b.rejectedLoads(), 1u); writeFile(dir.path() + "settings.json", R"({"appearance":"neon","showFeedAttachments":"yes","recentEmojis":[1,"a"],"draftPostTitle":5})"); SettingsStore c(dir.path()); CHECK(c.appearance() == AppearanceMode::Dark); // unknown raw -> dark CHECK(c.showFeedAttachments()); // wrong type -> default true CHECK_EQ(c.recentEmojis().size(), static_cast(1)); CHECK_EQ(c.draftPostTitle(), std::string("")); } DP_TEST(settings_observers_fire_and_unsubscribe) { test::TempDir dir; SettingsStore s(dir.path()); int calls = 0; const int id = s.subscribe([&] { ++calls; }); CHECK(id > 0); s.setShowReportButtons(false); CHECK_EQ(calls, 1); s.unsubscribe(id); s.setShowReportButtons(true); CHECK_EQ(calls, 1); } DP_TEST(session_store_persists_with_mode_0600) { test::TempDir dir; { SessionStore s(dir.path()); CHECK(!s.email().has_value()); CHECK(s.setEmail(std::string("a@b.c"))); CHECK(s.setPassword(std::string("p\"w/\xC3\xA9"))); } struct stat st{}; CHECK_EQ(::stat((dir.path() + "session.json").c_str(), &st), 0); CHECK_EQ(static_cast(st.st_mode & 0777), 0600u); SessionStore again(dir.path()); CHECK_EQ(*again.email(), std::string("a@b.c")); CHECK_EQ(*again.password(), std::string("p\"w/\xC3\xA9")); CHECK(again.setPassword(std::nullopt)); // nil removes the key SessionStore third(dir.path()); CHECK(third.email().has_value()); CHECK(!third.password().has_value()); CHECK(!std::filesystem::exists(dir.path() + "session.json.tmp")); } DP_TEST(session_store_rejects_oversize_and_unwritable) { test::TempDir dir; SessionStore s(dir.path()); CHECK(!s.setPassword(std::string(5000, 'x'))); CHECK(!s.password().has_value()); SessionStore broken(dir.path() + "does-not-exist/"); CHECK(!broken.setEmail(std::string("a@b.c"))); CHECK(!broken.email().has_value()); // rolled back, nothing vouched for }