Files
aevolution/tests/cellular_admin_view_tests.cpp
T
dingoandClaude Opus 5.5 cc2eaf2e05
Foundation / checks (postgres) (push) Successful in 5s
Foundation / checks (tsan) (push) Successful in 5s
Foundation / checks (deploy) (push) Successful in 4m42s
Foundation / checks (checks) (push) Successful in 6s
Foundation / checks (client) (push) Successful in 5s
Foundation / postgres-tools (push) Successful in 31s
Foundation / checks (ecs) (push) Successful in 4s
Foundation / server-image (push) Successful in 4m45s
Foundation / checks (network) (push) Successful in 4s
Foundation / image (push) Successful in 5m31s
feat(cellular): energy cycles back through the spent pool (ADR-0182 section 14)
Energy cells used went to the region's heat store and never returned, so a
region without photosynthesizers drained: region 16's wildlife ate its energy
and starved, and its matter-only drip filled the deposit limit.

Two optional rules-13 tuning keys, adoptable live: spent.heat_recycle_per_mille
moves that share of the heat store to the spent pool at the end of every tick,
before the drip (1,000 closes the loop and returns heat built up before), and
spent.drip_absorb lets a drip at the deposit limit join the first existing bubble
at or after its spot. Both use the reserved word after the offer interval, so
rules without them keep their bytes, digests, checkpoints and replays. The DEV
revamp file turns both on. The region view reports heat and the cycle; the
portal counts heat in the region's energy and ledger.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-10-28 10:18:24 -04:00

157 lines
7.5 KiB
C++

#include "evo/server/lifecycle/cellular_admin_view.hpp"
#include "fixtures/cellular_ecology.hpp"
#include <array>
#include <cstdio>
#include <string>
// 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");
// Counts and totals cover everything, whatever the limits; no drip before rules 13.
std::array<std::uint64_t, 5> reserve{}, body{}, bubbles{};
std::array<std::size_t, 3> kinds{};
const auto add = [](std::array<std::uint64_t, 5>& totals, const accounting::Inventory& stock) {
totals[0] += stock.energy.value;
for (std::size_t i = 0; i < 4; ++i)
totals[i + 1] += stock.matter[i].value;
};
for (const auto& life : state->lives()) {
add(reserve, life.reserve);
add(body, life.body);
}
for (const auto& patch : state->habitat().patches()) {
++kinds[static_cast<std::size_t>(patch.kind)];
if (patch.kind == biology::CellularDepositKind::mote)
add(bubbles, patch.stock);
}
const auto list = [](const std::array<std::uint64_t, 5>& totals) {
std::string text = "[";
for (std::size_t i = 0; i < totals.size(); ++i)
text += (i == 0 ? "" : ",") + std::to_string(totals[i]);
return text + "]";
};
const auto counts = "\"counts\":[4,0," + std::to_string(kinds[0]) + "," +
std::to_string(kinds[1]) + "," + std::to_string(kinds[2]) + "]";
require(reserve[0] > 0 && body[1] > 0, "the fixture's cells hold stock");
for (const auto& text : {view, none}) {
require(text.find(counts) != std::string::npos, "counts");
require(text.find("\"totals\":{\"reserve\":" + list(reserve) + ",\"body\":" + list(body) +
",") != std::string::npos,
"cell totals");
require(text.find(",\"bubbles\":" + list(bubbles) + "}") != std::string::npos,
"bubble totals");
require(text.find("\"drip\"") == std::string::npos, "a drip before rules 13");
require(text.find("],\"heat\":" + std::to_string(state->heat().energy.value) +
",\"cells_total\":") != std::string::npos,
"heat");
}
}
// The trial liquid rules (cellular_revamp_owner_tests).
CellularLiquidRules liquid() {
return {.currents = {5, 48'000, 48'000, 250, 2, 4, 3'000, 6'000, 60, 300},
.founder = {.thrust_q10 = 20'480,
.reverse_thrust_q10 = 20'480,
.forward_drag_ppm = 95'163,
.lateral_drag_ppm = 95'163,
.turn = 3'276},
.deposits = {.drift_radius_um = 200,
.max_drift_q10 = 100 * 1'024,
.drift_decay_ppm = 95'163,
.merged_cap = 8,
.bubble_radius_um = biology::cellular_mote_radius_um}};
}
// A rules-13 owner's view carries its spent-pool drip.
void renders_the_drip() {
auto state = opening(7, true, 2, false);
checked(state->advance_tick({}), "genesis tick");
state = checked(state->migration_upgrade_candidate(), "boundary upgrade");
state = checked(state->maturation_upgrade_candidate(), "maturation upgrade");
state = checked(state->inheritance_upgrade_candidate(), "inheritance upgrade");
state = checked(state->photosynthesis_upgrade_candidate({0, 1, {0}}), "light upgrade");
state = checked(state->liquid_upgrade_candidate(liquid()), "liquid upgrade");
CellularRevampRules rules{.body = {.essential_upkeep_per_thousand = 250},
.burn_pj_per_kfg = {2'000, 2'000, 2'000, 2'000},
.step = {.engulf_ratio_milli = 1'500,
.engulf_reserve_percent = 100,
.kill_bubble_per_thousand = 500},
.drip_interval_ticks = 20,
.drip_bubble_pj = 20'000'000,
.drip_bubble_fg = 1'000'000,
.heat_recycle_per_thousand = 1'000,
.drip_absorb = true};
state = checked(state->revamp_upgrade_candidate(rules), "revamp upgrade");
const auto view = render_cellular_admin_view(*state, {});
require(view.find(",\"drip\":[20,20000000,1000000,1000,1],\"cells_total\":") !=
std::string::npos,
"rules-13 drip and energy cycle");
}
} // namespace
int main() {
try {
renders_cells_and_deposits();
renders_the_drip();
} catch (const std::exception& error) {
std::fprintf(stderr, "%s\n", error.what());
return 1;
}
std::puts("cellular admin view tests passed");
return 0;
}