Files
Dreierpack-Manager/tests/modern_rankings_tests.cpp

125 lines
8.5 KiB
C++

// SPDX-License-Identifier: GPL-3.0-only
#include "modern/ui/demo_rankings.hpp"
#include <algorithm>
#include <array>
#include <chrono>
#include <fstream>
#include <iostream>
#include <set>
#include <stdexcept>
using namespace bmh::modern_ui;
namespace {
void require(bool ok, const char *why) { if (!ok) throw std::runtime_error(why); }
struct Fixture {
fs::path root;
std::shared_ptr<const Catalog> world;
Fixture(int clubs = 20, int field_players = 12, bool german = true) {
root = fs::temp_directory_path() / ("openbmh-rankings-test-" +
std::to_string(std::chrono::steady_clock::now().time_since_epoch().count()));
fs::create_directories(root / "world");
const auto write = [&](const std::string &path, const Json &value) {
std::ofstream file(root / path); file << value.dump();
require(bool(file), "Cannot write synthetic rankings fixture");
};
write("manifest.json", {{"format", "openbmh-world"}, {"kind", "world"},
{"format_version", "1.1"}, {"id", "world:rankings-test"},
{"requires", {"runtime:modern-v1"}}, {"start_date", "2026-07-01"}});
Json competitions = Json::array(), nations = Json::array(), teams = Json::array(), players = Json::array();
for (int group = 0; group < 2; ++group) {
const std::string nation = group == 0 && german ? "nation:de" : "nation:synthetic-" + std::to_string(group);
nations.push_back({{"id", nation}, {"name", "Synthetic nation " + std::to_string(group)}});
const auto league = "competition:rankings-" + std::to_string(group);
competitions.push_back({{"id", league}, {"type", "league"}, {"nation", nation}, {"name", "Synthetic league " + std::to_string(group)}});
for (int i = 0; i < (group ? 3 : clubs); ++i) {
const auto club = "club:rankings-" + std::to_string(group) + "-" + std::to_string(i);
teams.push_back({{"id", club}, {"name", "Synthetic team " + std::to_string(group) + " " + std::to_string(i)}, {"competition", league}, {"nation", nation}, {"crest", "asset:synthetic-crest-" + std::to_string(group) + "-" + std::to_string(i)}});
for (int p = 0; p < field_players + 2; ++p) {
const auto id = "player:rankings-" + std::to_string(group) + "-" + std::to_string(i) + "-" + std::to_string(p);
players.push_back({{"id", id}, {"name", "Synthetic player " + std::to_string(group) + " " + std::to_string(i) + " " + std::to_string(p)}, {"team", club}, {"positions", {p < 2 ? "gk" : std::array<const char *, 3>{"def", "mid", "fwd"}[size_t(p - 2) % 3]}}, {"squad_order", p}, {"photo", "asset:synthetic-photo-" + id}});
}
}
}
write("world/nations.json", {{"items", nations}});
write("world/competitions.json", {{"items", competitions}});
write("world/clubs.json", {{"items", teams}});
write("world/players.json", {{"items", players}});
world = Catalog::open(root);
}
~Fixture() { std::error_code error; fs::remove_all(root, error); }
};
void check_rows(const Catalog &world, const Json &rows, int kind,
const std::string &league, const std::string &own, int scope) {
std::set<std::string> seen;
const std::array<const char *, 4> metrics{"goals", "assists", "yellow", "red"};
for (size_t i = 0; i < rows.size(); ++i) {
const auto &row = rows[i];
const auto player = row.at("player").get<std::string>();
const auto *p = world.find("players", player);
const auto *club = world.find("clubs", row.at("club").get<std::string>());
require(p && club && p->at("team") == club->at("id"), "Invented or incorrectly bound ranking identity");
require(seen.insert(player).second, "Duplicate ranking player");
require(scope ? p->at("team") == own : club->at("competition") == league, "Ranking crossed requested club/league");
require(p->at("positions")[0] != "gk", "Goalkeeper substituted for an actual outfield player");
require(row.at("name") == world.name(*p) && row.at("club_name") == world.name(*club) && row.at("photo") == p->at("photo") && row.at("crest") == club->at("crest"), "Historical identity or asset reference invented");
require(row.at("value").get<int>() > 0 && row.at("value") == row.at(metrics[size_t(kind)]), "Zero value survived or category metric mismatched");
const int expected_rank = i && row.at("value") == rows[i - 1].at("value") ? rows[i - 1].at("rank").get<int>() : int(i + 1);
require(row.at("rank") == expected_rank, "Competition ranking mishandled a tie");
if (i) require(rows[i - 1].at("value").get<int>() >= row.at("value").get<int>(), "Ranking not sorted by active metric");
}
}
void full_world() {
Fixture fixture; const auto &world = *fixture.world;
const auto players_before = world.rows("players"), clubs_before = world.rows("clubs"), manifest_before = world.manifest();
const std::string league = "competition:rankings-0", own = "club:rankings-0-7";
for (int scope = 0; scope < 2; ++scope) {
const auto goals = demo_scorer_rows(world, league, own, scope, 0);
require(goals.size() == 10 && goals[0].at("value") == 19 && goals[9].at("value") == 10, "Gold ten-player goals fixture changed");
for (int kind = 0; kind < 4; ++kind) {
const auto rows = demo_scorer_rows(world, league, own, scope, kind);
check_rows(world, rows, kind, league, own, scope);
for (const auto &r : rows) {
const auto at = r.at("original").get<size_t>();
require(r.at("player") == goals[at].at("player"), "Tabs used different player cohorts");
for (const char *key : {"goals", "assists", "yellow", "red"}) require(r.at(key) == goals[at].at(key), "Statistics changed while selecting category");
}
if (kind == 1) require(rows[0].at("original") == 1 && rows[0].at("value") == 12, "Assists retained goal order");
if (kind == 2) require(rows[0].at("original") == 9 && rows[0].at("value") == 10, "Yellow retained goal order");
if (kind == 3) {
require(rows.size() == 7, "Red zero filter changed");
const std::array<int, 7> ranks{1, 2, 2, 4, 4, 4, 4}, values{3, 2, 2, 1, 1, 1, 1};
for (size_t i = 0; i < 7; ++i) require(rows[i].at("rank") == ranks[i] && rows[i].at("value") == values[i], "Red ties not 1/2/2/4/4/4/4");
}
auto detached = rows; detached[0]["name"] = "Changed returned projection";
require(demo_scorer_rows(world, league, own, scope, kind) == rows, "Projection aliases shared state");
}
}
const auto fair = demo_fairplay_rows(world);
require(fair.size() == 20 && fair[0].at("club") == "club:rankings-0-0", "Fairplay cut domestic clubs or selected foreign league");
std::set<std::string> fair_clubs;
for (size_t i = 0; i < fair.size(); ++i) {
const auto &r = fair[i];
require(fair_clubs.insert(r.at("club").get<std::string>()).second && r.at("rank") == i + 1, "Fairplay duplicated club or changed Gold ordinal ranks");
require(r.at("points").get<int>() == r.at("yellow").get<int>() + 4 * r.at("red").get<int>(), "Fairplay penalty weight wrong");
if (i) require(fair[i - 1].at("points").get<int>() <= r.at("points").get<int>(), "Fairplay not ascending");
}
require(fair[1].at("original") == 1 && fair[2].at("original") == 5 && fair[1].at("points") == fair[2].at("points"), "Fairplay stable tie order changed");
require(demo_scorer_rows(world, "missing", own).empty() && demo_scorer_rows(world, league, "missing", 1).empty() && demo_scorer_rows(world, league, own, -1).empty() && demo_scorer_rows(world, league, own, 0, 4).empty(), "Invalid selection invented replacement players");
require(world.rows("players") == players_before && world.rows("clubs") == clubs_before && world.manifest() == manifest_before, "Read-only rankings mutated catalogue");
}
} // namespace
int main() try {
full_world();
for (int count : {0, 1, 2, 5}) {
Fixture f(1, count); const std::string league = "competition:rankings-0", club = "club:rankings-0-0";
for (int scope = 0; scope < 2; ++scope) {
const auto rows = demo_scorer_rows(*f.world, league, club, scope);
require(rows.size() == size_t(count), "Short/empty outfield squad invented phantom rows");
check_rows(*f.world, rows, 0, league, club, scope);
}
}
Fixture no_clubs(0); require(demo_scorer_rows(*no_clubs.world, "competition:rankings-0", "").empty() && demo_fairplay_rows(*no_clubs.world).empty(), "Empty domestic league unsafe");
Fixture no_de(4, 4, false); require(demo_fairplay_rows(*no_de.world).empty(), "Fairplay silently substituted non-German league");
std::cout << "Modern UI rankings checks passed\n"; return 0;
} catch (const std::exception &error) { std::cerr << error.what() << '\n'; return 1; }