#include "evo/server/lifecycle/cellular_admin_view.hpp" #include "fixtures/cellular_ecology.hpp" #include #include // ADR-0182 admin region view: every cell and deposit of a cellular owner, read without change. namespace { using namespace evo; using namespace evo::server; using namespace evo::fixture; std::size_t count(const std::string& text, const std::string& part) { std::size_t found = 0; for (auto at = text.find(part); at != std::string::npos; at = text.find(part, at + 1)) ++found; return found; } void renders_cells_and_deposits() { const auto state = opening(7, true); const auto before = state->snapshot(); LifeAccount first{}; first.back() = 1; const auto view = render_cellular_admin_view( *state, [&](const LifeAccount& account) { return account == first; }); require(state->snapshot() == before, "rendering the view changed the owner"); require(view.starts_with("{\"world\":207,\"region\":1,\"tick\":" + std::to_string(state->tick()) + ",\"rules\":"), "view header"); require(view.ends_with("]}\n"), "view terminator"); require(view.find("\"area\":[6000,6000]") != std::string::npos, "view area"); require(view.find("\"cells_total\":4,") != std::string::npos, "cell total"); require(view.find("\"deposits_total\":" + std::to_string(state->habitat().patches().size())) != std::string::npos, "deposit total"); // Four player cells, each with its account text; only the first is connected. require(cellular_account_text(first) == "00000000-0000-0000-0000-000000000001", "account text"); require(count(view, "[\"10") == 4, "every life listed with its id as a string"); for (const auto& life : state->lives()) { const auto extent = state->extent_of(life).value_or(0); require(extent > 0, "a living cell has an extent"); const auto row = "[\"" + std::to_string(life.identity.life) + "\",\"" + std::to_string(life.identity.organism) + "\",\"" + cellular_account_text(life.account) + "\"," + std::to_string(life.position.x_um) + "," + std::to_string(life.position.y_um) + "," + std::to_string(extent) + "," + (life.account == first ? "2" : "0") + ",1,"; require(view.find(row) != std::string::npos, "cell row"); } // Deposits by ascending id, and limits keep the lowest ids while totals stay complete. const auto limited = render_cellular_admin_view(*state, {}, {.cells = 1, .deposits = 2}); require(count(limited, "[\"10") == 1, "cell limit"); require(limited.find("\"cells_total\":4,") != std::string::npos, "total beyond the limit"); require(limited.find("\"deposits\":[[1,") != std::string::npos && limited.find("],[2,") != std::string::npos && limited.find("],[3,") == std::string::npos, "deposit limit keeps ids 1 and 2"); const auto none = render_cellular_admin_view(*state, {}, {.cells = 0, .deposits = 0}); require(none.find("\"cells\":[],\"deposits\":[]}") != std::string::npos, "empty lists"); } } // namespace int main() { try { renders_cells_and_deposits(); } catch (const std::exception& error) { std::fprintf(stderr, "%s\n", error.what()); return 1; } std::puts("cellular admin view tests passed"); return 0; }