feat(client): tokens, BitmapText labels, statuses and lists (#71)
- slices/tokens.ts: token upsert, token_gone, hello reset; status names only. - slices/characters.ts + slices/encountered.ts: own characters, encountered list with grey entries; the GM's per-player copies are ignored. - render/tokenBoard.ts (pure): event -> entered/updated/left patches, so one changed token costs one container; seeds from the reducer on mount. - render/modules/tokens.ts: pooled world-coordinate nodes on the tokens layer, one shared unit-circle context per kind, BitmapText from one pre-installed BitmapFont assigned only on a name change, stuck/squeezed badges, canvas dataset instrumentation for the "only changed entities" criterion. - panels/lists.ts + render/modules/lists.ts: #lists under #panels, textContent only, greyed encountered rows; mounted by a render module so no shared bootstrap file changed.
This commit is contained in:
@@ -0,0 +1,202 @@
|
||||
// T8 (#71, TESTING 10.3): tokens, BitmapText labels, statuses and the two lists, against the real
|
||||
// server through the TLS/HTTP2 nginx. The browser's own WebSocket is only *recorded* (as in
|
||||
// session.test.mjs), so the assertions read the real per-recipient frames; the canvas dataset is
|
||||
// the token renderer's own instrumentation (published by render/modules/tokens.ts).
|
||||
import test from 'node:test'
|
||||
import assert from 'node:assert/strict'
|
||||
import { createPlayer, namePlayer, sleep } 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 {
|
||||
// not a frame; the app ignores it too
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
window.WebSocket = RecordingSocket
|
||||
})
|
||||
}
|
||||
|
||||
const frames = page => page.evaluate(() => window.__wsLog.frames)
|
||||
|
||||
async function waitForBadge(page, state, timeout = 30000) {
|
||||
await page.locator(`#badge[data-state="${state}"]`).waitFor({ state: 'visible', timeout })
|
||||
}
|
||||
|
||||
/** The token renderer's instrumentation, read from the canvas it draws on. */
|
||||
async function tokenRender(page) {
|
||||
return page.evaluate(() => {
|
||||
const canvas = document.querySelector('#stage canvas')
|
||||
if (!canvas) return null
|
||||
const data = canvas.dataset
|
||||
return {
|
||||
count: Number(data.tokens ?? '0'),
|
||||
entered: Number(data.tokenEntered ?? '0'),
|
||||
updated: Number(data.tokenUpdated ?? '0'),
|
||||
left: Number(data.tokenLeft ?? '0'),
|
||||
labels: Number(data.tokenLabels ?? '0'),
|
||||
ids: (data.tokenIds ?? '').split(' ').filter(Boolean),
|
||||
statuses: (data.tokenStatuses ?? '').split(' ').filter(Boolean),
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
async function waitForRender(page, predicate, timeout = 20000) {
|
||||
const deadline = Date.now() + timeout
|
||||
let last = null
|
||||
while (Date.now() < deadline) {
|
||||
last = await tokenRender(page)
|
||||
if (last !== null && predicate(last)) return last
|
||||
await sleep(50)
|
||||
}
|
||||
throw new Error(`token renderer never reached the expected state: ${JSON.stringify(last)}`)
|
||||
}
|
||||
|
||||
test('tokens: enter/leave/update, labels once, statuses, lists, GM sees gm_only', { timeout: 180000 }, async () => {
|
||||
await withStack(async ({ origin, gmKey }) => {
|
||||
const gm = await openAs(origin, gmKey)
|
||||
await gm.ws.waitText('hello')
|
||||
const scene = (await gm.ws.waitText('walls')).scene
|
||||
const player = await createPlayer(gm.ws)
|
||||
await namePlayer(origin, player.key, 'Ann')
|
||||
|
||||
// Story 31: a character owned by the player, with a value only the owner's side carries.
|
||||
const character = await gm.ws.request({
|
||||
t: 'character.create',
|
||||
descriptive_name: 'Ann',
|
||||
actual_name: 'Ann of the Vale',
|
||||
owner: player.id,
|
||||
props: { public: { kind: 'pc' }, owner: { hp: 12 } },
|
||||
})
|
||||
assert.equal(character.ok, true)
|
||||
|
||||
// Story 28: the player's own token (linked to the character) and another figure in sight.
|
||||
const own = await gm.ws.request({
|
||||
t: 'token.create', scene, kind: 'pc', character_id: character.id, controller: player.id,
|
||||
pos: [400, 400], flags: { vision: true },
|
||||
})
|
||||
assert.equal(own.ok, true)
|
||||
const rat = await gm.ws.request({
|
||||
t: 'token.create', scene, kind: 'npc', pos: [460, 400],
|
||||
descriptive_name: 'Rat', actual_name: 'Giant Rat',
|
||||
})
|
||||
assert.equal(rat.ok, true)
|
||||
// Story 34: a gm_only token next to the others; if the player ever received it, it would show.
|
||||
const ghost = await gm.ws.request({
|
||||
t: 'token.create', scene, kind: 'npc', pos: [520, 400],
|
||||
descriptive_name: 'Ghost', actual_name: 'Ghost', gm_only: true,
|
||||
})
|
||||
assert.equal(ghost.ok, true)
|
||||
// Story 30: a status the server records and every viewer sees (cause GM-only).
|
||||
const stuck = await gm.ws.request({ t: 'status.set', id: own.id, status: 'stuck' })
|
||||
assert.equal(stuck.ok, true)
|
||||
|
||||
const browser = await launchBrowser()
|
||||
try {
|
||||
const gmView = await newPage(browser)
|
||||
const playerView = await newPage(browser)
|
||||
await recordSockets(gmView.page)
|
||||
await recordSockets(playerView.page)
|
||||
|
||||
for (const view of [gmView, playerView]) {
|
||||
await view.page.goto(`${origin}/`)
|
||||
await waitForVisible(view.page, '#signin')
|
||||
}
|
||||
await signIn(gmView.page, gmKey)
|
||||
await waitForBadge(gmView.page, 'connected')
|
||||
await signIn(playerView.page, player.key)
|
||||
await waitForBadge(playerView.page, 'connected')
|
||||
|
||||
// Story 34: the GM renders every token, the gm_only one included.
|
||||
await waitForRender(gmView.page, view => view.ids.includes(ghost.id))
|
||||
const gmTokens = await tokenRender(gmView.page)
|
||||
assert.deepEqual(gmTokens.ids.slice().sort(), [own.id, rat.id, ghost.id].sort())
|
||||
assert.equal(gmTokens.count, 3)
|
||||
|
||||
// Story 28: the player renders their own token and the one in sight, labelled with the name
|
||||
// the server gave them, and never the gm_only one.
|
||||
await waitForRender(playerView.page, view => view.ids.includes(own.id) && view.ids.includes(rat.id))
|
||||
const playerTokens = await tokenRender(playerView.page)
|
||||
assert.deepEqual(playerTokens.ids.slice().sort(), [own.id, rat.id].sort())
|
||||
assert.equal(playerTokens.ids.includes(ghost.id), false, 'a gm_only token never reaches a player')
|
||||
// Story 30: the stuck badge is on the player's own token.
|
||||
assert.ok(playerTokens.statuses.includes(`${own.id}:stuck`), playerTokens.statuses.join(' '))
|
||||
// Q17/DESIGN 7.2: one label per name, built once (two names, two builds).
|
||||
assert.equal(playerTokens.labels, 2)
|
||||
|
||||
// The player's own frames: the status carries no `source`, and the gm_only id never appears.
|
||||
const playerFrames = await frames(playerView.page)
|
||||
const ownFrame = playerFrames.filter(frame => frame.t === 'token' && frame.id === own.id).at(-1)
|
||||
assert.ok(ownFrame, 'the player received their own token')
|
||||
assert.deepEqual(ownFrame.statuses, [{ status: 'stuck' }])
|
||||
assert.equal(playerFrames.some(frame => frame.t === 'token' && frame.id === ghost.id), false)
|
||||
|
||||
// Story 31: the owner's list, with the owner's own value.
|
||||
await waitForVisible(playerView.page, '#lists')
|
||||
const characterRow = playerView.page.locator(`#characters li[data-character="${character.id}"]`)
|
||||
await characterRow.waitFor({ state: 'visible' })
|
||||
assert.match(await characterRow.textContent(), /Ann/)
|
||||
assert.match(await characterRow.textContent(), /hp 12/)
|
||||
|
||||
// Story 32: met figures, in sight first.
|
||||
const ratRow = playerView.page.locator(`#encountered li[data-token="${rat.id}"]`)
|
||||
await ratRow.waitFor({ state: 'visible' })
|
||||
assert.equal(await ratRow.getAttribute('data-grey'), 'false')
|
||||
|
||||
// Story 29: the figure leaves sight -> token_gone, and the encountered entry greys out.
|
||||
const movedRat = await gm.ws.request({ t: 'token.move', id: rat.id, pos: [2000, 2000] })
|
||||
assert.equal(movedRat.ok, true)
|
||||
await waitForRender(playerView.page, view => !view.ids.includes(rat.id))
|
||||
const afterLeave = await tokenRender(playerView.page)
|
||||
assert.deepEqual(afterLeave.ids, [own.id], 'the token that left sight is gone from the renderer')
|
||||
assert.equal(afterLeave.left, 1, 'exactly one entity left')
|
||||
assert.equal(afterLeave.updated, 0)
|
||||
assert.equal(afterLeave.entered, 0)
|
||||
await playerView.page.locator(`#encountered li[data-token="${rat.id}"][data-grey="true"]`).waitFor({
|
||||
state: 'visible',
|
||||
timeout: 20000,
|
||||
})
|
||||
assert.match(await playerView.page.textContent(`#encountered li[data-token="${rat.id}"]`), /Rat/)
|
||||
|
||||
// DESIGN 7.2 / the instrumented count: moving one token updates one entity, nothing else.
|
||||
const movedOwn = await gm.ws.request({ t: 'token.move', id: own.id, pos: [400, 430] })
|
||||
assert.equal(movedOwn.ok, true)
|
||||
await waitForRender(playerView.page, view => view.updated === 1)
|
||||
const afterMove = await tokenRender(playerView.page)
|
||||
assert.equal(afterMove.updated, 1, 'one changed token, one updated entity')
|
||||
assert.equal(afterMove.entered, 0)
|
||||
assert.equal(afterMove.left, 0)
|
||||
assert.equal(afterMove.count, 1)
|
||||
// The label was built once per name: a move does not rebuild it.
|
||||
assert.equal(afterMove.labels, 2)
|
||||
|
||||
// The GM saw the same move (one entity updated) and still holds all three tokens.
|
||||
await waitForRender(gmView.page, view => view.updated === 1)
|
||||
const gmAfter = await tokenRender(gmView.page)
|
||||
assert.equal(gmAfter.updated, 1)
|
||||
assert.equal(gmAfter.count, 3)
|
||||
assert.ok(await waitForText(gmView.page, '#lists', /No characters yet/))
|
||||
} finally {
|
||||
gm.ws.close()
|
||||
await browser.close()
|
||||
}
|
||||
})
|
||||
})
|
||||
Reference in New Issue
Block a user