// T5 (#68, TESTING 10.3): the name prompt, the connection badge, reconnect + full resync and two // devices on one player key. Everything runs against the real server through the TLS/HTTP2 test // nginx; the browser's own WebSocket is wrapped only to *record* frames (the app is untouched), so // the resync assertions read the real wire. import test from 'node:test' import assert from 'node:assert/strict' import { createPlayer, namePlayer } from '../../tools/lib.mjs' import { openAs, startServer, stopServer } from '../../tools/withserver.mjs' import { launchBrowser, newPage, signIn, waitForHidden, waitForText, waitForVisible, withStack, } from './stack.mjs' /** Record every WS frame/close on `window.__wsLog` without changing what the app sees. */ async function recordSockets(page) { await page.addInitScript(() => { window.__wsLog = { sockets: 0, frames: [], closes: [] } const Native = window.WebSocket class RecordingSocket extends Native { constructor(url, protocols) { super(url, protocols) window.__wsLog.sockets += 1 this.addEventListener('message', event => { try { window.__wsLog.frames.push(JSON.parse(event.data)) } catch { // a non-JSON frame is not state; the client ignores it too } }) this.addEventListener('close', event => { window.__wsLog.closes.push({ code: event.code, reason: event.reason }) }) } } window.WebSocket = RecordingSocket }) } const frames = page => page.evaluate(() => window.__wsLog.frames) const frameCount = async page => (await frames(page)).length async function waitForFrame(page, predicate, timeout = 20000) { await page.waitForFunction(predicate, null, { timeout }) } async function waitForBadge(page, state, timeout = 30000) { await page.locator(`#badge[data-state="${state}"]`).waitFor({ state: 'visible', timeout }) } async function gmWorld(origin, gmKey) { const gm = await openAs(origin, gmKey) const hello = await gm.ws.waitText('hello') const walls = await gm.ws.waitText('walls') return { ...gm, hello, scene: walls.scene } } test('first sign-in asks once for a name and shows a taken name', { timeout: 120000 }, async () => { await withStack(async ({ origin, gmKey }) => { const gm = await openAs(origin, gmKey) await gm.ws.waitText('hello') const alice = await createPlayer(gm.ws) await namePlayer(origin, alice.key, 'Alice') const unnamed = await createPlayer(gm.ws) gm.ws.close() const browser = await launchBrowser() try { const { page } = await newPage(browser) await page.goto(`${origin}/`) await waitForVisible(page, '#signin') await signIn(page, unnamed.key) // Stories 5: the prompt, and no game data (canvas/HUD) before the name is accepted. await waitForVisible(page, '#name') assert.equal(await page.locator('#stage canvas').count(), 0) assert.equal(await page.locator('#hud').isVisible(), false) assert.equal(await page.locator('#name-error').isVisible(), false) // Story 6: a taken name is refused with a visible reason, and the prompt stays. await page.fill('#name-input', 'alice') await page.click('#name-submit') await waitForText(page, '#name-error', /taken/i) assert.equal(await page.locator('#name').isVisible(), true) // A free name lands on the map; `hello` (name set) + the resync rebuild the view. await page.fill('#name-input', 'Bob') await page.click('#name-submit') await waitForVisible(page, '#hud') await waitForVisible(page, '#stage canvas') await waitForText(page, '#me', /^Bob$/) // The HUD roster lists the other players; this connection's own name is #me. await waitForText(page, '#roster', /GM/) await waitForBadge(page, 'connected') await waitForHidden(page, '#name') // Asked once: a reload with the same session comes straight back to the table. await page.reload() await waitForVisible(page, '#hud') await waitForText(page, '#me', /^Bob$/) await waitForHidden(page, '#name') await waitForBadge(page, 'connected') } finally { await browser.close() } }) }) test('a killed server under a live player: badge reconnecting, then a full resync', { timeout: 180000 }, async () => { await withStack(async ({ origin, gmKey, server }) => { const gm = await gmWorld(origin, gmKey) const player = await createPlayer(gm.ws) await namePlayer(origin, player.key, 'Ann') // The view the resync must rebuild: a controlled token (vision + explored), another token in // sight (encountered + token) and a player-marked window (marker). const own = await gm.ws.request({ t: 'token.create', scene: gm.scene, kind: 'pc', pos: [400, 400], descriptive_name: 'Ann', actual_name: 'Ann', controller: player.id, }) assert.equal(own.ok, true) const npc = await gm.ws.request({ t: 'token.create', scene: gm.scene, kind: 'npc', pos: [460, 400], descriptive_name: 'Rat', actual_name: 'Rat', }) assert.equal(npc.ok, true) const windowAck = await gm.ws.request({ t: 'wall.add', scene: gm.scene, a: [430, 360], b: [430, 440], preset: 'window', }) assert.equal(windowAck.ok, true) gm.ws.close() const browser = await launchBrowser() let restarted = null try { const { page } = await newPage(browser) await recordSockets(page) await page.goto(`${origin}/`) await waitForVisible(page, '#signin') await signIn(page, player.key) await waitForVisible(page, '#hud') await waitForBadge(page, 'connected') // The first connect already carries the full view; wait for all of it before the drop. await waitForFrame(page, () => { const seen = new Set((window.__wsLog?.frames ?? []).map(frame => frame.t)) return ['hello', 'roster', 'encountered', 'vision', 'explored', 'marker', 'token'] .every(type => seen.has(type)) }) const before = await frameCount(page) await stopServer(server) // Story 7/8: the map stays up (stale) and the badge says reconnecting, with no reload. await waitForBadge(page, 'reconnecting') assert.equal(await page.locator('#stage canvas').count(), 1) assert.equal(await page.locator('#signin').isVisible(), false) // Restart the real server on the same port and state (sessions are durable, DESIGN 4.4). restarted = await startServer({ stateDir: server.stateDir, port: server.port, publicOrigin: server.publicOrigin, }) await waitForBadge(page, 'connected', 60000) // Story 11: the reconnect's `hello` is followed by the whole 6.3 resync, not a stale diff. await page.waitForFunction( beforeIndex => { const log = window.__wsLog.frames const helloAt = log.findIndex((frame, index) => index >= beforeIndex && frame.t === 'hello') if (helloAt < 0) return false const after = log.slice(helloAt) const seen = new Set(after.map(frame => frame.t)) return ['roster', 'encountered', 'vision', 'explored', 'marker', 'token'] .every(type => seen.has(type)) }, before, { timeout: 30000 }, ) const log = await frames(page) const helloAt = log.findLastIndex(frame => frame.t === 'hello') const after = log.slice(helloAt) const full = type => after.find(frame => frame.t === type) assert.equal(full('explored')?.full, true, 'explored comes back as a full snapshot') assert.equal(full('marker')?.full, true, 'markers come back as a full snapshot') assert.equal(full('encountered')?.full, true, 'the encountered list comes back full') assert.ok(after.some(frame => frame.t === 'token'), 'tokens come back') assert.ok(after.some(frame => frame.t === 'vision'), 'fog/vision comes back') assert.match(await page.textContent('#roster'), /GM/) assert.equal(await page.locator('#hud').isVisible(), true) } finally { if (restarted) await stopServer(restarted) await browser.close() } }) }) test('a dropped network is offline, and coming back reconnects to the same view', { timeout: 120000 }, async () => { await withStack(async ({ origin, gmKey }) => { const gm = await openAs(origin, gmKey) await gm.ws.waitText('hello') const player = await createPlayer(gm.ws) await namePlayer(origin, player.key, 'Cara') gm.ws.close() const browser = await launchBrowser() try { const { context, page } = await newPage(browser) await page.goto(`${origin}/`) await waitForVisible(page, '#signin') await signIn(page, player.key) await waitForVisible(page, '#hud') await waitForBadge(page, 'connected') await context.setOffline(true) // Story 7: no network at all is the `offline` badge, distinct from a socket retrying. await waitForBadge(page, 'offline', 15000) await context.setOffline(false) // The `online` event skips the rest of the backoff; the session is still valid. await waitForBadge(page, 'connected', 30000) await waitForText(page, '#me', /^Cara$/) assert.equal(await page.locator('#stage canvas').count(), 1) } finally { await browser.close() } }) }) test('two connections of one player are both live and stay in sync', { timeout: 120000 }, async () => { await withStack(async ({ origin, gmKey }) => { const gm = await gmWorld(origin, gmKey) const scene = gm.scene const player = await createPlayer(gm.ws) await namePlayer(origin, player.key, 'Dee') gm.ws.close() const browser = await launchBrowser() try { const first = await newPage(browser) const second = await newPage(browser) for (const view of [first, second]) { await recordSockets(view.page) await view.page.goto(`${origin}/`) await waitForVisible(view.page, '#signin') await signIn(view.page, player.key) await waitForVisible(view.page, '#hud') await waitForBadge(view.page, 'connected') } assert.equal(await first.page.textContent('#me'), 'Dee') assert.equal(await second.page.textContent('#me'), 'Dee') // A state change after both are connected reaches both connections: the GM places the // player's own token, and each device's `token` frame carries it with `mine:true`. const gm = await openAs(origin, gmKey) await gm.ws.waitText('hello') const created = await gm.ws.request({ t: 'token.create', scene, kind: 'pc', pos: [400, 400], descriptive_name: 'Dee', actual_name: 'Dee', controller: player.id, }) assert.equal(created.ok, true) for (const view of [first, second]) { await view.page.waitForFunction( id => (window.__wsLog?.frames ?? []).some(frame => frame.t === 'token' && frame.id === id && frame.mine === true), created.id, { timeout: 20000 }, ) } // A later roster change is broadcast to every connection of the named player too. const renamed = await gm.ws.request({ t: 'player.rename', id: player.id, name: 'Dee Two' }) assert.equal(renamed.ok, true) gm.ws.close() for (const view of [first, second]) { await waitForText(view.page, '#me', /Dee Two/) await waitForBadge(view.page, 'connected') } } finally { await browser.close() } }) })