396 lines
17 KiB
C++
396 lines
17 KiB
C++
// SPDX-License-Identifier: GPL-3.0-only
|
|
#include "modern/ui/demo_opening.hpp"
|
|
#include "modern/ui/demo_reports.hpp"
|
|
#include "modern/ui/desktop.hpp"
|
|
#include "modern/ui/model.hpp"
|
|
#include <array>
|
|
#include <iostream>
|
|
#include <set>
|
|
#include <stdexcept>
|
|
|
|
using namespace bmh::modern_ui;
|
|
namespace {
|
|
void require(bool value, const char *message) {
|
|
if (!value)
|
|
throw std::runtime_error(message);
|
|
}
|
|
struct Fixture {
|
|
fs::path root;
|
|
std::shared_ptr<const Catalog> world;
|
|
Fixture(int top, int lower)
|
|
: root(fs::temp_directory_path() /
|
|
bmh::editor::new_id("openbmh-opening-test-")) {
|
|
fs::create_directories(root / "world");
|
|
const auto write = [&](const char *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-opening"},
|
|
{"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 DE"}},
|
|
{{"id", "nation:es"}, {"name", "Synthetic ES"}}}}});
|
|
// Authored lower league first: top-tier selection must not guess from row0.
|
|
write("world/competitions.json", {{"items",
|
|
{{{"id", "league:lower"},
|
|
{"nation", "nation:de"},
|
|
{"type", "league"},
|
|
{"tier", 2}},
|
|
{{"id", "league:top"},
|
|
{"nation", "nation:de"},
|
|
{"type", "league"},
|
|
{"tier", 1}},
|
|
{{"id", "league:foreign"},
|
|
{"nation", "nation:es"},
|
|
{"type", "league"},
|
|
{"tier", 1}}}}});
|
|
Json clubs = Json::array();
|
|
for (const auto &[name, count] : std::vector<std::pair<std::string, int>>{
|
|
{"lower", lower}, {"top", top}})
|
|
for (int index = 0; index < count; ++index)
|
|
clubs.push_back(
|
|
{{"id", "club:" + name + "-" + std::to_string(index)},
|
|
{"name", "Synthetic " + name + " " + std::to_string(index)},
|
|
{"nation", "nation:de"},
|
|
{"competition", "league:" + name}});
|
|
clubs.push_back({{"id", "club:foreign"},
|
|
{"name", "Foreign"},
|
|
{"nation", "nation:es"},
|
|
{"competition", "league:foreign"}});
|
|
clubs.push_back({{"id", "club:de-in-foreign-league"},
|
|
{"name", "DE outside DE leagues"},
|
|
{"nation", "nation:de"},
|
|
{"competition", "league:foreign"}});
|
|
write("world/clubs.json", {{"items", clubs}});
|
|
write("world/players.json", {{"items", Json::array()}});
|
|
world = Catalog::open(root);
|
|
}
|
|
~Fixture() {
|
|
std::error_code error;
|
|
fs::remove_all(root, error);
|
|
}
|
|
};
|
|
Json state(const Catalog &world, bool created) {
|
|
Json result = {{"opening", created ? "pending" : "complete"},
|
|
{"opening_created", false},
|
|
{"unrelated", {1, 2, 3}}};
|
|
result["opening_flow"] = make_demo_opening(world, result, created);
|
|
return result;
|
|
}
|
|
void edit(Json &value, const char *kind, const Catalog &world) {
|
|
require(edit_demo_opening(value, {{"kind", kind}}, world).empty(),
|
|
"Valid preparation action rejected");
|
|
}
|
|
void rejected(Json &value, const Json &action, const Catalog &world) {
|
|
const auto before = value;
|
|
const auto error = edit_demo_opening(value, action, world);
|
|
require(!error.empty() && error.find(' ') != error.npos && value == before,
|
|
"Rejected flow edit mutated input or returned unreadable error");
|
|
}
|
|
void selection() {
|
|
Fixture fixture(18, 52);
|
|
const auto before_clubs = fixture.world->rows("clubs");
|
|
const auto first = make_demo_opening(*fixture.world, Json::object(), false);
|
|
require(first == make_demo_opening(*fixture.world, Json::object(), false),
|
|
"Opening order depends on runtime random state");
|
|
require(first.at("phase") == "done" && first.at("created") == false &&
|
|
first.at("playing") == false && first.at("revealed") == 64,
|
|
"Default preparation is not uncreated/completed");
|
|
const auto &order = first.at("order");
|
|
std::set<std::string> ids;
|
|
for (const auto &id : order)
|
|
require(fixture.world->manageable(id.get<std::string>()) &&
|
|
ids.insert(id.get<std::string>()).second,
|
|
"Draw has foreign/unknown/duplicate club");
|
|
require(ids.size() == 64 && !ids.contains("club:lower-46") &&
|
|
!ids.contains("club:foreign") &&
|
|
!ids.contains("club:de-in-foreign-league"),
|
|
"Draw pool does not respect max64 DE/DE-league membership");
|
|
// Independent expected Fisher-Yates result from the unchanged Gold seed/LCG.
|
|
const std::array<int, 64> expected = {
|
|
19, 57, 60, 61, 47, 37, 41, 33, 12, 21, 9, 35, 15, 28, 1, 31,
|
|
48, 4, 25, 32, 17, 52, 3, 27, 7, 14, 23, 51, 46, 54, 42, 8,
|
|
26, 56, 29, 44, 34, 10, 45, 24, 5, 22, 11, 49, 43, 18, 59, 63,
|
|
50, 0, 58, 55, 2, 30, 39, 16, 53, 6, 62, 20, 13, 36, 38, 40};
|
|
for (size_t index = 0; index < expected.size(); ++index) {
|
|
const int source = expected[index];
|
|
const auto id = source < 18 ? "club:top-" + std::to_string(source)
|
|
: "club:lower-" + std::to_string(source - 18);
|
|
require(order[index].get<std::string>() == id,
|
|
"LCG/Fisher-Yates order differs from Gold");
|
|
}
|
|
const auto &entrants = first.at("entrants");
|
|
std::set<std::string> roles;
|
|
require(entrants.at("uefa").size() == 4 &&
|
|
entrants.at("champions").size() == 1 &&
|
|
entrants.at("winners").size() == 1,
|
|
"Fictional top-league roles have wrong counts");
|
|
for (const auto &group : {"uefa", "champions", "winners"})
|
|
for (const auto &id : entrants.at(group))
|
|
require(roles.insert(id.get<std::string>()).second,
|
|
"Entrants appear in multiple roles");
|
|
require(entrants.at("uefa")[0] == "club:top-0" &&
|
|
entrants.at("champions")[0] == "club:top-4" &&
|
|
entrants.at("winners")[0] == "club:top-5",
|
|
"Entrants were inferred from a lower/foreign league");
|
|
require(fixture.world->rows("clubs") == before_clubs,
|
|
"Preparation changed immutable world IDs");
|
|
}
|
|
void tiny_worlds() {
|
|
for (int count : {0, 1, 2, 3, 5, 6}) {
|
|
Fixture fixture(count, 0);
|
|
auto current = state(*fixture.world, true);
|
|
const size_t expected = size_t(count / 2 * 2);
|
|
require(current.at("opening_flow").at("order").size() == expected,
|
|
"Tiny world invented or duplicated a draw club");
|
|
edit(current, "next", *fixture.world);
|
|
edit(current, "next", *fixture.world);
|
|
edit(current, "next", *fixture.world);
|
|
if (!expected)
|
|
require(current.at("opening_flow").at("phase") == "overview" &&
|
|
current.at("opening_flow").at("playing") == false,
|
|
"Zero-pair world entered an endless draw timer");
|
|
else {
|
|
require(current.at("opening_flow").at("phase") == "draw" &&
|
|
current.at("opening_flow").at("playing") == true,
|
|
"Real draw did not start playing");
|
|
edit(current, "skip", *fixture.world);
|
|
}
|
|
edit(current, "finish", *fixture.world);
|
|
require(current.at("opening") == "complete" &&
|
|
current.at("opening_created") == true,
|
|
"Tiny-world presentation cannot finish");
|
|
}
|
|
}
|
|
void actions_and_persistence() {
|
|
Fixture fixture(6, 4);
|
|
DemoSource source(fixture.world);
|
|
const auto base = source.snapshot();
|
|
const Json setup = {{"duration", 5}, {"difficulty", 3}, {"players", 1},
|
|
{"mode", 3}, {"league", true}, {"world", true}};
|
|
require(
|
|
source
|
|
.apply({base.revision,
|
|
"start",
|
|
{{"setup", setup}, {"managers", base.state.at("managers")}}})
|
|
.ok,
|
|
"League start rejected");
|
|
require(source.snapshot().state.at("opening_flow").at("phase") == "uefa",
|
|
"Start did not create fresh flow");
|
|
const auto ids = source.snapshot().state.at("opening_flow").at("order");
|
|
const auto act = [&](const char *kind) {
|
|
const auto before = source.snapshot();
|
|
auto predicted = before.state;
|
|
require(
|
|
edit_demo_opening(predicted, {{"kind", kind}}, *fixture.world).empty(),
|
|
"Precommit sidecar projection rejected");
|
|
const auto result =
|
|
source.apply({before.revision, "opening.flow", {{"kind", kind}}});
|
|
require(result.ok && source.snapshot().revision == before.revision + 1 &&
|
|
source.snapshot().state == predicted,
|
|
"Flow edit was not an exact single-revision publication of sidecar "
|
|
"projection");
|
|
require(source.snapshot().state.at("opening_flow").at("order") == ids,
|
|
"Flow action changed persisted draw IDs");
|
|
};
|
|
act("next");
|
|
act("next");
|
|
act("next");
|
|
act("reveal");
|
|
act("pause");
|
|
const auto paused = source.snapshot();
|
|
require(paused.state.at("opening_flow").at("revealed") == 1 &&
|
|
paused.state.at("opening_flow").at("playing") == false,
|
|
"Odd reveal/pause state was not retained");
|
|
const auto pairs =
|
|
report_opening_pairs(*fixture.world, paused.state.at("opening_flow"));
|
|
require(pairs.size() == ids.size() / 2,
|
|
"Reports show wrong persisted pair count");
|
|
for (size_t index = 0; index < pairs.size(); ++index)
|
|
require(pairs[index].at("home") == ids[index * 2] &&
|
|
pairs[index].at("away") == ids[index * 2 + 1] &&
|
|
pairs[index].at("home_goals").is_null() &&
|
|
pairs[index].at("away_goals").is_null(),
|
|
"Report opening pairs ignored saved IDs or invented results");
|
|
auto checkpoint = paused.state;
|
|
checkpoint["desktop"] = default_desktop();
|
|
checkpoint = Json::parse(checkpoint.dump());
|
|
const auto disk_checkpoint = fixture.root / "paused-opening.json";
|
|
source.save(disk_checkpoint, default_desktop());
|
|
DemoSource disk_restore(fixture.world);
|
|
disk_restore.load(disk_checkpoint);
|
|
require(disk_restore.snapshot().state == paused.state &&
|
|
!disk_restore.dirty(),
|
|
"Paused opening file save/load changed flow or clean marker");
|
|
DemoSource explicit_load(fixture.world);
|
|
explicit_load.load_data(checkpoint);
|
|
require(explicit_load.snapshot().state == paused.state &&
|
|
!explicit_load.dirty(),
|
|
"Explicit pending checkpoint did not roundtrip cleanly");
|
|
DemoSource resumed(fixture.world);
|
|
resumed.load_data(checkpoint, false);
|
|
require(resumed.snapshot().state == paused.state && resumed.dirty(),
|
|
"UI continuation incorrectly replaced explicit-save clean baseline");
|
|
const auto before_reject = resumed.snapshot();
|
|
const bool dirty = resumed.dirty();
|
|
auto result = resumed.apply(
|
|
{before_reject.revision, "opening.flow", {{"kind", "reveal"}}});
|
|
require(!result.ok && resumed.snapshot().state == before_reject.state &&
|
|
resumed.snapshot().revision == before_reject.revision &&
|
|
resumed.dirty() == dirty,
|
|
"Paused reveal changed state/revision/dirty");
|
|
result = resumed.apply(
|
|
{before_reject.revision - 1, "opening.flow", {{"kind", "toggle"}}});
|
|
require(!result.ok && result.error == "stale_revision" &&
|
|
resumed.snapshot().state == before_reject.state &&
|
|
resumed.dirty(),
|
|
"Stale flow request changed continuation");
|
|
act("toggle");
|
|
for (size_t count = 1; count < ids.size(); ++count)
|
|
act("reveal");
|
|
require(source.snapshot().state.at("opening_flow").at("phase") == "overview",
|
|
"Final reveal did not stop at overview");
|
|
act("finish");
|
|
require(source.snapshot().state.at("opening_flow").at("phase") == "done" &&
|
|
source.snapshot().state.at("opening_created") == true,
|
|
"Finish did not synchronize outer legacy fields");
|
|
// A valid saved permutation remains authoritative; load never reshuffles it.
|
|
std::swap(checkpoint["opening_flow"]["order"][0],
|
|
checkpoint["opening_flow"]["order"][1]);
|
|
resumed.load_data(checkpoint, false);
|
|
require(resumed.snapshot().state.at("opening_flow").at("order") ==
|
|
checkpoint.at("opening_flow").at("order"),
|
|
"Load silently changed saved IDs/order");
|
|
require(
|
|
resumed.apply({resumed.snapshot().revision, "opening", "complete"}).ok &&
|
|
resumed.snapshot().state.at("opening_flow").at("phase") == "done",
|
|
"Legacy opening complete is no longer compatible");
|
|
}
|
|
void rollback_and_migration() {
|
|
Fixture fixture(6, 0);
|
|
auto current = state(*fixture.world, true);
|
|
for (const auto &request : std::vector<Json>{Json(),
|
|
Json::array(),
|
|
Json::object(),
|
|
{{"kind", 9}},
|
|
{{"kind", "unknown"}},
|
|
{{"kind", "finish"}},
|
|
{{"kind", "reveal"}},
|
|
{{"kind", "skip"}},
|
|
{{"kind", "toggle"}},
|
|
{{"kind", "pause"}}})
|
|
rejected(current, request, *fixture.world);
|
|
edit(current, "next", *fixture.world);
|
|
edit(current, "next", *fixture.world);
|
|
edit(current, "next", *fixture.world);
|
|
rejected(current, {{"kind", "next"}}, *fixture.world);
|
|
edit(current, "toggle", *fixture.world);
|
|
rejected(current, {{"kind", "reveal"}}, *fixture.world);
|
|
edit(current, "toggle", *fixture.world);
|
|
edit(current, "reveal", *fixture.world);
|
|
edit(current, "pause", *fixture.world);
|
|
const auto good = current;
|
|
std::vector<Json> malformed;
|
|
for (const char *key : {"schema", "created", "phase", "revealed", "playing",
|
|
"order", "entrants"}) {
|
|
auto bad = good;
|
|
bad["opening_flow"].erase(key);
|
|
malformed.push_back(bad);
|
|
}
|
|
const auto alter = [&](const char *key, const Json &value) {
|
|
auto bad = good;
|
|
bad["opening_flow"][key] = value;
|
|
malformed.push_back(bad);
|
|
};
|
|
alter("schema", 2);
|
|
alter("schema", true);
|
|
alter("created", false);
|
|
alter("phase", "unknown");
|
|
alter("phase", "overview");
|
|
alter("phase", "done");
|
|
alter("revealed", -1);
|
|
alter("revealed", 6);
|
|
alter("revealed", 1.5);
|
|
alter("revealed", true);
|
|
auto bad = good;
|
|
bad["opening_flow"]["order"][1] = bad["opening_flow"]["order"][0];
|
|
malformed.push_back(bad);
|
|
bad = good;
|
|
bad["opening_flow"]["order"][0] = "club:foreign";
|
|
malformed.push_back(bad);
|
|
bad = good;
|
|
bad["opening_flow"]["order"].erase(0);
|
|
malformed.push_back(bad);
|
|
bad = good;
|
|
bad["opening_flow"]["entrants"]["champions"][0] = "club:top-0";
|
|
malformed.push_back(bad);
|
|
bad = good;
|
|
bad["opening_flow"]["extra"] = 1;
|
|
malformed.push_back(bad);
|
|
bad = good;
|
|
bad["opening"] = "complete";
|
|
malformed.push_back(bad);
|
|
bad = good;
|
|
bad["opening_created"] = true;
|
|
malformed.push_back(bad);
|
|
bad = good;
|
|
bad["opening_flow"]["phase"] = "uefa";
|
|
bad["opening_flow"]["revealed"] = 0;
|
|
bad["opening_flow"]["playing"] = true;
|
|
malformed.push_back(bad);
|
|
DemoSource stable(fixture.world);
|
|
require(stable.apply({0, "ticket", 17}).ok, "Synthetic dirty base failed");
|
|
const auto before = stable.snapshot();
|
|
for (auto value : malformed) {
|
|
const auto untouched = value;
|
|
require(!migrate_demo_opening(value, *fixture.world) && value == untouched,
|
|
"Invalid saved flow was repaired/partially migrated");
|
|
auto checkpoint = before.state;
|
|
for (const char *key : {"opening", "opening_created", "opening_flow"})
|
|
checkpoint[key] = untouched.at(key);
|
|
checkpoint["desktop"] = default_desktop();
|
|
bool failed = false;
|
|
try {
|
|
stable.load_data(checkpoint, false);
|
|
} catch (const std::exception &) {
|
|
failed = true;
|
|
}
|
|
require(failed && stable.snapshot().state == before.state &&
|
|
stable.snapshot().revision == before.revision && stable.dirty(),
|
|
"Invalid flow load partially published state/clean marker");
|
|
}
|
|
Json legacy = {{"opening", "pending"}, {"opening_created", false}};
|
|
require(migrate_demo_opening(legacy, *fixture.world) &&
|
|
legacy.at("opening_flow").at("phase") == "uefa",
|
|
"Legacy pending checkpoint not migrated to first phase");
|
|
const auto migrated = legacy;
|
|
require(migrate_demo_opening(legacy, *fixture.world) && legacy == migrated,
|
|
"Flow migration not idempotent");
|
|
legacy = {{"opening", "complete"}, {"opening_created", true}};
|
|
require(migrate_demo_opening(legacy, *fixture.world) &&
|
|
legacy.at("opening_flow").at("phase") == "done" &&
|
|
legacy.at("opening_flow").at("created") == true,
|
|
"Legacy completed checkpoint restarted opening");
|
|
}
|
|
} // namespace
|
|
int main() {
|
|
try {
|
|
selection();
|
|
tiny_worlds();
|
|
actions_and_persistence();
|
|
rollback_and_migration();
|
|
std::cout << "PASS UI opening: deterministic64/real tiny pools, entrants, "
|
|
"phase guards, odd paused continuation, saved IDs/reports, "
|
|
"stale rollback and legacy migration\n";
|
|
return 0;
|
|
} catch (const std::exception &error) {
|
|
std::cerr << error.what() << '\n';
|
|
return 1;
|
|
}
|
|
}
|