Port Bank, balance and contracts to the native Modern UI (by Codex)
This commit is contained in:
1 parent
edd59db604
commit
2114598fba
33 files changed
+2688
-25
No files matched your search
@@ -100,7 +100,7 @@ int main(int argc, char **argv) try {
|
||||
std::ostringstream css;
|
||||
for (const auto *name : {"theme.rcss", "ticket.rcss", "sports.rcss",
|
||||
"calendar.rcss", "reports.rcss", "report_details.rcss",
|
||||
"report_pickers.rcss", "indoor.rcss", "planning.rcss", "almanac.rcss"}) {
|
||||
"report_pickers.rcss", "indoor.rcss", "planning.rcss", "almanac.rcss", "finance.rcss", "bank.rcss", "contracts.rcss", "amount.rcss"}) {
|
||||
std::ifstream input(root / name);
|
||||
require(bool(input), "Production stylesheet missing");
|
||||
css << input.rdbuf() << '\n';
|
||||
|
||||
@@ -0,0 +1,322 @@
|
||||
// SPDX-License-Identifier: GPL-3.0-only
|
||||
#include "modern/ui/demo_finance.hpp"
|
||||
#include <chrono>
|
||||
#include <fstream>
|
||||
#include <iostream>
|
||||
#include <set>
|
||||
#include <stdexcept>
|
||||
|
||||
using namespace bmh::modern_ui;
|
||||
namespace {
|
||||
void require(bool value, const char *message) {
|
||||
if (!value) throw std::runtime_error(message);
|
||||
}
|
||||
std::string player(int i) { return "player:finance-test-" + std::to_string(i); }
|
||||
struct Fixture {
|
||||
fs::path root;
|
||||
std::shared_ptr<const Catalog> world;
|
||||
explicit Fixture(Json precision = 0, bool populated = true) {
|
||||
root = fs::temp_directory_path() / ("openbmh-finance-ui-" +
|
||||
std::to_string(std::chrono::steady_clock::now().time_since_epoch().count()));
|
||||
fs::create_directories(root / "world");
|
||||
const auto write = [&](const std::string &file, const Json &value) {
|
||||
std::ofstream out(root / file); out << value.dump();
|
||||
require(bool(out), "Cannot write synthetic catalogue");
|
||||
};
|
||||
write("manifest.json", {{"format", "openbmh-world"}, {"kind", "world"},
|
||||
{"format_version", "1.1"}, {"id", "world:synthetic-finance-ui"},
|
||||
{"start_date", "2026-07-01"}, {"currency", {{"code", "EUR"}, {"minor_units", precision}}},
|
||||
{"requires", {"editor:world-v1"}}, {"x-editor", {{"profile", "editor-world-v1"}}}});
|
||||
write("world/nations.json", {{"items", {{{"id", "nation:de"}, {"name", "Synthetic nation"}}}}});
|
||||
write("world/competitions.json", {{"items", {{{"id", "competition:finance-test"},
|
||||
{"name", "Synthetic league"}, {"nation", "nation:de"}, {"type", "league"}}}}});
|
||||
Json players = Json::array(), clubs = Json::array(), contracts = Json::array();
|
||||
if (populated) {
|
||||
clubs = {{{"id", "club:finance-home"}, {"name", "Synthetic home"}, {"nation", "nation:de"},
|
||||
{"competition", "competition:finance-test"}},
|
||||
{{"id", "club:finance-other"}, {"name", "Synthetic other"}, {"nation", "nation:de"},
|
||||
{"competition", "competition:finance-test"}}};
|
||||
for (int i = 0; i < 13; ++i) {
|
||||
Json p = {{"id", player(i)}, {"name", "Synthetic player " + std::to_string(i)},
|
||||
{"team", "club:finance-home"}, {"squad_order", i}, {"positions", {i ? "mid" : "gk"}}};
|
||||
if (i == 0) {
|
||||
p["birth_date"] = "2000-07-02";
|
||||
p["attributes"] = {{"strength", 82}, {"condition", 80}, {"technique", 75}, {"form", 77},
|
||||
{"shooting", 61}, {"passing", 72}, {"teamplay", 68}};
|
||||
}
|
||||
if (i == 1) p["age_at_start"] = 31;
|
||||
players.push_back(p);
|
||||
}
|
||||
players.push_back({{"id", "player:finance-other"}, {"team", "club:finance-other"},
|
||||
{"name", "Synthetic other player"}, {"positions", {"def"}}});
|
||||
const int64_t factor = precision == 2 ? 100 : 1;
|
||||
const auto add = [&](int i, Json salary, Json extra = Json::object()) {
|
||||
Json c = {{"id", "contract:synthetic-" + std::to_string(contracts.size())},
|
||||
{"person", player(i)}, {"team", "club:finance-home"}};
|
||||
if (!salary.is_null()) c["salary"] = salary;
|
||||
for (const auto &item : extra.items()) c[item.key()] = item.value();
|
||||
contracts.push_back(c);
|
||||
};
|
||||
add(0, 120000 * factor);
|
||||
add(1, nullptr, {{"initialization", {{"policy", "classic-player-contract@1"},
|
||||
{"source_identity_word", 65535}, {"source_skill_word", 65535}}}});
|
||||
add(3, 0);
|
||||
add(4, 120000 * factor); add(4, 240000 * factor);
|
||||
add(5, 120000 * factor, {{"end", "2026-06-30"}});
|
||||
add(6, 120000 * factor, {{"start", "2026-07-02"}});
|
||||
add(7, 120000 * factor, {{"team", "club:finance-other"}});
|
||||
add(8, 120006 * factor, {{"start", "2026-07-01"}, {"end", "2026-07-01"}});
|
||||
add(9, -1, {{"initialization", Json::object()}});
|
||||
add(10, nullptr, {{"initialization", Json::object()}, {"start", "2026-02-30"}});
|
||||
add(11, uint64_t(-1));
|
||||
add(12, "not-a-salary");
|
||||
contracts.push_back({{"id", "contract:synthetic-other"}, {"person", "player:finance-other"},
|
||||
{"team", "club:finance-other"}, {"initialization", Json::object()}});
|
||||
}
|
||||
write("world/clubs.json", {{"items", clubs}});
|
||||
write("world/players.json", {{"items", players}});
|
||||
write("world/contracts.json", {{"items", contracts}});
|
||||
world = Catalog::open(root);
|
||||
}
|
||||
~Fixture() { std::error_code ec; fs::remove_all(root, ec); }
|
||||
};
|
||||
Json legacy(int64_t factor) {
|
||||
return {{"cash", 12450000 * factor}, {"deposit", 600000 * factor}, {"debt", 0},
|
||||
{"ticket", 13}, {"date", "2026-07-01"},
|
||||
{"managers", {{{"club", "club:finance-home"}, {"name", "Synthetic manager"}}}},
|
||||
{"unrelated", {{"revision_marker", 97}, {"other_plan", {4, 5, 6}}}}};
|
||||
}
|
||||
void edit(Json &state, const Catalog &world, const Json &action) {
|
||||
const auto error = edit_demo_finance(state, action, world);
|
||||
if (!error.empty()) throw std::runtime_error("Unexpected rejection: " + error + " " + action.dump());
|
||||
require(valid_demo_finance(state, world), "Accepted action left invalid state");
|
||||
}
|
||||
void reject(Json &state, const Catalog &world, const Json &action) {
|
||||
const auto before = state;
|
||||
require(!edit_demo_finance(state, action, world).empty() && state == before,
|
||||
"Rejected action mutated state or was accepted");
|
||||
}
|
||||
int64_t quote(const Json &state, const Catalog &world, const char *kind, int index = -1) {
|
||||
Json request = {{"kind", kind}};
|
||||
if (index >= 0) request["index"] = index;
|
||||
const auto result = demo_bank_quote(state, request, world);
|
||||
require(result.value("ok", false) && result.at("min") == 1,
|
||||
"Bank quote failed or used zero minimum");
|
||||
return result.at("max").get<int64_t>();
|
||||
}
|
||||
int64_t net(const Json &state, const Catalog &world) {
|
||||
return demo_bank_summary(state, world).at("net").get<int64_t>();
|
||||
}
|
||||
void check_currency(int precision) {
|
||||
Fixture fixture(precision);
|
||||
const auto &world = *fixture.world;
|
||||
const auto factor = precision ? 100 : 1;
|
||||
const auto original_players = world.rows("players"), original_contracts = world.rows("contracts");
|
||||
auto state = legacy(factor);
|
||||
const auto original_legacy = state;
|
||||
require(!valid_demo_finance(state, world) && migrate_demo_finance(state, world) &&
|
||||
valid_demo_finance(state, world), "Finance legacy migration failed");
|
||||
require(finance_currency_factor(world) == factor && state.at("cash") == original_legacy.at("cash") &&
|
||||
state.at("deposit") == original_legacy.at("deposit") && state.at("debt") == original_legacy.at("debt") &&
|
||||
state.at("unrelated") == original_legacy.at("unrelated") && !state["finance"].contains("cash"),
|
||||
"Migration replaced sole shared balances or unrelated state");
|
||||
const auto &contracts = state.at("contracts").at("players");
|
||||
require(contracts.at(player(0)).at("salary") == 10000 * factor &&
|
||||
contracts.at(player(0)).at("source") == "obw_season" &&
|
||||
contracts.at(player(1)).at("salary") == 5250 * factor &&
|
||||
contracts.at(player(1)).at("source") == "demo" &&
|
||||
contracts.at(player(1)).at("months") == 36 &&
|
||||
contracts.at(player(3)).at("salary") == 0 &&
|
||||
contracts.at(player(3)).at("source") == "obw_season" &&
|
||||
contracts.at(player(8)).at("salary") == 10001 * factor,
|
||||
"Salary season/month conversion, rounding, source or explicit zero incorrect");
|
||||
for (int i : {2, 4, 5, 6, 7, 9, 10, 11, 12})
|
||||
require(contracts.at(player(i)).at("salary").is_null() &&
|
||||
contracts.at(player(i)).at("source") == "unavailable", "Unavailable or ambiguous salary was invented");
|
||||
const auto rows = demo_contract_rows(state, world);
|
||||
require(rows.at("salary_total") == 25251 * factor && rows.at("salary_known") == 4 &&
|
||||
rows.at("salary_unknown") == 9 && rows.at("rows")[0].at("age") == 25 &&
|
||||
rows.at("rows")[1].at("age") == 31 && rows.at("rows")[0].at("position") == "gk" &&
|
||||
rows.at("rows")[0].at("shoot") == 61 && rows.at("rows")[0].at("pass") == 72 &&
|
||||
rows.at("rows")[0].at("team") == 68 && rows.at("rows")[0].at("games").is_null() &&
|
||||
rows.at("rows")[0].at("grade").is_null(), "Contract rows lost real values or invented game statistics");
|
||||
const auto assets = demo_bank_assets(world);
|
||||
require(assets.at("properties").size() == 6 && assets.at("stocks").size() == 12 &&
|
||||
assets.at("properties")[5].at("price") == 3650000 * factor &&
|
||||
assets.at("stocks")[0].at("price") == 124 * factor &&
|
||||
assets.at("stocks")[0].at("previous") == 127 * factor && demo_bank_terms() == Json::array({6,12,18,24,30,36,42,48,54,60}),
|
||||
"Gold asset catalogue, terms or currency scaling changed");
|
||||
const auto initial = state;
|
||||
const auto total = net(state, world);
|
||||
require(quote(state, world, "deposit") == 12450000 && quote(state, world, "withdraw") == 600000 &&
|
||||
quote(state, world, "loan") == 5000000 && quote(state, world, "repay") == 0,
|
||||
"Bank maximum is in raw minor units or ignores balance limits");
|
||||
edit(state, world, {{"kind", "deposit"}, {"value", 100000}});
|
||||
require(state.at("cash") == 12350000 * factor && state.at("deposit") == 700000 * factor &&
|
||||
net(state, world) == total, "Deposit failed value conservation or unit conversion");
|
||||
edit(state, world, {{"kind", "withdraw"}, {"value", 100000}});
|
||||
require(state.at("cash") == initial.at("cash") && state.at("deposit") == initial.at("deposit"),
|
||||
"Withdrawal did not reverse deposit");
|
||||
edit(state, world, {{"kind", "term"}, {"value", 24}});
|
||||
edit(state, world, {{"kind", "loan"}, {"value", 1000000}});
|
||||
require(state.at("debt") == 1000000 * factor && net(state, world) == total &&
|
||||
quote(state, world, "loan") == 0, "Loan changed net assets or permits duplicate loans");
|
||||
reject(state, world, {{"kind", "term"}, {"value", 12}});
|
||||
reject(state, world, {{"kind", "loan"}, {"value", 1}});
|
||||
const auto before_projection = state;
|
||||
const auto summary = demo_bank_summary(state, world);
|
||||
require(summary.at("monthly_rate") == 48334 * factor && summary.at("credit_interest") == 80000 * factor &&
|
||||
summary.at("schedule").size() == 25 && summary.at("schedule")[0].at("remaining") == 1000000 * factor &&
|
||||
summary.at("schedule").back().at("remaining") == 0 && summary.at("schedule_example") == false && state == before_projection,
|
||||
"Interest/rate/schedule projection is incorrect or booked effects");
|
||||
edit(state, world, {{"kind", "repay"}, {"value", 500000}});
|
||||
edit(state, world, {{"kind", "repay"}, {"value", 500000}});
|
||||
require(state.at("cash") == initial.at("cash") && state.at("debt") == 0 && net(state, world) == total,
|
||||
"Loan repayment did not conserve balances");
|
||||
edit(state, world, {{"kind", "term"}, {"value", 60}});
|
||||
require(demo_bank_summary(state, world).at("schedule_example") == true &&
|
||||
demo_bank_summary(state, world).at("schedule_principal") == 1000000 * factor,
|
||||
"No-loan schedule is not marked as a currency-correct example");
|
||||
edit(state, world, {{"kind", "property-buy"}, {"index", 2}, {"value", 2}});
|
||||
require(state["finance"]["properties"][2] == 2 && state["finance"]["properties"][0] == 1 &&
|
||||
net(state, world) == total, "Property acquisition changed total value");
|
||||
edit(state, world, {{"kind", "property-sell"}, {"index", 0}, {"value", 1}});
|
||||
require(state["finance"]["properties"][0] == 0 && state["finance"]["properties"][2] == 2 &&
|
||||
net(state, world) == total, "Sale ignored chosen owned property type");
|
||||
reject(state, world, {{"kind", "property-sell"}, {"index", 0}, {"value", 1}});
|
||||
edit(state, world, {{"kind", "stock-buy"}, {"index", 11}, {"value", 1000}});
|
||||
require(state["finance"]["stocks"][11] == 1000 && net(state, world) == total,
|
||||
"Stock purchase did not conserve synthetic asset value");
|
||||
reject(state, world, {{"kind", "stock-sell"}, {"index", 1}, {"value", 1}});
|
||||
edit(state, world, {{"kind", "stock-sell"}, {"index", 11}, {"value", 1000}});
|
||||
require(state["finance"]["stocks"][11] == 0 && net(state, world) == total, "Stock sale changed total value");
|
||||
const auto current_balance = demo_balance_rows(state, world);
|
||||
require(current_balance.at("income").size() == 7 && current_balance.at("costs").size() == 11 &&
|
||||
current_balance.at("cash") == state.at("cash") && current_balance.at("ticket") == 13 * factor,
|
||||
"Balance projection duplicated cash/ticket or lost Gold categories");
|
||||
const auto cash = state.at("cash");
|
||||
edit(state, world, {{"kind", "plan"}, {"index", 0}, {"value", 1000}});
|
||||
require(state.at("cash") == cash && demo_balance_rows(state, world).at("costs") == current_balance.at("costs") &&
|
||||
demo_balance_rows(state, world).at("plan")[0].at("amount") == 1000 * factor,
|
||||
"Expense plan booked cash or changed fictional actual expenses");
|
||||
reject(state, world, {{"kind", "plan"}, {"index", 0}, {"value", 1234}});
|
||||
reject(state, world, {{"kind", "plan"}, {"index", 0}, {"value", 99999999}});
|
||||
edit(state, world, {{"kind", "plan"}, {"index", 10}, {"value", 99999000}});
|
||||
state["balance"]["plan"][1] = 123 * factor + (precision ? 45 : 0);
|
||||
const auto legacy_plan = state;
|
||||
require(valid_demo_finance(state, world) && migrate_demo_finance(state, world) && state == legacy_plan,
|
||||
"Migration repaired or rejected a valid older non-step plan value");
|
||||
edit(state, world, {{"kind", "insurance"}, {"player", player(0)}, {"value", 24}});
|
||||
edit(state, world, {{"kind", "bonus"}, {"index", 6}, {"value", 255}});
|
||||
edit(state, world, {{"kind", "contract-offer"}, {"player", player(0)}, {"salary", 12345}, {"months", 18}});
|
||||
require(state["contracts"]["players"][player(0)]["salary"] == 12345 * factor &&
|
||||
state["contracts"]["players"][player(0)]["months"] == 18 &&
|
||||
state["contracts"]["players"][player(0)]["insurance"] == 24 &&
|
||||
state["contracts"]["players"][player(0)]["source"] == "offer" && state.at("cash") == cash,
|
||||
"Contract offer converted units twice, reset insurance or charged cash");
|
||||
edit(state, world, {{"kind", "contract-offer"}, {"player", player(2)}, {"salary", 0}, {"months", 1}});
|
||||
edit(state, world, {{"kind", "contract-offer"}, {"player", player(3)}, {"salary", 200000}, {"months", 48}});
|
||||
auto near_cap = initial; near_cap["cash"] = 99999999LL * factor - 2 * factor;
|
||||
require(quote(near_cap, world, "loan") == 2 && quote(near_cap, world, "withdraw") == 2 &&
|
||||
quote(near_cap, world, "property-sell", 0) == 0, "Cash cap is ignored by loan, withdrawal or sale");
|
||||
reject(near_cap, world, {{"kind", "loan"}, {"value", 3}});
|
||||
edit(near_cap, world, {{"kind", "loan"}, {"value", 2}});
|
||||
require(near_cap["cash"] == 99999999LL * factor && quote(near_cap, world, "withdraw") == 0,
|
||||
"Cash cap exact boundary failed");
|
||||
auto full_asset = initial; full_asset["finance"]["stocks"][0] = 9998;
|
||||
require(quote(full_asset, world, "stock-buy", 0) == 1, "Asset quantity cap missing");
|
||||
reject(full_asset, world, {{"kind", "stock-buy"}, {"index", 0}, {"value", 2}});
|
||||
auto stale = initial;
|
||||
const auto stale_max = quote(stale, world, "stock-buy", 11);
|
||||
edit(stale, world, {{"kind", "deposit"}, {"value", quote(stale, world, "deposit")}});
|
||||
reject(stale, world, {{"kind", "stock-buy"}, {"index", 11}, {"value", stale_max}});
|
||||
auto empty_cash = initial; empty_cash["cash"] = 0;
|
||||
require(quote(empty_cash, world, "deposit") == 0 && quote(empty_cash, world, "stock-buy", 0) == 0,
|
||||
"Zero cash quote did not disable confirmation");
|
||||
reject(empty_cash, world, {{"kind", "deposit"}, {"value", 0}});
|
||||
for (const auto &action : std::vector<Json>{nullptr, Json::array(), "bad", Json::object(),
|
||||
{{"kind", 1}}, {{"kind", "unknown"}, {"value", 1}}, {{"kind", "deposit"}},
|
||||
{{"kind", "deposit"}, {"value", -1}}, {{"kind", "deposit"}, {"value", true}},
|
||||
{{"kind", "deposit"}, {"value", 1.0}}, {{"kind", "deposit"}, {"value", uint64_t(-1)}},
|
||||
{{"kind", "deposit"}, {"value", 99999999999999LL}},
|
||||
{{"kind", "deposit"}, {"value", 1}, {"cash", 0}},
|
||||
{{"kind", "property-buy"}, {"index", 6}, {"value", 1}},
|
||||
{{"kind", "stock-buy"}, {"index", uint64_t(-1)}, {"value", 1}},
|
||||
{{"kind", "stock-buy"}, {"index", 0.0}, {"value", 1}},
|
||||
{{"kind", "term"}, {"value", 7}}, {{"kind", "term"}, {"value", 66}},
|
||||
{{"kind", "plan"}, {"index", 11}, {"value", 1000}},
|
||||
{{"kind", "bonus"}, {"index", 7}, {"value", 1}},
|
||||
{{"kind", "bonus"}, {"index", 0}, {"value", 256}},
|
||||
{{"kind", "insurance"}, {"player", player(0)}, {"value", 6}},
|
||||
{{"kind", "insurance"}, {"player", "player:finance-other"}, {"value", 12}},
|
||||
{{"kind", "contract-offer"}, {"player", player(0)}, {"salary", 200001}, {"months", 12}},
|
||||
{{"kind", "contract-offer"}, {"player", player(0)}, {"salary", 1000}, {"months", 0}},
|
||||
{{"kind", "contract-offer"}, {"player", player(0)}, {"salary", 1000}, {"months", 49}},
|
||||
{{"kind", "contract-offer"}, {"player", player(0)}, {"salary", "1000"}, {"months", 12}}})
|
||||
reject(state, world, action);
|
||||
std::vector<Json> bad_states;
|
||||
for (const auto &value : std::vector<Json>{-1, true, 1.5, uint64_t(-1), 99999999999999LL}) {
|
||||
auto bad = state; bad["cash"] = value; bad_states.push_back(bad);
|
||||
}
|
||||
auto bad = state; bad["debt"] = 5000001LL * factor; bad_states.push_back(bad);
|
||||
bad = state; bad["finance"]["cash"] = 0; bad_states.push_back(bad);
|
||||
bad = state; bad["finance"].erase("term"); bad_states.push_back(bad);
|
||||
bad = state; bad["finance"]["properties"][0] = 10000; bad_states.push_back(bad);
|
||||
bad = state; bad["finance"]["stocks"].push_back(0); bad_states.push_back(bad);
|
||||
bad = state; bad["balance"]["plan"][0] = -1; bad_states.push_back(bad);
|
||||
bad = state; bad["balance"]["plan"].erase(bad["balance"]["plan"].end() - 1); bad_states.push_back(bad);
|
||||
bad = state; bad["contracts"]["players"].erase(player(0)); bad_states.push_back(bad);
|
||||
bad = state; bad["contracts"]["players"]["player:finance-other"] = bad["contracts"]["players"][player(0)]; bad_states.push_back(bad);
|
||||
bad = state; bad["contracts"]["club"] = "club:finance-other"; bad_states.push_back(bad);
|
||||
bad = state; bad["contracts"]["players"][player(0)]["salary"] = -1; bad_states.push_back(bad);
|
||||
bad = state; bad["contracts"]["players"][player(0)]["source"] = "unavailable"; bad_states.push_back(bad);
|
||||
bad = state; bad["contracts"]["players"][player(0)]["insurance"] = 1; bad_states.push_back(bad);
|
||||
bad = state; bad["contracts"]["bonuses"][0] = 256; bad_states.push_back(bad);
|
||||
bad = state; bad["managers"][0]["club"] = ""; bad_states.push_back(bad);
|
||||
for (auto &invalid : bad_states) {
|
||||
require(!valid_demo_finance(invalid, world), "Corrupt finance state accepted");
|
||||
reject(invalid, world, {{"kind", "term"}, {"value", 12}});
|
||||
const auto before = invalid;
|
||||
require(!migrate_demo_finance(invalid, world) && invalid == before,
|
||||
"Migration repaired present invalid finance data");
|
||||
}
|
||||
auto reset = state;
|
||||
reset["managers"][0]["club"] = "club:finance-other";
|
||||
require(!valid_demo_finance(reset, world), "Manager change inherited another roster's contracts");
|
||||
reset["contracts"] = demo_contracts(world, "club:finance-other");
|
||||
require(valid_demo_finance(reset, world) && reset["contracts"]["players"].size() == 1 &&
|
||||
reset["finance"] == state["finance"] && reset["cash"] == state["cash"],
|
||||
"Manager contract reset altered shared bank holdings");
|
||||
auto migrated = state; migrated.erase("balance"); migrated.erase("finance");
|
||||
const auto existing_contracts = migrated["contracts"];
|
||||
require(migrate_demo_finance(migrated, world) && migrated["contracts"] == existing_contracts,
|
||||
"Migration reset an existing valid contract draft");
|
||||
require(valid_demo_finance(Json::parse(state.dump()), world), "Finance save roundtrip invalid");
|
||||
if (precision == 2) {
|
||||
auto fractional = initial; fractional["cash"] = 12345; fractional["deposit"] = 876;
|
||||
require(quote(fractional, world, "deposit") == 123 && quote(fractional, world, "stock-buy", 0) == 0,
|
||||
"Quote did not convert raw cents into whole-unit limits");
|
||||
edit(fractional, world, {{"kind", "deposit"}, {"value", 123}});
|
||||
require(fractional["cash"] == 45 && fractional["deposit"] == 13176,
|
||||
"Whole-unit deposit lost pre-existing fractional balances");
|
||||
}
|
||||
require(state["unrelated"] == original_legacy["unrelated"] && world.rows("players") == original_players &&
|
||||
world.rows("contracts") == original_contracts, "Presentation finance modified world or unrelated state");
|
||||
}
|
||||
} // namespace
|
||||
int main() try {
|
||||
check_currency(0); check_currency(2);
|
||||
for (const auto &precision : std::vector<Json>{1, 3, -1, true, "2", uint64_t(-1)}) {
|
||||
Fixture malformed(precision);
|
||||
auto state = legacy(1); const auto before = state;
|
||||
require(!migrate_demo_finance(state, *malformed.world) && state == before,
|
||||
"Unsupported currency precision was silently substituted");
|
||||
}
|
||||
Fixture empty(0, false); auto state = Json{{"cash", 0}, {"deposit", 0}, {"debt", 0}};
|
||||
require(migrate_demo_finance(state, *empty.world) && valid_demo_finance(state, *empty.world) &&
|
||||
demo_contract_rows(state, *empty.world).at("rows").empty(), "Empty catalogue unsafe");
|
||||
std::cout << "Modern UI finance checks passed\n";
|
||||
return 0;
|
||||
} catch (const std::exception &error) {
|
||||
std::cerr << error.what() << '\n'; return 1;
|
||||
}
|
||||
@@ -2,6 +2,7 @@
|
||||
#include "modern/ui/demo_sports.hpp"
|
||||
#include "modern/ui/demo_calendar.hpp"
|
||||
#include "modern/ui/demo_reports.hpp"
|
||||
#include "modern/ui/demo_finance.hpp"
|
||||
#include "modern/ui/desktop.hpp"
|
||||
#include "modern/ui/gui_assets.hpp"
|
||||
#include "modern/ui/model.hpp"
|
||||
@@ -24,6 +25,134 @@ void table(const fs::path &root, const std::string &name, const Json &rows) {
|
||||
name == "assets" ? "assets/index.json" : "world/" + name + ".json",
|
||||
{{"schema", name + "@1.0"}, {"items", rows}});
|
||||
}
|
||||
void check_finance_source(const fs::path &root,
|
||||
const std::shared_ptr<const Catalog> &base) {
|
||||
const auto folder = root / "finance-source";
|
||||
auto manifest = base->manifest();
|
||||
manifest["id"] = "world:synthetic-finance-source";
|
||||
manifest["currency"] = {{"code", "EUR"}, {"minor_units", 2}};
|
||||
write(folder, "manifest.json", manifest);
|
||||
table(folder, "nations", base->rows("nations"));
|
||||
table(folder, "competitions", base->rows("competitions"));
|
||||
auto clubs = base->rows("clubs"), players = base->rows("players");
|
||||
clubs.push_back({{"id", "club:finance-next"}, {"name", "Synthetic next club"},
|
||||
{"nation", "nation:de"}, {"competition", "comp:home"}});
|
||||
players.push_back({{"id", "player:finance-next"}, {"name", "Synthetic next player"},
|
||||
{"team", "club:finance-next"}, {"positions", {"gk"}}, {"squad_order", 0}});
|
||||
table(folder, "clubs", clubs);
|
||||
table(folder, "players", players);
|
||||
table(folder, "contracts", Json::array({
|
||||
{{"id", "contract:finance-real"}, {"person", "player:a"}, {"team", "club:a"},
|
||||
{"salary", 12000000}},
|
||||
{{"id", "contract:finance-initialization"}, {"person", "player:b"}, {"team", "club:a"},
|
||||
{"initialization", {{"kind", "synthetic-demo"}}}},
|
||||
{{"id", "contract:finance-next"}, {"person", "player:finance-next"}, {"team", "club:finance-next"},
|
||||
{"salary", 24000000}}}));
|
||||
const auto immutable_players = bmh::editor::read_file(folder / "world/players.json");
|
||||
const auto immutable_contracts = bmh::editor::read_file(folder / "world/contracts.json");
|
||||
auto world = Catalog::open(folder);
|
||||
DemoSource source(world);
|
||||
const auto start = source.snapshot();
|
||||
const auto factor = finance_currency_factor(*world);
|
||||
require(factor == 100 && start.state["contracts"]["club"] == "club:a",
|
||||
"Synthetic cent-currency source did not select its first manager");
|
||||
const auto act = [&](const Json &value) {
|
||||
return source.apply({source.snapshot().revision, "finance", value});
|
||||
};
|
||||
const auto deposit = act({{"kind", "deposit"}, {"value", 400}});
|
||||
const auto deposited = source.snapshot();
|
||||
require(deposit.ok && deposit.revision == start.revision + 1 &&
|
||||
deposited.revision == deposit.revision && source.dirty() &&
|
||||
deposited.state["cash"] == start.state["cash"].get<int64_t>() - 400 * factor &&
|
||||
deposited.state["deposit"] == start.state["deposit"].get<int64_t>() + 400 * factor,
|
||||
"Finance source action lost whole-to-minor conversion or revision/dirty state");
|
||||
const auto bank = demo_bank_summary(deposited.state, *world);
|
||||
const auto balance = demo_balance_rows(deposited.state, *world);
|
||||
require(bank.value("ok", false) && balance.value("ok", false) &&
|
||||
bank["cash"] == deposited.state["cash"] && balance["cash"] == bank["cash"] &&
|
||||
bank["deposit"] == deposited.state["deposit"] &&
|
||||
source.snapshot().state == deposited.state && source.snapshot().revision == deposited.revision,
|
||||
"Bank/balance projections diverged from the shared wallet or mutated their source");
|
||||
const auto stale = source.apply({start.revision, "finance", {{"kind", "withdraw"}, {"value", 1}}});
|
||||
require(!stale.ok && stale.error == "stale_revision" &&
|
||||
source.snapshot().state == deposited.state && source.snapshot().revision == deposited.revision,
|
||||
"Stale finance source action mutated the wallet or advanced its revision");
|
||||
require(act({{"kind", "contract-offer"}, {"player", "player:a"}, {"salary", 1234}, {"months", 36}}).ok &&
|
||||
act({{"kind", "insurance"}, {"player", "player:a"}, {"value", 24}}).ok &&
|
||||
act({{"kind", "bonus"}, {"index", 6}, {"value", 255}}).ok &&
|
||||
act({{"kind", "plan"}, {"index", 0}, {"value", 1000}}).ok,
|
||||
"Contracts, insurance, bonuses or balance planning failed through the shared source");
|
||||
const auto edited = source.snapshot();
|
||||
require(edited.state["contracts"]["players"]["player:a"]["salary"] == 1234 * factor &&
|
||||
edited.state["contracts"]["players"]["player:a"]["months"] == 36 &&
|
||||
edited.state["contracts"]["players"]["player:a"]["insurance"] == 24 &&
|
||||
edited.state["contracts"]["bonuses"][6] == 255 &&
|
||||
edited.state["balance"]["plan"][0] == 1000 * factor &&
|
||||
edited.state["cash"] == deposited.state["cash"] &&
|
||||
edited.state["deposit"] == deposited.state["deposit"] && edited.state["debt"] == deposited.state["debt"],
|
||||
"Contract/plan edits double-converted money or booked unrelated wallet movements");
|
||||
const auto file = folder / "finance.ui.json";
|
||||
const auto layout = default_desktop();
|
||||
source.save(file, layout);
|
||||
require(!source.dirty(), "Saved finance source remained dirty");
|
||||
require(act({{"kind", "withdraw"}, {"value", 1}}).ok, "Post-save finance edit failed");
|
||||
const auto before_restore = source.snapshot().revision;
|
||||
require(source.load(file) == layout && source.snapshot().state == edited.state &&
|
||||
source.snapshot().revision == before_restore + 1 && !source.dirty(),
|
||||
"Finance checkpoint lost wallet/contracts/plans or failed revision and clean-state restoration");
|
||||
auto legacy = Json::parse(bmh::editor::read_file(file));
|
||||
for (const auto *key : {"finance", "balance", "contracts"}) legacy.erase(key);
|
||||
write(folder, "legacy-finance.ui.json", legacy);
|
||||
DemoSource migrated(world);
|
||||
migrated.load(folder / "legacy-finance.ui.json");
|
||||
const auto upgraded = migrated.snapshot();
|
||||
require(valid_demo_finance(upgraded.state, *world, "club:a") && !migrated.dirty() &&
|
||||
upgraded.state["contracts"] == demo_contracts(*world, "club:a") &&
|
||||
upgraded.state["managers"] == edited.state["managers"],
|
||||
"Legacy finance checkpoint did not initialize a complete manager-scoped preview");
|
||||
for (const auto *key : {"cash", "deposit", "debt", "ticket"})
|
||||
require(upgraded.state.at(key) == edited.state.at(key),
|
||||
"Legacy finance migration overwrote an existing wallet/ticket value");
|
||||
auto corrupt = Json::parse(bmh::editor::read_file(file));
|
||||
corrupt["contracts"]["players"]["player:a"]["salary"] = -1;
|
||||
write(folder, "invalid-finance.ui.json", corrupt);
|
||||
const auto accepted = source.snapshot();
|
||||
bool rejected = false;
|
||||
try { source.load(folder / "invalid-finance.ui.json"); }
|
||||
catch (const std::exception &) { rejected = true; }
|
||||
require(rejected && source.snapshot().state == accepted.state &&
|
||||
source.snapshot().revision == accepted.revision && !source.dirty(),
|
||||
"Malformed existing finance fields were repaired or partially loaded");
|
||||
const auto change = source.apply({accepted.revision, "managers", Json::array({
|
||||
{{"name", "Synthetic next manager"}, {"club", "club:finance-next"}, {"portrait", 1}}})});
|
||||
const auto changed = source.snapshot();
|
||||
require(change.ok && changed.revision == accepted.revision + 1 &&
|
||||
changed.state["contracts"] == demo_contracts(*world, "club:finance-next") &&
|
||||
changed.state["contracts"]["players"].contains("player:finance-next") &&
|
||||
!changed.state["contracts"]["players"].contains("player:a") &&
|
||||
changed.state["cash"] == accepted.state["cash"] &&
|
||||
changed.state["deposit"] == accepted.state["deposit"] &&
|
||||
changed.state["debt"] == accepted.state["debt"] &&
|
||||
changed.state["finance"] == accepted.state["finance"] &&
|
||||
changed.state["balance"] == accepted.state["balance"],
|
||||
"First-manager club change retained another club's contracts or reset the shared finances");
|
||||
require(!source.apply({accepted.revision, "finance", {{"kind", "insurance"},
|
||||
{"player", "player:a"}, {"value", 12}}}).ok &&
|
||||
!source.apply({changed.revision, "finance", {{"kind", "contract-offer"},
|
||||
{"player", "player:a"}, {"salary", 2000}, {"months", 24}}}).ok &&
|
||||
source.snapshot().state == changed.state && source.snapshot().revision == changed.revision,
|
||||
"Old-club contract targets were silently retargeted after a manager switch");
|
||||
require(act({{"kind", "bonus"}, {"index", 0}, {"value", 17}}).ok,
|
||||
"New club bonus edit failed");
|
||||
const auto before_rename = source.snapshot();
|
||||
require(source.apply({before_rename.revision, "managers", Json::array({
|
||||
{{"name", "Renamed manager"}, {"club", "club:finance-next"}, {"portrait", 1}}})}).ok &&
|
||||
source.snapshot().state["contracts"] == before_rename.state["contracts"],
|
||||
"Renaming a manager in the same club reset its contracts/bonuses");
|
||||
require(bmh::editor::read_file(folder / "world/players.json") == immutable_players &&
|
||||
bmh::editor::read_file(folder / "world/contracts.json") == immutable_contracts,
|
||||
"Finance source edits modified immutable catalogue players or contracts");
|
||||
}
|
||||
} // namespace
|
||||
int main(int argc, char **argv) try {
|
||||
if (argc == 2) {
|
||||
@@ -449,6 +578,7 @@ int main(int argc, char **argv) try {
|
||||
restored_planning.snapshot().state["calendar"]["friendly_league"] == "comp:home" &&
|
||||
restored_planning.snapshot().state["reports"]["sorts"]["indoor"]["key"] == "points",
|
||||
"Old checkpoint lost league preview or failed planning/indoor migration");
|
||||
check_finance_source(root, world);
|
||||
GuiAssets absent(root / "missing-gui");
|
||||
require(!absent.available(),
|
||||
"World resources were mistaken for GUI resources");
|
||||
|
||||
Reference in new issue
Block a user