Upstream/test/client-garrison.test.js
jvanvik 3c23c51902 Phase 4: close loot/refund leaks, landing-site garrisons, speed-scaled shipping, update/offsite tooling, broad e2e
Built:
- Economy: raid loot capped to the room the raiders have at home (rest stays with the victim, "lootLeft" in the report); trade escrow reserves its room so refunds always fit; trade accept refuses with no_room_for_goods when the goods would not fit. Loot destroyed per 30-day run 31-61k -> ~1k, refunds clipped ~27k -> 0 (docs/BALANCE.md, 3 seeds).
- End game: members of the holding alliance garrison landing sites (migration 005_site_garrison.sql), garrison counts in site defence, 72 h capture shield, withdraw route, POP view lists garrisoned units. Shipping capacity scales with world speed (3x world: winner 76-98 -> ~30-36 days after sites open). Bot runs show real contests (84-194 attacks on held sites at 1x).
- Ops: deploy/update.sh (backup, stop, ff-merge, npm ci --ignore-scripts, health check, rollback), deploy/upstream-offsite (rsync only), admin end-world snapshots results, EPIPE fix, docs/workflows/ filled in.
- e2e 62 -> 163 checks: units, raids/reports, alliance+messages, trade, end game, nb, PWA, reconnect, slow polling fallback (opt-in). Two app bugs found: message push ignored, "New" button stayed English.

Tested: npm test 274/274, openapi check, npm run e2e 163/163 (137 s), bot runs 50x30d seeds 1-3 and 140-day/60-day speed-3 end-game runs INVARIANTS OK, adversarial review pass (findings fixed or documented).
NOT tested: update.sh on a real box (systemd, real npm, runuser; stubs only, shellcheck not installed), garrison card/loot text on a phone, Node 22.13, humans. Known leaks left open and documented (BALANCE S6): loot/shipments can still overflow when stock arrives in flight, bounced backbone shipments lose overflow (reported to the sender). Garrison numbers (heldDefenceMult 2, shield 72 h) are first guesses from 2 seeds. Not claimed: balanced.

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

66 lines
4.8 KiB
JavaScript

'use strict';
/* Client side of the landing-site garrison (docs/DECISIONS.md E6): who may reinforce a site, how the manage sheet reads the server's
* garrison, strings in both languages. Pure logic and source checks, no browser. */
const test = require('node:test');
const assert = require('node:assert');
const fs = require('fs');
const path = require('path');
const Mil = require('../public/js/milcore.js');
const End = require('../public/js/endcore.js');
const I18n = require('../public/js/i18n.js');
const D = require('../public/js/data.js');
const ROOT = path.join(__dirname, '..', 'public', 'js');
for (const f of fs.readdirSync(path.join(ROOT, 'strings')).sort()) if (f.endsWith('.js')) require('../public/js/strings/' + f);
const tile = (mine) => ({ type: 'backbone', x: 10, y: 10, backbone: { index: 2, mine, holder: { allianceId: 7, tag: 'X', name: 'X' } } });
test('send dialog: members of the holding alliance can reinforce a site (and not attack it); everybody else attacks or scans', () => {
const home = { soc_analyst: 20, script_crew: 10, port_scanner: 3 };
const mine = Mil.moveOptions({ tile: tile(true), home, transport: { links: 0, active: 0 }, allianceId: 7 });
assert.deepEqual(mine.map((o) => [o.type, o.ok]), [['scan', true], ['attack', false], ['reinforce', true]]);
assert.equal(mine.find((o) => o.type === 'attack').reason, 'own_site');
const none = Mil.moveOptions({ tile: tile(true), home: { script_crew: 5 }, transport: {}, allianceId: 7 });
assert.equal(none.find((o) => o.type === 'reinforce').ok, true, 'attackers can garrison too');
const noUnits = Mil.moveOptions({ tile: tile(true), home: {}, transport: {}, allianceId: 7 });
assert.deepEqual(noUnits.find((o) => o.type === 'reinforce'), { type: 'reinforce', ok: false, reason: 'no_units' });
const rival = Mil.moveOptions({ tile: tile(false), home, transport: {}, allianceId: 8 });
assert.deepEqual(rival.map((o) => o.type), ['scan', 'attack'], 'a rival cannot reinforce');
const loner = Mil.moveOptions({ tile: tile(false), home, transport: {}, allianceId: null });
assert.deepEqual(loner.map((o) => o.type), ['scan', 'attack']);
assert.equal(loner.find((o) => o.type === 'attack').reason, 'no_alliance');
});
test('garrisonView: rows by defence, share of the whole defence, your own marked, offline groups count nothing', () => {
const data = {
defence: { static: 8000, garrison: 12000, total: 20000 },
garrison: [
{ ownerTownId: 5, townName: 'B POP', operator: { operatorId: 2, name: 'bea' }, units: { soc_analyst: 100 }, online: true, power: 2500, mine: false },
{ ownerTownId: 3, townName: 'A POP', operator: { operatorId: 1, name: 'ann' }, units: { blue_team: 120 }, online: true, power: 9600, mine: true },
{ ownerTownId: 9, townName: 'C POP', operator: null, units: { soc_analyst: 10 }, online: false, power: 250, mine: false },
],
};
const v = End.garrisonView(data);
assert.deepEqual(v.rows.map((r) => r.ownerTownId), [3, 5, 9]);
assert.equal(v.rows[0].mine, true);
assert.equal(v.rows[0].share, 9600 / 20000);
assert.equal(v.rows[2].share, 0, 'an offline group defends nothing');
assert.equal(v.rows[2].online, false);
assert.equal(v.rows[2].name, '');
assert.deepEqual([v.static, v.garrison, v.total], [8000, 12000, 20000]);
assert.deepEqual(End.garrisonView({}).rows, [], 'a missing block is an empty garrison');
assert.equal(End.garrisonView(null).total, 0);
});
test('every string the garrison UI uses exists in en and nb', () => {
const src = ['screens/world.js', 'screens/send.js', 'milstate.js', 'alertkinds.js'].map((f) => fs.readFileSync(path.join(ROOT, f), 'utf8')).join('\n');
const keys = new Set([...src.matchAll(/App\.t\('((?:wld\.gar|send\.landing|alerts\.garrison|alerts\.backbone_incoming)[a-zA-Z_.]*)'/g)].map((m) => m[1]).filter((k) => !k.endsWith('.')).concat(['send.invalid.not_holder', 'send.invalid.site_shielded', 'wld.shield', 'rep.landing.garrisonOf', 'alerts.garrison_released.alliance_dissolved', 'alerts.garrison_released.left_alliance', 'alerts.garrison_released.town_lost', 'alerts.garrison_released.other']));
assert.ok(keys.size >= 12, 'found the keys (' + keys.size + ')');
for (const l of ['en', 'nb']) for (const k of keys) assert.ok(I18n.strings[l][k], l + ' has ' + k);
});
test('nothing in the client reads TRANSPORT.capacityPerLink directly (a link carries capacityPerLink x world speed: rules.transportCapacity)', () => {
const walk = (dir) => fs.readdirSync(dir, { withFileTypes: true }).flatMap((e) => (e.isDirectory() ? walk(path.join(dir, e.name)) : e.name.endsWith('.js') ? [path.join(dir, e.name)] : []));
const hits = walk(ROOT).filter((f) => !/[\\/](data|rules)\.js$/.test(f) && /TRANSPORT\.capacityPerLink/.test(fs.readFileSync(f, 'utf8')));
assert.deepEqual(hits, []);
assert.ok(D.TRANSPORT.capacityPerLink > 0);
});