208 lines
8.9 KiB
C++
208 lines
8.9 KiB
C++
// SPDX-License-Identifier: GPL-3.0-only
|
|
#include "modern/ui/demo_career.hpp"
|
|
#include "modern/ui/demo_national_ui.hpp"
|
|
#include "modern/ui/desktop.hpp"
|
|
#include "modern/ui/model.hpp"
|
|
#include <iostream>
|
|
#include <set>
|
|
#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-entry-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-entry"},
|
|
{"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 Germany"}},
|
|
{{"id", "nation:es"}, {"name", "Synthetic Spain"}}}}});
|
|
write("world/competitions.json", {{"items",
|
|
{{{"id", "competition:de"},
|
|
{"name", "Synthetic DE"},
|
|
{"nation", "nation:de"},
|
|
{"type", "league"}},
|
|
{{"id", "competition:es"},
|
|
{"name", "Synthetic ES"},
|
|
{"nation", "nation:es"},
|
|
{"type", "league"}}}}});
|
|
Json clubs = Json::array();
|
|
for (int index = 0; index < 5; ++index)
|
|
clubs.push_back({{"id", "club:entry-" + std::to_string(index)},
|
|
{"name", "Synthetic club " + std::to_string(index)},
|
|
{"nation", "nation:de"},
|
|
{"competition", "competition:de"}});
|
|
clubs.push_back({{"id", "club:foreign"},
|
|
{"name", "Foreign club"},
|
|
{"nation", "nation:es"},
|
|
{"competition", "competition:es"}});
|
|
write("world/clubs.json", {{"items", clubs}});
|
|
Json players = Json::array();
|
|
for (int team = 0; team < 5; ++team)
|
|
for (int n = 0; n < 16; ++n)
|
|
players.push_back(
|
|
{{"id", "player:" + std::to_string(team) + "-" + std::to_string(n)},
|
|
{"name", "Synthetic player " + std::to_string(n)},
|
|
{"team", "club:entry-" + std::to_string(team)},
|
|
{"squad_order", n},
|
|
{"positions", Json::array({n < 2 ? "gk"
|
|
: n < 7 ? "def"
|
|
: n < 12 ? "mid"
|
|
: "fwd"})}});
|
|
players[0]["attributes"] = {{"caps", 3}, {"international_goals", 1}};
|
|
players[1]["attributes"] = {{"caps", -1},
|
|
{"international_goals", "unknown"}};
|
|
write("world/players.json", {{"items", players}});
|
|
world = Catalog::open(root);
|
|
}
|
|
~Fixture() {
|
|
std::error_code error;
|
|
fs::remove_all(root, error);
|
|
}
|
|
};
|
|
} // namespace
|
|
int main() {
|
|
try {
|
|
Fixture f;
|
|
DemoSource source(f.world);
|
|
const auto first = source.snapshot();
|
|
require(first.state.contains("national") && valid_national_ui(first.state),
|
|
"National state missing");
|
|
const auto apply = [&](Json action) {
|
|
return source.apply({source.snapshot().revision, "national", action});
|
|
};
|
|
auto action = [](const std::string &kind) {
|
|
return Json{{"kind", kind}, {"manager_portrait", 0}};
|
|
};
|
|
auto select = action("opponent");
|
|
select["opponent"] = "national-fr";
|
|
select["home"] = true;
|
|
require(!apply(select).ok && source.snapshot().state == first.state,
|
|
"Club context accepted national booking");
|
|
require(source
|
|
.apply({source.snapshot().revision,
|
|
"career",
|
|
{{"kind", "role"}, {"value", "national"}}})
|
|
.ok,
|
|
"National role");
|
|
auto before = source.snapshot();
|
|
auto wrong = select;
|
|
wrong["manager_portrait"] = 9;
|
|
require(!apply(wrong).ok && source.snapshot().state == before.state,
|
|
"Wrong owner changed planning");
|
|
require(!source.apply({before.revision - 1, "national", select}).ok &&
|
|
source.snapshot().state == before.state,
|
|
"Stale revision changed planning");
|
|
const auto achievements =
|
|
query_national_ui(*f.world, before.state, "achievements");
|
|
require(achievements.at("history").empty() &&
|
|
achievements.at("tournaments").size() == 2 &&
|
|
achievements.at("tournaments")[0].at("titles").is_null(),
|
|
"Invented national titles/history");
|
|
const auto scorers = query_national_ui(*f.world, before.state, "scorers");
|
|
require(scorers.at("recorded_matches") == 0 &&
|
|
scorers.at("rows").size() == 16,
|
|
"National POC roster unavailable");
|
|
for (const auto &p : scorers.at("rows")) {
|
|
require(p.at("goals").is_null() && p.at("assists").is_null(),
|
|
"Invented recorded goal/assist fact");
|
|
if (p.at("id") == "player:0-0")
|
|
require(p.at("caps") == 3 && p.at("catalog_international_goals") == 1,
|
|
"Verified catalog attribute was lost");
|
|
else
|
|
require(p.at("caps").is_null() &&
|
|
p.at("catalog_international_goals").is_null(),
|
|
"Missing/invalid catalog fact was invented");
|
|
}
|
|
require(national_ui_opponents().size() == 8, "Wrong country count");
|
|
std::set<std::string> codes;
|
|
for (const auto &n : national_ui_opponents())
|
|
codes.insert(n.at("code"));
|
|
require(codes == std::set<std::string>{"fr", "it", "es", "en", "nl", "pl",
|
|
"se", "at"},
|
|
"National countries became club leagues");
|
|
require(apply(select).ok, "Select opponent");
|
|
const auto role_state = source.snapshot().state;
|
|
for (int i = 0; i < 12; ++i) {
|
|
const auto dates = national_ui_dates(source.snapshot().state);
|
|
require(!dates.empty(), "Dummy slot unexpectedly missing");
|
|
auto day = action("day");
|
|
day["value"] = dates[0];
|
|
require(apply(day).ok && apply(action("book")).ok,
|
|
"National plan booking failed");
|
|
}
|
|
const auto booked = source.snapshot();
|
|
require(booked.state.at("national").at("friendlies").size() == 12,
|
|
"Booking cap differs");
|
|
require(!apply(action("book")).ok &&
|
|
source.snapshot().state == booked.state,
|
|
"13th booking changed state");
|
|
auto only = booked.state;
|
|
only["national"] = role_state.at("national");
|
|
require(only == role_state,
|
|
"National booking changed cash/date/club calendar or other state");
|
|
auto invalid = booked.state;
|
|
invalid["national"]["friendlies"][0]["date"] = "2034-07-01";
|
|
const auto bad_before = invalid;
|
|
require(!migrate_national_ui(invalid) && invalid == bad_before,
|
|
"Outside-season restore silently repaired");
|
|
invalid = booked.state;
|
|
invalid["national"]["friendlies"][1]["date"] =
|
|
invalid["national"]["friendlies"][0]["date"];
|
|
require(!valid_national_ui(invalid), "Duplicate restore accepted");
|
|
invalid = booked.state;
|
|
invalid["national"]["month"] = 12;
|
|
require(!valid_national_ui(invalid), "Invalid month accepted");
|
|
auto saved = booked.state;
|
|
saved["desktop"] = default_desktop();
|
|
DemoSource restored(f.world);
|
|
restored.load_data(saved);
|
|
require(restored.snapshot().state == booked.state,
|
|
"National checkpoint did not roundtrip");
|
|
auto wrong_file = saved;
|
|
wrong_file["national"]["home"] = 0;
|
|
const auto load_before = restored.snapshot();
|
|
bool rejected = false;
|
|
try {
|
|
restored.load_data(wrong_file);
|
|
} catch (const std::exception &) {
|
|
rejected = true;
|
|
}
|
|
require(rejected && restored.snapshot().state == load_before.state &&
|
|
restored.snapshot().revision == load_before.revision,
|
|
"Bad national restore partially published");
|
|
auto legacy = first.state;
|
|
legacy.erase("national");
|
|
legacy["desktop"] = default_desktop();
|
|
restored.load_data(legacy);
|
|
require(valid_national_ui(restored.snapshot().state),
|
|
"Old checkpoint did not gain empty national plan");
|
|
auto late = before.state;
|
|
late["date"] = "2033-06-29";
|
|
require(national_ui_dates(late).empty(),
|
|
"Invented slots beyond season end");
|
|
std::cout << "national presentation tests passed\n";
|
|
return 0;
|
|
} catch (const std::exception &e) {
|
|
std::cerr << e.what() << '\n';
|
|
return 1;
|
|
}
|
|
}
|