Files
Dreierpack-Manager/tests/modern_engine_bridge_probe.cpp
T

149 lines
13 KiB
C++

// SPDX-License-Identifier: GPL-3.0-only
// Opt-in private-data probe. Never registered as an unconditional CTest.
#include "modern/ui/engine_bridge.hpp"
#include "modern/app/modern_session.hpp"
#include "modern/content/world_package.hpp"
#include <iostream>
#include <algorithm>
using namespace bmh;
using namespace bmh::modern_ui;
int main(int argc, char **argv) {
try {
if (argc != 4 || (std::string(argv[1]) != "inspect" && std::string(argv[1]) != "prepare" && std::string(argv[1]) != "verify" && std::string(argv[1]) != "verify-lineup" && std::string(argv[1]) != "verify-sports" && std::string(argv[1]) != "verify-bank" && std::string(argv[1]) != "verify-commerce")) throw std::runtime_error("usage: modern_engine_bridge_probe inspect|prepare|verify|verify-lineup|verify-sports|verify-bank|verify-commerce <world.obw> <new-output-directory>");
const fs::path world = argv[2], out = argv[3]; fs::create_directories(out);
const auto write = [&](const char *file, const Json &j) { editor::write_atomic(out / file, j.dump(2)); };
if (std::string(argv[1]) == "prepare") {
auto package = modern::open_world_package(world);
content::Mount mount(world, package.classic); modern::set_current_world(package.world);
modern::app::ModernSession session(mount.root(), 17u);
ui::SetupOptions options; options.mode = 3;
ui::ManagerChoice manager; manager.name = "Codex Engine QA"; manager.master_club = 0;
std::string error; ui::CareerInitializationResult result;
if (!session.confirm_manager(options, manager, error) || !session.finish(result, error) ||
!session.save_game(out / "input.obs", 0, false, error)) throw std::runtime_error(error);
modern::set_current_world(nullptr);
}
auto link = EngineBridge::open(world, out / "input.obs");
const auto before = link->snapshot(); write("before.json", before);
if (before != link->snapshot()) throw std::runtime_error("reads changed the engine projection");
if (std::string(argv[1]) == "verify") {
auto camp = before.at("camps").at("data").at("camps").front();
const auto command = engine_camp_command(before, camp.at("id"), {{"condition", 3}, {"technique", 4}, {"play", 5}}, "request:probe-book");
const auto response = link->command(command); write("command.json", command); write("response.json", response);
if (response.value("messageType", "") != "command_result") throw std::runtime_error(response.dump());
if (link->command(command) != response) throw std::runtime_error("identical request was not idempotent");
const auto after = link->snapshot(); write("after.json", after);
if (!after.at("camps").at("data").at("bookedThisWeek").get<bool>()) throw std::runtime_error("camp not booked");
const auto delta = engine_money_minor(before.at("desktop").at("data").at("accounts").at("cash")) - engine_money_minor(after.at("desktop").at("data").at("accounts").at("cash"));
if (delta != engine_money_minor(camp.at("quote").at("total"))) throw std::runtime_error("wrong actual booking cost");
write("saved.json", link->save_copy(out / "booked.obs")); link.reset();
link = EngineBridge::open(world, out / "booked.obs"); const auto loaded = link->snapshot(); write("loaded.json", loaded);
if (loaded.at("desktop").at("data").at("accounts") != after.at("desktop").at("data").at("accounts") || !loaded.at("camps").at("data").at("bookedThisWeek").get<bool>()) throw std::runtime_error("OBS round trip lost booking");
}
if (std::string(argv[1]) == "verify-lineup") {
(void)engine_lineup_projection(before);
auto order = before.at("lineup").at("data").at("order");
std::swap(order[0], order[11]);
const auto request = engine_lineup_command(before, order, "request:lineup-probe");
const auto response = link->command(request); write("lineup-command.json", request); write("lineup-response.json", response);
if (response.value("messageType", "") != "command_result") throw std::runtime_error(response.dump());
if (link->command(request) != response) throw std::runtime_error("lineup request replay changed response");
const auto after = link->snapshot(); write("lineup-after.json", after);
if (after.at("lineup").at("data").at("order") != order || after.at("desktop").at("data").at("accounts") != before.at("desktop").at("data").at("accounts"))
throw std::runtime_error("lineup order not applied exactly or account changed");
auto stale = request; stale["requestId"] = "request:stale-lineup";
const auto rejected = link->command(stale); write("lineup-rejected.json", rejected);
if (rejected.value("code", "") != "stale_revision" || link->snapshot() != after) throw std::runtime_error("stale lineup changed engine state");
write("lineup-saved.json", link->save_copy(out / "lineup.obs")); link.reset();
link = EngineBridge::open(world, out / "lineup.obs"); const auto loaded = link->snapshot(); write("lineup-loaded.json", loaded);
if (loaded.at("lineup").at("data").at("order") != order) throw std::runtime_error("lineup lost during OBS round trip");
}
if (std::string(argv[1]) == "verify-sports") {
auto tactics = engine_tactics_projection(before); tactics["values"][0] = 1; tactics["values"][7] = 7;
const auto tc = engine_tactics_command(before, tactics.at("values"), "request:sports-tactics");
const auto tr = link->command(tc); write("tactics-command.json", tc); write("tactics-response.json", tr);
if (tr.value("messageType", "") != "command_result" || link->command(tc) != tr) throw std::runtime_error(tr.dump());
const auto tactic_after = link->snapshot(); write("tactics-after.json", tactic_after);
if (engine_tactics_projection(tactic_after).at("values") != tactics.at("values")) throw std::runtime_error("tactics not applied");
auto training = engine_training_projection(tactic_after);
training["types"] = Json::array({1,3,2,2}); training["areas"] = Json::array({1,3,3,1,2,2,2,2,2});
const auto player = training.at("roster")[0].at("id").get<std::string>(); training["players"][player]["intensity"] = 3;
const auto pc = engine_training_command(tactic_after, training, "request:sports-training");
const auto pr = link->command(pc); write("training-command.json", pc); write("training-response.json", pr);
if (pr.value("messageType", "") != "command_result" || link->command(pc) != pr) throw std::runtime_error(pr.dump());
const auto after = link->snapshot(); write("sports-after.json", after); const auto actual = engine_training_projection(after);
if (actual.at("types") != training.at("types") || actual.at("areas") != training.at("areas") || actual.at("players").at(player).at("intensity") != 3 || !actual.at("initialized").get<bool>()) throw std::runtime_error("training not applied");
if (after.at("desktop").at("data").at("accounts") != before.at("desktop").at("data").at("accounts")) throw std::runtime_error("sports changed account");
auto stale = pc; stale["requestId"] = "request:stale-sports";
const auto rejected = link->command(stale); write("sports-rejected.json", rejected);
if (rejected.value("code", "") != "stale_revision" || link->snapshot() != after) throw std::runtime_error("stale sports request changed state");
write("sports-saved.json", link->save_copy(out / "sports.obs")); link.reset();
link = EngineBridge::open(world, out / "sports.obs"); const auto loaded = link->snapshot(); write("sports-loaded.json", loaded);
if (engine_tactics_projection(loaded).at("values") != tactics.at("values") || engine_training_projection(loaded).at("types") != training.at("types") || engine_training_projection(loaded).at("areas") != training.at("areas")) throw std::runtime_error("sports lost in OBS round trip");
}
if (std::string(argv[1]) == "verify-bank") {
const auto M = [](int n) { return Json{{"currency","EUR"},{"minorUnits",2},{"minor",std::to_string(int64_t(n)*100)}}; };
const auto run = [&](const Json &payload, const std::string &id) {
const auto request = engine_bank_command(link->snapshot(),payload,id);
const auto response = link->command(request); write((id.substr(8)+"-response.json").c_str(),response);
if (response.value("messageType","") != "command_result" || link->command(request) != response) throw std::runtime_error(response.dump());
};
run({{"action","deposit"},{"amount",M(10000)}},"request:deposit");
run({{"action","withdraw"},{"amount",M(2500)}},"request:withdraw");
const auto deposited = link->snapshot();
if (engine_money_minor(deposited.at("bank").at("data").at("accounts").at("deposit")) != 750000) throw std::runtime_error("deposit arithmetic wrong");
run({{"action","buy"},{"assetId","stock:0"},{"count",10}},"request:stock-buy");
run({{"action","sell"},{"assetId","stock:0"},{"count",3}},"request:stock-sell");
run({{"action","buy"},{"assetId","fund:0"},{"count",1}},"request:property-buy");
run({{"action","sell"},{"assetId","fund:0"},{"count",1}},"request:property-sell");
auto now = link->snapshot();
const auto tc = engine_ticket_command(now,25,"request:ticket");
const auto tr = link->command(tc); write("ticket-response.json",tr);
if (tr.value("messageType","") != "command_result" || link->command(tc) != tr) throw std::runtime_error(tr.dump());
now = link->snapshot();
auto stale = tc; stale["requestId"] = "request:stale-bank";
if (link->command(stale).value("code","") != "stale_revision" || link->snapshot() != now) throw std::runtime_error("stale bank command changed state");
Json edits = Json::array({{{"field",0},{"value",1000000}}});
auto preview = link->read({{"kind","credit"},{"edits",edits}}); write("credit-preview.json",preview);
(void)engine_credit_projection(now,preview);
if (now != link->snapshot()) throw std::runtime_error("credit preview mutated state");
const auto cc = engine_credit_command(now,preview,edits,"request:credit");
auto cr = link->command(cc); write("credit-response.json",cr);
if (cr.value("messageType","") != "command_result" || link->command(cc) != cr) throw std::runtime_error(cr.dump());
const auto after = link->snapshot(); write("bank-after.json",after);
write("bank-saved.json",link->save_copy(out / "bank.obs")); link.reset();
link = EngineBridge::open(world,out / "bank.obs"); const auto loaded = link->snapshot(); write("bank-loaded.json",loaded);
for (const auto *kind : {"bank","credit","stadium"}) if (loaded.at(kind).at("data") != after.at(kind).at("data")) throw std::runtime_error("finance lost during OBS round trip");
}
if (std::string(argv[1]) == "verify-commerce") {
const auto run = [&](const Json &request, const std::string &name) {
const auto response=link->command(request);write((name+"-response.json").c_str(),response);
if(response.value("messageType","")!="command_result" || link->command(request)!=response)throw std::runtime_error(response.dump());
};
run(engine_environment_command(before,"environment:doctor",3,"request:environment"),"environment");
const auto env=link->snapshot();write("environment-after.json",env);
if(env.at("desktop").at("data").at("accounts")!=before.at("desktop").at("data").at("accounts"))throw std::runtime_error("monthly environment budget debited immediately");
for(int slot:{0,14,15}) {
const auto now=link->snapshot(),view=engine_advertising_projection(now);
const auto &choices=view.at("slots")[size_t(slot)].at("choices");
auto chosen=std::find_if(choices.begin(),choices.end(),[](const auto &q){return q.at("can_accept")==true;});
if(chosen==choices.end())throw std::runtime_error("no test advertising offer");
run(engine_advertising_command(now,chosen->at("offer_id"),"request:ad-"+std::to_string(slot)),"ad-"+std::to_string(slot));
}
for(const auto *id:{"budget:press","budget:fans","budget:merchandise"}) {
run(engine_budget_command(link->snapshot(),id,1000,std::string("request:")+id),std::string(id).substr(7));
}
const auto after=link->snapshot();write("commerce-after.json",after);
auto stale=engine_environment_command(before,"environment:doctor",4,"request:commerce-stale");
const auto rejected=link->command(stale);write("stale-rejected.json",rejected);
if(rejected.value("code","")!="stale_revision" || link->snapshot()!=after)throw std::runtime_error("stale commerce command mutated state");
write("commerce-saved.json",link->save_copy(out/"commerce.obs"));link.reset();link=EngineBridge::open(world,out/"commerce.obs");
const auto loaded=link->snapshot();write("commerce-loaded.json",loaded);
for(const auto *kind:{"environment","advertising","bank"})if(loaded.at(kind).at("data")!=after.at(kind).at("data"))throw std::runtime_error("commerce OBS round trip lost state");
}
std::cout << "engine bridge " << argv[1] << " passed\n"; return 0;
} catch (const std::exception &e) { std::cerr << e.what() << '\n'; return 1; }
}