feat(client): GM roster, player keys and token panel (#74)
This commit is contained in:
@@ -0,0 +1,275 @@
|
||||
// T11 (#74, TESTING 10.3): GM roster, player keys and the token panel against the real server
|
||||
// through the TLS/HTTP2 test nginx. The GM drives the panel; a player page records its own WS
|
||||
// frames so the test also proves that keys never reach player traffic and `gm_only` tokens never
|
||||
// reach the player until the GM allows them.
|
||||
|
||||
import test from 'node:test'
|
||||
import assert from 'node:assert/strict'
|
||||
import { createPlayer, join, namePlayer } from '../../tools/lib.mjs'
|
||||
import { openAs } from '../../tools/withserver.mjs'
|
||||
import {
|
||||
launchBrowser,
|
||||
newPage,
|
||||
signIn,
|
||||
waitForText,
|
||||
waitForVisible,
|
||||
withStack,
|
||||
} from './stack.mjs'
|
||||
|
||||
async function recordSockets(page) {
|
||||
await page.addInitScript(() => {
|
||||
window.__wsLog = { frames: [] }
|
||||
const Native = window.WebSocket
|
||||
class RecordingSocket extends Native {
|
||||
constructor(url, protocols) {
|
||||
super(url, protocols)
|
||||
this.addEventListener('message', event => {
|
||||
try {
|
||||
window.__wsLog.frames.push(JSON.parse(event.data))
|
||||
} catch {
|
||||
// non-JSON frames are not state
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
window.WebSocket = RecordingSocket
|
||||
})
|
||||
}
|
||||
|
||||
const frames = page => page.evaluate(() => window.__wsLog.frames ?? [])
|
||||
|
||||
async function lastTokenFrame(page, id) {
|
||||
return page.evaluate(
|
||||
tokenId => (window.__wsLog?.frames ?? []).filter(frame => frame.t === 'token' && frame.id === tokenId).at(-1),
|
||||
id,
|
||||
)
|
||||
}
|
||||
|
||||
async function keyFromStatus(page, pattern) {
|
||||
const text = await waitForText(page, '#gm-key-status', pattern)
|
||||
const match = /^(?:Key for player \d+|New key for player \d+): (.+)$/.exec(text.trim())
|
||||
assert.ok(match, `no key in status ${JSON.stringify(text)}`)
|
||||
return match[1]
|
||||
}
|
||||
|
||||
async function waitTokenFrame(page, id, predicate, timeout = 20000) {
|
||||
const deadline = Date.now() + timeout
|
||||
let last = null
|
||||
while (Date.now() < deadline) {
|
||||
last = await lastTokenFrame(page, id)
|
||||
if (last && predicate(last)) return last
|
||||
await new Promise(resolve => setTimeout(resolve, 50))
|
||||
}
|
||||
throw new Error(`token ${id} never matched; last was ${JSON.stringify(last)}`)
|
||||
}
|
||||
|
||||
async function waitForHiddenRow(page, tokenId) {
|
||||
await page.locator(`#gm-token-list li[data-token="${tokenId}"]`).waitFor({ state: 'detached', timeout: 20000 })
|
||||
}
|
||||
|
||||
test('GM roster, keys and token panel drive the real server', { timeout: 240000 }, async () => {
|
||||
await withStack(async ({ origin, gmKey, server }) => {
|
||||
const keys = []
|
||||
const browser = await launchBrowser()
|
||||
const raw = await openAs(origin, gmKey)
|
||||
const rawHello = await raw.ws.waitText('hello')
|
||||
const scene = rawHello.scene?.id ?? (await raw.ws.waitText('walls')).scene
|
||||
try {
|
||||
// ---- sign in as GM and see the roster with connected state ------------------------------
|
||||
const gmView = await newPage(browser)
|
||||
const gm = gmView.page
|
||||
await gm.goto(`${origin}/`)
|
||||
await waitForVisible(gm, '#signin')
|
||||
await signIn(gm, gmKey)
|
||||
await waitForVisible(gm, '#gm-panels')
|
||||
await waitForText(gm, '#gm-roster-list', /GM/)
|
||||
await waitForText(gm, '#gm-roster-list', /connected/)
|
||||
assert.equal(await gm.locator('#gm-panels').isVisible(), true)
|
||||
|
||||
// ---- key.create through the panel, key visible only in the GM DOM ----------------------
|
||||
await gm.click('#gm-key-create')
|
||||
const firstStatus = await waitForText(gm, '#gm-key-status', /Key for player \d+: /)
|
||||
const firstMatch = /^Key for player (\d+): (.+)$/.exec(firstStatus.trim())
|
||||
assert.ok(firstMatch, `unexpected key status ${firstStatus}`)
|
||||
const aliceId = Number(firstMatch[1])
|
||||
const aliceKey = firstMatch[2]
|
||||
keys.push(aliceKey)
|
||||
assert.equal(await gm.textContent(`#gm-key-${aliceId}`), aliceKey)
|
||||
|
||||
// A player created outside the panel (an unused key) can be shown by the panel's Show.
|
||||
const second = await createPlayer(raw.ws)
|
||||
keys.push(second.key)
|
||||
await waitForVisible(gm, `#gm-key-show-${second.id}`)
|
||||
await gm.click(`#gm-key-show-${second.id}`)
|
||||
await waitForText(gm, `#gm-key-${second.id}`, new RegExp(second.key.replace(/-/g, '\\-')))
|
||||
|
||||
// ---- key.replace through the panel ----------------------------------------------------
|
||||
await gm.click(`#gm-key-replace-${second.id}`)
|
||||
const replacement = await keyFromStatus(gm, /New key for player/)
|
||||
keys.push(replacement)
|
||||
assert.notEqual(replacement, second.key)
|
||||
assert.equal((await join(origin, second.key)).status, 401, 'the replaced key is dead')
|
||||
assert.equal((await join(origin, replacement)).status, 303, 'the new key works')
|
||||
|
||||
// ---- the cap, then remove frees the slot ----------------------------------------------
|
||||
// alice + second are 2 of 6; fill the table to the cap with real key.create acks.
|
||||
for (let i = 0; i < 4; i += 1) {
|
||||
const extra = await createPlayer(raw.ws)
|
||||
keys.push(extra.key)
|
||||
}
|
||||
await gm.click('#gm-key-create')
|
||||
await waitForText(gm, '#gm-key-status', /full/i)
|
||||
|
||||
await waitForVisible(gm, `#gm-player-remove-${second.id}`)
|
||||
await gm.click(`#gm-player-remove-${second.id}`)
|
||||
await waitForText(gm, '#gm-key-status', /removed/i)
|
||||
assert.equal(await gm.locator(`#gm-player-remove-${second.id}`).count(), 0)
|
||||
await gm.click('#gm-key-create')
|
||||
const freed = await keyFromStatus(gm, /Key for player \d+: /)
|
||||
keys.push(freed)
|
||||
|
||||
// ---- a named player page records its own wire for the token and key assertions ---------
|
||||
await namePlayer(origin, aliceKey, 'Alice')
|
||||
const playerView = await newPage(browser)
|
||||
const player = playerView.page
|
||||
await recordSockets(player)
|
||||
await player.goto(`${origin}/`)
|
||||
await waitForVisible(player, '#signin')
|
||||
await signIn(player, aliceKey)
|
||||
await waitForVisible(player, '#hud')
|
||||
await waitForText(player, '#me', /^Alice$/)
|
||||
await waitForText(gm, '#gm-roster-list', /Alice/)
|
||||
await waitForText(gm, '#gm-roster-list', /connected/)
|
||||
|
||||
// A scout gives the player a real line of sight, so a GM-controlled token can be shown and
|
||||
// hidden by `gm_only` (a token the player controls is always visible, by 3.1).
|
||||
const scout = await raw.ws.request({
|
||||
t: 'token.create',
|
||||
scene,
|
||||
kind: 'pc',
|
||||
pos: [400, 400],
|
||||
descriptive_name: 'Scout',
|
||||
actual_name: 'Scout',
|
||||
controller: aliceId,
|
||||
flags: { vision: true },
|
||||
})
|
||||
assert.equal(scout.ok, true)
|
||||
await waitTokenFrame(player, scout.id, frame => frame.mine === true)
|
||||
|
||||
// ---- token.create through the panel ---------------------------------------------------
|
||||
await gm.fill('#token-name', 'Rat')
|
||||
await gm.fill('#token-actual', 'Giant Rat')
|
||||
await gm.fill('#token-x', '460')
|
||||
await gm.fill('#token-y', '450')
|
||||
await gm.fill('#token-radius', '5')
|
||||
await gm.click('#token-create-submit')
|
||||
const tokenRow = gm.locator('#gm-token-list li', { hasText: 'Rat' }).first()
|
||||
await tokenRow.waitFor({ state: 'visible', timeout: 20000 })
|
||||
const tokenId = await tokenRow.getAttribute('data-token')
|
||||
assert.ok(tokenId, 'new token has no id in the panel')
|
||||
await waitTokenFrame(player, tokenId, frame => frame.mine === false)
|
||||
|
||||
// ---- controller: the player becomes the token's controller ----------------------------
|
||||
await gm.click(`#gm-token-select-${tokenId}`)
|
||||
await waitForVisible(gm, '#gm-token-editor')
|
||||
await gm.selectOption('#token-edit-controller', String(aliceId))
|
||||
await gm.click('#token-save')
|
||||
await waitTokenFrame(player, tokenId, frame => frame.mine === true)
|
||||
assert.equal((await lastTokenFrame(player, tokenId)).name, 'Rat')
|
||||
|
||||
// ---- edit: a rename reaches the player ------------------------------------------------
|
||||
await gm.fill('#token-edit-name', 'Big Rat')
|
||||
await gm.click('#token-save')
|
||||
await waitTokenFrame(player, tokenId, frame => frame.name === 'Big Rat')
|
||||
await waitForText(gm, '#gm-token-list', /Big Rat/)
|
||||
|
||||
// ---- gm_only hides the token from the player, clearing it reveals it again -------------
|
||||
// A controlled token is always visible (3.1), so hand it back to the GM while it is hidden.
|
||||
await gm.check('#token-edit-gm-only')
|
||||
await gm.click('#token-save')
|
||||
await waitForText(gm, '#gm-token-list', /gm_only/)
|
||||
await gm.selectOption('#token-edit-controller', '')
|
||||
await gm.click('#token-save')
|
||||
await waitForText(gm, '#gm-token-list', /controller GM/)
|
||||
await player.waitForFunction(
|
||||
tokenId => {
|
||||
const gone = (window.__wsLog?.frames ?? []).findLast(frame => frame.t === 'token_gone' && frame.id === tokenId)
|
||||
if (!gone) return false
|
||||
const last = (window.__wsLog?.frames ?? []).filter(frame => frame.t === 'token' && frame.id === tokenId).at(-1)
|
||||
return !last || (window.__wsLog.frames.indexOf(last) < window.__wsLog.frames.indexOf(gone))
|
||||
},
|
||||
tokenId,
|
||||
{ timeout: 20000 },
|
||||
)
|
||||
await gm.uncheck('#token-edit-gm-only')
|
||||
await gm.click('#token-save')
|
||||
await waitForText(gm, '#gm-token-list', /visible/)
|
||||
await player.waitForFunction(
|
||||
tokenId => {
|
||||
const log = window.__wsLog?.frames ?? []
|
||||
const last = log.filter(frame => frame.t === 'token' && frame.id === tokenId).at(-1)
|
||||
const gone = log.findLast(frame => frame.t === 'token_gone' && frame.id === tokenId)
|
||||
return Boolean(last) && (!gone || log.indexOf(last) > log.indexOf(gone))
|
||||
},
|
||||
tokenId,
|
||||
{ timeout: 20000 },
|
||||
)
|
||||
|
||||
// ---- status: GM-sourced stuck reaches the viewer, then clears --------------------------
|
||||
await gm.click('#token-status-stuck')
|
||||
await waitTokenFrame(player, tokenId, frame => (frame.statuses ?? []).some(entry => entry.status === 'stuck'))
|
||||
await waitForText(gm, '#token-status', /stuck \(GM\)/i)
|
||||
await gm.click('#token-status-clear-stuck')
|
||||
await waitTokenFrame(
|
||||
player,
|
||||
tokenId,
|
||||
frame => !(frame.statuses ?? []).some(entry => entry.status === 'stuck'),
|
||||
)
|
||||
|
||||
// ---- resize: the server pushes the grown token away from a blocking wall ---------------
|
||||
const wall = await raw.ws.request({
|
||||
t: 'wall.add',
|
||||
scene,
|
||||
a: [500, 400],
|
||||
b: [500, 500],
|
||||
preset: 'wall',
|
||||
})
|
||||
assert.equal(wall.ok, true)
|
||||
await gm.fill('#token-resize-radius', '60')
|
||||
await gm.click('#token-resize')
|
||||
await waitTokenFrame(player, tokenId, frame => Math.round(frame.radius) === 60)
|
||||
const pushed = await lastTokenFrame(player, tokenId)
|
||||
assert.ok(pushed.pos[0] < 460, `expected the grow push left of 460, got ${JSON.stringify(pushed.pos)}`)
|
||||
await waitForText(gm, '#gm-token-list', /r60/)
|
||||
|
||||
// ---- remove: the panel drives token.remove and the player is told ----------------------
|
||||
await gm.click('#token-remove')
|
||||
await waitForHiddenRow(gm, tokenId)
|
||||
await player.waitForFunction(
|
||||
tokenId => (window.__wsLog?.frames ?? []).some(frame => frame.t === 'token_gone' && frame.id === tokenId),
|
||||
tokenId,
|
||||
{ timeout: 20000 },
|
||||
)
|
||||
|
||||
// ---- keys never reach the player: no frame field, no DOM text, no server log ----------
|
||||
const playerFrames = await frames(player)
|
||||
assert.equal(
|
||||
playerFrames.some(frame => frame && Object.prototype.hasOwnProperty.call(frame, 'key')),
|
||||
false,
|
||||
'a `key` field reached the player wire',
|
||||
)
|
||||
const wire = JSON.stringify(playerFrames)
|
||||
const playerHtml = await player.content()
|
||||
const log = server.log()
|
||||
for (const key of keys) {
|
||||
assert.equal(wire.includes(key), false, 'a player key reached the player wire')
|
||||
assert.equal(playerHtml.includes(key), false, 'a player key reached the player DOM')
|
||||
assert.equal(log.includes(key), false, 'a player key reached the server log')
|
||||
}
|
||||
assert.equal(await player.locator('#gm-panels').isVisible(), false)
|
||||
} finally {
|
||||
raw.ws.close()
|
||||
await browser.close()
|
||||
}
|
||||
})
|
||||
})
|
||||
Reference in New Issue
Block a user