// T4 client shell (#67, TESTING 10.3): join -> canvas -> hello/roster for the GM and a player, no // data before join, one plain refusal for a wrong and an unknown key, and a revoked session // returning to sign-in. The server, the nginx vhost and the client are the real ones. import test from 'node:test' import assert from 'node:assert/strict' import { sleep } from '../../tools/lib.mjs' import { openAs } from '../../tools/withserver.mjs' import { launchBrowser, makePlayer, newPage, signIn, waitForText, waitForVisible, withStack, } from './stack.mjs' const KEY_REFUSED = 'Key not recognised.' async function canvasAndRoster(page, expectedNames) { await waitForVisible(page, '#stage canvas') await waitForVisible(page, '#hud') const roster = await waitForText(page, '#roster', new RegExp(expectedNames[0])) for (const name of expectedNames) assert.match(roster, new RegExp(name)) return roster } test('no session: only the sign-in page, and a wrong and an unknown key look identical', { timeout: 120000 }, async () => { await withStack(async ({ origin }) => { const browser = await launchBrowser() try { const { page } = await newPage(browser) const joins = [] page.on('response', response => { if (new URL(response.url()).pathname === '/join') joins.push(response.status()) }) await page.goto(`${origin}/`) await waitForVisible(page, '#signin') // Story 1: a stranger gets the door and no game data. assert.equal(await page.locator('#stage').isVisible(), false) assert.equal(await page.locator('#hud').isVisible(), false) assert.equal(await page.locator('#roster li').count(), 0) // Story 3: the same answer for a well-formed but wrong key and for an unknown one. await signIn(page, 'ABCD-EFGH-JKMN-PQRS') await waitForVisible(page, '#signin-error') const wrong = await page.textContent('#signin-error') await signIn(page, 'not-a-key') await waitForVisible(page, '#signin-error') const unknown = await page.textContent('#signin-error') assert.equal(wrong, KEY_REFUSED) assert.equal(unknown, KEY_REFUSED) assert.deepEqual(joins, [401, 401]) // No map request was made before a session existed. assert.equal(await page.locator('#stage canvas').count(), 0) } finally { await browser.close() } }) }) test('GM and player: join -> canvas -> hello and roster; revoked returns to sign-in', { timeout: 120000 }, async () => { await withStack(async ({ origin, gmKey }) => { const player = await makePlayer(origin, gmKey, 'Alice') const browser = await launchBrowser() try { const gmView = await newPage(browser) await gmView.page.goto(`${origin}/`) await waitForVisible(gmView.page, '#signin') await signIn(gmView.page, gmKey) // The HUD names this connection separately and lists the other players (no self row). const gmRoster = await canvasAndRoster(gmView.page, ['Alice']) assert.equal(await gmView.page.textContent('#role'), 'GM') // Alice has a key but no live connection yet: the other-player row says offline. assert.match(gmRoster, /offline/) const playerView = await newPage(browser) await playerView.page.goto(`${origin}/`) await waitForVisible(playerView.page, '#signin') await signIn(playerView.page, player.key) const playerRoster = await canvasAndRoster(playerView.page, ['GM']) assert.equal(await playerView.page.textContent('#role'), 'Player') assert.equal(await playerView.page.textContent('#me'), 'Alice') assert.match(playerRoster, /connected/) // Story 10: the GM replaces the key, the server revokes the session, and the browser stops // at the sign-in door instead of hammering a dead socket. const gm = await openAs(origin, gmKey) await gm.ws.waitText('hello') const ack = await gm.ws.request({ t: 'key.replace', id: player.id }) assert.equal(ack.ok, true) gm.ws.close() await waitForVisible(playerView.page, '#signin') await waitForText(playerView.page, '#signin-notice', /session has ended/i) assert.equal(await playerView.page.locator('#stage canvas').count(), 0) assert.equal(await playerView.page.locator('#roster li').count(), 0) // No automatic reconnection: still on the sign-in page a moment later. await sleep(300) assert.equal(await playerView.page.locator('#signin').isVisible(), true) } finally { await browser.close() } }) })