'use strict'; const test = require('node:test'); const assert = require('node:assert'); const T = require('./military-helpers'); const { R, D, H } = T; test('training: batches of one building run in order, other buildings in parallel, requirements and queue limit', () => { const h = T.setup(); const a = T.addOperator(h, 'alice'); const t0 = h.clock.now(); const r1 = h.run('train', { userId: a.userId, townId: a.townId, unit: 'soc_analyst', count: 10 }); const r2 = h.run('train', { userId: a.userId, townId: a.townId, unit: 'port_scanner', count: 4 }); // same building (noc): after r1 const r3 = h.run('train', { userId: a.userId, townId: a.townId, unit: 'script_crew', count: 5 }); // ops_lab: in parallel assert.equal(r1.startAt, t0); assert.equal(r2.startAt, r1.dueAt, 'second noc batch starts when the first is done'); assert.equal(r3.startAt, t0, 'other buildings train in parallel'); assert.equal(r1.dueAt - r1.startAt, R.trainTimeMs('soc_analyst', 10, 5, 1)); assert.deepEqual(T.units(h, a.townId), {}); h.advanceTo(r3.dueAt); assert.deepEqual(T.units(h, a.townId), { script_crew: 5 }, 'only the parallel batch is done so far'); h.advanceTo(r1.dueAt); assert.deepEqual(T.units(h, a.townId), { script_crew: 5, soc_analyst: 10 }); h.advanceTo(r2.dueAt); assert.deepEqual(T.units(h, a.townId), { script_crew: 5, soc_analyst: 10, port_scanner: 4 }); assert.equal(T.notes(h, a.userId, 'train_done').length, 3); // unit power draw is part of the town's rates once the units exist const t = require('../server/state').loadTown(h.db, a.townId); assert.equal(t.unitDraw, 10 * 1 + 4 * 1 + 5 * 1); // costs were paid; requirements are enforced; the queue is limited assert.throws(() => h.run('train', { userId: a.userId, townId: a.townId, unit: 'exploit_kit', count: 1 }), (e) => e.code === 'requirements_not_met'); assert.throws(() => h.run('train', { userId: a.userId, townId: a.townId, unit: 'nonsense', count: 1 }), (e) => e.code === 'unknown_unit'); assert.throws(() => h.run('train', { userId: a.userId, townId: a.townId, unit: 'soc_analyst', count: 100000 }), (e) => e.code === 'validation_failed'); const b = T.addOperator(h, 'bob', { res: 100 }); assert.throws(() => h.run('train', { userId: b.userId, townId: b.townId, unit: 'botnet', count: 50 }), (e) => e.code === 'not_enough_resources' || e.code === 'requirements_not_met'); for (let i = 0; i < D.MILITARY.maxTrainQueue; i++) h.run('train', { userId: a.userId, townId: a.townId, unit: 'soc_analyst', count: 1 }); assert.throws(() => h.run('train', { userId: a.userId, townId: a.townId, unit: 'soc_analyst', count: 1 }), (e) => e.code === 'queue_full'); }); test('raid end to end: send, incoming warning, arrive, loot, reports for both sides, return', () => { const h = T.setup(); const att = T.addOperator(h, 'raider', { x: 5, y: 0, units: { script_crew: 40 } }); const def = T.addOperator(h, 'target', { x: 9, y: 0, units: { soc_analyst: 20 }, infra: { rack_hall: 2 } }); h.db.run('UPDATE towns SET fiber = 600, hardware = 600, capital = 600 WHERE id = ?', def.townId); const before = T.town(h, def.townId); const sent = h.run('sendMovement', { userId: att.userId, fromTownId: att.townId, type: 'raid', toTownId: def.townId, units: { script_crew: 40 } }); assert.equal(T.units(h, att.townId).script_crew, undefined, 'units left home'); assert.equal(sent.arriveAt - sent.departAt, R.latencyMs(4, 8, 1)); // the target sees origin and ETA but not the type const mv = h.mq.movements(def.userId, {}); assert.equal(mv.incoming.length, 1); assert.equal(mv.incoming[0].type, 'unknown'); assert.equal(mv.incoming[0].units, undefined); assert.equal(mv.incoming[0].arriveAt, sent.arriveAt); assert.equal(h.mq.movements(att.userId, {}).outgoing[0].type, 'raid'); assert.equal(T.notes(h, def.userId, 'incoming').length, 1); assert.equal(T.notes(h, def.userId, 'incoming')[0].type, undefined); h.advanceTo(sent.arriveAt); const ra = T.reportsOf(h, att.userId), rd = T.reportsOf(h, def.userId); assert.equal(ra.length, 1); assert.equal(rd.length, 1); assert.equal(ra[0].id, rd[0].id, 'one report shared by both sides'); assert.equal(ra[0].role, 'attacker'); assert.equal(rd[0].role, 'defender'); const rep = ra[0].data; assert.equal(rep.outcome, 'attacker_won'); assert.ok(rep.loot.fiber > 0 && rep.loot.fiber === rep.loot.hardware, 'loot is split evenly'); // 10% per rack hall level hides stock from a raid: level 2 hides 20% assert.equal(rep.cacheHidden, 0.2); const d1 = T.town(h, def.townId); assert.ok(d1.fiber < before.fiber, 'target lost fiber'); const ret = h.db.get("SELECT * FROM movements WHERE type = 'return' AND status = 'out'"); assert.ok(ret, 'a return movement carries the loot'); assert.deepEqual(JSON.parse(ret.cargo), rep.loot); const attBefore = T.town(h, att.townId).fiber; h.advanceTo(ret.arrive_at); assert.equal(T.units(h, att.townId).script_crew, 40 - (rep.attacker.units.lost.script_crew || 0)); assert.ok(T.town(h, att.townId).fiber > attBefore, 'loot arrived home'); assert.equal(T.notes(h, att.userId, 'movement_returned').length, 1); const stats = h.db.get('SELECT * FROM operator_stats WHERE operator_id = ?', att.opId); assert.ok(stats.attack_points > 0, 'killed units give battle points'); assert.equal(h.db.get("SELECT COUNT(*) AS n FROM events WHERE status = 'failed'").n, 0); }); test('a failed raid loses units and loots nothing; both sides still get the report', () => { const h = T.setup(); const att = T.addOperator(h, 'weak', { x: 5, y: 0, units: { script_crew: 2 } }); const def = T.addOperator(h, 'strong', { x: 8, y: 0, units: { blue_team: 20 }, infra: { firewall: 10 } }); const sent = h.run('sendMovement', { userId: att.userId, fromTownId: att.townId, type: 'raid', toTownId: def.townId, units: { script_crew: 2 } }); h.advanceTo(sent.arriveAt); const rep = T.reportsOf(h, att.userId)[0].data; assert.equal(rep.outcome, 'defender_won'); assert.deepEqual(rep.loot, { fiber: 0, hardware: 0, capital: 0 }); assert.equal(T.units(h, def.townId).blue_team, 20 - (rep.defender.groups[0].lost.blue_team || 0)); assert.equal(T.reportsOf(h, def.userId).length, 1); }); test('scan: success shows units, resources, buildings at 2:1; failed scan loses scanners and reveals the scanner', () => { const h = T.setup(); const att = T.addOperator(h, 'spy', { x: 5, y: 0, units: { port_scanner: 10 } }); const def = T.addOperator(h, 'watched', { x: 8, y: 0, units: { port_scanner: 3, soc_analyst: 4 } }); let s = h.run('sendMovement', { userId: att.userId, fromTownId: att.townId, type: 'scan', toTownId: def.townId, units: { port_scanner: 10 } }); h.advanceTo(s.arriveAt); const ok = T.reportsOf(h, att.userId).find((r) => r.type === 'scan').data; assert.equal(ok.success, true); assert.equal(ok.detail, 'full'); // 10 >= 2 * 3 assert.deepEqual(ok.intel.units.home, { port_scanner: 3, soc_analyst: 4 }); assert.ok(ok.intel.buildings.length > 0 && ok.intel.resources.fiber > 0); const det = T.reportsOf(h, def.userId).find((r) => r.type === 'scan_detected').data; assert.equal(det.identityRevealed, false, 'the defender only learns who scanned when their scanners win'); assert.equal(det.by, null); // now a scan that fails: 2 scanners against 3 h.advance(3 * H); h.db.run("UPDATE town_units SET count = 2 WHERE town_id = ? AND unit = 'port_scanner'", att.townId); // 10 came home (minus losses); keep 2 for the test s = h.run('sendMovement', { userId: att.userId, fromTownId: att.townId, type: 'scan', toTownId: def.townId, units: { port_scanner: 2 } }); h.advanceTo(s.arriveAt); const bad = T.reportsOf(h, att.userId).filter((r) => r.type === 'scan')[1].data; assert.equal(bad.success, false); assert.deepEqual(bad.scanners.survivors, {}); const det2 = T.reportsOf(h, def.userId).filter((r) => r.type === 'scan_detected')[1].data; assert.equal(det2.identityRevealed, true); assert.equal(det2.by.operator.name, 'spy'); }); test('event order: combat resolves in (arrive time, id) order and the whole run is deterministic', () => { function run() { const h = T.setup({ seed: 777 }); const slow = T.addOperator(h, 'slow', { x: 40, y: 0, units: { script_crew: 60 } }); const fast = T.addOperator(h, 'fast', { x: 20, y: 0, units: { botnet: 25 } }); const def = T.addOperator(h, 'victim', { x: 8, y: 0, units: { soc_analyst: 30 } }); h.db.run('UPDATE towns SET fiber = 5000, hardware = 5000, capital = 5000 WHERE id = ?', def.townId); const a = h.run('sendMovement', { userId: slow.userId, fromTownId: slow.townId, type: 'attack', toTownId: def.townId, units: { script_crew: 60 } }); const b = h.run('sendMovement', { userId: fast.userId, fromTownId: fast.townId, type: 'raid', toTownId: def.townId, units: { botnet: 25 } }); assert.ok(a.movementId < b.movementId); h.advance(200 * H); return { h, a, b, slow, fast, def }; } const x = run(), y = run(); assert.ok(x.b.arriveAt < x.a.arriveAt, 'the botnet raid is faster although it was sent second'); const reps = x.h.db.all("SELECT id, type, created_at FROM reports WHERE type IN ('raid','attack') ORDER BY id"); assert.deepEqual(reps.map((r) => r.type), ['raid', 'attack'], 'reports are created in arrival order'); assert.ok(T.snapshot(x.h) === T.snapshot(y.h), 'same inputs, same world seed: identical state'); assert.equal(x.h.db.get("SELECT COUNT(*) AS n FROM events WHERE status = 'failed'").n, 0); });