245 lines
18 KiB
C++
245 lines
18 KiB
C++
// SPDX-License-Identifier: GPL-3.0-only
|
|
#include "modern/ui/demo_post_match.hpp"
|
|
#include "modern/ui/demo_live.hpp"
|
|
#include "modern/ui/desktop.hpp"
|
|
#include "modern/ui/model.hpp"
|
|
#include <algorithm>
|
|
#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 = fs::temp_directory_path() / bmh::editor::new_id("openbmh-post-test-");
|
|
std::shared_ptr<const Catalog> world;
|
|
Fixture() {
|
|
fs::create_directories(root / "world");
|
|
const auto write = [&](const char *path, const Json &value) {
|
|
bmh::editor::write_atomic(root / path, value.dump());
|
|
};
|
|
write("manifest.json", {{"format", "openbmh-world"}, {"kind", "world"},
|
|
{"format_version", "1.1"}, {"id", "world:synthetic-post-match"},
|
|
{"start_date", "2034-07-01"}, {"requires", {"editor:world-v1"}},
|
|
{"x-editor", {{"profile", "editor-world-v1"}}}});
|
|
write("world/nations.json", {{"items", {{{"id", "nation:de"}, {"name", "Deutschland"}},
|
|
{{"id", "nation:fr"}, {"name", "Frankreich"}}}}});
|
|
write("world/competitions.json", {{"items", {{{"id", "competition:post"}, {"name", "POC-Liga"},
|
|
{"nation", "nation:de"}, {"type", "league"}}}}});
|
|
Json clubs = Json::array(), players = Json::array();
|
|
for (int c = 0; c < 8; ++c) {
|
|
const auto club = "club:post-" + std::to_string(c);
|
|
clubs.push_back({{"id", club}, {"name", "Verein " + std::to_string(c)},
|
|
{"nation", "nation:de"}, {"competition", "competition:post"}});
|
|
for (int i = 0; i < 16; ++i)
|
|
players.push_back({{"id", "player:post-" + std::to_string(c) + "-" + std::to_string(i)},
|
|
{"name", "Kaderspieler " + std::to_string(i)}, {"team", club},
|
|
{"nation", "nation:de"}, {"squad_order", i},
|
|
{"positions", Json::array({i == 0 ? "gk" : i < 6 ? "def" : i < 12 ? "mid" : "fwd"})}});
|
|
}
|
|
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(std::initializer_list<int> clubs) {
|
|
Json managers = Json::array();
|
|
for (const auto club : clubs)
|
|
managers.push_back({{"name", "Trainer " + std::to_string(managers.size())},
|
|
{"club", "club:post-" + std::to_string(club)},
|
|
{"portrait", managers.size()}, {"portrait_style", "photo"}, {"strength_choice", 1}});
|
|
return {{"setup", {{"duration", 5}, {"difficulty", 3}, {"players", managers.size()},
|
|
{"mode", 3}, {"league", true}, {"world", true}}}, {"managers", managers}};
|
|
}
|
|
Json live(DemoSource &source) { return source.read_live({{"kind", "fixture"}}); }
|
|
Json request(const Json &recording, const char *kind = "review") {
|
|
return {{"kind", kind}, {"completed_sequence", recording.at("frames").size() - 1},
|
|
{"revision", recording.at("source_revision")}, {"fixture_id", recording.at("fixture_id")}};
|
|
}
|
|
} // namespace
|
|
|
|
int main(int argc, char **argv) {
|
|
try {
|
|
require(argc == 2, "expected separate synthetic nomination layout fixture");
|
|
const auto layout = Json::parse(bmh::editor::read_file(argv[1]));
|
|
require(layout.at("schema") == "openbmh-post-match-eleven-layout@1" &&
|
|
layout.at("eleven").at("source") == "synthetic-layout-fixture",
|
|
"filled nomination example is explicitly synthetic and outside product startup");
|
|
struct Unavailable final : DataSource {
|
|
Snapshot snapshot() const override { return {}; }
|
|
ActionResult apply(const Action &) override { return {false, 0, "unavailable"}; }
|
|
} unavailable;
|
|
require(unavailable.read_post_match(Json::object()).at("error") == "post_match_unavailable",
|
|
"authoritative adapter cannot invent completed results");
|
|
Fixture f; DemoSource source(f.world);
|
|
require(source.apply({0, "start", start({0,1,2,3})}).ok, "paired humans start");
|
|
require(source.mark_saved(source.snapshot().revision), "clean baseline");
|
|
const auto before = source.snapshot(); const auto world_before = f.world->summary();
|
|
const auto recording = live(source), original = recording;
|
|
const auto review = make_demo_post_match(recording, 8);
|
|
require(review.value("ok", false) && review.at("source") == "ui-demo-fixture" &&
|
|
review.at("fixture_id") == recording.at("fixture_id") &&
|
|
review.at("source_revision") == before.revision && review.at("completed_sequence") == 8,
|
|
"completed review keeps explicit provenance and sequence");
|
|
require(review.at("owners") == recording.at("owners") && review.at("matches").size() == 2 &&
|
|
review.at("matches")[0].at("owners").size() == 2,
|
|
"four owners share exactly two unique human matches");
|
|
require(review.at("tabs") == Json::array({"results", "report", "eleven", "overview", "table"}),
|
|
"outdoor club has five review tabs");
|
|
require(review.at("overview").at("games") == 2 && review.at("overview").at("goals") == 6 &&
|
|
review.at("overview").at("yellow") == 2 && review.at("overview").at("red") == 0 &&
|
|
review.at("overview").at("chances") == 28 &&
|
|
review.at("overview").at("scope") == "recorded_human_matches",
|
|
"overview aggregates final recorded matches once, not once per human or whole league");
|
|
require(review.at("overview").at("attendance") == 57550 &&
|
|
!review.at("eleven").at("available").get<bool>() && review.at("eleven").at("reason") == "not_recorded" &&
|
|
review.at("eleven").at("rows").empty(), "crowds counted once per match while unrecorded nominations stay unknown");
|
|
require(review.at("tables").size() == 1 && review.at("tables")[0].at("match_ids").size() == 2 &&
|
|
review.at("tables")[0].at("rows") == recording.at("frames").back().at("matches")[0].at("standings") &&
|
|
review.at("tables")[0].at("wins").is_null(), "shared final table is copied verbatim without season formula");
|
|
for (size_t i = 0; i < review.at("matches").size(); ++i) {
|
|
const auto &row = review.at("matches")[i];
|
|
require(row.at("final") == recording.at("frames").back().at("matches")[i] &&
|
|
row.at("id") == recording.at("matches")[i].at("id") &&
|
|
row.at("attendance") == recording.at("matches")[i].at("attendance") &&
|
|
row.at("weather") == recording.at("matches")[i].at("weather"),
|
|
"result fields and identities equal exact final snapshot");
|
|
for (const auto &event : row.at("events")) require(event.at("match_id") == row.at("id"), "events cannot leak from another match");
|
|
}
|
|
auto old_recording = recording;
|
|
for (auto &match : old_recording["matches"]) { match.erase("attendance"); match.erase("weather"); }
|
|
const auto old_review = make_demo_post_match(old_recording, 8);
|
|
require(old_review.at("overview").at("attendance").is_null() &&
|
|
old_review.at("matches")[0].at("attendance").is_null() &&
|
|
old_review.at("matches")[0].at("weather").is_null(),
|
|
"old recordings still keep unrecorded crowds and weather unknown");
|
|
require(source.read_post_match(request(recording)) == review, "DemoSource read hook gives pure final review");
|
|
Json supplied = {{"schema", "openbmh-post-match-records@1"},
|
|
{"fixture_id", recording.at("fixture_id")},
|
|
{"source_revision", recording.at("source_revision")}, {"eleven", layout.at("eleven")}};
|
|
const auto filled = make_demo_post_match(recording, 8, supplied);
|
|
require(filled.value("ok", false) && filled.at("eleven") == layout.at("eleven"),
|
|
"explicitly supplied synthetic nominations are preserved for filled layout coverage");
|
|
auto expected_filled = review; expected_filled["eleven"] = layout.at("eleven");
|
|
auto &unknown = expected_filled["unknown_fields"];
|
|
unknown.erase(std::remove(unknown.begin(), unknown.end(), Json("eleven")), unknown.end());
|
|
require(filled == expected_filled && make_demo_post_match(recording, 8).dump() == review.dump() &&
|
|
make_demo_post_match(recording, 8, Json::object()).dump() == review.dump(),
|
|
"filled path only supplies its nominations; no-records output remains byte-identical");
|
|
require(query_demo_post_match(recording, request(recording), supplied) == filled && recording == original,
|
|
"query overload accepts bound records without rewriting recording frames or identity");
|
|
auto actual_supplied = supplied; actual_supplied["eleven"]["source"] = "supplied-records";
|
|
require(make_demo_post_match(recording, 8, actual_supplied).at("eleven").at("source") == "supplied-records",
|
|
"non-layout records retain explicit supplied provenance without claiming engine authority");
|
|
for (int fault = 0; fault < 15; ++fault) {
|
|
auto bad = supplied;
|
|
if (fault == 0) bad["eleven"]["rows"][1]["player"]["id"] = bad["eleven"]["rows"][0]["player"]["id"];
|
|
if (fault == 1) bad["eleven"]["rows"][1]["position"] = "gk";
|
|
if (fault == 2) bad["eleven"]["rows"].erase(0);
|
|
if (fault == 3) bad["eleven"]["rows"][0]["rating"] = 1;
|
|
if (fault == 4) bad["source_revision"] = before.revision + 1;
|
|
if (fault == 5) bad["fixture_id"] = "foreign";
|
|
if (fault == 6) bad["eleven"]["rows"][0]["team"]["id"] = "club:foreign";
|
|
if (fault == 7) bad["eleven"]["rows"][0]["team"]["name"] = "Renamed facts";
|
|
if (fault == 8) bad["eleven"]["source"] = "engine";
|
|
if (fault == 9) bad["eleven"]["formation"] = "4-4-2";
|
|
if (fault == 10) bad["generate"] = true;
|
|
if (fault == 11) bad["eleven"]["rows"][0]["player"]["name"] = std::string(1, char(0xff));
|
|
if (fault == 12) bad["eleven"]["available"] = false;
|
|
if (fault == 13) bad["eleven"]["rows"][0]["team"]["role"] = "national";
|
|
if (fault == 14) bad = nullptr;
|
|
require(!make_demo_post_match(recording, 8, bad).at("ok") &&
|
|
!query_demo_post_match(recording, request(recording), bad).at("ok"),
|
|
"malformed, stale, unknown-scale, foreign or non-3-4-3 nominations reject atomically");
|
|
}
|
|
auto seek = request(recording, "report"); seek["match_id"] = recording.at("matches")[1].at("id");
|
|
for (int minute : {0, 11, 12, 23, 45, 61, 62, 77, 78, 89, 90}) {
|
|
seek["minute"] = minute;
|
|
const auto report = query_demo_post_match(recording, seek);
|
|
require(query_demo_post_match(recording, seek, supplied) == report,
|
|
"optional nominations cannot change report outcomes, events or minutes");
|
|
require(report.value("ok", false) && report.at("minute") == minute && report.at("recorded_minute") <= minute &&
|
|
report.at("match_id") == seek.at("match_id"), "seek selects existing frame for selected match");
|
|
require(report.at("score") == (minute < 12 ? Json::array({0,0}) : minute < 62 ? Json::array({1,0}) :
|
|
minute < 78 ? Json::array({1,1}) : Json::array({2,1})),
|
|
"seek never interpolates or invents a goal between recorded moments");
|
|
require(report.at("final_score") == Json::array({2,1}) && report.at("timeline").size() == 9 &&
|
|
report.at("actor").is_null() && report.at("assists").is_null() &&
|
|
report.at("ratings").is_null() && report.at("playing_minutes").is_null(),
|
|
"full outcome remains known, absent player facts remain unknown");
|
|
for (const auto &event : report.at("events"))
|
|
require(event.at("minute") <= minute && event.at("match_id") == seek.at("match_id"),
|
|
"report only reveals that match's recorded past events");
|
|
}
|
|
require(source.snapshot().state == before.state && source.snapshot().revision == before.revision &&
|
|
!source.dirty() && f.world->summary() == world_before && recording == original,
|
|
"all review and seek reads preserve source, revision, clean baseline, catalogue and original recording");
|
|
for (size_t sequence = 0; sequence < 8; ++sequence)
|
|
require(make_demo_post_match(recording, sequence).at("error") == "post_match_not_finished",
|
|
"cannot review incomplete playback");
|
|
require(!make_demo_post_match(recording, 9).at("ok"), "out-of-range completion rejected");
|
|
for (const auto &bad : Json::array({Json(), Json::array(), Json{{"completed_sequence", -1}},
|
|
Json{{"completed_sequence", 8.0}}, Json{{"completed_sequence", 8}, {"kind", "apply"}},
|
|
Json{{"completed_sequence", 8}, {"fixture_id", "wrong"}},
|
|
Json{{"completed_sequence", 8}, {"revision", before.revision + 1}},
|
|
Json{{"completed_sequence", 8}, {"minute", 1}},
|
|
Json{{"completed_sequence", 8}, {"kind", "report"}, {"minute", -1}},
|
|
Json{{"completed_sequence", 8}, {"kind", "report"}, {"minute", 91}},
|
|
Json{{"completed_sequence", 8}, {"kind", "report"}, {"minute", 1.5}},
|
|
Json{{"completed_sequence", 8}, {"kind", "report"}, {"match_id", "foreign"}},
|
|
Json{{"completed_sequence", 8}, {"owner", "manager:0"}}}))
|
|
require(!query_demo_post_match(recording, bad).at("ok"), "malformed, stale and unsupported query rejects explicitly");
|
|
require(source.read_post_match({{"completed_sequence", 8}}).at("error") == "post_match_identity_required",
|
|
"source hook requires both revision and fixture identity");
|
|
auto invalid = recording; invalid["source"] = "engine";
|
|
require(!make_demo_post_match(invalid, 8).at("ok"), "demo review cannot claim engine authority");
|
|
invalid = recording; invalid["owners"][0]["match_id"] = "wrong";
|
|
require(!make_demo_post_match(invalid, 8).at("ok"), "owner-match consistency required");
|
|
|
|
require(source.apply({source.snapshot().revision, "career", {{"kind", "select_manager"}, {"portrait", 2}}}).ok,
|
|
"switch active presentation owner");
|
|
require(!source.read_post_match(request(recording)).at("ok") && make_demo_post_match(recording, 8) == review,
|
|
"stale source read fails while retained historical recording remains identical");
|
|
require(source.apply({source.snapshot().revision, "career", {{"kind", "surface"}, {"value", "indoor"}}}).ok,
|
|
"switch indoor board");
|
|
const auto hall = live(source); const auto hall_review = make_demo_post_match(hall, 8);
|
|
auto hall_records = supplied; hall_records["fixture_id"] = hall.at("fixture_id");
|
|
hall_records["source_revision"] = hall.at("source_revision");
|
|
require(make_demo_post_match(hall, 8, hall_records).at("error") == "post_match_records_scope_unavailable",
|
|
"indoor never receives an outdoor eleven tab through supplied records");
|
|
require(hall_review.at("duration") == 40 && hall_review.at("field_count") == 6 && hall_review.at("tables").empty() &&
|
|
hall_review.at("tabs") == Json::array({"results", "report", "overview"}), "hall has three tabs and no league table");
|
|
auto hall_seek = request(hall, "report"); hall_seek["minute"] = 36;
|
|
require(query_demo_post_match(hall, hall_seek).at("score") == Json::array({2,1}), "hall seeks actual36-minute goal");
|
|
hall_seek["minute"] = 41;
|
|
require(!query_demo_post_match(hall, hall_seek).at("ok"), "hall cannot seek past40");
|
|
require(source.apply({source.snapshot().revision, "career", {{"kind", "role"}, {"value", "national"}}}).ok,
|
|
"switch national role");
|
|
const auto national = live(source); const auto national_review = make_demo_post_match(national, 8);
|
|
auto national_records = supplied; national_records["fixture_id"] = national.at("fixture_id");
|
|
national_records["source_revision"] = national.at("source_revision");
|
|
require(make_demo_post_match(national, 8, national_records).at("error") == "post_match_records_scope_unavailable",
|
|
"national cannot display old club nominations even when rebound to its fixture");
|
|
require(national_review.at("duration") == 90 && national_review.at("role") == "national" &&
|
|
national_review.at("owners").size() == 1 && national_review.at("tables").empty() &&
|
|
national_review.at("tabs") == Json::array({"results", "report", "overview"}) &&
|
|
national_review.at("owners")[0].at("view").at("national_demo_basis") == true,
|
|
"national retains own identity and POC squad boundary without club-only tabs");
|
|
for (int count = 1; count <= 4; ++count) {
|
|
DemoSource several(f.world);
|
|
const auto setup = count == 1 ? start({0}) : count == 2 ? start({0,2}) : count == 3 ? start({0,2,4}) : start({0,2,4,6});
|
|
require(several.apply({0, "start", setup}).ok, "unique human fixtures start");
|
|
const auto view = make_demo_post_match(live(several), 8);
|
|
require(view.at("matches").size() == size_t(count) && view.at("overview").at("goals") == count * 3,
|
|
"one through four distinct matches retain their own final totals");
|
|
}
|
|
source.save(f.root / "checkpoint.json", default_desktop());
|
|
const auto saved = bmh::editor::read_file(f.root / "checkpoint.json");
|
|
require(saved.find("openbmh-post-match@1") == std::string::npos && saved.find("ui-demo-fixture") == std::string::npos,
|
|
"read-only review never enters career checkpoint or advances season");
|
|
std::cout << "post-match read-only projection tests passed\n"; return 0;
|
|
} catch (const std::exception &error) { std::cerr << error.what() << '\n'; return 1; }
|
|
}
|