'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); });