feat: GM characters, reveals, who-sees-it and seen_by (#75)

Server: GM token frames gain the additive, GM-only seen_by:[player ids],
computed from each player's existing sight (PlayerVision.visibleTo) with
no extra polygon pass. Leakcheck BannedAlways bans seen_by; a seenBy
fault build is caught as exactly WALLDATA. proto stays 2 (6.2 additive).

Client: new cast slice parses character/character_gone/encountered frames
and seen_by; gmcast panel adds character create/rename/assign/remove,
reveal to one/all, encountered forget (grey entries only) and the
who-sees-it list (0/100 in M4). Mounted beside the #74 admin panel.

Docs: DESIGN 5.9/6.1/6.2/7.4 record the round-1 shape and the no-bump
call. Gates: devbox run test, client-test, e2e green; bench recorded.
This commit is contained in:
2026-11-21 01:04:10 +01:00
parent 429f023c62
commit 0323fbd2ae
12 changed files with 1234 additions and 16 deletions
+209
View File
@@ -0,0 +1,209 @@
// T12 (#75, TESTING 10.3): GM characters, reveals, encountered remove and who-sees-it against the
// real server through the TLS/HTTP2 test nginx. The GM drives the panels; a player page records its
// own WS frames, so the reveal and the encountered removal are proven on the player wire and the
// GM-only `seen_by` field is proven absent from it.
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 {
// non-JSON frames are not state
}
})
}
}
window.WebSocket = RecordingSocket
})
}
const frames = page => page.evaluate(() => window.__wsLog.frames ?? [])
async function waitTokenFrame(page, id, predicate, timeout = 20000) {
const deadline = Date.now() + timeout
let last = null
while (Date.now() < deadline) {
last = await page.evaluate(
tokenId =>
(window.__wsLog?.frames ?? []).filter(frame => frame.t === 'token' && frame.id === tokenId).at(-1),
id,
)
if (last && predicate(last)) return last
await sleep(50)
}
throw new Error(`token ${id} never matched; last was ${JSON.stringify(last)}`)
}
test('GM characters, reveals, encountered remove and who-sees-it', { timeout: 240000 }, async () => {
await withStack(async ({ origin, gmKey }) => {
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 {
// Two named players and one scout each; Alice's scout stands beside the linked token.
const alice = await createPlayer(raw.ws)
await namePlayer(origin, alice.key, 'Alice')
const bob = await createPlayer(raw.ws)
await namePlayer(origin, bob.key, 'Bob')
const playerView = await newPage(browser)
const player = playerView.page
await recordSockets(player)
await player.goto(`${origin}/`)
await waitForVisible(player, '#signin')
await signIn(player, alice.key)
await waitForVisible(player, '#hud')
await waitForText(player, '#me', /^Alice$/)
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 waitForVisible(gm, '#gm-cast')
// ---- character.create through the panel (owner defaults to the GM) --------------------
await gm.fill('#character-name', 'a cloaked figure')
await gm.fill('#character-actual', 'Lord Vex')
await gm.click('#character-create-submit')
await waitForText(gm, '#gm-character-status', /created/i)
const row = gm.locator('#gm-character-list li[data-character]').first()
await row.waitFor({ state: 'visible', timeout: 20000 })
const characterId = await row.getAttribute('data-character')
assert.ok(characterId, 'created character has no id in the panel')
await waitForText(gm, `#gm-character-title-${characterId}`, /a cloaked figure/)
// ---- fixture tokens: Alice's scout, Bob's far scout and one token linked to the character
const aScout = await raw.ws.request({
t: 'token.create', scene, kind: 'pc', pos: [400, 400], descriptive_name: 'Alice',
actual_name: 'Alice', controller: alice.id, flags: { vision: true },
})
assert.equal(aScout.ok, true, JSON.stringify(aScout))
const bScout = await raw.ws.request({
t: 'token.create', scene, kind: 'pc', pos: [3900, 3900], descriptive_name: 'Bob',
actual_name: 'Bob', controller: bob.id, flags: { vision: true },
})
assert.equal(bScout.ok, true, JSON.stringify(bScout))
const linked = await raw.ws.request({
t: 'token.create', scene, kind: 'npc', pos: [500, 500], character_id: characterId,
controller: alice.id,
})
assert.equal(linked.ok, true, 'a linked token needs no names of its own (6.1)')
// Alice sees it by sight, Bob by control; a far, uncontrolled token has nobody.
const shared = await raw.ws.request({
t: 'token.create', scene, kind: 'npc', pos: [300, 300], descriptive_name: 'a shared thing',
actual_name: 'Shared Thing', controller: bob.id,
})
assert.equal(shared.ok, true, JSON.stringify(shared))
const unseen = await raw.ws.request({
t: 'token.create', scene, kind: 'npc', pos: [2000, 2000], descriptive_name: 'a far thing',
actual_name: 'Far Thing',
})
assert.equal(unseen.ok, true, JSON.stringify(unseen))
// The owner sees the descriptive name until the GM reveals the actual one.
await waitTokenFrame(player, linked.id, frame => frame.name === 'a cloaked figure')
await waitTokenFrame(player, linked.id, frame => frame.seen_by === undefined)
// ---- who-sees-it: Alice's sight reaches the token, Bob's does not ---------------------
const seenText = await waitForText(gm, `#gm-seen-${linked.id}`, /Alice.*100 %/)
assert.equal(seenText.includes('Bob'), false, `Bob must not be a viewer: ${seenText}`)
await waitForText(gm, `#gm-seen-${shared.id}`, /Alice, Bob.*100 %/)
await waitForText(gm, `#gm-seen-${unseen.id}`, /nobody \(0 %\)/)
assert.equal(await gm.locator('#gm-cast').isVisible(), true)
// ---- reveal to one player through the panel ------------------------------------------
await gm.selectOption(`#gm-character-reveal-player-${characterId}`, String(alice.id))
await gm.click(`#gm-character-reveal-${characterId}`)
await waitForText(gm, '#gm-character-status', /revealed/i)
await waitTokenFrame(player, linked.id, frame => frame.name === 'Lord Vex')
await waitForText(gm, `#gm-character-meta-${characterId}`, /told: Alice/)
// ---- a character owner change never moves a token controller -------------------------
const before = await new Promise(resolve => {
const deadline = Date.now() + 20000
const poll = () => {
const frame = raw.ws.messages.filter(m => m.t === 'token' && m.id === linked.id).at(-1)
if (frame) resolve(frame)
else if (Date.now() > deadline) resolve(null)
else setTimeout(poll, 25)
}
poll()
})
assert.equal(before?.controller, alice.id)
await gm.selectOption(`#gm-character-owner-${characterId}`, String(bob.id))
await gm.click(`#gm-character-reassign-${characterId}`)
await waitForText(gm, '#gm-character-status', /reassigned/i)
await waitForText(gm, `#gm-character-meta-${characterId}`, /owner Bob/)
await sleep(250)
const after = raw.ws.messages.filter(m => m.t === 'token' && m.id === linked.id).at(-1)
assert.equal(after.controller, alice.id, 'character.reassign must not touch token controllers')
await waitForText(
gm,
`#gm-token-list li[data-token="${linked.id}"]`,
/controller Alice/,
)
// ---- encountered remove through the panel --------------------------------------------
// The entry exists while the token is in sight; forgetting becomes meaningful once the
// player can no longer see it (DESIGN 8: a sighting is re-recorded while it stays in view).
await waitForText(gm, `#gm-encountered-${alice.id}`, /Lord Vex/)
assert.equal(await gm.locator(`#gm-encountered-remove-${alice.id}-${linked.id}`).count(), 0,
'an in-sight entry has no Forget button')
assert.equal((await raw.ws.request({ t: 'token.remove', id: linked.id })).ok, true)
await player.waitForFunction(
tokenId =>
(window.__wsLog?.frames ?? []).some(
frame => frame.t === 'token_gone' && frame.id === tokenId,
),
linked.id,
{ timeout: 20000 },
)
await waitForText(gm, `#gm-encountered-${alice.id}`, /met, not in sight/)
await waitForVisible(gm, `#gm-encountered-remove-${alice.id}-${linked.id}`)
await gm.click(`#gm-encountered-remove-${alice.id}-${linked.id}`)
await waitForText(gm, '#gm-encountered-status', /forgotten/i)
await player.waitForFunction(
tokenId =>
(window.__wsLog?.frames ?? []).some(
frame =>
frame.t === 'encountered' &&
(frame.del ?? []).includes(tokenId),
),
linked.id,
{ timeout: 20000 },
)
// ---- the GM-only field never reaches a player ----------------------------------------
const playerFrames = await frames(player)
assert.equal(JSON.stringify(playerFrames).includes('seen_by'), false,
'seen_by reached the player wire')
assert.equal(await player.locator('#gm-cast').isVisible(), false,
'a player page must not show the GM cast panel')
} finally {
raw.ws.close()
await browser.close()
}
})
})