Upstream/test/client-i18n.test.js
jvanvik 931f59e11e Phase 3: trade/end-game client, bot simulator, first balance pass
Built:
- Client: trade screen, world/end-game screen (landing sites, shipping, node build, final ranking), report sharing + alliance reports tab, alert describers for all notification types, error-code strings (en+nb) for every server CODE.
- tools/bot.js (+ tools/bot/): 50 headless bots against the real server in-process with a fake clock; resource/unit conservation ledger, exit 1 on broken invariants. docs/BALANCE.md has method and every run behind a number.
- Balance changes (evidence in docs/BALANCE.md): secondQueueCoreLevel 10 -> 3, TRANSPORT.capacityPerLink 500 -> 1000 (end game was shipping-limited in bot runs).
- Server fixes: POST /reports/:id/share 500 without a pending reportCleanup; bounced link shipments were silent about cargo overflow.

Tested: npm test 207/207 (new: bot determinism + invariants, client alerts/world/i18n over CODES, two regression tests that failed before their fix); openapi check up to date. Real-browser scratch runs (not in repo): WebSocket push shows an incoming-attack banner ~214 ms after launch, polling fallback ~52 s, 15 scripted flows, 71 screenshots.

NOT tested: Playwright suite (e2e/ still empty), real devices/Safari, nginx proxy websockets, throttled networks, HTTP-level bot load. NOT claimed: balance. Bots are not humans; open balance items (loot waste, escrow refund clipping, no garrisons at landing sites, end game not scaling with world speed) are listed in docs/BALANCE.md.

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

78 lines
3.8 KiB
JavaScript
Raw Permalink Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

'use strict';
const test = require('node:test');
const assert = require('node:assert');
const I = require('../public/js/i18n.js');
const D = require('../public/js/data.js');
// Strings added by later screens: public/js/strings/*.js, loaded for the parity test the same way the browser loads them.
for (const f of require('fs').readdirSync(require('path').join(__dirname, '..', 'public', 'js', 'strings')).sort()) {
if (f.endsWith('.js')) require('../public/js/strings/' + f);
}
const placeholders = (s) => (s.match(/\{\w+\}/g) || []).sort().join(',');
test('en and nb have identical key sets and placeholders', () => {
const en = Object.keys(I.strings.en).sort(), nb = Object.keys(I.strings.nb).sort();
assert.deepStrictEqual(nb.filter((k) => !en.includes(k)), [], 'keys only in nb');
assert.deepStrictEqual(en.filter((k) => !nb.includes(k)), [], 'keys only in en');
for (const k of en) assert.strictEqual(placeholders(I.strings.nb[k]), placeholders(I.strings.en[k]), 'placeholders differ for ' + k);
});
test('every building, resource and tile type has a player-facing name in both languages', () => {
for (const l of I.LANGS) {
for (const b of Object.keys(D.BUILDINGS)) {
assert.ok(I.strings[l]['building.' + b + '.name'], l + ' name for ' + b);
assert.ok(I.strings[l]['building.' + b + '.desc'], l + ' desc for ' + b);
}
for (const r of D.RESOURCES.concat(['power', 'battery'])) assert.ok(I.strings[l]['res.' + r], l + ' res.' + r);
for (const t of ['land', 'rural', 'incumbent', 'pop', 'void']) assert.ok(I.strings[l]['tile.' + t], l + ' tile.' + t);
}
});
test('every error code the server can send is translated in en and nb (iterates server/errors.js CODES)', () => {
const { CODES } = require('../server/errors');
// client-side codes (api.js ApiError) come on top of the server's table
const codes = Object.keys(CODES).concat(['network_error', 'unknown']);
assert.ok(codes.length > 50, 'the CODES table was read');
for (const l of I.LANGS) {
const missing = codes.filter((c) => !I.strings[l]['err.' + c]);
assert.deepStrictEqual(missing, [], l + ' is missing err.<code> translations');
}
});
test('t() fills placeholders, falls back to en then to the key', () => {
assert.strictEqual(I.t('en', 'sheet.build', { n: 3 }), 'Build level 3');
assert.strictEqual(I.t('nb', 'sheet.build', { n: 3 }), 'Bygg nivå 3');
assert.strictEqual(I.t('xx', 'common.close'), 'Close');
assert.strictEqual(I.t('en', 'no.such.key'), 'no.such.key');
assert.strictEqual(I.t('en', 'sheet.build'), 'Build level {n}');
});
test('duration and clock formatting', () => {
assert.strictEqual(I.fmtDuration('en', 0), '0s');
assert.strictEqual(I.fmtDuration('en', 45000), '45s');
assert.strictEqual(I.fmtDuration('en', 133000), '2m 13s');
assert.strictEqual(I.fmtDuration('en', 3600000), '1h');
assert.strictEqual(I.fmtDuration('en', 3600000 * 26 + 60000 * 5), '1d 2h');
assert.strictEqual(I.fmtDuration('nb', 3600000 * 2 + 60000 * 5), '2t 5m');
assert.strictEqual(I.fmtClock(1), '0:01');
assert.strictEqual(I.fmtClock(65000), '1:05');
assert.strictEqual(I.fmtClock(3661000), '1:01:01');
assert.strictEqual(I.fmtClock(-5), '0:00');
});
test('number formatting', () => {
assert.strictEqual(I.fmtNum('en', 749.9), '749');
assert.strictEqual(I.fmtNum('en', 123456), '123k');
assert.strictEqual(I.fmtNum('en', 2500000), '2.5M');
assert.strictEqual(I.fmtDec('en', 12.34, true), '+12.3');
assert.strictEqual(I.fmtDec('en', -3, true), '−3.0');
assert.strictEqual(I.fmtDec('en', 420.4), '420');
});
test('language detection', () => {
assert.strictEqual(I.detectLang('nb-NO'), 'nb');
assert.strictEqual(I.detectLang('nn'), 'nb');
assert.strictEqual(I.detectLang('no'), 'nb');
assert.strictEqual(I.detectLang('en-GB'), 'en');
assert.strictEqual(I.detectLang(undefined), 'en');
});