Files
Dreierpack-Manager/tests/modern_live_ui_tests.cpp
T

234 lines
16 KiB
C++

// SPDX-License-Identifier: GPL-3.0-only
#include "modern/ui/demo_live.hpp"
#include "modern/ui/demo_career.hpp"
#include "modern/ui/desktop.hpp"
#include "modern/ui/model.hpp"
#include <iostream>
#include <stdexcept>
using namespace bmh::modern_ui;
namespace {
void require(bool yes, const char *message) {
if (!yes) throw std::runtime_error(message);
}
struct Fixture {
fs::path root;
std::shared_ptr<const Catalog> world;
Fixture() : root(fs::temp_directory_path() / bmh::editor::new_id("openbmh-live-test-")) {
fs::create_directories(root / "world");
const auto write = [&](const char *path, const Json &data) {
bmh::editor::write_atomic(root / path, data.dump());
};
write("manifest.json", {{"format", "openbmh-world"}, {"kind", "world"},
{"format_version", "1.1"}, {"id", "world:synthetic-live"},
{"start_date", "2032-07-01"}, {"requires", {"editor:world-v1"}},
{"x-editor", {{"profile", "editor-world-v1"}}}});
write("world/nations.json", {{"items", {{{"id", "nation:de"}, {"name", "Deutschland"}},
{{"id", "nation:es"}, {"name", "Spanien"}}}}});
write("world/competitions.json", {{"items", {{{"id", "competition:de"}, {"name", "Demo-Liga"},
{"nation", "nation:de"}, {"type", "league"}}}}});
Json clubs = Json::array(), players = Json::array();
for (int team = 0; team < 8; ++team) {
const auto id = "club:live-" + std::to_string(team);
clubs.push_back({{"id", id}, {"name", "Demo-Verein " + std::to_string(team)},
{"nation", "nation:de"}, {"competition", "competition:de"}});
for (int n = 0; n < 18; ++n)
players.push_back({{"id", "player:live-" + std::to_string(team) + "-" + std::to_string(n)},
{"name", "Spieler " + std::to_string(team) + "-" + std::to_string(n)},
{"team", id}, {"squad_order", n}, {"nation", "nation:de"},
{"positions", Json::array({n < 2 ? "gk" : n < 7 ? "def" : n < 13 ? "mid" : "fwd"})},
{"attributes", n == 0 ? Json{{"strength", 73}, {"caps", 4}} : Json::object()}});
}
write("world/clubs.json", {{"items", clubs}});
write("world/players.json", {{"items", players}});
world = Catalog::open(root);
}
~Fixture() { std::error_code error; fs::remove_all(root, error); }
};
Json start(const std::vector<int> &clubs) {
Json managers = Json::array(); int index = 0;
for (int club : clubs) {
managers.push_back({{"name", "Manager " + std::to_string(index)},
{"club", "club:live-" + std::to_string(club)}, {"portrait", index},
{"portrait_style", "photo"}, {"strength_choice", 1}});
++index;
}
return {{"setup", {{"duration", 5}, {"difficulty", 3}, {"players", managers.size()},
{"mode", 3}, {"league", true}, {"world", true}}}, {"managers", managers}};
}
Json fixture(DemoSource &source) {
const auto snapshot = source.snapshot();
return make_demo_live(*snapshot.world, snapshot.state, snapshot.revision);
}
} // namespace
int main(int argc, char **argv) {
try {
struct UnavailableSource final : DataSource {
Snapshot snapshot() const override { return {}; }
ActionResult apply(const Action &) override { return {false, 0, "unavailable"}; }
} unavailable;
require(unavailable.read_live(Json::object()).at("error") == "live_unavailable", "unbound authoritative provider cannot invent live data");
Fixture f;
DemoSource source(f.world);
require(source.apply({0, "start", start({0,1,2,3})}).ok, "four-human presentation start");
const auto initial = source.snapshot(); const bool dirty = source.dirty();
const auto catalog_before = f.world->summary();
const auto live = fixture(source);
require(valid_live_fixture(live), "well-formed finite authored recording");
require(live.at("source") == "ui-demo-fixture" && live.at("source_revision") == initial.revision,
"fixture provenance separate from source revision");
require(live.at("owners").size() == 4 && live.at("matches").size() == 2,
"opposing human owners share one match card");
require(live.at("matches")[0].at("owners").size() == 2 &&
live.at("owners")[0].at("match_id") == live.at("owners")[1].at("match_id"),
"owner IDs remain separate from match ID");
require(live.at("frames")[1].at("matches")[0].at("standings") ==
live.at("frames")[1].at("matches")[1].at("standings"),
"owners in the same catalogue competition share one declared table snapshot");
require(live == fixture(source), "repeat reads deterministic");
require(source.read_live({{"kind", "fixture"}}) == live, "DemoSource explicitly overrides read-only live hook");
auto another_date = initial.state; another_date["date"] = "2032-07-02";
const auto other_day = make_demo_live(*f.world, another_date, initial.revision);
require(other_day.at("fixture_id") != live.at("fixture_id") && other_day.at("matches")[0].at("id") != live.at("matches")[0].at("id"),
"match/event identity separates worlds/dates from owner identity");
for (int i = 0; i < 25; ++i) {
require(query_demo_live(*f.world, initial.state, initial.revision, {{"kind", "fixture"}}) == live,
"read query deterministic");
const auto frame = query_demo_live(*f.world, initial.state, initial.revision,
{{"kind", "frame"}, {"sequence", i % 9}, {"fixture_id", live.at("fixture_id")}});
require(frame.value("ok", false) && frame.at("sequence") == i % 9, "explicit recorded sequence read");
}
require(source.snapshot().state == initial.state && source.snapshot().revision == initial.revision &&
source.dirty() == dirty && f.world->summary() == catalog_before,
"reads preserve source state, revision, dirty baseline and catalogue");
require(!query_demo_live(*f.world, initial.state, initial.revision, {{"revision", initial.revision + 1}}).at("ok"), "stale revision rejected");
for (const auto &query : Json::array({Json{{"kind", "tick"}}, Json{{"sequence", 1}},
Json{{"kind", "frame"}, {"sequence", -1}},
Json{{"kind", "frame"}, {"sequence", 9}},
Json{{"kind", "frame"}, {"sequence", 0.5}},
Json{{"kind", "fixture"}, {"owner", "manager:0"}},
Json{{"fixture_id", "wrong"}}}))
require(!query_demo_live(*f.world, initial.state, initial.revision, query).at("ok"), "unsupported/stale request rejected");
LivePlayback playback;
require(playback.open(live, "manager:0"), "recording opens paused");
require(playback.sequence() == 0 && playback.speed() == 0 && !playback.elapse(100000),
"pause never advances recording");
require(playback.next() && playback.frame().at("minute") == 12, "next reveals predetermined goal snapshot");
const auto before_owner = playback.frame(); const auto status = playback.status();
require(playback.cycle_owner(1) && playback.owner() == "manager:1" && playback.frame() == before_owner,
"manager arrow cannot change match clock, score or common ticker");
require(playback.owner("manager:3") && playback.frame() == before_owner,
"select another match owner does not filter/seek left recording");
const auto selected = playback.status();
require(!playback.owner("manager:unknown") && playback.status() == selected, "unknown owner rejected without mutation");
require(!playback.speed(-1) && !playback.speed(5), "speed bounds");
require(playback.speed(4) && !playback.elapse(399) && playback.elapse(1) && playback.sequence() == 2,
"elapsed time reveals exactly next authored snapshot");
require(playback.elapse(1000000) && playback.sequence() == 3,
"delayed window cannot replay unbounded timer backlog");
require(playback.speed(0) && !playback.elapse(1000000) && playback.frame().at("phase") == "interval",
"pause is only playback state");
while (playback.next()) {}
require(playback.finished() && playback.frame().at("minute") == 90 && playback.speed() == 0 &&
!playback.next() && !playback.speed(1), "outdoor recording ends exactly at90 without season progression");
require(source.snapshot().state == initial.state && source.snapshot().revision == initial.revision && source.dirty() == dirty,
"playback has no source/session side effects");
(void)status;
const auto ended_status = playback.status(); auto invalid = live; invalid["source"] = "pretend-engine";
require(!playback.open(invalid) && playback.status() == ended_status, "reject fictional authority without replacing active recording");
for (int failure = 0; failure < 5; ++failure) {
invalid = live;
if (failure == 0) invalid["matches"][1]["id"] = invalid["matches"][0]["id"];
if (failure == 1) invalid["frames"][2]["sequence"] = 7;
if (failure == 2) invalid["frames"][2]["events"][0]["match_id"] = "wrong";
if (failure == 3) invalid["frames"][2]["matches"][0]["score"][0] = -1;
if (failure == 4) invalid["owners"][0]["match_id"] = "wrong";
require(!valid_live_fixture(invalid), "malformed external recording rejected");
}
require(source.apply({source.snapshot().revision, "career", {{"kind", "surface"}, {"value", "indoor"}}}).ok,
"choose existing indoor presentation board");
auto hall = fixture(source);
require(valid_live_fixture(hall) && hall.at("duration") == 40 && hall.at("field_count") == 6,
"indoor scope duration and field count");
for (const auto &owner : hall.at("owners"))
require(owner.at("view").at("rows").size() == 18 && owner.at("view").at("on_field_limit") == 6 &&
owner.at("view").at("rotation_rows") == 11 && owner.at("view").at("roster_size") == 18 &&
owner.at("view").at("roster_limit").is_null() &&
owner.at("view").at("board").at("points").size() == 11,
"hall separates six active, five rotation partners and all further reserves without invented roster limit");
require(playback.open(hall), "indoor recording opens"); while (playback.next()) {}
require(playback.frame().at("minute") == 40 && playback.finished(), "indoor recording ends exactly at40");
require(source.apply({source.snapshot().revision, "career", {{"kind", "role"}, {"value", "national"}}}).ok,
"choose existing national presentation role");
const auto national = fixture(source);
require(valid_live_fixture(national) && national.at("owners").size() == 1 &&
national.at("owners")[0].at("team").at("id") == "national-de" &&
national.at("owners")[0].at("view").at("national_demo_basis") == true,
"national identity and explicit demonstration squad basis");
require(national.at("frames")[0].at("matches")[0].at("standings").empty(),
"national scope has no invented club league table");
require(national.at("owners")[0].at("view").at("rows")[0].at("caps") == 4 &&
national.at("owners")[0].at("view").at("rows")[1].at("caps").is_null(),
"caps use only catalogue values; unknown is never fabricated");
auto bad_scope = source.snapshot().state; bad_scope["career"]["surface"] = "indoor";
require(!make_demo_live(*f.world, bad_scope, 0).at("ok"), "national indoor rejected");
bad_scope["career"]["surface"] = "outdoor"; bad_scope["career"]["ended"] = true;
require(!make_demo_live(*f.world, bad_scope, 0).at("ok"), "ended career cannot launch recording");
DemoSource four(f.world);
require(four.apply({0, "start", start({0,2,4,6})}).ok, "four distinct human matches start");
require(four.apply({four.snapshot().revision, "tactics", {{"kind", "cycle"}, {"index", 0}}}).ok,
"owner zero has a real presentation tactic edit");
const auto owner_zero_tactics = four.snapshot().state.at("tactics");
const auto old_order = four.snapshot().state.at("lineup").at("order");
require(four.apply({four.snapshot().revision, "lineup", {{"kind", "swap"}, {"a", old_order[1]}, {"b", old_order[4]}}}).ok,
"owner zero has a real presentation lineup edit");
const auto owner_zero_board = four.snapshot().state.at("lineup");
const auto four_live = fixture(four);
require(valid_live_fixture(four_live) && four_live.at("matches").size() == 4, "one card per unique human match, up tofour");
require(four_live.at("owners")[0].at("view").at("tactics") == owner_zero_tactics &&
four_live.at("owners")[0].at("view").at("board") == owner_zero_board &&
four_live.at("owners")[1].at("view").at("board").at("club") == "club:live-2",
"recording reads actual edited owner board and separate unvisited owner defaults");
const auto live_id = four_live.at("fixture_id");
require(four.apply({four.snapshot().revision, "career", {{"kind", "select_manager"}, {"portrait", 2}}}).ok,
"change actual active desktop manager");
require(fixture(four).at("fixture_id") == live_id, "right-owner identity does not define shared recording identity");
require(fixture(four).at("owners")[0].at("view").at("board") == owner_zero_board &&
fixture(four).at("owners")[0].at("view").at("tactics") == owner_zero_tactics,
"inactive owner board comes from retained career view, never the active manager");
require(four.mark_saved(four.snapshot().revision), "clean source baseline");
for (int i = 0; i < 5; ++i) (void)fixture(four);
require(!four.dirty(), "read cannot dirty saved career");
four.save(f.root / "checkpoint.json", default_desktop());
const auto checkpoint = Json::parse(bmh::editor::read_file(f.root / "checkpoint.json"));
require(checkpoint.dump().find("ui-demo-fixture") == std::string::npos, "playback never enters career checkpoint");
DemoSource high_portrait(f.world); auto high_start = start({0}); high_start["managers"][0]["portrait"] = 11;
require(high_portrait.apply({0, "start", high_start}).ok && fixture(high_portrait).at("owners")[0].at("id") == "manager:11",
"all twelve supported portraits retain stable owner identity");
if (argc == 3 && std::string(argv[1]) == "--write-fixtures") {
fs::create_directories(argv[2]);
fs::copy(f.root / "world", fs::path(argv[2]) / "world", fs::copy_options::recursive | fs::copy_options::overwrite_existing);
fs::copy_file(f.root / "manifest.json", fs::path(argv[2]) / "manifest.json", fs::copy_options::overwrite_existing);
const auto complete = [&](DemoSource &s) {
while (s.snapshot().state.at("opening_flow").at("phase") != "done") {
const auto phase = s.snapshot().state.at("opening_flow").at("phase");
const auto action = phase == "draw" ? "skip" : phase == "overview" ? "finish" : "next";
require(s.apply({s.snapshot().revision, "opening.flow", {{"kind", action}}}).ok, "fixture setup preparation");
}
};
complete(four); four.save(fs::path(argv[2]) / "demo.json", default_desktop());
DemoSource paired(f.world); require(paired.apply({0, "start", start({0,1,2,3})}).ok, "paired fixture");
complete(paired); paired.save(fs::path(argv[2]) / "paired.json", default_desktop());
DemoSource single(f.world); single.save(fs::path(argv[2]) / "single.json", default_desktop());
require(single.apply({single.snapshot().revision, "career", {{"kind", "surface"}, {"value", "indoor"}}}).ok, "indoor GUI fixture");
single.save(fs::path(argv[2]) / "indoor.json", default_desktop());
require(single.apply({single.snapshot().revision, "career", {{"kind", "role"}, {"value", "national"}}}).ok, "national GUI fixture");
single.save(fs::path(argv[2]) / "national.json", default_desktop());
}
std::cout << "live fixture and pure playback tests passed\n"; return 0;
} catch (const std::exception &e) { std::cerr << e.what() << '\n'; return 1; }
}