// 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 #include 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" && std::string(argv[1]) != "verify-contracts")) throw std::runtime_error("usage: modern_engine_bridge_probe inspect|prepare|verify|verify-lineup|verify-sports|verify-bank|verify-commerce|verify-contracts "); 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()) 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()) 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(); 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()) 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"); } if(std::string(argv[1])=="verify-contracts") { const auto run=[&](const Json &request,const std::string &name){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());return response;}; const auto players=engine_contracts_projection(before).at("rows");const auto first=players[0].at("id").get(),second=players[1].at("id").get(); run(engine_contract_request(before,first,"request:quote-one"),"quote-one");const auto queried=link->snapshot();write("queried.json",queried); if(queried!=link->snapshot() || queried.at("contracts").at("data").at("players")!=before.at("contracts").at("data").at("players"))throw std::runtime_error("pure reads/demands changed actual contracts"); write("queried-saved.json",link->save_copy(out/"queried.obs")); const auto q=engine_contracts_projection(queried).at("quote");auto cancel=engine_contract_cancel(queried,q.at("id"),"request:cancel"); auto bad=cancel;bad["payload"]["quoteId"]="quote:contract-999999";bad["requestId"]="request:wrong-quote";if(link->command(bad).value("code","")!="stale_quote"||link->snapshot()!=queried)throw std::runtime_error("wrong quote cancellation changed owner"); bad=cancel;bad["owner"]["managerId"]="manager:foreign";bad["requestId"]="request:wrong-owner";if(link->command(bad).value("messageType","")!="error"||link->snapshot()!=queried)throw std::runtime_error("foreign owner closed quote"); run(cancel,"cancel");const auto cancelled=link->snapshot();write("cancelled.json",cancelled);if(!cancelled.at("contracts").at("data").at("quote").is_null()||cancelled.at("contracts").at("data").at("players")!=before.at("contracts").at("data").at("players"))throw std::runtime_error("cancel did not clear quote or edited salaries"); write("cancelled-saved.json",link->save_copy(out/"cancelled.obs")); bad=cancel;bad["requestId"]="request:old-revision";if(link->command(bad).value("code","")!="stale_revision"||link->snapshot()!=cancelled)throw std::runtime_error("stale cancel changed state"); run(engine_ticket_command(cancelled,21,"request:owner-released"),"owner-released"); run(engine_contract_request(link->snapshot(),first,"request:quote-reject"),"quote-reject");auto now=link->snapshot();auto q2=engine_contracts_projection(now).at("quote");int year=1;while(year<=4&&q2.at("demands")[size_t(year-1)].is_null())++year;if(year>4)throw std::runtime_error("no available rejection term"); const auto refused=run(engine_contract_offer(now,first,q2.at("id"),year,0,"request:offer-reject"),"offer-reject");if(refused.value("outcome","")!="offer_rejected")throw std::runtime_error("zero offer not actually rejected"); run(engine_contract_request(link->snapshot(),second,"request:quote-accept"),"quote-accept");now=link->snapshot();q2=engine_contracts_projection(now).at("quote");year=2;while(year<=4&&q2.at("demands")[size_t(year-1)].is_null())++year;if(year>4)year=1; const int64_t salary=q2.at("max_salary").get()/100; const auto accepted=run(engine_contract_offer(now,second,q2.at("id"),year,salary,"request:offer-accept"),"offer-accept");if(accepted.value("outcome","")=="offer_rejected")throw std::runtime_error("maximum salary unexpectedly rejected"); const auto after=link->snapshot();write("contracts-after.json",after);bool found=false;const auto final_contracts=engine_contracts_projection(after);for(const auto &p:final_contracts.at("rows"))if(p.at("id")==second)found=p.at("salary")==salary*100&&p.at("months")==year*12; if(!found||!after.at("contracts").at("data").at("quote").is_null())throw std::runtime_error("actual accepted contract wrong or quote retained"); write("saved.json",link->save_copy(out/"contracts.obs"));link.reset();link=EngineBridge::open(world,out/"contracts.obs");auto loaded=link->snapshot();write("loaded.json",loaded);if(loaded.at("contracts").at("data")!=after.at("contracts").at("data"))throw std::runtime_error("contract 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; } }