'use strict'; 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'); const D = require('../public/js/data.js'); const Soc = require('../public/js/social.js'); for (const f of fs.readdirSync(path.join(__dirname, '..', 'public', 'js', 'strings')).sort()) if (f.endsWith('.js')) require('../public/js/strings/' + f); const S = D.SOCIAL; test('selfActions follows the permission table and the leader-must-transfer rule', () => { const leader = Soc.selfActions('leader', 4), officer = Soc.selfActions('officer', 4), diplomat = Soc.selfActions('diplomat', 4), member = Soc.selfActions('member', 4); assert.ok(leader.invite && leader.disband && leader.diplomacy && leader.editNotes && leader.transfer && leader.setRole); assert.ok(leader.leaveNeedsTransfer); assert.ok(!Soc.selfActions('leader', 1).leaveNeedsTransfer, 'alone: leaving disbands, no transfer needed'); assert.ok(officer.invite && officer.viewInvites && officer.editNotes && !officer.diplomacy && !officer.disband && !officer.setRole); assert.ok(diplomat.diplomacy && diplomat.editNotes && !diplomat.invite); assert.ok(member.broadcast && !member.invite && !member.editNotes && !member.diplomacy && member.leave && !member.leaveNeedsTransfer); const none = Soc.selfActions(null, 0); assert.ok(!none.leave && !none.invite && !none.broadcast); // every key of SOCIAL.permissions the screens use is derived, not copied for (const [k, roles] of Object.entries(S.permissions)) { for (const r of S.roles) { const a = Soc.selfActions(r, 3); if (a[k] !== undefined) assert.strictEqual(a[k], roles.includes(r), k + ' for ' + r); } } }); test('memberActions: kick needs permission and strictly higher rank, roles and transfer are leader-only, never on yourself', () => { const m = (role, id) => ({ operatorId: id || 7, role }); const lead = Soc.memberActions('leader', m('officer'), 1); assert.ok(lead.kick && lead.transfer && lead.message); assert.deepStrictEqual(lead.roles.sort(), ['diplomat', 'member']); assert.deepStrictEqual(Soc.memberActions('leader', m('member'), 1).roles.sort(), ['diplomat', 'officer']); const off = Soc.memberActions('officer', m('member'), 1); assert.ok(off.kick && !off.transfer && off.roles.length === 0); assert.ok(!Soc.memberActions('officer', m('officer'), 1).kick, 'equal rank'); assert.ok(!Soc.memberActions('officer', m('leader'), 1).kick, 'higher rank'); assert.ok(!Soc.memberActions('diplomat', m('member'), 1).kick, 'no kick permission'); assert.ok(!Soc.memberActions('member', m('member'), 1).kick); const self = Soc.memberActions('leader', m('leader', 1), 1); assert.ok(self.self && !self.kick && !self.transfer && !self.message && self.roles.length === 0); assert.strictEqual(Soc.memberActions('leader', m('leader', 9), 1).roles.length, 0, 'the leader role is only moved by transfer'); }); test('diplomacyOptions mirrors SOCIAL.diplomacyMoves and locks pending pairs', () => { const kinds = (from) => Object.fromEntries(Soc.diplomacyOptions(from, null).map((o) => [o.state, o.kind])); assert.deepStrictEqual(kinds('neutral'), { war: 'immediate', nap: 'proposal', confed: 'proposal' }); assert.deepStrictEqual(kinds('nap'), { war: 'delayed', neutral: 'delayed', confed: 'proposal' }); assert.deepStrictEqual(kinds('confed'), { war: 'delayed', neutral: 'delayed', nap: 'delayed' }); assert.deepStrictEqual(kinds('war'), { neutral: 'proposal', nap: 'proposal' }, 'war -> confed is not allowed'); assert.deepStrictEqual(Soc.diplomacyOptions('nap', { to: 'neutral' }), []); }); test('relationTo defaults to neutral and exposes pending changes', () => { const rels = [{ other: { id: 5 }, state: 'nap', since: 10, pending: { to: 'neutral', effectiveAt: 99, byUs: true } }]; assert.deepStrictEqual(Soc.relationTo(rels, 6), { state: 'neutral', since: null, pending: null }); assert.strictEqual(Soc.relationTo(rels, 5).state, 'nap'); assert.strictEqual(Soc.relationTo(rels, 5).pending.byUs, true); assert.strictEqual(Soc.pendingRemaining({ effectiveAt: 1000 }, 400), 600); assert.strictEqual(Soc.pendingRemaining({ effectiveAt: 1000 }, 5000), 0); assert.strictEqual(Soc.pendingRemaining(null, 5), 0); }); test('character counting matches the server: code points, CRLF as one', () => { assert.strictEqual(Soc.charCount('abc'), 3); assert.strictEqual(Soc.charCount('a\r\nb'), 3); assert.strictEqual(Soc.charCount('\u{1F680}\u{1F680}'), 2, 'astral characters count once'); assert.strictEqual(Soc.charCount(null), 0); const ok = Soc.composeState('x'.repeat(S.messageMax)); assert.ok(!ok.over && ok.left === 0 && !ok.empty); assert.ok(Soc.composeState('x'.repeat(S.messageMax + 1)).over); assert.ok(Soc.composeState(' \n ').empty); assert.ok(Soc.composeState('').empty); assert.strictEqual(Soc.composeState('abc', 10).left, 7); }); test('retrySeconds is at least 1 and rounds up', () => { assert.strictEqual(Soc.retrySeconds({ retryAfter: 12.2 }), 13); assert.strictEqual(Soc.retrySeconds({ retryAfter: 0 }), 1); assert.strictEqual(Soc.retrySeconds(null), 1); }); test('message lists merge without duplicates, newest first; conversations read oldest first', () => { const a = [{ id: 9 }, { id: 8 }], b = [{ id: 8 }, { id: 7 }, { id: 6 }]; assert.deepStrictEqual(Soc.mergeMessages(a, b).map((m) => m.id), [9, 8, 7, 6]); assert.deepStrictEqual(Soc.chronological([{ id: 3 }, { id: 1 }, { id: 2 }]).map((m) => m.id), [1, 2, 3]); assert.deepStrictEqual(a.map((m) => m.id), [9, 8], 'inputs are not mutated'); }); test('peerOf and unreadIds', () => { const inMsg = { id: 1, direction: 'in', from: { operatorId: 2, name: 'b' }, to: { operatorId: 1, name: 'a' }, readAt: null }; const outMsg = { id: 2, direction: 'out', from: { operatorId: 1, name: 'a' }, to: { operatorId: 2, name: 'b' }, readAt: null }; const read = { id: 3, direction: 'in', readAt: 5 }; assert.strictEqual(Soc.peerOf(inMsg).operatorId, 2); assert.strictEqual(Soc.peerOf(outMsg).operatorId, 2); assert.deepStrictEqual(Soc.unreadIds([inMsg, outMsg, read]), [1], 'own messages and read ones are not unread'); }); test('ranking pages merge by rank and your row is recognised per kind', () => { const p1 = [{ rank: 1 }, { rank: 2 }], p2 = [{ rank: 2 }, { rank: 3 }]; assert.deepStrictEqual(Soc.mergeRanks(p1, p2).map((r) => r.rank), [1, 2, 3]); assert.ok(Soc.isYou('operators', { operatorId: 4 }, { operatorId: 4, rank: 9 })); assert.ok(!Soc.isYou('operators', { operatorId: 5 }, { operatorId: 4 })); assert.ok(Soc.isYou('alliances', { allianceId: 2 }, { allianceId: 2 })); assert.ok(Soc.isYou('battles', { operatorId: 4 }, { operatorId: 4 })); assert.ok(!Soc.isYou('operators', { operatorId: 4 }, null)); const r = { attackPoints: 30, defensePoints: 12 }; assert.deepStrictEqual(['attack', 'defense', 'total'].map((by) => Soc.battlePoints(r, by)), [30, 12, 42]); }); test('cleanName collapses whitespace; sinceMs handles never-online', () => { assert.strictEqual(Soc.cleanName(' Bob the Builder '), 'Bob the Builder'); assert.strictEqual(Soc.cleanName(null), ''); assert.strictEqual(Soc.sinceMs(null, 100), null); assert.strictEqual(Soc.sinceMs(40, 100), 60); assert.strictEqual(Soc.sinceMs(400, 100), 0); }); // ---- strings: every key the three screens use exists in both languages, every role/state/kind has a name const dir = path.join(__dirname, '..', 'public', 'js', 'screens'); const SRC = ['alliance.js', 'messages.js', 'rankings.js', 'social-ui.js'].map((f) => fs.readFileSync(path.join(dir, f), 'utf8')).join('\n'); test('every static i18n key used by the social screens exists in en and nb', () => { const used = new Set(); for (const m of SRC.matchAll(/\bApp\.t\('([a-zA-Z0-9_.]+)'/g)) if (!m[1].endsWith('.')) used.add(m[1]); for (const m of SRC.matchAll(/\bt: '([a-zA-Z0-9_.]+)'/g)) used.add(m[1]); assert.ok(used.size > 100, 'found the keys (' + used.size + ')'); for (const l of I.LANGS) for (const k of used) assert.ok(I.strings[l][k], l + ' is missing ' + k); }); test('dynamic i18n key families are complete', () => { const need = []; for (const r of S.roles) need.push('soc.role.' + r); for (const s of S.diplomacyStates) need.push('soc.state.' + s); for (const k of ['immediate', 'proposal', 'delayed']) need.push('soc.kind.' + k, 'soc.kind.' + k + '.hint'); for (const k of ['applied', 'proposed', 'scheduled']) need.push('soc.dip.result.' + k); for (const k of ['inbox', 'sent', 'alliance']) need.push('soc.msg.empty.' + k); for (const k of ['attack', 'defense', 'total']) need.push('soc.rank.by.' + k); for (const k of ['inbox', 'sent', 'alliance', 'blocked']) need.push('soc.msg.tab.' + k); for (const k of ['operators', 'alliances', 'battles']) need.push('soc.rank.tab.' + k); for (const k of ['overview', 'members', 'diplomacy']) need.push('soc.tab.' + k); for (const l of I.LANGS) for (const k of need) assert.ok(I.strings[l][k], l + ' is missing ' + k); }); test('every error code the social routes can return is translated', () => { const errors = require('../server/errors'); const social = ['peering_office_required', 'already_in_alliance', 'not_in_alliance', 'alliance_name_taken', 'alliance_tag_taken', 'alliance_full', 'invite_expired', 'leader_must_transfer', 'notes_full', 'diplomacy_invalid', 'diplomacy_pending', 'proposal_exists', 'proposal_expired', 'forbidden', 'not_found', 'conflict', 'rate_limited', 'validation_failed']; for (const l of I.LANGS) for (const c of social) assert.ok(I.strings[l]['err.' + c], l + ' err.' + c); const defined = Object.keys(errors.CODES || errors.codes || {}); if (defined.length) for (const c of defined.filter((x) => social.includes(x))) assert.ok(I.strings.en['err.' + c]); }); test('screens never put player-controlled text through innerHTML', () => { const code = SRC.replace(/\/\*[\s\S]*?\*\//g, '').replace(/^\s*\/\/.*$/gm, ''); assert.ok(!/innerHTML|insertAdjacentHTML|outerHTML|document\.write/.test(code), 'no HTML injection sinks in the social screens'); });