ModernSession::ui_command checks careerId, expectedRevision and owner, runs the engine action and answers command_result or error. A refused command restores the state and both generators; a repeated requestId gets the stored answer, a different command under the same id duplicate_request. The camps read lists maps and camps with a binding, draw-free quote per camp, focus and the season's stays. Tests cover stale revision, foreign owner, unknown camp, booking, repeat, conflict and a refused second booking. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
156 lines
9.3 KiB
C++
156 lines
9.3 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{}; }
|
|
bool truth(const J& j) { const bool* b = std::get_if<bool>(&j.value); return b && *b; }
|
|
} // 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");
|
|
|
|
// camps: read with binding quotes, then camp.book as the first command
|
|
const J camps = session.ui_read(O{{"kind", "camps"}}, revision);
|
|
const auto* list = at(at(camps, "data"), "camps").array();
|
|
check(list && list->size() == session.state().training_camps.size() && !list->empty(), "camp catalogue");
|
|
const std::string camp_id = list && !list->empty() ? s(at((*list)[0], "id")) : "";
|
|
const std::string price = list && !list->empty() ? s(at(at(at((*list)[0], "quote"), "total"), "minor")) : "";
|
|
check(list && !list->empty() && truth(at(at((*list)[0], "quote"), "allowed")), "camp quote allowed");
|
|
uint64_t rev = revision;
|
|
const auto command = [&](const std::string& id, const std::string& expected, const std::string& manager,
|
|
const std::string& camp) {
|
|
return J(O{{"messageType", "command"}, {"requestId", id}, {"expectedRevision", expected},
|
|
{"owner", O{{"managerId", manager}}}, {"kind", "camp.book"}, {"payload", O{{"campId", camp}}}});
|
|
};
|
|
const auto unchanged = modern::save_stream(session.state());
|
|
check(s(at(session.ui_command(command("request:c1", "999", "manager:1", camp_id), rev), "code")) == "stale_revision",
|
|
"stale revision refused");
|
|
check(s(at(session.ui_command(command("request:c2", std::to_string(rev), "manager:9", camp_id), rev), "code")) ==
|
|
"manager_not_active", "foreign owner refused");
|
|
check(s(at(session.ui_command(command("request:c3", std::to_string(rev), "manager:1", "resort:none"), rev), "code")) ==
|
|
"unknown_camp", "unknown camp refused");
|
|
check(rev == revision && modern::save_stream(session.state()) == unchanged, "refused commands change nothing");
|
|
const int64_t cash_before = *at(session.read(O{{"kind", "status"}}), "money").integer();
|
|
const J booked = session.ui_command(command("request:c4", std::to_string(rev), "manager:1", camp_id), rev);
|
|
check(s(at(booked, "messageType")) == "command_result" && s(at(booked, "newRevision")) == std::to_string(revision + 1) &&
|
|
rev == revision + 1, "camp booked, one revision");
|
|
const int64_t cash_after = *at(session.read(O{{"kind", "status"}}), "money").integer();
|
|
check(std::to_string((cash_before - cash_after) * 100) == price, "the quoted price was charged");
|
|
const J again = session.ui_command(command("request:c4", std::to_string(revision), "manager:1", camp_id), rev);
|
|
check(cli::dump_json(again) == cli::dump_json(booked) && rev == revision + 1 &&
|
|
*at(session.read(O{{"kind", "status"}}), "money").integer() == cash_after, "repeated request answered, not booked twice");
|
|
check(s(at(session.ui_command(command("request:c4", std::to_string(rev), "manager:1", camp_id), rev), "code")) ==
|
|
"duplicate_request", "same id, other command");
|
|
const auto after_booking = modern::save_stream(session.state());
|
|
check(s(at(session.ui_command(command("request:c5", std::to_string(rev), "manager:1", camp_id), rev), "code")) ==
|
|
"booked_this_week" && modern::save_stream(session.state()) == after_booking && rev == revision + 1,
|
|
"second booking in a week refused without change");
|
|
const J after = session.ui_read(O{{"kind", "camps"}}, rev);
|
|
check(at(at(after, "data"), "staysThisSeason").integer() && *at(at(after, "data"), "staysThisSeason").integer() == 1 &&
|
|
truth(at(at(after, "data"), "bookedThisWeek")), "camp read shows the stay");
|
|
} 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;
|
|
}
|