Files
FFF_vtt_frozen/tools/characters.test.mjs
T
SirStone 8e045b0ac8 names: per-player reveal, one encountered write path, link by update (#27, #29, #30)
name.reveal wrote the subject's actual name into the encountered row of every
player who had met it: the SQL had no player predicate, so the leak was silent
live and visible only after a restart. rewriteEncountered is now the one path
that keeps a snapshot in step with its subject: it rewrites the SQLite row and
the live in-memory entry together from the name rule, takes onlyPlayer for
name.reveal (every player for reveal_all), and also runs on a rename, a link
change and an owner change, so the wire and a post-restart reload cannot
disagree (#30). The DB-only renameEncountered*/revealEncountered* helpers are
gone, which is what let the two stores drift.

token.update {character_id} now drops the token's own two names as part of the
link patch (#29): a plain token can be linked, names supplied with a link stay
invalid and unlinking still needs both names.

Tests: tcharacters covers the per-player row, the DB/memory equality, the link
handover; restart.test.mjs builds three named players, reveals to one of them
and asserts the rows directly and the whole resync over SIGTERM/SIGKILL, plus a
mid-session rename; leak.mjs/leakcheck.nim pit two players on the same subject
and pin each entry's expected name (STALE_NAME). Mutations e1-e3 and the
repointed b2 pin the three rules.
2026-11-15 14:26:07 +01:00

621 lines
36 KiB
JavaScript

// M3 slice 8 (#23): characters and names over the real wire - `character.create`/`update`/
// `remove`/`reassign`, the per-player reveal of an actual name (`name.reveal`, `name.reveal_all`),
// the per-player encountered list (`encountered.remove`), character props through `prop.*`, the
// token link (`character_id`), and the players/keys half that gives a player a name
// (`player.setname`, `player.rename`, `key.show`, `roster`).
//
// The leak/fault proof lives in tools/leak.test.mjs + tools/leakcheck.nim; this file is the
// "characters and names" acceptance group of DESIGN 13: reveal per player and to all, the owner
// sees its own actual name, an owner change leaves token controllers alone, encountered survives
// token removal and a `gm_only` flip, and a status source is never shown to a non-GM.
import test from 'node:test'
import assert from 'node:assert/strict'
import { createPlayer, openWs, sleep } from './lib.mjs'
import { signIn, withGmServer } from './withserver.mjs'
async function gmConnect(origin, gmKey) {
const { cookie } = await signIn(origin, gmKey)
const ws = await openWs(origin, cookie)
const hello = await ws.waitText('hello')
await ws.waitText('walls')
return { ...{ ws }, sceneId: hello.scene.id, scene: hello.scene }
}
// A player connection that names itself the way a browser does: sign in, connect, `player.setname`,
// then the second `hello` (6.1) and the resync (6.3).
async function connectNamed(origin, key, name) {
const { cookie } = await signIn(origin, key)
const ws = await openWs(origin, cookie)
await ws.waitText('hello')
const from = ws.messages.length
const ack = await ws.request({ t: 'player.setname', name })
assert.equal(ack.ok, true, `player.setname refused: ${JSON.stringify(ack)}`)
const hello = await ws.waitFor((m, i) => i >= from && m.t === 'hello')
return { ws, hello, ack }
}
// One intent plus a cursor, so a wait can never be satisfied by an older frame.
async function mutate(client, payload) {
const from = client.messages.length
const ack = await client.request(payload)
return {
ack,
wait: (pred, ms = 5000) => client.waitFor((m, i) => i >= from && pred(m), ms),
eventually: async (pred, ms = 5000) => {
const deadline = Date.now() + ms
for (;;) {
const hit = client.messages.find((m, i) => i >= from && pred(m))
if (hit) return hit
if (Date.now() > deadline) throw new Error(`no matching frame in ${ms} ms`)
await sleep(10)
}
},
}
}
const characterFrames = ws => ws.messages.filter(m => m.t === 'character')
const encounteredFrames = ws => ws.messages.filter(m => m.t === 'encountered')
test('player.setname: the first name, the second hello, a roster to everyone, and the codes of 6.1',
async () => {
await withGmServer(async (s, gmKey) => {
const gm = await gmConnect(s.origin, gmKey)
const alice = await createPlayer(gm.ws)
const bob = await createPlayer(gm.ws)
// a token Alice controls, so naming her must produce a vision polygon on the resync
const sightTok = await gm.ws.request({ t: 'token.create', scene: gm.sceneId, kind: 'pc',
pos: [400, 400], descriptive_name: 'Alice',
actual_name: 'Alice', controller: alice.id,
flags: { vision: true } })
const a = await openAs_(s.origin, alice.key)
const hello = await a.ws.waitText('hello')
assert.equal(hello.me.name, null, 'a fresh key has no name yet')
assert.equal(hello.scene, undefined, 'an unnamed player gets no scene block (6.1)')
const from = a.ws.messages.length
const ack = await a.ws.request({ t: 'player.setname', name: ' Alice ' })
assert.equal(ack.ok, true)
const named = await a.ws.waitFor((m, i) => i >= from && m.t === 'hello')
assert.equal(named.me.name, 'Alice', 'the name is trimmed before it is stored (6.5)')
assert.equal(named.scene.id, 1, 'a named player gets the scene block')
// the resync computes this connection's sight for the first time: an unnamed connection never
// had a refresh, so a vision token it controls must show up now
const vision = await a.ws.waitFor((m, i) => i >= from && m.t === 'vision')
assert.deepEqual(vision.polys.map(p => p.id), [sightTok.id])
const roster = await a.ws.waitFor((m, i) => i >= from && m.t === 'roster')
assert.deepEqual(roster.players.map(p => p.name).sort(), ['Alice', 'GM'],
'a player sees the named players only (6.1 leak note 4)')
assert.equal(roster.players.find(p => p.name === 'Alice').connected, true)
assert.equal(roster.players.find(p => p.name === 'GM').connected, true)
// the GM hears about it too
const gmRoster = await gm.ws.waitFor(m => m.t === 'roster' && m.players.some(p => p.name === 'Alice'))
assert.equal(gmRoster.players.filter(p => p.name === null).length, 1, 'the unused key, name: null')
// a second setname is refused (6.1: only while own name is null)
assert.equal((await a.ws.request({ t: 'player.setname', name: 'Alicia' })).code, 'not-allowed')
// a taken name, in another case, is refused
const b = await openAs_(s.origin, bob.key)
await b.ws.waitText('hello')
const taken = await b.ws.request({ t: 'player.setname', name: 'alice' })
assert.equal(taken.code, 'name-taken')
assert.equal((await b.ws.request({ t: 'player.setname', name: 'GM' })).code, 'name-taken',
'the GM name is a name like any other (6.5)')
for (const bad of ['', ' ', 'two\nlines', 'x'.repeat(33)]) {
assert.equal((await b.ws.request({ t: 'player.setname', name: bad })).code, 'invalid',
JSON.stringify(bad))
}
// the GM does not name itself on the wire (its name comes from the CLI)
assert.equal((await gm.ws.request({ t: 'player.setname', name: 'Boss' })).code, 'not-allowed')
b.ws.close(); a.ws.close(); gm.ws.close()
})
})
test('player.rename and key.show: the GM renames, re-shows a key, and never touches its own',
async () => {
await withGmServer(async (s, gmKey) => {
const gm = await gmConnect(s.origin, gmKey)
const alice = await createPlayer(gm.ws)
const a = await connectNamed(s.origin, alice.key, 'Alice')
const rename = await mutate(gm.ws, { t: 'player.rename', id: alice.id, name: 'Alicia' })
assert.equal(rename.ack.ok, true)
const roster = await rename.wait(m => m.t === 'roster' && m.players.some(p => p.name === 'Alicia'))
assert.ok(roster)
await a.ws.waitFor(m => m.t === 'roster' && m.players.some(p => p.name === 'Alicia'))
assert.equal((await gm.ws.request({ t: 'player.rename', id: alice.id, name: 'GM' })).code,
'name-taken')
assert.equal((await gm.ws.request({ t: 'player.rename', id: 999, name: 'Nobody' })).code,
'not-found')
assert.equal((await gm.ws.request({ t: 'player.rename', id: alice.id, name: '' })).code,
'invalid')
// renaming to the same name (the player's own) is not "taken"
assert.equal((await gm.ws.request({ t: 'player.rename', id: alice.id, name: 'alicia' })).ok,
true)
const shown = await gm.ws.request({ t: 'key.show', id: alice.id })
assert.equal(shown.ok, true)
assert.equal(shown.key, alice.key, 'Q11: the GM can re-show a player key')
const gmSelf = await gm.ws.request({ t: 'key.show', id: 1 })
assert.equal(gmSelf.code, 'not-allowed', 'the GM key is CLI-only (3.2)')
assert.equal((await gm.ws.request({ t: 'key.show', id: 999 })).code, 'not-found')
// a player may not see another player's key (G-only, 6.5 step 6)
assert.equal((await a.ws.request({ t: 'key.show', id: alice.id })).code, 'not-allowed')
a.ws.close(); gm.ws.close()
})
})
test('character.create: the record reaches the GM and the owner only, and the codes of 6.1',
async () => {
await withGmServer(async (s, gmKey) => {
const gm = await gmConnect(s.origin, gmKey)
const alice = await createPlayer(gm.ws)
const bob = await createPlayer(gm.ws)
const a = await connectNamed(s.origin, alice.key, 'Alice')
const b = await connectNamed(s.origin, bob.key, 'Bob')
const create = await mutate(gm.ws, { t: 'character.create', descriptive_name: 'a grumpy man',
actual_name: 'Lord Vex', owner: alice.id,
props: { public: { klass: 'fighter' },
owner: { hp: 12 }, gm: { secret: true } } })
assert.equal(create.ack.ok, true)
assert.equal(typeof create.ack.id, 'string')
assert.equal(create.ack.id.length, 12, 'the same id alphabet as a token (8)')
const gmFrame = await gm.ws.waitFor(m => m.t === 'character' && m.id === create.ack.id)
assert.equal(gmFrame.owner, alice.id)
assert.deepEqual(gmFrame.revealed, [])
assert.equal(gmFrame.reveal_all, false)
assert.deepEqual(Object.keys(gmFrame.props).sort(), ['gm', 'owner', 'public'])
const ownerFrame = await a.ws.waitFor(m => m.t === 'character' && m.id === create.ack.id)
assert.equal(ownerFrame.owner, undefined, 'an owner is never told `owner`')
assert.equal(ownerFrame.reveal_all, undefined, 'nor `reveal_all`')
assert.equal(ownerFrame.revealed, undefined, 'nor who else was told the name')
assert.equal(ownerFrame.actual_name, 'Lord Vex', 'the owner always sees the actual name (8)')
assert.deepEqual(Object.keys(ownerFrame.props).sort(), ['owner', 'public'])
await sleep(120)
assert.equal(characterFrames(b.ws).length, 0, 'no other player gets a character record (T16)')
// codes
assert.equal((await gm.ws.request({ t: 'character.create', descriptive_name: '',
actual_name: 'X' })).code, 'invalid')
assert.equal((await gm.ws.request({ t: 'character.create', descriptive_name: 'a',
actual_name: 'X', owner: 999 })).code, 'not-found')
assert.equal((await gm.ws.request({ t: 'character.create', descriptive_name: 'a',
actual_name: 'X', props: { nope: {} } })).code, 'invalid')
// G-only
assert.equal((await a.ws.request({ t: 'character.create', descriptive_name: 'a',
actual_name: 'X' })).code, 'not-allowed')
b.ws.close(); a.ws.close(); gm.ws.close()
})
})
test('names: the owner sees the actual name, a reveal covers one player, reveal_all covers everyone',
async () => {
await withGmServer(async (s, gmKey) => {
const gm = await gmConnect(s.origin, gmKey)
const alice = await createPlayer(gm.ws)
const bob = await createPlayer(gm.ws)
const a = await connectNamed(s.origin, alice.key, 'Alice')
const b = await connectNamed(s.origin, bob.key, 'Bob')
// Owned by the GM: neither viewer owns it, so both see the descriptive name (8)
const char = await gm.ws.request({ t: 'character.create', descriptive_name: 'a cloaked figure',
actual_name: 'Lady Vex' })
const tok = await gm.ws.request({ t: 'token.create', scene: gm.sceneId, kind: 'pc',
pos: [400, 400], character_id: char.id })
assert.equal(tok.ok, true, 'a linked token needs no names of its own (6.1)')
// Both players control a token next to the linked one, so both see it while it is unrevealed
await gm.ws.request({ t: 'token.create', scene: gm.sceneId, kind: 'pc', pos: [350, 400],
descriptive_name: 'Alice', actual_name: 'Alice', controller: alice.id,
flags: { vision: true } })
await gm.ws.request({ t: 'token.create', scene: gm.sceneId, kind: 'pc', pos: [350, 450],
descriptive_name: 'Bob', actual_name: 'Bob', controller: bob.id,
flags: { vision: true } })
const seen = await a.ws.waitFor(m => m.t === 'token' && m.id === tok.id)
assert.equal(seen.name, 'a cloaked figure', 'the descriptive name until a reveal (8)')
assert.equal(seen.character_id, undefined, 'character_id only for the owner and the GM')
assert.deepEqual(seen.cprops, { public: {} })
// the character's public bucket reaches someone who sees one of its tokens
const prop = await mutate(gm.ws, { t: 'prop.set', target: 'character', id: char.id,
bucket: 'public', key: 'rank', value: 'noble' })
assert.equal(prop.ack.ok, true, 'prop.set now honours target:"character" (6.1)')
const withProp = await a.ws.waitFor(m => m.t === 'token' && m.id === tok.id &&
m.cprops.public.rank === 'noble')
assert.equal(withProp.cprops.owner, undefined, 'the owner bucket of a character stays private')
await gm.ws.request({ t: 'prop.set', target: 'character', id: char.id, bucket: 'gm',
key: 'trap', value: true })
await gm.ws.request({ t: 'prop.set', target: 'character', id: char.id, bucket: 'owner',
key: 'hp', value: 3 })
await sleep(120)
const bobCprops = b.ws.messages.filter(m => m.t === 'token' && m.id === tok.id).pop()
assert.deepEqual(Object.keys(bobCprops.cprops), ['public'],
'cprops carries the public bucket and nothing else')
// reveal to Alice only
const reveal = await mutate(gm.ws, { t: 'name.reveal', subject: 'character', id: char.id,
player: alice.id })
assert.equal(reveal.ack.ok, true)
const revealed = await a.ws.waitFor(m => m.t === 'token' && m.id === tok.id &&
m.name === 'Lady Vex')
assert.ok(revealed)
const gmReveal = await gm.ws.waitFor(m => m.t === 'character' && m.id === char.id &&
m.revealed.includes(alice.id))
assert.ok(gmReveal)
await b.ws.waitFor(m => m.t === 'token' && m.id === tok.id)
await sleep(100)
const bobToken = b.ws.messages.filter(m => m.t === 'token' && m.id === tok.id).pop()
assert.equal(bobToken.name, 'a cloaked figure', 'the reveal was for Alice only')
// reveal_all covers everyone, idempotently
assert.equal((await gm.ws.request({ t: 'name.reveal_all', subject: 'character',
id: char.id })).ok, true)
await b.ws.waitFor(m => m.t === 'token' && m.id === tok.id && m.name === 'Lady Vex')
assert.equal((await gm.ws.request({ t: 'name.reveal_all', subject: 'character',
id: char.id })).ok, true)
// codes: a token that has a character is revealed through the character (6.1)
assert.equal((await gm.ws.request({ t: 'name.reveal', subject: 'token', id: tok.id,
player: alice.id })).code, 'invalid')
assert.equal((await gm.ws.request({ t: 'name.reveal', subject: 'nope', id: char.id,
player: alice.id })).code, 'invalid')
assert.equal((await gm.ws.request({ t: 'name.reveal', subject: 'character', id: 'nope',
player: alice.id })).code, 'not-found')
assert.equal((await gm.ws.request({ t: 'name.reveal', subject: 'character', id: char.id,
player: 999 })).code, 'not-found')
b.ws.close(); a.ws.close(); gm.ws.close()
})
})
test('a plain token has its own names: reveal, reveal_all and the cprops of a plain token',
async () => {
await withGmServer(async (s, gmKey) => {
const gm = await gmConnect(s.origin, gmKey)
const alice = await createPlayer(gm.ws)
const a = await connectNamed(s.origin, alice.key, 'Alice')
const tok = await gm.ws.request({ t: 'token.create', scene: gm.sceneId, kind: 'item',
pos: [400, 400], descriptive_name: 'a rune',
actual_name: 'The Rune' })
const mine = await gm.ws.request({ t: 'token.create', scene: gm.sceneId, kind: 'pc',
pos: [350, 400], descriptive_name: 'Alice',
actual_name: 'Alice', controller: alice.id,
flags: { vision: true } })
assert.ok(mine.ok)
const seen = await a.ws.waitFor(m => m.t === 'token' && m.id === tok.id)
assert.equal(seen.name, 'a rune')
assert.deepEqual(seen.cprops, { public: {} }, 'a plain token still carries cprops (leak note 3)')
assert.equal((await gm.ws.request({ t: 'name.reveal', subject: 'token', id: tok.id,
player: alice.id })).ok, true)
await a.ws.waitFor(m => m.t === 'token' && m.id === tok.id && m.name === 'The Rune')
// the GM frame keeps the reveal bookkeeping for a plain token
const gmFrame = await gm.ws.waitFor(m => m.t === 'token' && m.id === tok.id &&
m.revealed.includes(alice.id))
assert.equal(gmFrame.reveal_all, false)
assert.equal(gmFrame.actual_name, 'The Rune')
a.ws.close(); gm.ws.close()
})
})
test('character.reassign: the character changes hands and the token controllers do not (3.1, 8)',
async () => {
await withGmServer(async (s, gmKey) => {
const gm = await gmConnect(s.origin, gmKey)
const alice = await createPlayer(gm.ws)
const bob = await createPlayer(gm.ws)
const a = await connectNamed(s.origin, alice.key, 'Alice')
const b = await connectNamed(s.origin, bob.key, 'Bob')
const char = await gm.ws.request({ t: 'character.create', descriptive_name: 'a grumpy man',
actual_name: 'Lord Vex', owner: bob.id })
// Alice controls a token next to the linked one, so she sees it while not owning the character
await gm.ws.request({ t: 'token.create', scene: gm.sceneId, kind: 'pc', pos: [350, 400],
descriptive_name: 'Alice', actual_name: 'Alice', controller: alice.id,
flags: { vision: true } })
// the token starts controlled by the character's owner (6.1 default)
const tok = await gm.ws.request({ t: 'token.create', scene: gm.sceneId, kind: 'pc',
pos: [400, 400], character_id: char.id })
const gmTok = await gm.ws.waitFor(m => m.t === 'token' && m.id === tok.id)
assert.equal(gmTok.controller, bob.id, "a new linked token is controlled by the character's owner")
const before = await a.ws.waitFor(m => m.t === 'token' && m.id === tok.id)
assert.equal(before.character_id, undefined, 'not her character yet (6.1 note 2)')
assert.equal(before.name, 'a grumpy man')
const aFrom = a.ws.messages.length
const reassign = await mutate(gm.ws, { t: 'character.reassign', id: char.id, owner: alice.id })
assert.equal(reassign.ack.ok, true)
await a.ws.waitFor(m => m.t === 'character' && m.id === char.id)
await b.ws.waitFor(m => m.t === 'character_gone' && m.id === char.id)
const after = await gm.ws.waitFor(m => m.t === 'token' && m.id === tok.id &&
m.controller === bob.id)
assert.equal(after.controller, bob.id, 'a character owner change never moves a controller (3.1)')
const mine = await a.ws.waitFor((m, i) => i >= aFrom && m.t === 'token' &&
m.id === tok.id && m.character_id === char.id)
assert.equal(mine.mine, false, 'the token is still Bob\'s')
assert.equal(mine.name, 'Lord Vex', 'the owner of the character sees the actual name')
// codes
assert.equal((await gm.ws.request({ t: 'character.reassign', id: char.id, owner: 999 })).code,
'not-found')
assert.equal((await gm.ws.request({ t: 'character.reassign', id: 'nope', owner: alice.id })).code,
'not-found')
b.ws.close(); a.ws.close(); gm.ws.close()
})
})
test('character.update and character.remove: rename, in-use, and a token link that must be cut first',
async () => {
await withGmServer(async (s, gmKey) => {
const gm = await gmConnect(s.origin, gmKey)
const alice = await createPlayer(gm.ws)
const a = await connectNamed(s.origin, alice.key, 'Alice')
const char = await gm.ws.request({ t: 'character.create', descriptive_name: 'a grumpy man',
actual_name: 'Lord Vex', owner: alice.id })
const tok = await gm.ws.request({ t: 'token.create', scene: gm.sceneId, kind: 'pc',
pos: [400, 400], character_id: char.id })
const rename = await mutate(gm.ws, { t: 'character.update', id: char.id,
descriptive_name: 'a tall stranger' })
assert.equal(rename.ack.ok, true)
const frame = await a.ws.waitFor(m => m.t === 'character' && m.id === char.id &&
m.descriptive_name === 'a tall stranger')
assert.equal(frame.actual_name, 'Lord Vex')
const gmTok = await gm.ws.waitFor(m => m.t === 'token' && m.id === tok.id &&
m.name === 'a tall stranger')
assert.equal(gmTok.character_id, char.id)
assert.equal((await gm.ws.request({ t: 'character.update', id: char.id })).code, 'invalid')
assert.equal((await gm.ws.request({ t: 'character.update', id: 'nope',
actual_name: 'X' })).code, 'not-found')
// `props` is not a `character.update` field (6.1): it is an unknown field, so it is ignored
// (6.2) and the intent then names no field at all, which is `invalid`.
const withProps = await gm.ws.request({ t: 'character.update', id: char.id,
props: { public: {} } })
assert.equal(withProps.code, 'invalid')
// in-use while a token links it (6.5)
assert.equal((await gm.ws.request({ t: 'character.remove', id: char.id })).code, 'in-use')
// unlinking needs both names (6.1)
assert.equal((await gm.ws.request({ t: 'token.update', id: tok.id,
character_id: null })).code, 'invalid')
assert.equal((await gm.ws.request({ t: 'token.update', id: tok.id, character_id: null,
descriptive_name: 'a wolf',
actual_name: 'Fang' })).ok, true)
const unlinked = await gm.ws.waitFor(m => m.t === 'token' && m.id === tok.id &&
m.character_id === null &&
m.descriptive_name === 'a wolf')
assert.equal(unlinked.actual_name, 'Fang')
assert.equal((await gm.ws.request({ t: 'character.remove', id: char.id })).ok, true)
await a.ws.waitFor(m => m.t === 'character_gone' && m.id === char.id)
assert.equal((await gm.ws.request({ t: 'character.remove', id: char.id })).code, 'not-found')
// a token cannot be linked with names of its own (6.1)
assert.equal((await gm.ws.request({ t: 'token.create', scene: gm.sceneId, kind: 'pc',
pos: [100, 100], character_id: char.id,
descriptive_name: 'x', actual_name: 'X' })).code, 'invalid')
assert.equal((await gm.ws.request({ t: 'token.create', scene: gm.sceneId, kind: 'pc',
pos: [100, 100], character_id: 'nope' })).code, 'not-found')
a.ws.close(); gm.ws.close()
})
})
test('token.update links a plain token to a character: the names are handed over (#29)', async () => {
await withGmServer(async (s, gmKey) => {
const gm = await gmConnect(s.origin, gmKey)
const alice = await createPlayer(gm.ws)
const a = await connectNamed(s.origin, alice.key, 'Alice')
// Alice has sight on the wolf's square, so she meets it before it is linked.
const eye = await gm.ws.request({ t: 'token.create', scene: gm.sceneId, kind: 'pc',
pos: [350, 350], descriptive_name: 'Alice',
actual_name: 'Alice', controller: alice.id,
flags: { vision: true } })
assert.equal(eye.ok, true, JSON.stringify(eye))
const char = await gm.ws.request({ t: 'character.create',
descriptive_name: 'a cloaked figure',
actual_name: 'Lady Vex', owner: alice.id })
assert.equal(char.ok, true, JSON.stringify(char))
const wolf = await gm.ws.request({ t: 'token.create', scene: gm.sceneId, kind: 'npc',
pos: [450, 450], descriptive_name: 'a wolf',
actual_name: 'Fang' })
assert.equal(wolf.ok, true, JSON.stringify(wolf))
const met = await a.ws.waitFor(m => m.t === 'encountered' &&
m.set.some(e => e.id === wolf.id))
assert.equal(met.set.find(e => e.id === wolf.id).name, 'a wolf')
// Linking *and* supplying names is still the 6.1 `invalid`.
const withNames = await gm.ws.request({ t: 'token.update', id: wolf.id,
character_id: char.id,
descriptive_name: 'a wolf',
actual_name: 'Fang' })
assert.equal(withNames.code, 'invalid')
// A bare link is `ok` and drops the token's own names: the character's names reach clients.
const aFrom = a.ws.messages.length
const link = await mutate(gm.ws, { t: 'token.update', id: wolf.id, character_id: char.id })
assert.equal(link.ack.ok, true, JSON.stringify(link.ack))
const gmTok = await link.wait(m => m.t === 'token' && m.id === wolf.id &&
m.character_id === char.id)
assert.equal(gmTok.name, 'a cloaked figure')
assert.equal(gmTok.descriptive_name, undefined,
'a linked token carries no names of its own (6.1)')
assert.equal(gmTok.actual_name, undefined)
// Alice owns the character, so its actual name is the one she sees.
const aTok = await a.ws.waitFor((m, i) => i >= aFrom && m.t === 'token' && m.id === wolf.id)
assert.equal(aTok.name, 'Lady Vex')
assert.equal(aTok.character_id, char.id)
// The snapshot follows the link too: her encountered entry is the character's name now.
const relinked = await a.ws.waitFor((m, i) => i >= aFrom && m.t === 'encountered' &&
m.set.some(e => e.id === wolf.id))
assert.equal(relinked.set.find(e => e.id === wolf.id).name, 'Lady Vex')
// Unlinking without both names stays `invalid`; with both it restores the token's own names.
assert.equal((await gm.ws.request({ t: 'token.update', id: wolf.id,
character_id: null })).code, 'invalid')
const unlink = await mutate(gm.ws, { t: 'token.update', id: wolf.id, character_id: null,
descriptive_name: 'a wolf', actual_name: 'Fang' })
assert.equal(unlink.ack.ok, true, JSON.stringify(unlink.ack))
const plain = await unlink.wait(m => m.t === 'token' && m.id === wolf.id &&
m.character_id === null)
assert.equal(plain.name, 'a wolf')
assert.equal(plain.descriptive_name, 'a wolf')
assert.equal(plain.actual_name, 'Fang')
// A linked token still cannot receive names of its own afterwards.
assert.equal((await gm.ws.request({ t: 'token.update', id: wolf.id,
character_id: char.id,
actual_name: 'X' })).code, 'invalid')
a.ws.close(); gm.ws.close()
})
})
test('encountered: a sighting is kept under the name the player knows, and survives everything',
async () => {
await withGmServer(async (s, gmKey) => {
const gm = await gmConnect(s.origin, gmKey)
const alice = await createPlayer(gm.ws)
const a = await connectNamed(s.origin, alice.key, 'Alice')
const mine = await gm.ws.request({ t: 'token.create', scene: gm.sceneId, kind: 'pc',
pos: [400, 400], descriptive_name: 'Alice',
actual_name: 'Alice', controller: alice.id,
flags: { vision: true } })
const other = await gm.ws.request({ t: 'token.create', scene: gm.sceneId, kind: 'npc',
pos: [500, 400], descriptive_name: 'a wolf',
actual_name: 'Fang' })
assert.ok(mine.ok && other.ok)
const entry = await a.ws.waitFor(m => m.t === 'encountered' &&
m.set.some(e => e.id === other.id))
assert.equal(entry.full, false)
const e = entry.set.find(x => x.id === other.id)
assert.equal(e.name, 'a wolf', 'the name the player knows (8)')
assert.equal(e.grey, false)
assert.equal(e.character_id, undefined, 'no character link for a player (6.1)')
assert.equal(e.pos, undefined, 'no position either')
// a reveal updates the snapshot
await gm.ws.request({ t: 'name.reveal', subject: 'token', id: other.id, player: alice.id })
const told = await a.ws.waitFor(m => m.t === 'encountered' &&
m.set.some(x => x.id === other.id && x.name === 'Fang'))
assert.ok(told)
// `gm_only` hides the token but the entry stays, greyed (8)
const hidden = await mutate(gm.ws, { t: 'token.update', id: other.id, gm_only: true })
assert.equal(hidden.ack.ok, true)
const greyed = await a.ws.waitFor(m => m.t === 'encountered' &&
m.set.some(x => x.id === other.id && x.grey === true))
assert.deepEqual(greyed.del, [], 'nothing is deleted by a gm_only flip')
await gm.ws.request({ t: 'token.update', id: other.id, gm_only: false })
// removing the token keeps the entry (still greyed)
assert.equal((await gm.ws.request({ t: 'token.remove', id: other.id })).ok, true)
await a.ws.waitFor(m => m.t === 'token_gone' && m.id === other.id)
const still = await a.ws.waitFor(m => m.t === 'encountered' &&
m.set.some(x => x.id === other.id && x.grey === true))
assert.ok(still)
// only the GM removes an entry, and `del` tells the player
const gone = await mutate(gm.ws, { t: 'encountered.remove', player: alice.id, id: other.id })
assert.equal(gone.ack.ok, true)
const del = await a.ws.waitFor(m => m.t === 'encountered' && (m.del ?? []).includes(other.id))
assert.deepEqual(del.set, [])
assert.equal((await gm.ws.request({ t: 'encountered.remove', player: alice.id,
id: other.id })).code, 'not-found')
assert.equal((await gm.ws.request({ t: 'encountered.remove', player: 999,
id: other.id })).code, 'not-found')
// the GM sees one list per player, tagged with the player id
const gmList = await gm.ws.waitFor(m => m.t === 'encountered' && m.player === alice.id)
assert.ok(gmList)
// a connection that starts later gets the whole list (`full:true`, 6.1)
const a2 = await openAs_(s.origin, alice.key)
const full = await a2.ws.waitFor(m => m.t === 'encountered' && m.full === true)
assert.ok(full.set.some(x => x.id === mine.id), 'the list is the list the player had')
a2.ws.close()
a.ws.close(); gm.ws.close()
})
})
test('a status source is never shown to a non-GM, the controller included (5.5, 6.1 note 6)',
async () => {
await withGmServer(async (s, gmKey) => {
const gm = await gmConnect(s.origin, gmKey)
const alice = await createPlayer(gm.ws)
const a = await connectNamed(s.origin, alice.key, 'Alice')
const tok = await gm.ws.request({ t: 'token.create', scene: gm.sceneId, kind: 'pc',
pos: [400, 400], descriptive_name: 'Alice',
actual_name: 'Alice', controller: alice.id,
flags: { vision: true } })
assert.equal((await gm.ws.request({ t: 'status.set', id: tok.id, status: 'stuck' })).ok, true)
assert.equal((await gm.ws.request({ t: 'status.set', id: tok.id, status: 'squeezed' })).ok,
true)
const gmView = await gm.ws.waitFor(m => m.t === 'token' && m.id === tok.id &&
m.statuses.length === 2)
assert.deepEqual(gmView.statuses, [{ status: 'squeezed', source: 'GM' },
{ status: 'stuck', source: 'GM' }].sort(
(x, y) => x.status.localeCompare(y.status)).reverse(),
'the GM sees the cause')
const playerView = await a.ws.waitFor(m => m.t === 'token' && m.id === tok.id &&
m.statuses.length === 2)
assert.deepEqual(playerView.statuses.map(x => x.status).sort(), ['squeezed', 'stuck'])
for (const st of playerView.statuses) {
assert.deepEqual(Object.keys(st), ['status'], 'no `source` on a player entry')
}
a.ws.close(); gm.ws.close()
})
})
test('restart: characters, reveals and encountered lists come back', async () => {
const { makeStateDir, gmKeyOf, startServer, stopServer } = await import('./withserver.mjs')
const fs = await import('node:fs')
const stateDir = makeStateDir()
try {
const gmKey = gmKeyOf(stateDir)
let s = await startServer({ stateDir })
let saved
try {
const gm = await gmConnect(s.origin, gmKey)
const alice = await createPlayer(gm.ws)
const a = await connectNamed(s.origin, alice.key, 'Alice')
const char = await gm.ws.request({ t: 'character.create', descriptive_name: 'a cloaked figure',
actual_name: 'Lady Vex', owner: alice.id,
props: { public: { rank: 'noble' },
owner: { hp: 5 } } })
const tok = await gm.ws.request({ t: 'token.create', scene: gm.sceneId, kind: 'pc',
pos: [400, 400], character_id: char.id })
await gm.ws.request({ t: 'name.reveal_all', subject: 'character', id: char.id })
await a.ws.waitFor(m => m.t === 'encountered' && m.set.some(e => e.id === tok.id))
saved = { char, tok, alice }
a.ws.close(); gm.ws.close()
} finally {
await stopServer(s)
}
s = await startServer({ stateDir })
try {
const gm = await gmConnect(s.origin, gmKey)
const gmChar = await gm.ws.waitFor(m => m.t === 'character' && m.id === saved.char.id)
assert.equal(gmChar.reveal_all, true)
assert.equal(gmChar.actual_name, 'Lady Vex')
assert.deepEqual(gmChar.props.public, { rank: 'noble' })
const gmTok = await gm.ws.waitFor(m => m.t === 'token' && m.id === saved.tok.id)
assert.equal(gmTok.character_id, saved.char.id)
assert.equal(gmTok.name, 'a cloaked figure')
// The name survived, so this is a plain signed-in connection (no `player.setname`).
const a = await openAs_(s.origin, saved.alice.key)
const hello = await a.ws.waitText('hello')
assert.equal(hello.me.name, 'Alice', 'the display name survived the restart')
const enc = await a.ws.waitFor(m => m.t === 'encountered' && m.full === true)
assert.ok(enc.set.some(e => e.id === saved.tok.id), 'the sighting survived the restart')
assert.equal(enc.set.find(e => e.id === saved.tok.id).name, 'Lady Vex',
'the reveal_all survived too')
const tokFrame = await a.ws.waitFor(m => m.t === 'token' && m.id === saved.tok.id)
assert.equal(tokFrame.character_id, saved.char.id)
a.ws.close(); gm.ws.close()
} finally {
await stopServer(s)
}
} finally {
fs.rmSync(stateDir, { recursive: true, force: true })
}
})
// Local alias so the imports stay at the top of the file.
async function openAs_(origin, key) {
const { cookie } = await signIn(origin, key)
const ws = await openWs(origin, cookie)
return { ws, cookie }
}