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.
This commit is contained in:
2026-11-15 14:26:07 +01:00
parent e68c6eefbf
commit 8e045b0ac8
9 changed files with 528 additions and 93 deletions
+68
View File
@@ -396,6 +396,74 @@ test('character.update and character.remove: rename, in-use, and a token link th
})
})
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) => {