Files
Dreierpack-Manager/tests/modern_stadium_projection_tests.cpp
T

143 lines
6.3 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 yes, const char *why) {
if (!yes)
throw std::runtime_error(why);
}
template <class F> void rejected(F f) {
bool caught = false;
try {
f();
} catch (const std::exception &) {
caught = true;
}
check(caught, "invalid stadium accepted");
}
Json money(int n) {
return {{"currency", "EUR"}, {"minorUnits", 2}, {"minor", std::to_string(n * 100)}};
}
Json fixture() {
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", {"quote.request", "stadium.commit"}}}}};
Json b;
for (const auto *kind : {"session", "desktop", "camps", "stadium"})
b[kind] = {{"messageType", "snapshot"}, {"meta", meta}, {"data", {{"kind", kind}}}};
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["stadium"]["data"];
d.update({{"teamId", "club:test"},
{"quote", nullptr},
{"blocks", Json::array()},
{"facilities", Json::array()},
{"periphery", Json::array()}});
for (int i = 0; i < 8; ++i) {
d["blocks"].push_back({{"id", "block:" + std::string(1, char('a' + i))},
{"name", "Synthetic block"},
{"places", 4000},
{"maxPlaces", 8000},
{"seats", false},
{"roof", false},
{"seatsAfterConstruction", i == 0},
{"roofAfterConstruction", i == 0},
{"constructionWeeks", i == 0 ? 3 : 0},
{"placesAfterConstruction", i == 0 ? 6000 : 0}});
d["facilities"].push_back({{"id", "facility:" + std::to_string(i)},
{"name", "Synthetic facility"},
{"level", 1},
{"maxLevel", i == 2 || i == 3 ? 2 : 4},
{"plannedLevel", i == 0 ? 3 : 1},
{"constructionWeeks", i == 0 ? 4 : 0},
{"upgradeAvailable", i != 0}});
}
for (int i = 0; i < 14; ++i) {
Json p = {{"id", "periphery:" + std::to_string(i)},
{"name", "Synthetic site"},
{"constructionDays", i == 0 ? 5 : 0}};
if (i == 0) {
p["stage"] = 1;
p["maxStage"] = 4;
} else
p["built"] = false;
d["periphery"].push_back(p);
}
return b;
}
} // namespace
int main() try {
const auto b = fixture(), before = b;
const auto now = engine_stadium_projection(b, false), future = engine_stadium_projection(b, true);
check(now.at("available_capacity") == 28000 && future.at("capacity") == 34000,
"pending block capacity not separated");
check(now.at("blocks")[0].at("display_places") == 0 &&
future.at("blocks")[0].at("display_seats") == true,
"construction seats/capacity ignored");
check(now.at("facilities")[0].at("display_level") == 1 &&
future.at("facilities")[0].at("display_level") == 3,
"planned facility replaced current value");
check(now.at("periphery")[0].at("display_level") == 1 &&
future.at("periphery")[0].at("display_level") == 2 &&
future.at("periphery")[4].at("max_level") == 1,
"periphery invented unsupported parking stages");
check(b == before, "projection changed state");
auto full = b;
for (auto &block : full["stadium"]["data"]["blocks"]) {
block["places"] = 8000;
block["seats"] = true;
block["roof"] = true;
block["constructionWeeks"] = 0;
}
check(engine_stadium_projection(full, false).at("super_stadium") == true,
"super stadium ignores actual maxima/features");
auto quote = b;
quote["stadium"]["data"]["quote"] = {{"id", "quote:stadium-test"},
{"kind", "block"},
{"blockId", "block:b"},
{"places", 5000},
{"seats", true},
{"roof", true},
{"price", money(12345)},
{"weeks", 8},
{"breakdown",
{{{"code", "places"}, {"amount", money(10000)}},
{{"code", "seats"}, {"amount", money(2000)}},
{{"code", "roof"}, {"amount", money(345)}}}}};
const Json draft = {
{"kind", "block"}, {"index", 1}, {"places", 5000}, {"seats", true}, {"roof", true}};
const auto value = engine_stadium_quote(quote, draft);
check(value.at("cost") == 1234500 && value.at("roofCost") == 34500,
"authoritative price rounded or rescaled");
const auto edit = engine_stadium_request(quote, draft, "request:edit", true);
check(edit.at("payload").at("quoteId") == "quote:stadium-test" &&
edit.at("payload").at("blockId") == "block:b" && edit.at("expectedRevision") == "3",
"edit loses exact quote/owner");
const auto cancel = engine_stadium_commit(quote, false, "request:cancel");
check(cancel.at("payload").at("accept") == false && cancel.at("payload").at("tip") == false,
"cancel enables unquoted acceleration");
auto changed = draft;
changed["places"] = 6000;
rejected([&] { engine_stadium_quote(quote, changed); });
auto stale = quote;
stale["stadium"]["meta"]["stateRevision"] = "4";
rejected([&] { engine_stadium_quote(stale, draft); });
std::cout << "stadium: actual capacities, future facilities, periphery limits and scoped prices "
"passed\n";
return 0;
} catch (const std::exception &e) {
std::cerr << e.what() << '\n';
return 1;
}