Upstream/test/bot-run.test.js
jvanvik bf7af0679c Early-level build discount (B4) and start stock 1000 (B5)
Playtest on the live world: first hour feels a bit slow. B4: levels 1-3 of any building take x0.5/x0.67/x0.83 of the time (FORMULAS.earlyLevels/earlyTimeFactor, off at earlyLevels 0). Builds at 1 h (4 sittings/day) 8.9 -> 11.4, at 24 h 16.5 -> 20.4; mid game moves ~1 day head start (default mix), more for 2 sittings/day. B5: start stock 750 -> 1000 (measured separately: mid game unchanged, 60-min first sitting 12.1 -> 15.4 builds at 1 h). Numbers in docs/BALANCE.md.

Tested: npm test 208/208, openapi check, npm run e2e 61/61 (before B5), 50x30-day bot run INVARIANTS OK after B5. NOT tested: humans (bots only, 2 seeds), B4+B5 combined beyond one run, e2e does not exercise the discount (runs at 4000x speed). Not claimed: balanced.

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

76 lines
5.6 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)');
assert.ok(a.early && a.early['1h'].n > 0 && a.early['1h'].builds > 0 && a.early['24h'].builds >= a.early['1h'].builds && a.early['1h'].core >= 1, 'the first-hours view (builds and Core router at 1/6/24 h) is filled');
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);
});