Bind original settings and automation to atomic native drafts (by Codex)
This commit is contained in:
1 parent
2ce563a4b9
commit
98cff2185d
23 files changed
+959
-189
No files matched your search
@@ -0,0 +1,122 @@
|
||||
// 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 x, const char *s) {
|
||||
if (!x)
|
||||
throw std::runtime_error(s);
|
||||
}
|
||||
template <class F> void rejects(F f) {
|
||||
bool ok = false;
|
||||
try {
|
||||
f();
|
||||
} catch (const std::exception &) {
|
||||
ok = true;
|
||||
}
|
||||
check(ok, "invalid settings accepted");
|
||||
}
|
||||
} // namespace
|
||||
int main() try {
|
||||
const Json meta = {{"contractVersion", "bmh-modern-ui@draft1"},
|
||||
{"careerId", "career:test"},
|
||||
{"worldId", "world:test"},
|
||||
{"worldRevision", "1"},
|
||||
{"stateRevision", "8"},
|
||||
{"scope",
|
||||
{{"managerId", "manager:one"},
|
||||
{"teamId", "club:one"},
|
||||
{"role", "club"},
|
||||
{"allowedCommands", {"settings.commit"}}}}};
|
||||
Json b;
|
||||
for (auto *k : {"session", "desktop", "camps", "settings"})
|
||||
b[k] = {
|
||||
{"messageType", "snapshot"}, {"meta", meta}, {"data", {{"kind", k}}}};
|
||||
b["desktop"]["data"].update(
|
||||
{{"manager", {{"id", "manager:one"}}},
|
||||
{"team", {{"id", "club:one"}}},
|
||||
{"accounts",
|
||||
{{"cash", {{"minor", "0"}, {"currency", "EUR"}, {"minorUnits", 2}}}}}});
|
||||
b["camps"]["data"]["camps"] = Json::array();
|
||||
auto &d = b["settings"]["data"];
|
||||
d.update({{"cells", Json::array()},
|
||||
{"automation", Json::array()},
|
||||
{"formations",
|
||||
Json::array({{{"value", 2}, {"label", "Synthetic slot 2"}},
|
||||
{{"value", 6}, {"label", "Synthetic slot 6"}}})},
|
||||
{"formation", 2}});
|
||||
for (int i = 0; i < 180; ++i)
|
||||
d["cells"].push_back(
|
||||
{{"code", std::string(1, char('A' + i / 9)) + char('1' + i % 9)},
|
||||
{"raw", 0},
|
||||
{"displayRaw", 0},
|
||||
{"value", i % 9 < 7 ? 1 : 0},
|
||||
{"editable", true},
|
||||
{"min", 0},
|
||||
{"max", i % 9 < 7 ? 1
|
||||
: i % 9 == 7 ? 9
|
||||
: 99},
|
||||
{"step", 1},
|
||||
{"reason", ""}});
|
||||
d["cells"][0].update(
|
||||
{{"raw", 128}, {"displayRaw", 128}, {"value", 1}, {"editable", false}});
|
||||
d["cells"][1].update(
|
||||
{{"raw", 2}, {"displayRaw", 2}, {"value", nullptr}, {"editable", false}});
|
||||
d["cells"][8].update({{"raw", 131}, {"value", 65}});
|
||||
for (auto *key :
|
||||
{"lineup", "substitutions", "tactics", "marking", "training"})
|
||||
d["automation"].push_back(
|
||||
{{"id", key}, {"raw", 0}, {"enabled", false}, {"editable", true}});
|
||||
const auto original = b;
|
||||
const auto p = engine_settings_projection(b);
|
||||
check(b == original && p.at("local").at("cells")[1].is_null() &&
|
||||
p.at("local").at("cells")[0] == 1 &&
|
||||
p.at("local").at("cells")[8] == 65,
|
||||
"raw flags became booleans or odd source number lost");
|
||||
auto draft = p.at("local");
|
||||
draft["cells"][2] = 0;
|
||||
draft["cells"][8] = 66;
|
||||
draft["formation"] = 6;
|
||||
for (auto &[key, v] : draft["automation"].items())
|
||||
v = true;
|
||||
auto command = engine_settings_command(b, draft, "request:test");
|
||||
check(command["expectedRevision"] == "8" &&
|
||||
command["payload"]["edits"].size() == 2 &&
|
||||
command["payload"]["automation"].size() == 5 &&
|
||||
command["payload"]["edits"][1]["value"] == 66,
|
||||
"sparse edits scaled or lost");
|
||||
bool explicit_slot = false;
|
||||
for (const auto &a : command["payload"]["automation"])
|
||||
if (a["id"] == "lineup")
|
||||
explicit_slot = a["formation"] == 6;
|
||||
check(explicit_slot, "automatic lineup lacks explicit available slot");
|
||||
auto noop = engine_settings_command(b, p.at("local"), "request:noop");
|
||||
check(noop["payload"]["edits"].empty() &&
|
||||
noop["payload"]["automation"].empty(),
|
||||
"unchanged settings generated edits");
|
||||
auto bad = draft;
|
||||
bad["cells"][0] = 0;
|
||||
rejects([&] { engine_settings_command(b, bad, "request:x"); });
|
||||
bad = draft;
|
||||
bad["cells"][2] = 2;
|
||||
rejects([&] { engine_settings_command(b, bad, "request:x"); });
|
||||
bad = draft;
|
||||
bad["formation"] = 3;
|
||||
rejects([&] { engine_settings_command(b, bad, "request:x"); });
|
||||
auto foreign = b;
|
||||
foreign["settings"]["meta"]["stateRevision"] = "9";
|
||||
rejects([&] { engine_settings_projection(foreign); });
|
||||
foreign = b;
|
||||
foreign["settings"]["data"]["cells"][1]["code"] = "A1";
|
||||
rejects([&] { engine_settings_projection(foreign); });
|
||||
foreign = b;
|
||||
foreign["settings"]["data"]["automation"][4]["editable"] = false;
|
||||
rejects([&] { engine_settings_command(foreign, draft, "request:x"); });
|
||||
std::cout << "settings raw flags, parity display, sparse edits, explicit "
|
||||
"slots, owner and readonly checks passed\n";
|
||||
return 0;
|
||||
} catch (const std::exception &e) {
|
||||
std::cerr << e.what() << '\n';
|
||||
return 1;
|
||||
}
|
||||
Reference in new issue
Block a user