#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/create/CreatePostScreen.hpp" #include "store/AppState.hpp" #include "store/SessionStore.hpp" #include "store/SettingsStore.hpp" #include "ui/Overlays.hpp" using namespace dp; namespace { struct CreateScope { CreateScope() { static test::TempDir sessionDir; static test::TempDir settingsDir; SessionStore::instance().rebind(sessionDir.path()); SettingsStore::instance().rebind(settingsDir.path()); SettingsStore::instance().setDraftPostTitle(""); SettingsStore::instance().setDraftPostMessage(""); MockDevPlaceApi::shared().setDelayScale(0.0); AppContext::setApi(&MockDevPlaceApi::shared()); AppState::instance().clear(); bool done = false; MockDevPlaceApi::shared().logIn("a@b.c", "pw", [&](Result) { done = true; }); test::pumpUntil([&] { return done; }); ui::Overlays::instance().clear(); } ~CreateScope() { ui::Overlays::instance().clear(); } }; } // namespace DP_TEST(compose_create_validation_uses_utf16_units) { CreateScope scope; CreatePostScreen s; CHECK(!s.canSubmit()); // message below 10 s.setMessage("123456789"); CHECK(!s.canSubmit()); s.setMessage("1234567890"); CHECK(s.canSubmit()); s.setMessage("12345\xF0\x9F\x98\x80"); // 5 + 2 units = 7 CHECK(!s.canSubmit()); s.setMessage("1234567\xF0\x9F\x98\x80"); // 7 + 2 = 9 CHECK(!s.canSubmit()); s.setMessage("12345678\xF0\x9F\x98\x80"); // 10 CHECK(s.canSubmit()); s.setTitle(std::string(500, 'x')); CHECK(s.canSubmit()); s.setTitle(std::string(501, 'x')); CHECK(!s.canSubmit()); s.setTitle(std::string(250, '\xF0') .empty() ? "" : ""); s.setMessage(std::string(125001, 'm')); CHECK(!s.canSubmit()); } DP_TEST(compose_create_drafts_persist_only_in_create_mode) { CreateScope scope; { CreatePostScreen s; s.setTitle("draft title"); s.setMessage("draft message body"); } CHECK_EQ(SettingsStore::instance().draftPostTitle(), std::string("draft title")); CreatePostScreen again; CHECK_EQ(again.postTitle(), std::string("draft title")); CHECK_EQ(again.message(), std::string("draft message body")); CreatePostScreen edit(MockFixtures::postDetail()); CHECK(edit.isEditing()); edit.setTitle("edited"); CHECK_EQ(SettingsStore::instance().draftPostTitle(), std::string("draft title")); // untouched CHECK_EQ(edit.title(), std::string("Edit Post")); CHECK_EQ(again.title(), std::string("Create New Post")); CHECK_EQ(edit.message(), MockFixtures::postDetail().post.content); } DP_TEST(compose_create_poll_toggle_pads_two_options) { CreateScope scope; CreatePostScreen s; CHECK(!s.isPollAdded()); s.addPoll(); CHECK(s.isPollAdded()); CHECK_EQ(s.poll().options.size(), static_cast(2)); s.removePoll(); CHECK(!s.isPollAdded()); } DP_TEST(compose_create_submit_resets_fields_and_calls_back) { CreateScope scope; CreatePostScreen s; s.setTitle(" A title "); s.setMessage("A message that is long enough"); s.addPoll(); s.poll().question = "Q?"; bool completed = false; s.setOnComplete([&] { completed = true; }); s.submit(); CHECK(s.isLoading()); CHECK(test::pumpUntil([&] { return completed; }, 5.0)); CHECK(!s.isLoading()); CHECK(s.postTitle().empty()); CHECK(s.message().empty()); CHECK(!s.isPollAdded()); CHECK(SettingsStore::instance().draftPostTitle().empty()); CHECK(SettingsStore::instance().draftPostMessage().empty()); } DP_TEST(compose_create_submit_blocked_when_invalid) { CreateScope scope; CreatePostScreen s; s.setMessage("short"); s.submit(); CHECK(!s.isLoading()); } DP_TEST(compose_uploader_state_machine) { CreateScope scope; int changes = 0; AttachmentUploader::Options o; o.onAttachmentsChange = [&](const std::vector&) { ++changes; }; AttachmentUploader up(o); CHECK(!up.isUploading()); up.uploadFromUrl(" "); // blank: ignored CHECK(!up.isUploading()); up.uploadFromUrl(" https://example.com/cat.png "); CHECK(up.isUploading()); up.uploadFromUrl("https://example.com/second.png"); // guarded while busy CHECK(test::pumpUntil([&] { return !up.isUploading(); })); CHECK_EQ(up.attachments().size(), static_cast(1)); CHECK_EQ(up.attachments()[0].filename.value_or(""), std::string("cat.png")); CHECK_EQ(changes, 1); up.uploadFile("bytes", "clip.mp4", "video/mp4"); CHECK(test::pumpUntil([&] { return !up.isUploading(); })); CHECK_EQ(up.attachments().size(), static_cast(2)); CHECK(up.attachments()[1].isVideo); const UploadResponse first = up.attachments()[0]; up.remove(first); CHECK(up.isUploading()); CHECK(test::pumpUntil([&] { return !up.isUploading(); })); CHECK_EQ(up.attachments().size(), static_cast(1)); up.deleteUnsubmittedAttachments(); CHECK(up.attachments().empty()); CHECK_EQ(changes, 4); } DP_TEST(compose_uploader_caps_at_ten) { CreateScope scope; AttachmentUploader up; for (int i = 0; i < 12; ++i) { up.uploadFile("x", "f" + std::to_string(i) + ".png", "image/png"); test::pumpUntil([&] { return !up.isUploading(); }); } CHECK_EQ(up.attachments().size(), static_cast(10)); CHECK(!up.canAddMore()); }