#include "test_inputs.hpp" // SPDX-License-Identifier: GPL-3.0-only // Soak test of the console language (proposal "Dauertest"): 1-4 human managers play many // seasons through bmh::cli::console::Console in instant mode. At every desk the driver picks // random commands from what ListOptions offers (random but legal arguments: integer ranges, // enum choices, booleans, ids/rows seen in earlier output), answers every question with a // random option, and after each kickoff checks invariants of the game state: // - no "! E_INTERNAL" / "! E_IO" line (a rule refusal like E_RULE is a normal answer) // - every player id appears in at most one squad slot, ids are inside the player table // - every league table adds up: goals for = goals against, wins = losses, draws even, // points = 3W + D (a human club may have fewer: the point deduction), // games played differ by at most six (postponed games are replayed later), and at the end // of the season every club has played 2 * (clubs - 1) games // - at every season change: Save, then Load + Save in a fresh console gives the same // decrypted save stream // At each season change it prints one line per manager: club, league, final rank, balance. // // Usage: cli_soak [seasons=1] [managers=2] [seed=1] [actions=4] // The CTest entry runs a short variant; long runs are started by hand. #include "cli/console/console.hpp" #include "formats/fbm.hpp" #include "game/state.hpp" #include "ui/league_table.hpp" #include #include #include #include #include #include #include #include #include #include #include #include #include "game/schedule.hpp" #include "game/trace.hpp" using namespace bmh; using bmh::cli::console::Console; using bmh::cli::console::Options; using bmh::cli::console::TimeMode; namespace { int failures = 0; void fail(const std::string& what) { ++failures; std::cerr << "SOAK FAIL: " << what << "\n"; } bool starts(const std::string& s, const char* prefix) { return s.rfind(prefix, 0) == 0; } // Commands the random driver never calls: they end the session, replace the game, or touch // files (the save roundtrip runs on its own schedule). const std::set kSkipped = { "Quit", "PullPlug", "Load", "Save", "LoadGame", "SaveGame", "NewGame", "Kickoff", "Help", "ListOptions", "ChatLog", "Delete", "Key", "Open", "GoBack", "Answer", "Run", "Step"}; // Windows the driver leaves at once (files and the start screens). const std::set kLeave = {"LoadGame", "SaveGame", "NewGame", "NewGame.LoadGame"}; struct Option { std::string name; std::vector params; // "price:8..25", "letter:enum(A,B)", "[on:bool]", ... }; // "> Name(p1, p2) - description" -> Option. Lines starting with "> " (indented) are the // Open() menu lines and are skipped. bool parse_option(const std::string& line, Option& out) { if (!starts(line, "> ") || starts(line, "> ")) return false; const size_t open = line.find('('); if (open == std::string::npos) return false; out.name = line.substr(2, open - 2); out.params.clear(); int depth = 0; std::string cur; size_t i = open + 1; for (; i < line.size(); ++i) { const char c = line[i]; if ((c == '(' || c == '[') ) ++depth; if (c == ')' && depth == 0) break; if ((c == ')' || c == ']')) --depth; if (c == ',' && depth == 0) { out.params.push_back(cur); cur.clear(); continue; } if (!(cur.empty() && c == ' ')) cur += c; } if (!cur.empty()) out.params.push_back(cur); return true; } class Driver { public: Driver(const std::filesystem::path& data, const std::filesystem::path& dir, uint32_t seed, int managers, int actions) : data_(data), dir_(dir), rng_(seed), managers_(managers), actions_(actions) { Options o; o.data = data; o.save_directory = dir; o.seed = seed; o.time = TimeMode::instant; console_ = std::make_unique(o, [this](const std::string& l) { out_.push_back(l); }); console_->start(); } // Feeds one block, answers questions randomly, keeps track of window/ids. std::vector block(const std::vector& lines, bool answer = true) { std::vector all; std::vector input = lines; for (int guard = 0; guard < 200; ++guard) { out_.clear(); for (const auto& l : input) console_->feed(l); console_->feed("go."); all.insert(all.end(), out_.begin(), out_.end()); scan(out_, input); check_pay(input); input.clear(); // an open question: answer with a random option std::string q; for (const auto& l : out_) if (starts(l, "? Q")) q = l; else if (starts(l, "+ ") || starts(l, "! ")) {} // keep the last question if (q.empty() || !answer || console_->ended()) break; const std::string id = q.substr(3, q.find(' ', 3) - 3); std::vector opts; const size_t at = q.find("options: "); if (at != std::string::npos) { std::string rest = q.substr(at + 9); size_t p = 0; while (p <= rest.size()) { size_t bar = rest.find(" | ", p); opts.push_back(rest.substr(p, bar == std::string::npos ? std::string::npos : bar - p)); if (bar == std::string::npos) break; p = bar + 3; } } if (opts.empty() || pick(5) == 0) input = {"GoBack"}; else input = {"Answer(" + id + ", " + quote_if_needed(opts[pick(int(opts.size()))]) + ")"}; } return all; } void new_game() { static const char* strength[] = {"Upper", "Middle", "Lower"}; static const char* leagues[] = {bmh::test::expected_text("cli_soak-external-1").c_str(), bmh::test::expected_text("cli_soak-external-1").c_str(), "SecondLeague"}; block({"NewGame(5, 3, " + std::to_string(managers_) + ", 3, true, false)"}, false); // clubs by number (any version): the first 16 clubs of the database are league clubs in // the German (18 per league) and the English (20) versions std::vector order(16); for (int i = 0; i < 16; ++i) order[size_t(i)] = i; std::shuffle(order.begin(), order.end(), rng_); const int portrait = pick(12); // portraits must differ between the managers for (int m = 0; m < managers_; ++m) { const int c = order[size_t(m)]; const std::string league = leagues[pick(3)]; block({"SelectTeam(M" + std::to_string(m + 1) + ", club:" + std::to_string(c) + ", " + league + ", " + strength[pick(3)] + ", " + std::to_string((portrait + 3 * m) % 12) + ")"}, false); for (const auto& l : out_) if (starts(l, "! ")) std::cerr << "setup: " << l << "\n"; for (const auto& l : block({"Answer(" + std::to_string(m + 1) + ", Yes)"}, false)) if (starts(l, "! ")) std::cerr << "setup: " << l << "\n"; } // the start info windows end at the desk for (int i = 0; i < 10 && window_ != "Desktop"; ++i) block({"GoBack"}); if (window_ != "Desktop") { fail("the new game did not reach the desk (window " + window_ + ")"); std::exit(1); } playing_ = true; check_pay({"(game start)"}); } // One desk turn: random actions, back to the desk, Kickoff. void turn() { to_desk(); const int n = pick(actions_ + 1); for (int i = 0; i < n; ++i) random_action(); to_desk(); const auto status = block({"Status"}); for (const auto& l : status) { std::smatch m; static const std::regex re(R"(^\+ (\S+) \| (.+) \| (\d{4}-\d{2}-\d{2}) \| balance (-?\d+))"); if (std::regex_search(l, m, re)) balance_[m[1]] = std::stoll(m[4]); } if (getenv("SOAK_TRACE")) std::cerr << "turn " << kickoffs_ << " date " << console_->session().state().date << " window " << window_ << std::endl; block({"Kickoff"}); ++kickoffs_; } bool ended() const { return console_->ended(); } const GameState& state() const { return console_->session().state(); } std::map& balances() { return balance_; } int kickoffs() const { return kickoffs_; } int actions_done() const { return actions_done_; } const std::map& errors() const { return errors_; } Console& console() { return *console_; } private: int pick(int n) { return n <= 1 ? 0 : int(std::uniform_int_distribution(0, n - 1)(rng_)); } static std::string quote_if_needed(const std::string& s) { if (s.find_first_of(" ,()") == std::string::npos) return s; return "\"" + s + "\""; } void scan(const std::vector& lines, const std::vector& input) { static const std::regex token(R"(\b(id|row|club|player|game|msg|slot):(\d+))"); for (const auto& i : input) history_.push_back("<< " + i); for (const auto& l : lines) history_.push_back(l.size() > 160 ? l.substr(0, 160) : l); if (history_.size() > 400) history_.erase(history_.begin(), history_.end() - 400); for (const auto& l : lines) { if (starts(l, "# Window: ")) window_ = l.substr(10); if (starts(l, "! ")) { const std::string code = l.substr(2, l.find_first_of(" :", 2) - 2); ++errors_[code]; if (code == "E_INTERNAL" || code == "E_IO") { std::string in; for (const auto& i : input) in += i + "; "; fail("in window " + window_ + " after [" + in + "]: " + l); if (!dumped_) { dumped_ = true; for (const auto& h : history_) std::cerr << " " << h << "\n"; } } } for (std::sregex_iterator it(l.begin(), l.end(), token), end; it != end; ++it) { auto& pool = tokens_[(*it)[1]]; if (pool.size() < 64) pool.push_back((*it)[0]); else pool[size_t(pick(64))] = (*it)[0]; } } } // Every occupied squad row of a human club has a base pay on the tactics board (+0x50): // team_rating divides by it. Checked after each block to name the command that broke it. void check_pay(const std::vector& input) { if (!playing_) return; const GameState& g = console_->session().state(); for (size_t m = 0; m < g.manager_teams.size(); ++m) { const int team = g.manager_teams[m]; if (team < 0 || team >= 138 || g.tactics.size() < (m + 1) * 0x840) continue; const SquadRange r = squad_range(team); for (size_t row = 0; row < r.count && row < 24; ++row) { if ((g.squad_slots[r.first + row] & 0xFFF) == 0xFFF) continue; const size_t at = m * 0x840 + row * 0x58 + 0x50; const uint32_t base = g.tactics[at] | g.tactics[at + 1] << 8 | g.tactics[at + 2] << 16 | uint32_t(g.tactics[at + 3]) << 24; const std::string key = std::to_string(m) + "/" + std::to_string(row); if (base == 0 && !zero_pay_.count(key)) { zero_pay_.insert(key); std::string in; for (const auto& i : input) in += i + "; "; fail("manager " + std::to_string(m + 1) + " row " + std::to_string(row) + " has no base pay after [" + in + "] in " + window_); if (!dumped_) { dumped_ = true; for (const auto& h : history_) std::cerr << " " << h << "\n"; } } if (base != 0) zero_pay_.erase(key); } } } void to_desk() { for (int i = 0; i < 12 && window_ != "Desktop" && !console_->ended(); ++i) block({"GoBack"}); if (window_ != "Desktop") { fail("cannot return to the desk from " + window_); if (!dumped_) { dumped_ = true; for (const auto& l : history_) std::cerr << " " << l << "\n"; } } } std::string argument(const std::string& param) { std::string p = param; const bool optional = !p.empty() && p.front() == '['; if (optional) p = p.substr(1, p.size() - 2); const size_t colon = p.find(':'); const std::string type = colon == std::string::npos ? p : p.substr(colon + 1); std::smatch m; static const std::regex range(R"(^(-?\d+)\.\.(-?\d+)$)"); if (std::regex_match(type, m, range)) { const long long lo = std::stoll(m[1]), hi = std::stoll(m[2]); // money ranges are wide: mostly small values, sometimes the edges const int r = pick(10); if (r == 0) return std::to_string(lo); if (r == 1) return std::to_string(hi); const long long span = std::min(hi - lo, r < 6 ? 30 : 2000000); return std::to_string(lo + std::uniform_int_distribution(0, std::max(0LL, span))(rng_)); } if (starts(type, "enum(")) { std::vector c; std::string body = type.substr(5, type.size() - 6), cur; for (char ch : body) { if (ch == ',' || ch == '|') { c.push_back(cur); cur.clear(); } else cur += ch; } c.push_back(cur); return c[size_t(pick(int(c.size())))]; } if (type == "bool") return pick(2) ? "true" : "false"; if (type == "int") return std::to_string(pick(30)); // text / reference: an id or row seen before, a club, or a short word const std::string name = colon == std::string::npos ? p : p.substr(0, colon); for (const char* kind : {"id", "row", "club", "player", "game", "msg", "slot"}) { if (name.find(kind) == std::string::npos) continue; auto it = tokens_.find(kind); if (it != tokens_.end() && !it->second.empty() && pick(4) != 0) return it->second[size_t(pick(int(it->second.size())))]; if (std::string(kind) == "row") return "row:" + std::to_string(pick(24)); if (std::string(kind) == "club") return "club:" + std::to_string(pick(138)); } std::vector pool; for (auto& [k, v] : tokens_) pool.insert(pool.end(), v.begin(), v.end()); if (!pool.empty() && pick(2)) return pool[size_t(pick(int(pool.size())))]; static const char* words[] = {"\"soak\"", "1", "row:0", "club:0", "\"x\""}; return words[pick(5)]; } // Opens a random window (or stays) and calls a few random commands there. void random_action() { if (window_ == "Desktop") { // menus 0..29 without the file windows int menu; do menu = pick(30); while (menu == 10 || menu == 11 || menu == 12); block({pick(4) == 0 ? "Key(" + std::string(1, char('A' + pick(26))) + ")" : "Open(" + std::to_string(menu) + ")"}); if (kLeave.count(window_)) { block({"GoBack"}); return; } } for (int k = 0; k < 1 + pick(5); ++k) { const auto listed = block({"ListOptions"}); std::vector