182 lines
13 KiB
C++
182 lines
13 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 <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() {
|
|
try {
|
|
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").is_null() &&
|
|
!review.at("eleven").at("available").get<bool>() && review.at("eleven").at("reason") == "not_recorded" &&
|
|
review.at("eleven").at("rows").empty(), "unrecorded crowd and 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").is_null(),
|
|
"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");
|
|
}
|
|
require(source.read_post_match(request(recording)) == review, "DemoSource read hook gives pure final review");
|
|
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(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);
|
|
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);
|
|
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; }
|
|
}
|