'use strict'; /* The Alerts feed: every notification type the server writes has a describer, and each describer turns the real payload shapes * into a readable title (no raw keys, no "undefined", no "[object Object]"). The browser files cannot be loaded here, so the pure * describers (public/js/alertkinds.js) are run directly and the others are found by reading the sources. */ const test = require('node:test'); const assert = require('node:assert'); const fs = require('fs'); const path = require('path'); const I = require('../public/js/i18n.js'); for (const f of fs.readdirSync(path.join(__dirname, '..', 'public', 'js', 'strings')).sort()) if (f.endsWith('.js')) require('../public/js/strings/' + f); const AK = require('../public/js/alertkinds.js'); const ROOT = path.join(__dirname, '..'); 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)] : [])); // every notification type the server writes: notify(uid, worldId, 'type', ...), A.notifyAlliance(ctx, id, 'type', ...), notifyWorld(...) function serverTypes() { const out = new Set(); for (const f of walk(path.join(ROOT, 'server'))) { const src = fs.readFileSync(f, 'utf8'); for (const m of src.matchAll(/\bnotify(?:Alliance|Operator|World)?\(([^'`\n]*?)'([a-z_]+)'/g)) out.add(m[2]); } return out; } const lang = { en: 'en', nb: 'nb' }; function ctxFor(l) { return { t: (k, v) => I.t(l, k, v), num: (n) => I.fmtNum(l, n), resName: (r) => I.t(l, 'res.' + r), townName: (id) => (id === 1 ? 'Home POP' : null), clockTime: () => '12:00', dayTime: () => '1 Jan, 12:00', }; } const T = { resource: 'fiber', amount: 400 }, W = { resource: 'hardware', amount: 300 }; const alliance = { id: 7, tag: 'NSP', name: 'North Sea Peering' }; // payloads in the shapes the server writes them (grep the notify calls) const SAMPLES = { trade_accepted: { offerId: 3, townId: 1, byTownId: 2, give: T, want: W }, trade_completed: { offerId: 3, give: T, want: W }, trade_expired: { offerId: 3, townId: 1, returned: T }, trade_void: { offerId: 3, townId: 1 }, trade_refund_clipped: { offerId: 3, townId: 1, resource: 'fiber', lost: 120, credited: 280 }, backbone_open: { sites: [{ siteId: 1, index: 0, x: 3, y: 4 }, { siteId: 2, index: 1, x: 5, y: 6 }] }, backbone_taken: { siteId: 1, index: 2, x: 10, y: -4, level: 12, by: { allianceId: 7, tag: 'NSP', name: 'North Sea Peering' }, from: { allianceId: 8, tag: 'FTC', name: 'Fjord' } }, backbone_lost: { siteId: 1, index: 2, x: 10, y: -4, level: 12, by: { allianceId: 8, tag: 'FTC', name: 'Fjord' }, from: { allianceId: 7, tag: 'NSP', name: 'North Sea Peering' } }, backbone_level: { siteId: 1, index: 2, level: 20 }, backbone_incoming: { siteId: 1, index: 2, x: 10, y: -4, arriveAt: 1767225600000 }, backbone_delivered: { movementId: 5, siteId: 1, cargo: { fiber: 500, hardware: 0, capital: 120 } }, backbone_ship_bounced: { movementId: 5, siteId: 1, cargo: { fiber: 500 } }, backbone_won: { allianceId: 7, name: 'North Sea Peering', tag: 'NSP', siteId: 1, endsAt: 1767225600000 }, world_ended: { winner: { allianceId: 7, name: 'North Sea Peering', tag: 'NSP', siteId: 1, wonAt: 1 }, endedAt: 1767225600000 }, alliance_invite: { inviteId: 1, allianceId: 7, tag: 'NSP', name: 'North Sea Peering', by: 'Alice', expiresAt: 1767225600000 }, alliance_invite_declined: { inviteId: 1, allianceId: 7, by: 'Bob' }, alliance_member_joined: { allianceId: 7, operatorId: 3, name: 'Bob' }, alliance_member_left: { allianceId: 7, operatorId: 3, name: 'Bob', kicked: false }, alliance_kicked: { allianceId: 7, tag: 'NSP', name: 'North Sea Peering' }, alliance_role: { allianceId: 7, tag: 'NSP', from: 'member', role: 'officer' }, alliance_leader: { allianceId: 7, operatorId: 3, name: 'Bob', from: 'Alice' }, alliance_disbanded: { allianceId: 7, tag: 'NSP', name: 'North Sea Peering', reason: 'disbanded' }, diplomacy_proposal: { proposalId: 1, from: alliance, state: 'nap', expiresAt: 1767225600000 }, diplomacy_declined: { proposalId: 1, by: alliance, state: 'nap' }, diplomacy_changed: { allianceId: 8, other: alliance, from: 'neutral', to: 'war' }, diplomacy_cancel_pending: { allianceId: 8, other: alliance, from: 'nap', to: 'neutral', effectiveAt: 1767225600000, startedBy: 7 }, diplomacy_cancel_revoked: { allianceId: 8, other: alliance, state: 'nap' }, message: { messageId: 4, from: { operatorId: 3, name: 'Bob' } }, alliance_message: { messageId: 4, allianceId: 7, tag: 'NSP', from: 'Alice' }, }; const KNOWN_ELSEWHERE = new Set(['build_done']); // screens/alerts.js test('every notification type the server writes has a describer (alertkinds.js, milstate.js or alerts.js)', () => { const have = new Set(Object.keys(AK.make(ctxFor('en')))); const mil = fs.readFileSync(path.join(ROOT, 'public', 'js', 'milstate.js'), 'utf8'); for (const m of mil.matchAll(/\bkinds\.([a-z_]+) =/g)) have.add(m[1]); for (const k of KNOWN_ELSEWHERE) have.add(k); const types = serverTypes(); assert.ok(types.size >= 40, 'the notify calls were found (' + types.size + ')'); const missing = Array.from(types).filter((t) => !have.has(t)).sort(); assert.deepStrictEqual(missing, [], 'notification types without an Alerts describer (they would read "Event: ")'); }); test('each new describer renders its server payload as readable text in en and nb, with an icon and a jump target', () => { const icons = new Set(fs.readFileSync(path.join(ROOT, 'public', 'icons', 'sprite.svg'), 'utf8').match(/id="i-([a-z_-]+)"/g).map((s) => s.slice(6, -1))); for (const l of Object.keys(lang)) { const kinds = AK.make(ctxFor(l)); for (const [type, p] of Object.entries(SAMPLES)) { assert.ok(kinds[type], 'describer for ' + type); const d = kinds[type]({ id: 1, type, payload: p, createdAt: 0 }, p); const text = d.title + ' | ' + d.detail; assert.ok(d.title && d.title.length > 3, l + ' ' + type + ' has a title'); assert.ok(!/undefined|\[object|NaN|\{\w+\}|alerts\./.test(text), l + ' ' + type + ' looks raw: ' + text); assert.ok(icons.has(d.icon), type + ' icon ' + d.icon + ' exists in the sprite'); assert.ok(typeof d.go === 'string', type + ' jumps somewhere'); } } }); test('describers survive an empty payload (a newer server field never breaks the feed)', () => { const kinds = AK.make(ctxFor('en')); for (const type of Object.keys(SAMPLES)) assert.doesNotThrow(() => kinds[type]({ id: 1, type, payload: {} }, {}), type); }); test('diplomacy_changed has two payload shapes and both read well', () => { const k = AK.make(ctxFor('en')).diplomacy_changed; assert.match(k({}, { allianceId: 8, other: alliance, from: 'nap', to: 'neutral' }).title, /\[NSP\].*Neutral/); const d = k({}, { allianceId: 8, otherId: 7, otherTag: 'NSP', from: 'war', to: 'neutral', reason: 'disbanded' }); assert.match(d.title, /\[NSP\]/); assert.match(d.detail, /disbanded/); }); test('backbone alerts jump to the world screen, to the right site when the payload has one', () => { const k = AK.make(ctxFor('en')); assert.deepStrictEqual([k.backbone_taken({}, SAMPLES.backbone_taken).go, k.backbone_taken({}, SAMPLES.backbone_taken).params], ['world', { site: 2 }]); assert.strictEqual(k.backbone_won({}, SAMPLES.backbone_won).go, 'world'); assert.strictEqual(k.trade_accepted({}, SAMPLES.trade_accepted).go, 'trade'); assert.deepStrictEqual(k.trade_accepted({}, SAMPLES.trade_accepted).params, { tab: 'mine' }); });