// SPDX-License-Identifier: GPL-3.0-only #include "modern/ui/demo_news.hpp" #include "modern/ui/desktop.hpp" #include "modern/ui/model.hpp" #include #include #include #include using namespace bmh::modern_ui; namespace { void require(bool yes, const char *why) { if (!yes) throw std::runtime_error(why); } struct Fixture { fs::path root; std::shared_ptr world; Fixture() { root = fs::temp_directory_path() / ("openbmh-news-ui-" + std::to_string( std::chrono::steady_clock::now().time_since_epoch().count())); fs::create_directories(root / "world"); const auto write = [&](const std::string &name, const Json &data) { bmh::editor::write_atomic(root / name, data.dump()); }; write("manifest.json", {{"format", "openbmh-world"}, {"kind", "world"}, {"format_version", "1.1"}, {"id", "world:synthetic-news"}, {"start_date", "2032-07-01"}, {"requires", {"editor:world-v1"}}, {"x-editor", {{"profile", "editor-world-v1"}}}}); write("world/nations.json", {{"items", {{{"id", "nation:de"}, {"name", "Synthetic nation"}}}}}); write("world/competitions.json", {{"items", {{{"id", "competition:news-test"}, {"type", "league"}, {"nation", "nation:de"}, {"name", "Synthetic league"}}}}}); Json clubs = Json::array(); for (int i = 0; i < 2; ++i) clubs.push_back({{"id", "club:news-" + std::to_string(i)}, {"name", "Synthetic Club " + std::to_string(i)}, {"nation", "nation:de"}, {"competition", "competition:news-test"}}); write("world/clubs.json", {{"items", clubs}}); write("world/players.json", {{"items", Json::array()}}); world = Catalog::open(root); } ~Fixture() { std::error_code ec; fs::remove_all(root, ec); } }; Json state() { return { {"date", "2032-03-01"}, {"managers", {{{"name", "Synthetic Manager"}, {"club", "club:news-0"}}}}, {"cash", 123456}, {"other", {{"kept", true}}}}; } void reject(Json &s, const Json &a, const Catalog &w) { const auto before = s; require(!edit_demo_news(s, a, w).empty() && s == before, "Rejected news action altered state"); } void provider(Fixture &f) { auto s = state(); const auto unrelated = s, clubs = f.world->rows("clubs"); require(migrate_demo_news(s, *f.world) && valid_demo_news(s), "Old checkpoint migration failed"); const auto initial = s; require(migrate_demo_news(s, *f.world) && s == initial, "Existing mailbox was reinitialized"); auto rest = s; rest.erase("news"); require(rest == unrelated, "News migration changed unrelated fields"); const auto view = demo_news_view(*f.world, s); require(view.at("items").size() == 9 && view.at("counts").at("inbox") == 9 && view.at("counts").at("archive") == 3 && view.at("unread") == 5, "Initial mailbox counts wrong"); require(view.at("date") == "01.03.2032" && s.at("news").at("items")[2].at("date") == "2032-02-29" && s.at("news").at("items")[11].at("date") == "2032-02-18", "Relative fixture dates or leap day wrong"); require(view.at("items")[0].at("body").get().find( "Synthetic Manager. Synthetic Club 0") != std::string::npos, "Current manager and club not interpolated"); auto changed = s; changed["managers"][0] = {{"name", "Other Manager"}, {"club", "club:news-1"}}; require(demo_news_view(*f.world, changed) .at("items")[0] .at("body") .get() .find("Other Manager. Synthetic Club 1") != std::string::npos, "Manager change retained stale template identity"); require(demo_news_view(*f.world, s, "inbox", "FINANZEN").at("items").size() == 2 && demo_news_view(*f.world, s, "inbox", "VERTRAGSGESPRÄCHE") .at("items") .size() == 1, "German case-insensitive category/title search failed"); require(demo_news_view(*f.world, s, "inbox", "unmatchable-search") .at("items") .empty() && demo_news_view(*f.world, s, "inbox", "01.03.2032") .at("items") .size() == 2, "Search empty/date projection failed"); require(s == initial && f.world->rows("clubs") == clubs, "Read-only projection mutated state or catalogue"); require( edit_demo_news(s, {{"kind", "read-all"}, {"folder", "inbox"}}, *f.world) .empty() && demo_news_unread(s) == 0, "Read-all must mark the entire folder, including hidden results"); s = initial; require(edit_demo_news(s, {{"kind", "read"}, {"id", "season"}}, *f.world) .empty() && demo_news_unread(s) == 4, "Open read action failed"); s = initial; require(edit_demo_news( s, {{"kind", "move"}, {"id", "season"}, {"folder", "archive"}}, *f.world) .empty() && demo_news_unread(s) == 4, "Archive must also mark read"); const Json patch = Json::array({{{"id", "season"}, {"before", {{"read", false}, {"folder", "inbox"}}}, {"after", {{"read", true}, {"folder", "archive"}}}}}); const auto archived = s; require(edit_demo_news(s, {{"kind", "undo"}, {"patch", patch}}, *f.world) .empty() && s == initial, "Undo failed to restore the small read/folder patch"); reject(s, {{"kind", "undo"}, {"patch", patch}}, *f.world); s = archived; require(edit_demo_news( s, {{"kind", "move"}, {"id", "season"}, {"folder", "inbox"}}, *f.world) .empty() && s.at("news").at("items")[0].at("read") == true, "Restore must retain read state"); require(edit_demo_news( s, {{"kind", "move"}, {"id", "season"}, {"folder", "trash"}}, *f.world) .empty() && demo_news_view(*f.world, s).at("counts").at("inbox") == 8, "Delete did not remove item from visible folders"); reject(s, {{"kind", "read"}, {"id", "season"}}, *f.world); s = archived; auto multi = patch; multi.push_back({{"id", "missing"}, {"before", {{"read", false}, {"folder", "inbox"}}}, {"after", {{"read", true}, {"folder", "inbox"}}}}); reject(s, {{"kind", "undo"}, {"patch", multi}}, *f.world); multi = patch; multi.push_back(patch[0]); reject(s, {{"kind", "undo"}, {"patch", multi}}, *f.world); for (const auto &a : std::vector{ nullptr, Json::array(), Json{{"kind", 4}}, Json{{"kind", "move"}, {"id", "season"}, {"folder", "lost"}}, Json{{"kind", "read"}, {"id", 4}}, Json{{"kind", "read"}, {"id", "season"}, {"unexpected", true}}, Json{{"kind", "read-all"}, {"folder", "trash"}}, Json{{"kind", "undo"}, {"patch", Json::array()}}, Json{{"kind", "unknown"}}}) reject(s, a, *f.world); std::vector bad; auto invalid = initial; invalid["news"]["items"].push_back(invalid["news"]["items"][0]); bad.push_back(invalid); for (const auto &[key, value] : std::vector>{ {"id", "BAD-ID"}, {"id", ""}, {"date", "2031-02-29"}, {"date", "2032-13-01"}, {"kind", 4}, {"kind", "unsupported"}, {"title", " "}, {"title", std::string(161, 'x')}, {"body", std::string(1201, 'x')}, {"read", 1}, {"folder", "removed"}}) { invalid = initial; invalid["news"]["items"][0][key] = value; bad.push_back(invalid); } invalid = initial; invalid["news"]["version"] = 1.0; bad.push_back(invalid); invalid = initial; invalid["news"]["query"] = "must-be-local"; bad.push_back(invalid); invalid = initial; for (int i = 12; i < 101; ++i) { auto m = invalid["news"]["items"][0]; m["id"] = "test-" + std::to_string(i); invalid["news"]["items"].push_back(m); } bad.push_back(invalid); for (auto invalid : bad) { const auto before = invalid; require(!valid_demo_news(invalid) && !migrate_demo_news(invalid, *f.world) && invalid == before, "Malformed imported mailbox was repaired or accepted"); reject(invalid, {{"kind", "read-all"}, {"folder", "inbox"}}, *f.world); } auto empty = initial; empty["news"]["items"] = Json::array(); require(valid_demo_news(empty) && demo_news_view(*f.world, empty).at("items").empty() && demo_news_unread(empty) == 0, "Valid empty mailbox rejected"); } void source(Fixture &f) { DemoSource source(f.world); auto initial = source.snapshot(); require(valid_demo_news(initial.state) && !source.dirty(), "Default source mailbox missing or dirty"); const auto clean = initial.state; demo_news_view(*f.world, initial.state, "archive", "search"); require(source.snapshot().revision == initial.revision && !source.dirty(), "Navigation dirtied source"); require( source .apply( {initial.revision, "news", {{"kind", "read"}, {"id", "season"}}}) .ok, "Source rejected news action"); auto current = source.snapshot(); require(source.dirty() && current.revision == initial.revision + 1, "News action did not advance source revision"); require( !source.apply({initial.revision, "news", {{"kind", "move"}, {"id", "season"}, {"folder", "trash"}}}) .ok && source.snapshot().state == current.state, "Stale news action changed source"); require(!source.apply({current.revision, "news", {{"kind", "move"}, {"id", "season"}, {"folder", 9}}}) .ok && source.snapshot().state == current.state && source.snapshot().revision == current.revision, "Malformed source action was not atomic"); const auto save = f.root / "checkpoint.json"; source.save(save, default_desktop()); require(!source.dirty(), "Saving news did not clear dirty"); require( source .apply({source.snapshot().revision, "news", {{"kind", "move"}, {"id", "season"}, {"folder", "trash"}}}) .ok, "Delete source action failed"); source.load(save); require(source.snapshot().state.at("news") == current.state.at("news") && !source.dirty(), "Saved mailbox did not reload"); auto data = Json::parse(bmh::editor::read_file(save)); data.erase("news"); bmh::editor::write_atomic(save, data.dump()); source.load(save); require(valid_demo_news(source.snapshot().state) && demo_news_unread(source.snapshot().state) == 5 && !source.dirty(), "Old checkpoint did not initialize clean news"); source.save(save, default_desktop()); data = Json::parse(bmh::editor::read_file(save)); data["news"]["items"][0]["kind"] = 44; bmh::editor::write_atomic(save, data.dump()); const auto before = source.snapshot(); bool rejected = false; try { source.load(save); } catch (const std::exception &) { rejected = true; } require(rejected && source.snapshot().state == before.state && source.snapshot().revision == before.revision && !source.dirty(), "Malformed checkpoint replaced state or advanced revision"); auto rest = before.state; rest.erase("news"); auto base = clean; base.erase("news"); require(rest == base, "Mailbox actions modified unrelated presentation data"); } } // namespace int main() { try { Fixture f; provider(f); source(f); std::cout << "modern news UI tests passed\n"; return 0; } catch (const std::exception &e) { std::cerr << e.what() << '\n'; return 1; } }