Files
Dreierpack-Manager/tests/modern_ui_contract_tests.cpp
T
ClaudeandClaude Opus 5.5 fec4c6c7e7 Modern UI contract: squad read and contract tests
The squad read lists a squad (own club, any league club or a national team
with a nominated squad) with ids, positions, values, absences and, for the
manager's own club, monthly salary and remaining contract months.
modern_ui_contract_tests checks envelope, identities, money, errors and
that reads leave the state unchanged.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-10-27 08:19:55 +01:00

115 lines
5.6 KiB
C++

// SPDX-License-Identifier: GPL-3.0-only
// The engine side of the Modern interface contract (docs/new_engine/ui-anbindung.md): the
// envelope, identities, money, errors and that a read changes nothing. Needs game data
// (a Modern session on the Classic data).
#include <cstdio>
#include <string>
#include <variant>
#include "cli/json.hpp"
#include "modern/app/modern_session.hpp"
#include "modern/content/ui_contract.hpp"
#include "modern/game/save.hpp"
using namespace bmh;
using J = cli::Json;
using O = J::Object;
namespace {
int failures = 0;
void check(bool ok, const char* what) {
if (!ok) {
std::fprintf(stderr, "FAIL: %s\n", what);
++failures;
}
}
const J& at(const J& j, const char* key) {
static const J none;
const auto* o = j.object();
if (!o) return none;
const auto it = o->find(key);
return it == o->end() ? none : it->second;
}
std::string s(const J& j) { return j.string() ? *j.string() : std::string{}; }
} // namespace
int main(int argc, char** argv) {
if (argc < 2) {
std::fprintf(stderr, "usage: modern_ui_contract_tests <BMH data>\n");
return 2;
}
try {
modern::app::ModernSession session(argv[1], 1u);
ui::SetupOptions options;
ui::ManagerChoice choice;
choice.name = "TEST";
choice.master_club = 0;
std::string error;
if (!session.confirm_manager(options, choice, error)) throw std::runtime_error("manager: " + error);
ui::CareerInitializationResult init;
if (!session.finish(init, error)) throw std::runtime_error("career start: " + error);
const uint64_t revision = init.desktop.revision;
const auto before = modern::save_stream(session.state());
const std::string career = session.state().career_id;
// session: envelope and identities
const J sess = session.ui_read(O{{"kind", "session"}, {"requestId", "request:1"}}, revision);
check(s(at(sess, "messageType")) == "snapshot", "session is a snapshot");
check(s(at(sess, "requestId")) == "request:1", "request id echoed");
const J& meta = at(sess, "meta");
check(s(at(meta, "contractVersion")) == "bmh-modern-ui@draft1", "contract version");
check(s(at(meta, "careerId")).starts_with("career:") && s(at(meta, "careerId")).size() == 23, "career id");
check(s(at(meta, "stateRevision")) == std::to_string(revision), "state revision as decimal string");
check(s(at(at(meta, "scope"), "managerId")) == "manager:1", "first manager id");
check(s(at(at(meta, "currency"), "code")) == "EUR" && at(at(meta, "currency"), "minorUnits").integer() &&
*at(at(meta, "currency"), "minorUnits").integer() == 2, "currency");
const auto* managers = at(at(sess, "data"), "managers").array();
check(managers && managers->size() == 1 && s(at((*managers)[0], "status")) == "active", "one active manager");
// desktop: money in minor units, menus
const J desk = session.ui_read(O{{"kind", "desktop"}}, revision);
const J status = session.read(O{{"kind", "status"}});
const int64_t euros = at(status, "money").integer() ? *at(status, "money").integer() : -1;
check(s(at(at(at(at(desk, "data"), "accounts"), "cash"), "minor")) == std::to_string(euros * 100), "cash in cents");
const auto* menus = at(at(desk, "data"), "menuAvailability").object();
check(menus && menus->size() == 16 && s(at(menus->at("bank"), "status")) == "available", "menus at a club desk");
// squad: own club with salaries, another club without
const J own = session.ui_read(O{{"kind", "squad"}}, revision);
const auto* players = at(at(own, "data"), "players").array();
check(players && players->size() >= 11, "own squad");
check(players && !players->empty() && at((*players)[0], "salaryMonthly").object(), "own salaries known");
const J other = session.ui_read(O{{"kind", "squad"}, {"teamId", "team:5"}}, revision);
const auto* others = at(at(other, "data"), "players").array();
check(others && !others->empty() && std::holds_alternative<std::nullptr_t>(at((*others)[0], "salaryMonthly").value), "foreign salaries unknown");
// errors
check(s(at(session.ui_read(O{{"kind", "nonsense"}}, revision), "code")) == "unknown_read", "unknown read");
check(s(at(session.ui_read(O{{"kind", "desktop"}, {"managerId", "manager:7"}}, revision), "code")) ==
"manager_not_active", "other manager refused");
check(s(at(session.ui_read(O{{"kind", "squad"}, {"teamId", "club:none"}}, revision), "code")) == "unknown_team",
"unknown team");
// reads change nothing
check(modern::save_stream(session.state()) == before, "reads leave the state unchanged");
check(session.state().career_id == career, "career id unchanged");
// identities: never reused, nothing set is replaced
modern::ModernState copy = session.state();
modern::ensure_ui_identities(copy);
check(copy.manager_ids == session.state().manager_ids && copy.career_id == career, "identities stable");
copy.manager_ids.clear();
modern::ensure_ui_identities(copy);
check(copy.manager_ids.size() == 1 && copy.manager_ids[0] == "manager:2", "a freed slot gets a new id");
// money
check(s(at(modern::ui_money(15), "minor")) == "1500" && s(at(modern::ui_money(-3), "minor")) == "-300", "money");
} catch (const std::exception& ex) {
std::fprintf(stderr, "FAIL: %s\n", ex.what());
return 1;
}
if (failures == 0) std::printf("modern ui contract tests passed\n");
return failures == 0 ? 0 : 1;
}