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