'use strict'; /* * Versus in the controllers: RemoteController speaks the versus protocol (vs / priv messages, the create-join fields, no pause / speed) and * LocalController plays a whole match against NWBot in the browser, with the SAME vs / priv messages the server sends. controllers.js runs in a * vm context with a fake WebSocket and a manual clock, like test/controllers.test.js. */ const test = require('node:test'); const assert = require('node:assert/strict'); const fs = require('fs'); const path = require('path'); const vm = require('node:vm'); const WebSocket = require('ws'); const TD = require('../public/js/sim.js'); const V = require('../server/versus'); const { createServer } = require('../server'); const JS = path.join(__dirname, '..', 'public', 'js'); const SRC = fs.readFileSync(path.join(JS, 'controllers.js'), 'utf8'); const BOT = fs.readFileSync(path.join(JS, 'bot.js'), 'utf8'); const { VS } = TD; function harness({ bot = true } = {}) { const H = { now: 1000, timers: [], seq: 0, sockets: [] }; const timer = (fn, ms, repeat) => { const t = { id: ++H.seq, at: H.now + ms, fn, ms, repeat }; H.timers.push(t); return t.id; }; class FakeWS { constructor(url) { this.url = url; this.readyState = 0; this.sent = []; H.sockets.push(this); } send(s) { this.sent.push(JSON.parse(s)); } close() { this.readyState = 2; } open() { this.readyState = 1; if (this.onopen) this.onopen(); } recv(o) { if (this.onmessage) this.onmessage({ data: JSON.stringify(o) }); } } const sandbox = { TD, console, URL, WebSocket: FakeWS, document: { hidden: false, addEventListener() {}, removeEventListener() {} }, performance: { now: () => H.now }, location: { href: 'http://localhost/index.html' }, addEventListener() {}, removeEventListener() {}, setTimeout: (fn, ms) => timer(fn, ms, false), clearTimeout: (id) => { H.timers = H.timers.filter((t) => t.id !== id); }, setInterval: (fn, ms) => timer(fn, ms, true), clearInterval: (id) => { H.timers = H.timers.filter((t) => t.id !== id); }, }; sandbox.self = sandbox; vm.runInNewContext(SRC, sandbox, { filename: 'controllers.js' }); if (bot) vm.runInNewContext(BOT, sandbox, { filename: 'bot.js' }); H.NW = sandbox.NW; H.sandbox = sandbox; H.advance = (ms) => { // runs the timers that fall due, in order, moving the clock along const end = H.now + ms; for (;;) { const due = H.timers.filter((t) => t.at <= end).sort((a, b) => a.at - b.at || a.id - b.id)[0]; if (!due) break; H.now = Math.max(H.now, due.at); if (due.repeat) due.at += due.ms; else H.timers = H.timers.filter((t) => t !== due); due.fn(); } H.now = end; }; return H; } // Objects built inside the controllers' vm context have another Object.prototype: compare them as plain JSON (assert/strict is prototype-strict). const plain = (v) => JSON.parse(JSON.stringify(v)); // the messages a versus server would send for a sim function joinedVs(sim, vs, extra) { return Object.assign({ t: 'joined', room: 'ABCD', resumed: false, you: { pid: 'pidpidpid1', slot: 1, host: false }, players: [{ pid: 'pidpidpid1', slot: 1, name: 'Bob', host: false, online: true }, { pid: 'aliasaliasalias', slot: 0, name: 'Alice', host: true, online: true }], state: sim.fullState(), snap: sim.snapshot(), speed: 1, paused: false, vs, }, extra); } const lobbyVs = (extra) => Object.assign({ t: 'vs', phase: 'lobby', round: 1, rounds: 2, roles: { 0: 'attacker', 1: 'defender' }, random: false, bot: null, limit: 720, ready: { 0: false, 1: false }, frozen: false }, extra); let seq = 0; const snapMsg = (sim, tw) => { const m = Object.assign({ t: 'snap', seq: ++seq }, sim.snapshot(), { ev: [], cur: [], pa: 0, sp: 1 }); if (tw) m.tw = sim.towerList(); return m; }; // ================================================================== RemoteController test('remote versus: a creating join says so; a co-op or joining one does not', () => { let H = harness(); let rc = new H.NW.RemoteController({ pid: 'pidpidpid1', name: 'A', url: 'ws://x/ws', mode: 'versus', limit: 480, map: 'twin' }); rc.connect(); H.sockets[0].open(); assert.deepEqual(H.sockets[0].sent[0], { t: 'join', pid: 'pidpidpid1', name: 'A', map: 'twin', difficulty: 'normal', mode: 'versus', limit: 480 }); assert.equal(rc.mode, 'versus'); assert.equal(rc.view.mode, 'versus'); H = harness(); rc = new H.NW.RemoteController({ pid: 'pidpidpid1', name: 'A', url: 'ws://x/ws', map: 'twin' }); rc.connect(); H.sockets[0].open(); assert.deepEqual(H.sockets[0].sent[0], { t: 'join', pid: 'pidpidpid1', name: 'A', map: 'twin', difficulty: 'normal' }, 'co-op: exactly the old message'); assert.equal(rc.mode, 'coop'); H = harness(); rc = new H.NW.RemoteController({ pid: 'pidpidpid1', name: 'A', url: 'ws://x/ws', mode: 'versus', limit: 480, room: 'ABCD' }); rc.connect(); H.sockets[0].open(); assert.deepEqual(H.sockets[0].sent[0], { t: 'join', pid: 'pidpidpid1', name: 'A', room: 'ABCD' }, 'a joiner takes the room\'s settings'); }); test('remote versus: joined carries the match picture; vs and priv messages are stored and announced; role follows the seat', () => { const H = harness(); const sim = new TD.Sim({ map: 'open', mode: 'versus', limit: 480 }); const rc = new H.NW.RemoteController({ pid: 'pidpidpid1', name: 'Bob', room: 'ABCD', url: 'ws://x/ws' }); const log = []; rc.on('joined', () => log.push('joined')); rc.on('vs', (m) => log.push('vs:' + m.phase)); rc.on('priv', (m) => log.push('priv:' + m.role)); rc.connect(); const ws = H.sockets[0]; ws.open(); assert.equal(rc.role, null); ws.recv(joinedVs(sim, lobbyVs())); assert.deepEqual(log, ['joined', 'vs:lobby']); assert.equal(rc.mode, 'versus'); assert.equal(rc.limit, 480); assert.equal(rc.view.mode, 'versus'); assert.equal(rc.vs.phase, 'lobby'); assert.equal(rc.role, 'defender', 'seat 1 is the defender in this picture'); ws.recv(lobbyVs({ phase: 'countdown', countdown: 3, roles: { 0: 'defender', 1: 'attacker' } })); assert.equal(rc.role, 'attacker'); assert.equal(rc.vs.countdown, 3); ws.recv({ t: 'priv', role: 'attacker', credits: 150, income: 4, tech: {}, staged: [], cd: {}, active: { surge: 0, ddos: 0 } }); assert.equal(rc.priv.role, 'attacker'); assert.equal(rc.priv.credits, 150); assert.deepEqual(log, ['joined', 'vs:lobby', 'vs:countdown', 'priv:attacker']); }); test('remote versus: the shared round block and the EMP field reach the view', () => { const H = harness(); const sim = new TD.Sim({ map: 'open', mode: 'versus', limit: 480 }); sim.cmd({ c: 'place', type: 'filter', x: 6, y: 6, role: 'defender', by: 0 }); const rc = new H.NW.RemoteController({ pid: 'pidpidpid1', name: 'Bob', room: 'ABCD', url: 'ws://x/ws' }); rc.connect(); const ws = H.sockets[0]; ws.open(); ws.recv(joinedVs(sim, lobbyVs())); sim.cmd({ c: 'stage', kind: 'trojan', n: 2, role: 'attacker', by: 1 }); sim.cmd({ c: 'ability', id: 'emp', tid: sim.towers[0].id, role: 'attacker', by: 1 }); for (let i = 0; i < 30; i++) sim.tick(TD.STEP); ws.recv(snapMsg(sim)); H.advance(500); rc.update(H.now); assert.deepEqual(Object.keys(rc.view.vs).sort(), ['lim', 'st', 't']); assert.equal(rc.view.vs.lim, 480); assert.equal(rc.view.vs.st, 2); assert.ok(rc.view.vs.t > 0.4); assert.equal(rc.view.towers.length, 1); assert.ok(rc.view.towers[0].empT > 5 && rc.view.towers[0].empT <= 6, 'seconds of EMP left: ' + rc.view.towers[0].empT); // a co-op controller's towers and view carry none of it const H2 = harness(), s2 = new TD.Sim({ map: 'open' }); const rc2 = new H2.NW.RemoteController({ pid: 'pidpidpid1', name: 'Bob', room: 'ABCD', url: 'ws://x/ws' }); rc2.connect(); H2.sockets[0].open(); H2.sockets[0].recv({ t: 'joined', room: 'ABCD', resumed: false, you: { pid: 'pidpidpid1', slot: 1, host: false }, players: [], state: s2.fullState(), snap: s2.snapshot(), speed: 1, paused: false }); H2.advance(500); rc2.update(H2.now); assert.equal(rc2.mode, 'coop'); assert.equal(rc2.view.vs, null); assert.equal(rc2.vs, null); }); test('remote versus: commands carry no role, the lobby has methods, pause / speed are not even sent', () => { const H = harness(); const sim = new TD.Sim({ map: 'open', mode: 'versus', limit: 480 }); const rc = new H.NW.RemoteController({ pid: 'pidpidpid1', name: 'Bob', room: 'ABCD', url: 'ws://x/ws' }); rc.connect(); const ws = H.sockets[0]; ws.open(); ws.recv(joinedVs(sim, lobbyVs())); const sent = () => ws.sent.slice(1); rc.send({ c: 'spawn', kind: 'ping', n: 3, role: 'attacker', by: 0 }); rc.send({ c: 'tool', id: 'scan', x: 5, y: 6 }); assert.deepEqual(sent(), [{ t: 'cmd', c: 'spawn', kind: 'ping', n: 3 }, { t: 'cmd', c: 'tool', id: 'scan', x: 5, y: 6 }]); rc.ctl('pause', true); rc.ctl('speed', 2); assert.equal(sent().length, 2, 'versus: no pause / speed messages'); rc.vsRole('random'); rc.vsReady(true); rc.vsBot('attacker', 0.5); rc.vsBot(null); rc.vsStart(); rc.vsNext(); rc.vsRematch(); assert.deepEqual(sent().slice(2), [ { t: 'vs', c: 'role', v: 'random' }, { t: 'vs', c: 'ready', v: true }, { t: 'vs', c: 'bot', v: 'attacker', skill: 0.5 }, { t: 'vs', c: 'bot', v: null }, { t: 'vs', c: 'start' }, { t: 'vs', c: 'next' }, { t: 'vs', c: 'rematch' }, ]); // co-op still sends them const H2 = harness(), s2 = new TD.Sim({ map: 'open' }); const rc2 = new H2.NW.RemoteController({ pid: 'pidpidpid1', name: 'Bob', room: 'ABCD', url: 'ws://x/ws' }); rc2.connect(); H2.sockets[0].open(); H2.sockets[0].recv({ t: 'joined', room: 'ABCD', resumed: false, you: { pid: 'pidpidpid1', slot: 1, host: false }, players: [], state: s2.fullState(), snap: s2.snapshot(), speed: 1, paused: false }); rc2.ctl('pause', true); assert.deepEqual(H2.sockets[0].sent.slice(1), [{ t: 'ctl', c: 'pause', v: true }]); }); test('remote versus: the new join errors have readable messages', () => { const H = harness(); const rc = new H.NW.RemoteController({ pid: 'pidpidpid1', name: 'A', url: 'ws://x/ws', mode: 'versus' }); const fatal = []; rc.on('fatal', (f) => fatal.push(f)); rc.connect(); H.sockets[0].open(); H.sockets[0].recv({ t: 'error', code: 'bad_limit', msg: 'x' }); assert.equal(fatal[0].code, 'bad_limit'); assert.match(fatal[0].msg, /round length/); }); // ================================================================== LocalController // runs the controller's clock `seconds` forward in 50 ms frames function run(H, ctrl, seconds, until) { for (let i = 0; i < Math.round(seconds * 20); i++) { H.now += 50; ctrl.update(H.now); if (until && until()) return true; } return !until; } const localOpts = (o) => Object.assign({ map: 'open', mode: 'versus', role: 'attacker', countdownMs: 1000, seed: 7 }, o); test('local versus: needs the computer player script, with a clear message', () => { const H = harness({ bot: false }); assert.throws(() => new H.NW.LocalController(localOpts()), /bot\.js/); // a normal solo game never needs it assert.doesNotThrow(() => new H.NW.LocalController({ map: 'open' })); assert.equal(new H.NW.LocalController({ map: 'open' }).mode, 'solo'); }); test('local versus: starts in a countdown, nothing is accepted until the round starts, then the human plays its role against the computer', () => { const H = harness(); const c = new H.NW.LocalController(localOpts({ role: 'attacker' })); const log = []; c.on('vs', (m) => log.push(m.phase + (m.countdown !== undefined ? ':' + m.countdown : ''))); c.on('reject', (r) => log.push('reject:' + r.reason)); assert.equal(c.kind, 'local'); assert.equal(c.mode, 'versus'); assert.equal(c.vs.phase, 'countdown'); assert.equal(c.vs.round, 1); assert.equal(c.role, 'attacker'); assert.deepEqual(plain(c.vs.bot), { slot: 1, role: 'defender', skill: 1, name: 'Computer' }); assert.equal(c.priv.role, 'attacker'); assert.equal(c.priv.credits, VS.ATT_CREDITS); assert.equal(c.view.mode, 'versus'); assert.deepEqual(Object.keys(c.view.vs).sort(), ['lim', 'st', 't']); // countdown: the sim does not move; commands are refused assert.equal(c.send({ c: 'spawn', kind: 'ping' }).reason, 'phase'); run(H, c, 0.5); assert.equal(c.sim.time, 0); assert.deepEqual(log.slice(0, 1), ['reject:phase']); run(H, c, 1); assert.equal(c.vs.phase, 'playing'); assert.ok(c.sim.time > 0); // the human attacks: spawn works, place is across the role line, pause / speed do nothing c.sim.vs.credits = 500; const r = c.send({ c: 'spawn', kind: 'ping', n: 2 }); assert.equal(r.ok, true); assert.equal(c.priv.credits, 480, 'the private data is refreshed at once'); assert.equal(c.send({ c: 'place', type: 'filter', x: 3, y: 3 }).reason, 'role'); assert.equal(c.send({ c: 'tool', id: 'scan', x: 3, y: 3 }).reason, 'role'); c.ctl('pause', true); c.ctl('speed', 3); assert.equal(c.paused, false); assert.equal(c.speed, 1); const t0 = c.sim.time; run(H, c, 2); assert.ok(c.sim.time > t0 + 1.5, 'not paused'); assert.ok(c.view.monsters.length >= 1); // the computer defender builds on its own run(H, c, 6); assert.ok(c.sim.towers.length >= 1, 'the computer defends: ' + c.sim.towers.length); assert.ok(c.sim.towers.every((t) => t.owner === 1), 'its towers are owned by seat 1'); // `by` is always the human's seat 0: a forged one changes nothing assert.equal(c.send({ c: 'launch', by: 5, role: 'defender' }).reason, 'empty'); assert.ok(c.priv.cd); // lobby messages mean nothing locally for (const m of ['role', 'ready', 'bot', 'start', 'next', 'rematch']) assert.equal(c.vsSend(m, 'x').ok, false, m); }); test('local versus: as the defender the human builds with the tools, and the computer attacks', () => { const H = harness(); const c = new H.NW.LocalController(localOpts({ role: 'defender' })); assert.equal(c.role, 'defender'); assert.equal(c.vs.bot.role, 'attacker'); assert.deepEqual(Object.keys(c.priv).sort(), ['cd', 'role', 't']); run(H, c, 1.2); assert.equal(c.vs.phase, 'playing'); const r = c.send({ c: 'place', type: 'filter', x: 10, y: 6 }); assert.equal(r.ok, true, JSON.stringify(r)); assert.equal(c.sim.towers[0].owner, 0); assert.equal(c.send({ c: 'spawn', kind: 'ping' }).reason, 'role'); assert.equal(c.send({ c: 'wave' }).reason, 'versus'); assert.equal(c.send({ c: 'tool', id: 'scan', x: 5, y: 5 }).ok, true); assert.equal(c.send({ c: 'tool', id: 'scan', x: 5, y: 5 }).reason, 'cooldown'); assert.ok(c.priv.cd.scan > 0); c.sim.vs.credits = 3000; // (a purse that makes the computer attacker act at once) run(H, c, 8); assert.ok(c.sim.vs.stats.unitsSpent > 0, 'the computer attacker buys units'); assert.ok(c.view.monsters.length > 0); }); test('local versus: a whole match: round over, next, swapped roles, match over with scores and a winner, rematch swaps again', () => { const H = harness(); const c = new H.NW.LocalController(localOpts({ role: 'attacker' })); const phases = [c.vs.phase + c.vs.round]; // (the constructor already announced the first countdown) c.on('vs', (m) => { if (phases[phases.length - 1] !== m.phase + m.round) phases.push(m.phase + m.round); }); run(H, c, 1.2); assert.equal(c.vs.phase, 'playing'); // the server falls (the human attacks): round over c.sim.lives = 0; run(H, c, 0.5); assert.equal(c.vs.phase, 'roundover'); assert.equal(c.vs.result.winner, 'attacker'); assert.equal(c.vs.result.attacker, 0); assert.equal(c.vs.result.defender, 1); assert.equal(c.vs.history.length, 1); assert.deepEqual(plain(c.vs.next), { 0: false, 1: true }); assert.equal(c.send({ c: 'spawn', kind: 'ping' }).reason, 'over'); const frozenAt = c.sim.time; run(H, c, 1); assert.equal(c.sim.time, frozenAt, 'a finished round does not run on'); // next: round 2, roles swapped, a fresh sim, a countdown const sim1 = c.sim; assert.equal(c.vsNext().ok, true); assert.equal(c.vs.phase, 'countdown'); assert.equal(c.vs.round, 2); assert.equal(c.role, 'defender'); assert.equal(c.vs.bot.role, 'attacker'); assert.notEqual(c.sim, sim1); assert.equal(c.priv.role, 'defender'); run(H, c, 1.2); assert.equal(c.vs.phase, 'playing'); assert.equal(c.vs.history.length, 1, 'round 1 stays on the picture while round 2 is played'); // the clock runs out: the defender (the human) wins round 2; the match is over, the computer's attack felled the server first c.sim.time = c.sim.vs.limit - 0.05; run(H, c, 0.5); assert.equal(c.vs.phase, 'matchover'); assert.equal(c.vs.result.winner, 'defender'); assert.equal(c.vs.history.length, 2); assert.equal(c.vs.scores.length, 2); assert.equal(c.vs.scores[0].name, 'You'); assert.equal(c.vs.scores[1].name, 'Computer'); assert.equal(typeof c.vs.scores[0].fellAt, 'number'); assert.equal(c.vs.scores[1].fellAt, null); assert.equal(c.vs.winner, 0); assert.equal(c.vs.forfeit, null); assert.deepEqual(phases, ['countdown1', 'playing1', 'roundover1', 'countdown2', 'playing2', 'matchover2']); // rematch: a new match, the first round's roles swapped (I attacked first, so now I defend first) assert.equal(c.vsRematch().ok, true); assert.equal(c.vs.phase, 'countdown'); assert.equal(c.vs.round, 1); assert.equal(c.role, 'defender'); assert.equal(c.vs.history, undefined, 'a new match has no finished rounds'); // newGame restarts a match at once, in any phase run(H, c, 1.2); c.newGame(); assert.equal(c.vs.phase, 'countdown'); assert.equal(c.role, 'attacker', 'swapped again'); assert.equal(c.vs.round, 1); }); test('local versus: a random role is one of the two; a bot that throws is dropped and the match goes on', () => { const H = harness(); const roles = new Set(); for (let i = 0; i < 40; i++) roles.add(new H.NW.LocalController(localOpts({ role: 'random' })).role); assert.deepEqual([...roles].sort(), ['attacker', 'defender'], 'both come up'); const c = new H.NW.LocalController(localOpts()); run(H, c, 1.2); c.bot = { think() { throw new Error('boom'); } }; const err = console.error; console.error = () => {}; try { run(H, c, 1); } finally { console.error = err; } assert.equal(c.bot, null); assert.equal(c.vs.phase, 'playing'); }); // ================================================================== the same language as the server test('the match rules in controllers.js are the server\'s rules: the same winner and scores for 400 random match histories', () => { const H = harness(); let a = 12345; const rnd = () => { a = (a + 0x6D2B79F5) >>> 0; let t = Math.imul(a ^ (a >>> 15), a | 1); t ^= t + Math.imul(t ^ (t >>> 7), t | 61); return ((t ^ (t >>> 14)) >>> 0) / 4294967296; }; const names = ['A', 'B']; for (let i = 0; i < 400; i++) { const first = rnd() < 0.5 ? { 0: 'attacker', 1: 'defender' } : { 0: 'defender', 1: 'attacker' }; const rounds = 1 + Math.floor(rnd() * 3); const history = []; let roles = first; for (let r = 1; r <= rounds; r++) { const fell = rnd() < 0.55; const res = { winner: fell ? 'attacker' : 'defender', t: fell ? Math.round(rnd() * 5) * 60 : 720, integrityLost: Math.floor(rnd() * 4) * 5, forfeit: rnd() < 0.1 ? true : undefined }; if (res.forfeit === undefined) delete res.forfeit; history.push(V.historyEntry(r, roles, res)); roles = V.swapRoles(roles); } const server = V.matchScores(history, names); const client = H.NW.matchScores(history, names); assert.deepEqual(JSON.parse(JSON.stringify(client)), server); assert.equal(H.NW.matchWinner(client), V.matchWinner(server)); } }); test('the local match speaks the same vs messages as the server: the same fields in every phase', async () => { // what the real server sends for a human against a computer, phase by phase const srv = await createServer({ port: 0, host: '127.0.0.1', log: () => {}, vsCountdownMs: 100, pingMs: 0 }); const msgs = []; const privs = []; const ws = new WebSocket('ws://127.0.0.1:' + srv.port + '/ws'); try { await new Promise((res, rej) => { ws.on('open', res); ws.on('error', rej); }); ws.on('message', (d) => { const m = JSON.parse(d.toString()); if (m.t === 'vs') msgs.push(m); else if (m.t === 'joined') msgs.push(m.vs); else if (m.t === 'priv') privs.push(m); }); // (the joiner's first picture is `joined.vs`) const wait = async (fn, what) => { const end = Date.now() + 5000; while (!fn()) { if (Date.now() > end) throw new Error('timed out: ' + what); await new Promise((r) => setTimeout(r, 10)); } }; ws.send(JSON.stringify({ t: 'join', pid: 'localparity01', name: 'Me', mode: 'versus' })); await wait(() => msgs.length, 'joined'); ws.send(JSON.stringify({ t: 'vs', c: 'role', v: 'attacker' })); ws.send(JSON.stringify({ t: 'vs', c: 'bot', v: 'defender' })); await wait(() => msgs.some((m) => m.bot), 'bot'); ws.send(JSON.stringify({ t: 'vs', c: 'start' })); await wait(() => msgs.some((m) => m.phase === 'playing'), 'playing'); const room = [...srv.rooms.values()][0]; room.sim.lives = 0; await wait(() => msgs.some((m) => m.phase === 'roundover'), 'roundover'); ws.send(JSON.stringify({ t: 'vs', c: 'next' })); await wait(() => msgs.some((m) => m.phase === 'playing' && m.round === 2), 'round 2'); room.sim.time = room.sim.vs.limit - 0.05; await wait(() => msgs.some((m) => m.phase === 'matchover'), 'matchover'); const byPhase = (arr, ph) => arr.find((m) => m.phase === ph); const keys = (m) => Object.keys(m).filter((k) => k !== 'nextIn').sort(); // `nextIn` (the server's auto-advance timer) has no local counterpart const H = harness(); const c = new H.NW.LocalController(localOpts({ role: 'attacker', countdownMs: 1000 })); const local = []; c.on('vs', (m) => local.push(m)); local.push(c.vs); run(H, c, 1.2); c.sim.lives = 0; run(H, c, 0.3); c.vsNext(); run(H, c, 1.2); c.sim.time = c.sim.vs.limit - 0.05; run(H, c, 0.3); for (const ph of ['countdown', 'playing', 'roundover', 'matchover']) { assert.deepEqual(keys(byPhase(local, ph)), keys(byPhase(msgs, ph)), 'the vs message of phase ' + ph); assert.deepEqual(Object.keys(byPhase(local, ph).bot).sort(), Object.keys(byPhase(msgs, ph).bot).sort(), 'bot of ' + ph); assert.deepEqual(Object.keys(byPhase(local, ph).roles), Object.keys(byPhase(msgs, ph).roles)); } const lm = byPhase(local, 'matchover'), sm = byPhase(msgs, 'matchover'); assert.deepEqual(Object.keys(lm.result).sort(), Object.keys(sm.result).sort()); assert.deepEqual(Object.keys(lm.scores[0]).sort(), Object.keys(sm.scores[0]).sort()); assert.deepEqual(Object.keys(lm.history[0]).sort(), Object.keys(sm.history[0]).sort()); // and the private data const pa = privs.find((p) => p.role === 'defender'); assert.ok(pa, 'the server sent the human defender its private data'); const ld = new H.NW.LocalController(localOpts({ role: 'defender' })); assert.deepEqual(Object.keys(ld.priv).sort(), Object.keys(pa).sort()); const la = new H.NW.LocalController(localOpts({ role: 'attacker' })); assert.deepEqual(Object.keys(la.priv).sort(), Object.keys(privs.find((p) => p.role === 'attacker')).sort()); } finally { try { ws.terminate(); } catch (e) { /* gone */ } await srv.close(); } });