'use strict'; const test = require('node:test'); const assert = require('node:assert'); const T = require('./military-helpers'); const { R, D, H } = T; test('transport: capacity and links come from the Internet exchange; cargo arrives, carriers drive back, hooks fire', () => { const h = T.setup(); const a = T.addOperator(h, 'sender', { x: 5, y: 0, infra: { internet_exchange: 2 } }); const b = T.addOperator(h, 'receiver', { x: 9, y: 0 }); h.db.run('UPDATE towns SET fiber = 1000, hardware = 1000, capital = 1000 WHERE id = ?', b.townId); const M = require('../server/military/movements'); const seen = []; M.hooks.transportArrived.push((ctx, mv, r) => seen.push({ id: mv.id, added: r.added, extra: mv.extra })); const send = (cargo) => h.run('sendTransport', { userId: a.userId, fromTownId: a.townId, toTownId: b.townId, cargo }); assert.throws(() => send({ fiber: D.TRANSPORT.capacityPerLink + 100 }), (e) => e.code === 'capacity_exceeded'); assert.throws(() => send({ fiber: 0 }), (e) => e.code === 'validation_failed'); assert.throws(() => send({ fiber: 10 * D.TRANSPORT.capacityPerLink }), (e) => e.code === 'capacity_exceeded'); const noIx = T.addOperator(h, 'no-ix', { x: 12, y: 0, infra: { internet_exchange: 0 } }); assert.throws(() => h.run('sendTransport', { userId: noIx.userId, fromTownId: noIx.townId, toTownId: b.townId, cargo: { fiber: 10 } }), (e) => e.code === 'no_transport_link'); const before = T.town(h, a.townId).fiber; const t1 = send({ fiber: 300, capital: 200 }); const t2 = send({ hardware: 100 }); assert.throws(() => send({ hardware: 10 }), (e) => e.code === 'no_transport_link', 'two links, both busy'); assert.ok(Math.abs(T.town(h, a.townId).fiber - (before - 300)) < 1, 'cargo left the POP at once'); assert.equal(t1.arriveAt - t1.departAt, R.transportMs(4, 1)); const rb = T.town(h, b.townId).fiber; h.advanceTo(t1.arriveAt); assert.ok(T.town(h, b.townId).fiber >= rb + 300 - 1, 'delivered'); assert.equal(seen.length, 2, 'the arrival hook fired for both transports (same distance, same time)'); assert.deepEqual(seen[0].added, { fiber: 300, capital: 200 }); assert.equal(T.notes(h, b.userId, 'transport_arrived').length, 2); // the link is busy until the carriers are home, then free again assert.throws(() => send({ hardware: 10 }), (e) => e.code === 'no_transport_link'); h.advance(10 * H); assert.doesNotThrow(() => send({ hardware: 10 })); assert.equal(h.db.get("SELECT COUNT(*) AS n FROM events WHERE status = 'failed'").n, 0); }); test('transport above the Rack hall cap is lost; transports are recallable only in the first 90 s', () => { const h = T.setup(); const a = T.addOperator(h, 'a', { x: 5, y: 0, infra: { internet_exchange: 3 } }); const b = T.addOperator(h, 'b', { x: 30, y: 0 }); const cap = R.storageCap(require('../server/state').loadTown(h.db, b.townId)); h.db.run('UPDATE towns SET fiber = ?, hardware = 0, capital = 0 WHERE id = ?', cap - 100, b.townId); const t = h.run('sendTransport', { userId: a.userId, fromTownId: a.townId, toTownId: b.townId, cargo: { fiber: 400 } }); h.advanceTo(t.arriveAt); assert.ok(Math.abs(T.town(h, b.townId).fiber - cap) < 1e-6 + 60, 'capped at the Rack hall'); assert.equal(T.notes(h, b.userId, 'transport_arrived')[0].cargoLost.fiber > 0, true); const c = h.run('sendTransport', { userId: a.userId, fromTownId: a.townId, toTownId: b.townId, cargo: { capital: 250 } }); const capBefore = T.town(h, a.townId).capital; h.advance(60 * 1000); const rc = h.run('recallMovement', { userId: a.userId, movementId: c.movementId }); assert.ok(rc.arriveAt <= h.clock.now() + 61 * 1000 + 1000); assert.equal(h.db.get('SELECT status FROM movements WHERE id = ?', c.movementId).status, 'recalled'); h.advanceTo(rc.arriveAt); assert.ok(T.town(h, a.townId).capital > capBefore + 200, 'the cargo is back home'); // too late const d = h.run('sendTransport', { userId: a.userId, fromTownId: a.townId, toTownId: b.townId, cargo: { capital: 50 } }); h.advance(91 * 1000); assert.throws(() => h.run('recallMovement', { userId: a.userId, movementId: d.movementId }), (e) => e.code === 'recall_expired'); // others cannot recall your movements const e2 = h.run('sendTransport', { userId: a.userId, fromTownId: a.townId, toTownId: b.townId, cargo: { capital: 10 } }); assert.throws(() => h.run('recallMovement', { userId: b.userId, movementId: e2.movementId }), (e) => e.code === 'not_found'); }); test('recall: a recalled raid never fights and its units come home after as long as they were out', () => { const h = T.setup(); const a = T.addOperator(h, 'a', { x: 5, y: 0, units: { script_crew: 20 } }); const d = T.addOperator(h, 'd', { x: 12, y: 0, units: { soc_analyst: 2 } }); const s = h.run('sendMovement', { userId: a.userId, fromTownId: a.townId, type: 'raid', toTownId: d.townId, units: { script_crew: 20 } }); h.advance(30 * 1000); const r = h.run('recallMovement', { userId: a.userId, movementId: s.movementId }); assert.equal(r.arriveAt, h.clock.now() + 30 * 1000); assert.equal(h.mq.movements(d.userId, {}).incoming.length, 0, 'the warning disappears'); h.advance(2 * H); assert.equal(T.units(h, a.townId).script_crew, 20); assert.equal(T.reportsOf(h, d.userId).length, 0); assert.equal(h.db.get("SELECT COUNT(*) AS n FROM events WHERE status = 'failed'").n, 0); }); test('research: levels need a Research lab, take time, cost resources, and raise unit strength by 5% per level', () => { const h = T.setup(); const a = T.addOperator(h, 'scholar', { infra: { research_lab: 3, ops_lab: 3 } }); const r1 = h.run('research', { userId: a.userId, townId: a.townId, unit: 'script_crew', stat: 'attack' }); assert.equal(r1.toLevel, 1); assert.throws(() => h.run('research', { userId: a.userId, townId: a.townId, unit: 'soc_analyst', stat: 'defence' }), (e) => e.code === 'queue_full', 'one research at a time per POP'); h.advanceTo(r1.dueAt); assert.equal(h.db.get('SELECT level FROM town_research WHERE town_id = ?', a.townId).level, 1); assert.equal(T.notes(h, a.userId, 'research_done').length, 1); // level 2 needs lab 4 assert.throws(() => h.run('research', { userId: a.userId, townId: a.townId, unit: 'script_crew', stat: 'attack' }), (e) => e.code === 'requirements_not_met'); T.setBuildings(h, a.townId, { research_lab: 8 }); const r2 = h.run('research', { userId: a.userId, townId: a.townId, unit: 'script_crew', stat: 'attack' }); h.advanceTo(r2.dueAt); assert.equal(h.db.get('SELECT level FROM town_research WHERE town_id = ?', a.townId).level, 2); assert.throws(() => h.run('research', { userId: a.userId, townId: a.townId, unit: 'port_scanner', stat: 'attack' }), (e) => e.code === 'not_researchable'); assert.equal(R.unitStat('script_crew', 'attack', { 'script_crew:attack': 2 }), 40 * 1.1); // research is applied in combat: the snapshot taken at departure is what the attack uses T.giveUnits(h, a.townId, { script_crew: 10 }); const d = T.addOperator(h, 'd', { x: 12, y: 0 }); const s = h.run('sendMovement', { userId: a.userId, fromTownId: a.townId, type: 'raid', toTownId: d.townId, units: { script_crew: 10 } }); h.advanceTo(s.arriveAt); assert.ok(Math.abs(T.reportsOf(h, a.userId).find((r) => r.type === 'raid').data.power.attack - 10 * 40 * 1.1) < 1e-9); }); test('movement list: own outgoing movements are detailed, foreign incoming ones hide type, units and cargo', () => { const h = T.setup(); const a = T.addOperator(h, 'a', { x: 5, y: 0, units: { script_crew: 10, port_scanner: 2 }, infra: { internet_exchange: 1 } }); const d = T.addOperator(h, 'd', { x: 9, y: 0 }); h.run('sendMovement', { userId: a.userId, fromTownId: a.townId, type: 'attack', toTownId: d.townId, units: { script_crew: 10 } }); h.run('sendMovement', { userId: a.userId, fromTownId: a.townId, type: 'scan', toTownId: d.townId, units: { port_scanner: 2 } }); h.run('sendTransport', { userId: a.userId, fromTownId: a.townId, toTownId: d.townId, cargo: { fiber: 10 } }); const mine = h.mq.movements(a.userId, {}); assert.deepEqual(mine.outgoing.map((m) => m.type).sort(), ['attack', 'scan', 'transport']); assert.ok(mine.outgoing.every((m) => m.recallableUntil !== null)); const theirs = h.mq.movements(d.userId, {}); assert.equal(theirs.incoming.length, 3); for (const m of theirs.incoming) { assert.equal(m.type, 'unknown'); assert.equal(m.units, undefined); assert.equal(m.cargo, undefined); assert.equal(m.from.townId, a.townId); assert.equal(m.from.owner.name, 'a'); } // and the town view carries the same warnings assert.equal(h.queries.town(d.userId, d.townId).town.military.movements.incoming.length, 3); // someone else cannot look at your POP assert.throws(() => h.queries.town(d.userId, a.townId), (e) => e.code === 'not_found'); }); test('tile info: public facts, distances, settle verdict', () => { const h = T.setup(); const a = T.addOperator(h, 'a', { x: 5, y: 0 }); const d = T.addOperator(h, 'd', { x: 9, y: 0, protectedUntil: h.clock.now() + H }); const info = h.mq.tileInfo(a.userId, { x: d.x, y: d.y }); assert.equal(info.type, 'pop'); assert.equal(info.town.owner.protected, true); assert.equal(info.town.mine, false); assert.ok(Math.abs(info.distances[0].distance - 4) < 1e-9); const land = T.landNear(h, 8, 3); const li = h.mq.tileInfo(a.userId, { x: land.x, y: land.y }); assert.equal(li.type, 'land'); assert.equal(li.settle.possible, false); assert.equal(li.settle.code, 'footprint_too_low'); assert.equal(li.settle.footprintNeeded, R.footprintNeeded(1)); assert.throws(() => h.mq.tileInfo(a.userId, { x: 999, y: 0 }), (e) => e.code === 'not_found'); }); test('reports: cursor pages newest first, read flags, 14 day retention cleanup', () => { const h = T.setup(); const a = T.addOperator(h, 'a', { x: 5, y: 0, units: { port_scanner: 30 } }); const d = T.addOperator(h, 'd', { x: 9, y: 0 }); for (let i = 0; i < 3; i++) { h.run('sendMovement', { userId: a.userId, fromTownId: a.townId, type: 'scan', toTownId: d.townId, units: { port_scanner: 5 } }); h.advance(2 * H); } const p1 = h.mq.reports(a.userId, { limit: 2 }); assert.equal(p1.items.length, 2); assert.ok(p1.items[0].id > p1.items[1].id); assert.equal(p1.unread, 3); const p2 = h.mq.reports(a.userId, { limit: 2, after: p1.next }); assert.equal(p2.items.length, 1); assert.equal(p2.next, null); h.run('markReportRead', { userId: a.userId, reportId: p1.items[0].id }); assert.equal(h.mq.reports(a.userId, {}).unread, 2); assert.equal(h.mq.report(a.userId, p1.items[0].id).report.read, true); assert.throws(() => h.mq.report(d.userId, p1.items[0].id), (e) => e.code === 'not_found', 'the scanner owner only: the defender has his own scan_detected report'); h.run('markReportRead', { userId: a.userId, all: true }); assert.equal(h.mq.reports(a.userId, { unread: true }).items.length, 0); // retention assert.equal(h.db.get('SELECT COUNT(*) AS n FROM reports').n, 6); h.advance(15 * 24 * H); assert.equal(h.db.get('SELECT COUNT(*) AS n FROM reports').n, 0, 'expired reports are gone'); assert.equal(h.db.get("SELECT COUNT(*) AS n FROM events WHERE type = 'reportCleanup' AND status = 'pending'").n, 0, 'no cleanup pending without reports'); assert.equal(h.db.get('SELECT COUNT(*) AS n FROM report_recipients').n, 0); }); test('moving units between your own POPs makes them live there (no "stationed" bookkeeping)', () => { const h = T.setup(); const a = T.addOperator(h, 'mover', { x: 5, y: 0, units: { soc_analyst: 10 } }); const second = T.addTown(h, a, { x: 8, y: 1 }); const s = h.run('sendMovement', { userId: a.userId, fromTownId: a.townId, type: 'reinforce', toTownId: second.townId, units: { soc_analyst: 6 } }); h.advanceTo(s.arriveAt); assert.deepEqual(T.units(h, second.townId), { soc_analyst: 6 }); assert.deepEqual(T.units(h, a.townId), { soc_analyst: 4 }); assert.equal(h.db.get('SELECT COUNT(*) AS n FROM stationed').n, 0); assert.throws(() => h.run('sendMovement', { userId: a.userId, fromTownId: a.townId, type: 'reinforce', toTownId: a.townId, units: { soc_analyst: 1 } }), (e) => e.code === 'invalid_target'); });