115 lines
4.6 KiB
C++
115 lines
4.6 KiB
C++
// SPDX-License-Identifier: GPL-3.0-only
|
|
#include "modern/ui/engine_bridge.hpp"
|
|
#include <iostream>
|
|
#include <stdexcept>
|
|
using namespace bmh::modern_ui;
|
|
namespace {
|
|
void check(bool y, const char *s) {
|
|
if (!y)
|
|
throw std::runtime_error(s);
|
|
}
|
|
template <class F> void rejected(F f) {
|
|
bool caught = false;
|
|
try {
|
|
f();
|
|
} catch (const std::exception &) {
|
|
caught = true;
|
|
}
|
|
check(caught, "invalid balance accepted");
|
|
}
|
|
Json money(int64_t n) {
|
|
return {{"currency", "EUR"},
|
|
{"minorUnits", 2},
|
|
{"minor", std::to_string(n * 100)}};
|
|
}
|
|
} // namespace
|
|
int main() try {
|
|
const Json meta = {{"contractVersion", "bmh-modern-ui@draft1"},
|
|
{"careerId", "career:test"},
|
|
{"worldId", "world:test"},
|
|
{"worldRevision", "1"},
|
|
{"stateRevision", "3"},
|
|
{"scope",
|
|
{{"role", "club"},
|
|
{"managerId", "manager:test"},
|
|
{"teamId", "club:test"},
|
|
{"allowedCommands", {"balance.commit"}}}}};
|
|
Json b;
|
|
for (auto *k : {"session", "desktop", "camps", "balance"})
|
|
b[k] = {
|
|
{"messageType", "snapshot"}, {"meta", meta}, {"data", {{"kind", k}}}};
|
|
b["desktop"]["data"].update({{"manager", {{"id", "manager:test"}}},
|
|
{"team", {{"id", "club:test"}}},
|
|
{"accounts", {{"cash", money(5000000)}}}});
|
|
b["camps"]["data"]["camps"] = Json::array();
|
|
auto &d = b["balance"]["data"];
|
|
d.update(
|
|
{{"teamId", "club:test"},
|
|
{"view", "future"},
|
|
{"items", Json::array()},
|
|
{"summaries", Json::array()},
|
|
{"edits", Json::array({{{"id", "balance:7"}, {"amount", money(7000)}}})},
|
|
{"income", money(100000)},
|
|
{"costs", money(60000)},
|
|
{"depreciation", money(500)},
|
|
{"depreciationLabel", "Synthetic depreciation"},
|
|
{"net", money(39500)},
|
|
{"warning", true},
|
|
{"warningText", "Synthetic fiscal warning"}});
|
|
for (int i = 0; i < 18; ++i)
|
|
d["items"].push_back({{"id", "balance:" + std::to_string(i)},
|
|
{"label", "Synthetic item " + std::to_string(i)},
|
|
{"section", i < 7 ? "income" : "costs"},
|
|
{"amount", money(i * 1000)},
|
|
{"editable", true},
|
|
{"maximum", money(30000000)}});
|
|
for (int i = 0; i < 6; ++i)
|
|
d["summaries"].push_back({{"value", i == 3 ? money(15)
|
|
: i == 5 ? money(5000000)
|
|
: Json(i + 10)}});
|
|
const auto original = b;
|
|
const auto v = engine_balance_projection(b, b.at("balance"));
|
|
check(v.at("income").size() == 7 && v.at("costs").size() == 12 &&
|
|
v.at("plan").size() == 11,
|
|
"actual item groups lost");
|
|
check(v.at("ticket") == 1500 && v.at("cash") == 500000000 &&
|
|
v.at("plan")[0].at("amount") == 700000,
|
|
"money scaled incorrectly");
|
|
check(v.at("plan")[0].at("maximum") == 30000000, "actual edit bounds lost");
|
|
check(v.at("balance") == 3950000 && v.at("warning"),
|
|
"actual net/warning lost");
|
|
check(b == original, "projection mutated source");
|
|
const auto c = engine_balance_command(b, b.at("balance"), "request:test");
|
|
check(c.at("payload").at("edits") == d.at("edits") &&
|
|
c.at("payload").at("acceptWarning") == true &&
|
|
c.at("expectedRevision") == "3",
|
|
"confirmed draft changed");
|
|
auto bad = b.at("balance");
|
|
bad["meta"]["stateRevision"] = "4";
|
|
rejected([&] { engine_balance_projection(b, bad); });
|
|
bad = b.at("balance");
|
|
bad["data"]["teamId"] = "foreign";
|
|
rejected([&] { engine_balance_projection(b, bad); });
|
|
bad = b.at("balance");
|
|
bad["data"]["items"][1]["id"] = "balance:0";
|
|
rejected([&] { engine_balance_projection(b, bad); });
|
|
bad = b.at("balance");
|
|
bad["data"]["edits"] = Json::array();
|
|
rejected([&] { engine_balance_command(b, bad, "request:x"); });
|
|
bad = b.at("balance");
|
|
bad["data"]["view"] = "current";
|
|
rejected([&] { engine_balance_command(b, bad, "request:x"); });
|
|
bad = b.at("balance");
|
|
bad["data"]["depreciation"] = money(-500);
|
|
auto credit = engine_balance_projection(b, bad);
|
|
check(credit.at("income").size() == 8 && credit.at("costs").size() == 11 &&
|
|
credit.at("income").back().at("amount") == 50000,
|
|
"negative depreciation shown as an expense");
|
|
std::cout << "balance actual money, depreciation, owner, bounds and "
|
|
"confirmed draft checks passed\n";
|
|
return 0;
|
|
} catch (const std::exception &e) {
|
|
std::cerr << e.what() << '\n';
|
|
return 1;
|
|
}
|