Modern UI contract: camps read and the first command, camp.book

ModernSession::ui_command checks careerId, expectedRevision and owner, runs
the engine action and answers command_result or error. A refused command
restores the state and both generators; a repeated requestId gets the stored
answer, a different command under the same id duplicate_request. The camps
read lists maps and camps with a binding, draw-free quote per camp, focus and
the season's stays. Tests cover stale revision, foreign owner, unknown camp,
booking, repeat, conflict and a refused second booking.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
ClaudeandClaude Opus 5.5 committed 2026-10-27 08:25:54 +01:00
1 parent fec4c6c7e7
commit 99a48a463d
7 files changed
+252 -2

No files matched your search

+41
View File
@@ -31,6 +31,7 @@ const J& at(const J& j, const char* key) {
return it == o->end() ? none : it->second;
}
std::string s(const J& j) { return j.string() ? *j.string() : std::string{}; }
bool truth(const J& j) { const bool* b = std::get_if<bool>(&j.value); return b && *b; }
} // namespace
int main(int argc, char** argv) {
@@ -105,6 +106,46 @@ int main(int argc, char** argv) {
// money
check(s(at(modern::ui_money(15), "minor")) == "1500" && s(at(modern::ui_money(-3), "minor")) == "-300", "money");
// camps: read with binding quotes, then camp.book as the first command
const J camps = session.ui_read(O{{"kind", "camps"}}, revision);
const auto* list = at(at(camps, "data"), "camps").array();
check(list && list->size() == session.state().training_camps.size() && !list->empty(), "camp catalogue");
const std::string camp_id = list && !list->empty() ? s(at((*list)[0], "id")) : "";
const std::string price = list && !list->empty() ? s(at(at(at((*list)[0], "quote"), "total"), "minor")) : "";
check(list && !list->empty() && truth(at(at((*list)[0], "quote"), "allowed")), "camp quote allowed");
uint64_t rev = revision;
const auto command = [&](const std::string& id, const std::string& expected, const std::string& manager,
const std::string& camp) {
return J(O{{"messageType", "command"}, {"requestId", id}, {"expectedRevision", expected},
{"owner", O{{"managerId", manager}}}, {"kind", "camp.book"}, {"payload", O{{"campId", camp}}}});
};
const auto unchanged = modern::save_stream(session.state());
check(s(at(session.ui_command(command("request:c1", "999", "manager:1", camp_id), rev), "code")) == "stale_revision",
"stale revision refused");
check(s(at(session.ui_command(command("request:c2", std::to_string(rev), "manager:9", camp_id), rev), "code")) ==
"manager_not_active", "foreign owner refused");
check(s(at(session.ui_command(command("request:c3", std::to_string(rev), "manager:1", "resort:none"), rev), "code")) ==
"unknown_camp", "unknown camp refused");
check(rev == revision && modern::save_stream(session.state()) == unchanged, "refused commands change nothing");
const int64_t cash_before = *at(session.read(O{{"kind", "status"}}), "money").integer();
const J booked = session.ui_command(command("request:c4", std::to_string(rev), "manager:1", camp_id), rev);
check(s(at(booked, "messageType")) == "command_result" && s(at(booked, "newRevision")) == std::to_string(revision + 1) &&
rev == revision + 1, "camp booked, one revision");
const int64_t cash_after = *at(session.read(O{{"kind", "status"}}), "money").integer();
check(std::to_string((cash_before - cash_after) * 100) == price, "the quoted price was charged");
const J again = session.ui_command(command("request:c4", std::to_string(revision), "manager:1", camp_id), rev);
check(cli::dump_json(again) == cli::dump_json(booked) && rev == revision + 1 &&
*at(session.read(O{{"kind", "status"}}), "money").integer() == cash_after, "repeated request answered, not booked twice");
check(s(at(session.ui_command(command("request:c4", std::to_string(rev), "manager:1", camp_id), rev), "code")) ==
"duplicate_request", "same id, other command");
const auto after_booking = modern::save_stream(session.state());
check(s(at(session.ui_command(command("request:c5", std::to_string(rev), "manager:1", camp_id), rev), "code")) ==
"booked_this_week" && modern::save_stream(session.state()) == after_booking && rev == revision + 1,
"second booking in a week refused without change");
const J after = session.ui_read(O{{"kind", "camps"}}, rev);
check(at(at(after, "data"), "staysThisSeason").integer() && *at(at(after, "data"), "staysThisSeason").integer() == 1 &&
truth(at(at(after, "data"), "bookedThisWeek")), "camp read shows the stay");
} catch (const std::exception& ex) {
std::fprintf(stderr, "FAIL: %s\n", ex.what());
return 1;