// SPDX-License-Identifier: GPL-3.0-only #include "modern/ui/engine_bridge.hpp" #include "modern/ui/settings_presentation.hpp" #include #include using namespace bmh::modern_ui; namespace { void check(bool x, const char *s) { if (!x) throw std::runtime_error(s); } template void rejects(F f) { bool ok = false; try { f(); } catch (const std::exception &) { ok = true; } check(ok, "invalid settings accepted"); } } // namespace int main() try { const Json meta = {{"contractVersion", "bmh-modern-ui@draft1"}, {"careerId", "career:test"}, {"worldId", "world:test"}, {"worldRevision", "1"}, {"stateRevision", "8"}, {"scope", {{"managerId", "manager:one"}, {"teamId", "club:one"}, {"role", "club"}, {"allowedCommands", {"settings.commit"}}}}}; Json b; for (auto *k : {"session", "desktop", "camps", "settings"}) b[k] = { {"messageType", "snapshot"}, {"meta", meta}, {"data", {{"kind", k}}}}; b["desktop"]["data"].update( {{"manager", {{"id", "manager:one"}}}, {"team", {{"id", "club:one"}}}, {"accounts", {{"cash", {{"minor", "0"}, {"currency", "EUR"}, {"minorUnits", 2}}}}}}); b["camps"]["data"]["camps"] = Json::array(); auto &d = b["settings"]["data"]; d.update({{"cells", Json::array()}, {"leagueGroups", Json::array()}, {"automation", Json::array()}, {"formations", Json::array({{{"value", 2}, {"label", "Synthetic slot 2"}}, {{"value", 6}, {"label", "Synthetic slot 6"}}})}, {"formation", 2}}); for (int i = 0; i < 5; ++i) d["leagueGroups"].push_back( {{"row", std::string(1, char('A' + i))}, {"competitionId", "competition:renamed-" + std::to_string(i)}, {"name", "Neue Spielklasse " + std::to_string(i)}}); for (int i = 0; i < 180; ++i) d["cells"].push_back( {{"code", std::string(1, char('A' + i / 9)) + char('1' + i % 9)}, {"raw", 0}, {"displayRaw", 0}, {"value", i % 9 < 7 ? 1 : 0}, {"editable", true}, {"min", 0}, {"max", i % 9 < 7 ? 1 : i % 9 == 7 ? 9 : 99}, {"step", 1}, {"reason", ""}}); d["cells"][0].update( {{"raw", 128}, {"displayRaw", 128}, {"value", 1}, {"editable", false}}); d["cells"][1].update( {{"raw", 2}, {"displayRaw", 2}, {"value", nullptr}, {"editable", false}}); d["cells"][8].update({{"raw", 131}, {"value", 65}}); for (auto *key : {"lineup", "substitutions", "tactics", "marking", "training"}) d["automation"].push_back( {{"id", key}, {"raw", 0}, {"enabled", false}, {"editable", true}}); const auto original = b; const auto demo_catalogue = live_settings_catalogue(); const auto p = engine_settings_projection(b); for (const auto &group : p.at("catalogue").at("groups")) if (group.contains("competitions")) for (size_t i = 0; i < 5; ++i) check(group.at("competitions")[i].at("title") == "Neue Spielklasse " + std::to_string(i), "POC league name replaced authoritative session name"); check(live_settings_catalogue() == demo_catalogue, "actual settings changed demo catalogue"); check(b == original && p.at("local").at("cells")[1].is_null() && p.at("local").at("cells")[0] == 1 && p.at("local").at("cells")[8] == 65, "raw flags became booleans or odd source number lost"); auto draft = p.at("local"); draft["cells"][2] = 0; draft["cells"][8] = 66; draft["formation"] = 6; for (auto &[key, v] : draft["automation"].items()) v = true; auto command = engine_settings_command(b, draft, "request:test"); check(command["expectedRevision"] == "8" && command["payload"]["edits"].size() == 2 && command["payload"]["automation"].size() == 5 && command["payload"]["edits"][1]["value"] == 66, "sparse edits scaled or lost"); bool explicit_slot = false; for (const auto &a : command["payload"]["automation"]) if (a["id"] == "lineup") explicit_slot = a["formation"] == 6; check(explicit_slot, "automatic lineup lacks explicit available slot"); auto noop = engine_settings_command(b, p.at("local"), "request:noop"); check(noop["payload"]["edits"].empty() && noop["payload"]["automation"].empty(), "unchanged settings generated edits"); auto bad = draft; bad["cells"][0] = 0; rejects([&] { engine_settings_command(b, bad, "request:x"); }); bad = draft; bad["cells"][2] = 2; rejects([&] { engine_settings_command(b, bad, "request:x"); }); bad = draft; bad["formation"] = 3; rejects([&] { engine_settings_command(b, bad, "request:x"); }); auto foreign = b; foreign["settings"]["meta"]["stateRevision"] = "9"; rejects([&] { engine_settings_projection(foreign); }); foreign = b; foreign["settings"]["data"]["cells"][1]["code"] = "A1"; rejects([&] { engine_settings_projection(foreign); }); foreign = b; foreign["settings"]["data"]["automation"][4]["editable"] = false; rejects([&] { engine_settings_command(foreign, draft, "request:x"); }); foreign = b; foreign["settings"]["data"]["leagueGroups"][1]["row"] = "A"; rejects([&] { engine_settings_projection(foreign); }); foreign = b; foreign["settings"]["data"]["leagueGroups"][1]["competitionId"] = "competition:renamed-0"; rejects([&] { engine_settings_projection(foreign); }); foreign = b; foreign["settings"]["data"]["leagueGroups"][2]["competitionId"] = nullptr; rejects([&] { engine_settings_projection(foreign); }); foreign["settings"]["data"]["leagueGroups"][2]["name"] = nullptr; const auto unknown = engine_settings_projection(foreign); for (const auto &group : unknown.at("catalogue").at("groups")) if (group.contains("competitions")) check(group.at("competitions")[2].at("title") == "Wettbewerb C", "unknown competition silently used POC league name"); std::cout << "settings raw flags, parity display, sparse edits, explicit " "slots, owner and readonly checks passed\n"; return 0; } catch (const std::exception &e) { std::cerr << e.what() << '\n'; return 1; }