Upstream/test/client-mil.test.js
jvanvik b18645dca1 Phase 2: client military/social screens, WebSocket push, trade + endgame, review findings pending
Built: units/movements/reports/send/expansion screens, alliance/messages/rankings screens, ws client + server /ws push
(docs/PROTOCOL.md), Internet exchange trade, backbone endgame + world end, shared reports, buildDone relative levels.
Tested: npm test 175/175. Two adversarial reviews found issues (trade-leg recall exploit, __proto__ via validate.js,
rankings DoS, static headers, ...): fixes follow in the next commit.
NOT done: bots/balance, e2e suite, real-device checks.

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

83 lines
5.5 KiB
JavaScript

'use strict';
const test = require('node:test');
const assert = require('node:assert');
const M = require('../public/js/milcore.js');
const D = require('../public/js/data.js');
const land = (settle) => ({ x: 3, y: 4, type: 'land', settle: Object.assign({ possible: true, code: null, footprint: 100, footprintNeeded: 50, maxDistance: 15 }, settle) });
const rival = (owner) => ({ x: 5, y: 5, type: 'pop', town: { id: 9, name: 'R', mine: false, owner: Object.assign({ id: 2, protected: false, allianceId: null }, owner) } });
const types = (o) => o.map((x) => x.type + (x.ok ? '' : '!'));
test('unitsForType: roles, fixed units and zero counts are filtered', () => {
const home = { script_crew: 5, honeypot: 3, port_scanner: 2, soc_analyst: 0, rollout_crew: 3 };
assert.deepStrictEqual(M.unitsForType('raid', home).map((u) => u.unit), ['script_crew']); // honeypot is fixed, scanner is a scout
assert.deepStrictEqual(M.unitsForType('scan', home).map((u) => u.unit), ['port_scanner']);
assert.deepStrictEqual(M.unitsForType('settle', home).map((u) => u.unit), ['rollout_crew']);
assert.deepStrictEqual(M.unitsForType('reinforce', home).map((u) => u.unit), ['port_scanner', 'script_crew', 'rollout_crew']);
assert.deepStrictEqual(M.unitsForType('transport', home), []);
});
test('moveOptions on a rival POP: hostile types need units, protection blocks them, takeover needs an M&A team', () => {
const home = { script_crew: 10, port_scanner: 1 };
assert.deepStrictEqual(types(M.moveOptions({ tile: rival(), home })), ['scan', 'raid', 'attack', 'takeover!']);
assert.strictEqual(M.moveOptions({ tile: rival(), home }).find((o) => o.type === 'takeover').reason, 'needs_ma_team');
const prot = M.moveOptions({ tile: rival({ protected: true }), home });
assert.deepStrictEqual(types(prot), ['scan', 'raid!', 'attack!', 'takeover!']);
assert.strictEqual(prot[1].reason, 'target_protected');
assert.deepStrictEqual(types(M.moveOptions({ tile: rival(), home: {} })), ['scan!', 'raid!', 'attack!', 'takeover!']);
assert.ok(M.moveOptions({ tile: rival(), home: { ma_team: 1, script_crew: 1 } }).find((o) => o.type === 'takeover').ok);
});
test('moveOptions: reinforce only for the same alliance; own POP offers reinforce and transport', () => {
const home = { soc_analyst: 4 };
const ally = rival({ allianceId: 7 });
assert.ok(!M.moveOptions({ tile: ally, home, allianceId: 5 }).some((o) => o.type === 'reinforce'));
assert.ok(M.moveOptions({ tile: ally, home, allianceId: 7 }).some((o) => o.type === 'reinforce' && o.ok));
const mine = { x: 1, y: 1, type: 'pop', town: { id: 3, mine: true, owner: { id: 1 } } };
assert.deepStrictEqual(types(M.moveOptions({ tile: mine, home, transport: { links: 2, active: 2 } })), ['reinforce', 'transport!']);
assert.strictEqual(M.moveOptions({ tile: mine, home, transport: { links: 0, active: 0 } })[1].reason, 'no_link');
assert.deepStrictEqual(types(M.moveOptions({ tile: mine, home, transport: { links: 2, active: 1 } })), ['reinforce', 'transport']);
});
test('moveOptions: incumbent sites take scan and attack; land offers settle with its reason; rural nothing', () => {
assert.deepStrictEqual(types(M.moveOptions({ tile: { type: 'incumbent' }, home: { port_scanner: 1, script_crew: 1 } })), ['scan', 'attack']);
assert.deepStrictEqual(types(M.moveOptions({ tile: land(), home: {}, crews: 3 })), ['settle']);
assert.deepStrictEqual(types(M.moveOptions({ tile: land(), home: {}, crews: 2 })), ['settle!']);
const far = M.moveOptions({ tile: land({ possible: false, code: 'out_of_range' }), home: {}, crews: 3 });
assert.strictEqual(far[0].reason, 'settle:out_of_range');
assert.deepStrictEqual(M.moveOptions({ tile: { type: 'rural' }, home: { script_crew: 1 } }), []);
});
test('defaultUnits / cleanSelection / preview', () => {
assert.deepStrictEqual(M.defaultUnits('settle', {}), { rollout_crew: D.EXPANSION.crews });
assert.deepStrictEqual(M.defaultUnits('raid', {}), {});
assert.deepStrictEqual(M.cleanSelection({ a: 0, script_crew: 99, botnet: 2.7 }, { script_crew: 5, botnet: 1 }), { script_crew: 5, botnet: 1 });
const p = M.preview('raid', { x: 0, y: 0 }, { x: 16, y: 0 }, { script_crew: 10, botnet: 1 }, {}, 1);
assert.strictEqual(p.dist, 16);
assert.strictEqual(p.slowest, 8); // script crew 8 tiles/h
assert.strictEqual(p.ms, 2 * 3600 * 1000);
assert.strictEqual(p.carry, 10 * 50 + 150);
assert.strictEqual(M.preview('transport', { x: 0, y: 0 }, { x: 12, y: 0 }, {}, { fiber: 5 }, 2).ms, 30 * 60 * 1000); // 12 tiles at 12/h and speed 2x = 30 min
});
test('perspective: the report role decides who won', () => {
assert.strictEqual(M.perspective('attacker', 'attacker_won'), 'victory');
assert.strictEqual(M.perspective('attacker', 'defender_won'), 'defeat');
assert.strictEqual(M.perspective('defender', 'attacker_won'), 'defeat');
assert.strictEqual(M.perspective('reinforcer', 'defender_won'), 'victory');
});
test('backoffMs grows to a 60 s cap with +-25% jitter', () => {
assert.strictEqual(M.backoffMs(0, 0.5), 1000);
assert.strictEqual(M.backoffMs(1, 0.5), 2000);
assert.strictEqual(M.backoffMs(3, 0.5), 8000);
assert.strictEqual(M.backoffMs(20, 0.5), 60000);
assert.strictEqual(M.backoffMs(2, 0), 3000);
assert.ok(M.backoffMs(2, 0.999) >= 4990 && M.backoffMs(2, 0.999) <= 5000);
});
test('siegeTargets are infrastructure types only', () => {
const t = M.siegeTargets();
assert.ok(t.includes('firewall') && t.includes('core_router'));
assert.ok(!t.includes('backbone_node') && !t.some((k) => D.BUILDINGS[k].kind !== 'infra'));
});