Upstream/test/bot-run.test.js
jvanvik 931f59e11e Phase 3: trade/end-game client, bot simulator, first balance pass
Built:
- Client: trade screen, world/end-game screen (landing sites, shipping, node build, final ranking), report sharing + alliance reports tab, alert describers for all notification types, error-code strings (en+nb) for every server CODE.
- tools/bot.js (+ tools/bot/): 50 headless bots against the real server in-process with a fake clock; resource/unit conservation ledger, exit 1 on broken invariants. docs/BALANCE.md has method and every run behind a number.
- Balance changes (evidence in docs/BALANCE.md): secondQueueCoreLevel 10 -> 3, TRANSPORT.capacityPerLink 500 -> 1000 (end game was shipping-limited in bot runs).
- Server fixes: POST /reports/:id/share 500 without a pending reportCleanup; bounced link shipments were silent about cargo overflow.

Tested: npm test 207/207 (new: bot determinism + invariants, client alerts/world/i18n over CODES, two regression tests that failed before their fix); openapi check up to date. Real-browser scratch runs (not in repo): WebSocket push shows an incoming-attack banner ~214 ms after launch, polling fallback ~52 s, 15 scripted flows, 71 screenshots.

NOT tested: Playwright suite (e2e/ still empty), real devices/Safari, nginx proxy websockets, throttled networks, HTTP-level bot load. NOT claimed: balance. Bots are not humans; open balance items (loot waste, escrow refund clipping, no garrisons at landing sites, end game not scaling with world speed) are listed in docs/BALANCE.md.

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01Q2MJaQ5LXAHzGnTDgceT14
2026-10-11 20:24:41 +02:00

75 lines
5.4 KiB
JavaScript

'use strict';
/* tools/bot.js: short deterministic runs of the bot harness against the real server. Asserts the invariants (conservation of
* resources and units, no negative stock, no failed events, no unexpected error codes), that the same seed gives identical stats
* (wall-clock `perf` excepted), that the harness parses its options, and that the data.js override mechanism restores what it
* changed. Keep it fast: it is part of `npm test`. */
const test = require('node:test');
const assert = require('node:assert');
const { runBot, parseArgs, applyOverrides } = require('../tools/bot.js');
const D = require('../public/js/data.js');
const strip = (s) => { const c = JSON.parse(JSON.stringify(s)); delete c.perf; return c; };
test('bot run: invariants hold and the same seed gives identical stats', async () => {
const t0 = Date.now();
const a = await runBot({ players: 12, days: 6, seed: 7 }); // long enough for scans, raids and battle losses
const b = await runBot({ players: 12, days: 6, seed: 7 });
assert.equal(a.invariants.ok, true, JSON.stringify(a.invariants).slice(0, 800));
assert.equal(a.invariants.resources.ok, true);
assert.equal(a.invariants.units.ok, true);
assert.equal(a.invariants.negativeStock.ok, true);
assert.equal(a.invariants.failedEvents.n, 0);
assert.equal(a.invariants.unexpectedErrors.n, 0);
assert.deepStrictEqual(strip(a), strip(b), 'same options and seed, different stats');
assert.ok(a.perf.commands > 50 && a.botLogs.builds > 20 && a.botLogs.trains > 0, 'the bots built and trained');
assert.ok(a.days.length === 6 && a.meta.players === 12);
assert.ok(a.combat.scans.n > 0, 'and scouted (the military paths ran, so determinism covers them)');
const c = await runBot({ players: 12, days: 6, seed: 8 });
assert.notDeepStrictEqual(strip(a).days, strip(c).days, 'a different seed should change the world');
assert.ok(Date.now() - t0 < 15000, 'the harness must stay fast (took ' + (Date.now() - t0) + ' ms)');
});
test('bot run: a fast world reaches the landing sites, an alliance takes one and ships; conservation holds with backbone sinks', async () => {
// 20 bots, 12 sittings a day, a world at speed 15: day 100 of world time is day 6.7; takes ~5 s
const s = await runBot({ players: 20, days: 8, speed: 15, seed: 3, sessionsPerDay: 12, endgameStepMin: 60 });
assert.equal(s.invariants.ok, true, JSON.stringify(s.invariants).slice(0, 800));
assert.equal(s.endgame.open, true, 'age 120 world days: the sites are open');
assert.ok(s.endgame.strikes >= 1 && s.combat.landing.taken >= 1, 'an alliance took a landing site');
assert.ok(s.endgame.shipments > 0 && s.endgame.bestLevel >= 1, 'and its members shipped enough for Backbone node levels');
assert.ok(s.endgame.sites.some((x) => x.holder && x.contributors >= 1));
assert.equal(s.invariants.resources.final.fiber.backbone > 0, true, 'the ledger booked the node levels as a sink');
});
test('bot options: parsing, overrides and restore', () => {
const o = parseArgs(['--players', '12', '--days', '5', '--speed', '2', '--seed', '9', '--set', 'FORMULAS.costGrowth=1.3', '--set', 'UNITS.script_crew.cost.fiber=70', '--always-on']);
assert.equal(o.players, 12); assert.equal(o.speed, 2); assert.equal(o.seed, 9); assert.equal(o.alwaysOn, true); assert.equal(o.set.length, 2);
assert.throws(() => parseArgs(['--nope']), /unknown option/);
assert.throws(() => parseArgs(['--players']), /missing value/);
const before = [D.FORMULAS.costGrowth, D.UNITS.script_crew.cost.fiber];
const restore = applyOverrides(o.set);
assert.equal(D.FORMULAS.costGrowth, 1.3);
assert.equal(D.UNITS.script_crew.cost.fiber, 70);
restore();
assert.deepEqual([D.FORMULAS.costGrowth, D.UNITS.script_crew.cost.fiber], before);
assert.throws(() => applyOverrides(['FORMULAS.nope=1']), /no such path/);
});
test('bot invariants catch tampering: created resources, vanished units, negative stock and failed events', async () => {
// tamper on the last day boundary: the final checkpoint follows at the same instant, nothing settles the damage away
const tamper = (fn, o) => runBot(Object.assign({ players: 6, days: 3, seed: 2, onDay: (sim, day) => { if (day === (o && o.days || 3)) fn(sim.db); } }, o));
const a = await tamper((db) => db.run('UPDATE towns SET fiber = fiber + 500 WHERE id = 1'));
assert.equal(a.invariants.ok, false);
assert.equal(a.invariants.resources.ok, false, 'conjured Fiber must show as drift');
assert.ok(a.invariants.resources.worstDrift >= 400);
let vanished = null;
const b = await tamper((db) => { const u = db.get('SELECT town_id, unit, count FROM town_units ORDER BY count DESC LIMIT 1'); if (u) { vanished = u; db.run('DELETE FROM town_units WHERE town_id = ? AND unit = ?', u.town_id, u.unit); } }, { players: 12, days: 6 });
assert.ok(vanished, 'the bots trained some units by day 6');
assert.equal(b.invariants.units.ok, false, 'units that vanish must show as drift');
assert.equal(b.invariants.ok, false);
const c = await tamper((db) => db.run('UPDATE towns SET hardware = -5 WHERE id = 2'));
assert.equal(c.invariants.negativeStock.ok, false);
assert.equal(c.invariants.ok, false);
const d = await tamper((db) => db.run("INSERT INTO events (world_id, due_at, type, payload, status, created_at) VALUES (1, 0, 'nope', '{}', 'pending', 0)"));
assert.ok(d.invariants.failedEvents.n >= 1, 'an event without a handler fails and is counted');
assert.equal(d.invariants.ok, false);
});