470 lines
20 KiB
C++
470 lines
20 KiB
C++
// SPDX-License-Identifier: GPL-3.0-only
|
|
#include "modern/ui/demo_news.hpp"
|
|
#include "modern/ui/desktop.hpp"
|
|
#include "modern/ui/model.hpp"
|
|
#include "modern/ui/news_catalog.hpp"
|
|
#include "modern/ui/news_journal.hpp"
|
|
#include <chrono>
|
|
#include <iostream>
|
|
#include <set>
|
|
#include <stdexcept>
|
|
#include <tuple>
|
|
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<const Catalog> 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<std::string>().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").empty() &&
|
|
demo_news_unread(changed) == 0,
|
|
"Other manager's club must not inherit or rename old club articles");
|
|
changed = s;
|
|
changed["managers"][0]["name"] = "Renamed Manager";
|
|
require(demo_news_view(*f.world, changed)
|
|
.at("items")[0]
|
|
.at("body")
|
|
.get<std::string>()
|
|
.find("Synthetic Manager. Synthetic Club 0") !=
|
|
std::string::npos,
|
|
"Historical owner text must never be rewritten by the current name");
|
|
auto national = s;
|
|
national["career"] = {{"role", "national"}, {"surface", "outdoor"}};
|
|
require(demo_news_view(*f.world, national).at("items").empty() &&
|
|
demo_news_unread(national) == 0 &&
|
|
demo_news_view(*f.world, national).at("club_name") ==
|
|
"Deutschland",
|
|
"National header must never expose club financial stories");
|
|
reject(national, {{"kind", "read"}, {"id", "season"}}, *f.world);
|
|
const auto national_before = national;
|
|
require(edit_demo_news(national, {{"kind", "read-all"}, {"folder", "inbox"}},
|
|
*f.world)
|
|
.empty() &&
|
|
national == national_before,
|
|
"National read-all must not alter hidden club rows");
|
|
auto legacy = s;
|
|
Json raw = Json::array();
|
|
for (const auto &item : s.at("news").at("items")) {
|
|
auto row = item;
|
|
row.erase("scope");
|
|
row.erase("owner");
|
|
raw.push_back(row);
|
|
}
|
|
raw[0]["body"] = "Hallo {{manager}}. {{club}}.";
|
|
legacy["news"] = {{"version", 1}, {"items", raw}};
|
|
const auto legacy_before = legacy;
|
|
require(demo_news_view(*f.world, legacy).at("items").empty() &&
|
|
legacy == legacy_before,
|
|
"Reading unbound v1 cannot guess owner or mutate state");
|
|
require(
|
|
migrate_demo_news(legacy, *f.world) &&
|
|
legacy.at("news").at("items").empty() &&
|
|
legacy.at("news").at("unbound") == raw &&
|
|
demo_news_view(*f.world, legacy).at("unbound_count") == 12,
|
|
"Ownerless old article facts remain intact in separate unbound storage");
|
|
const auto migrated = legacy;
|
|
require(migrate_demo_news(legacy, *f.world) && legacy == migrated,
|
|
"Conservative migration is idempotent");
|
|
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<Json>{
|
|
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<Json> bad;
|
|
auto invalid = initial;
|
|
invalid["news"]["items"].push_back(invalid["news"]["items"][0]);
|
|
bad.push_back(invalid);
|
|
for (const auto &[key, value] : std::vector<std::pair<std::string, Json>>{
|
|
{"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 < 501; ++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");
|
|
}
|
|
void accepted_start_identity(Fixture &f) {
|
|
DemoSource source(f.world);
|
|
Json options = {{"duration", 5}, {"difficulty", 3}, {"players", 1},
|
|
{"mode", 3}, {"league", true}, {"world", true}};
|
|
Json managers = Json::array({{{"name", "FreshCoach"},
|
|
{"club", "club:news-1"},
|
|
{"portrait", 4},
|
|
{"portrait_style", "painted"},
|
|
{"strength_choice", 1}}});
|
|
require(source
|
|
.apply({source.snapshot().revision,
|
|
"start",
|
|
{{"setup", options}, {"managers", managers}}})
|
|
.ok,
|
|
"Actual accepted new career start");
|
|
const auto original = source.snapshot().state.at("news");
|
|
require(original.at("items").size() == 12,
|
|
"Known club demo starts with existing journal fixtures");
|
|
for (const auto &row : original.at("items"))
|
|
require(row.at("scope").at("id") == "club:news-1" &&
|
|
row.at("owner").at("name") == "FreshCoach" &&
|
|
row.at("owner").at("id") == "portrait:4",
|
|
"New start freezes accepted person and nondefault club, never "
|
|
"fresh defaults");
|
|
require(original.at("items")[0].at("body").get<std::string>().find(
|
|
"FreshCoach. Synthetic Club 1") != std::string::npos,
|
|
"Actual start story uses confirmed identity");
|
|
managers[0]["name"] = "LaterName";
|
|
managers.push_back({{"name", "OtherCoach"},
|
|
{"club", "club:news-0"},
|
|
{"portrait", 1},
|
|
{"portrait_style", "photo"},
|
|
{"strength_choice", 1}});
|
|
require(source.apply({source.snapshot().revision, "managers", managers}).ok &&
|
|
source.snapshot().state.at("news") == original,
|
|
"Join/name updates cannot rewrite historical journal");
|
|
require(source.apply({source.snapshot().revision,
|
|
"career",
|
|
{{"kind", "role"}, {"value", "national"}}})
|
|
.ok &&
|
|
source.snapshot().state.at("news") == original &&
|
|
demo_news_unread(source.snapshot().state) == 0,
|
|
"Role change retains facts but hides club journal");
|
|
options["league"] = false;
|
|
managers = Json::array({managers[0]});
|
|
require(source.apply({source.snapshot().revision,
|
|
"start",
|
|
{{"setup", options}, {"managers", managers}}})
|
|
.ok &&
|
|
source.snapshot().state.at("news").at("items").empty(),
|
|
"National-only new start must not receive fresh club stories");
|
|
}
|
|
void write_fixtures(Fixture &f, const fs::path &out,
|
|
const fs::path &catalogue_path) {
|
|
fs::create_directories(out);
|
|
fs::copy(f.root / "world", out / "world",
|
|
fs::copy_options::recursive | fs::copy_options::overwrite_existing);
|
|
fs::copy_file(f.root / "manifest.json", out / "manifest.json",
|
|
fs::copy_options::overwrite_existing);
|
|
DemoSource source(f.world);
|
|
auto managers = source.snapshot().state.at("managers");
|
|
managers.push_back({{"name", "Mara König"},
|
|
{"club", "club:news-1"},
|
|
{"portrait", 1},
|
|
{"portrait_style", "painted"},
|
|
{"strength_choice", 1}});
|
|
require(source.apply({source.snapshot().revision, "managers", managers}).ok,
|
|
"Two-owner native scope fixture");
|
|
auto data = source.snapshot().state;
|
|
data["desktop"] = default_desktop();
|
|
const auto original = data;
|
|
const auto catalog = NewsCatalog::load(catalogue_path);
|
|
for (const auto &[template_id, scope, id] :
|
|
std::vector<std::tuple<std::string, Json, std::string>>{
|
|
{"injury-recovered",
|
|
{{"kind", "club"},
|
|
{"id", "club:news-0"},
|
|
{"name", "Synthetic Club 0"}},
|
|
"native:press:own"},
|
|
{"sponsor-signed",
|
|
{{"kind", "club"},
|
|
{"id", "club:news-1"},
|
|
{"name", "Synthetic Club 1"}},
|
|
"native:press:other"},
|
|
{"national-squad",
|
|
{{"kind", "national"},
|
|
{"id", "national-de"},
|
|
{"name", "Deutschland"},
|
|
{"names", {{"de", "Deutschland"}, {"en", "Germany"}}}},
|
|
"native:press:national"}}) {
|
|
const auto example = catalog.example(
|
|
template_id, {{"scope", scope}, {"date", data.at("date")}, {"id", id}});
|
|
require(example.value("ok", false),
|
|
"Explicit native catalogue demo fixture");
|
|
const auto built = catalog.from_event(example.at("event"));
|
|
require(built.value("ok", false), "Typed catalogue demo renders");
|
|
require(publish_news_article(data["news"], catalog, built.at("article"))
|
|
.value("ok", false),
|
|
"Demo fixture publication");
|
|
}
|
|
require(source.load_data(data) == default_desktop(), "Native fixture layout");
|
|
source.save(out / "scoped.json", default_desktop());
|
|
auto legacy = original;
|
|
legacy["news"] = {
|
|
{"version", 1},
|
|
{"items", Json::array({{{"id", "legacy-unknown"},
|
|
{"date", legacy.at("date")},
|
|
{"kind", "finance"},
|
|
{"title", "Altbestand {{club}}"},
|
|
{"body", "Unveränderte Meldung für {{manager}}. "
|
|
"Historischer Besitzer unbekannt."},
|
|
{"read", false},
|
|
{"folder", "inbox"}}})}};
|
|
// This intentionally old input is consumed by the normal native load path.
|
|
bmh::editor::write_atomic(out / "legacy.json", legacy.dump(2));
|
|
}
|
|
} // namespace
|
|
int main(int argc, char **argv) {
|
|
try {
|
|
Fixture f;
|
|
provider(f);
|
|
source(f);
|
|
accepted_start_identity(f);
|
|
if (argc == 4 && std::string(argv[1]) == "--write-fixtures")
|
|
write_fixtures(f, argv[2], argv[3]);
|
|
std::cout << "modern news UI tests passed\n";
|
|
return 0;
|
|
} catch (const std::exception &e) {
|
|
std::cerr << e.what() << '\n';
|
|
return 1;
|
|
}
|
|
}
|