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:
@@ -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) => {
|
||||
|
||||
@@ -298,6 +298,13 @@ export async function runLeak({ origin, gmKey, stateDir, transcript, keepOpen =
|
||||
if (ack.ok !== true) throw new Error(`token.create refused: ${JSON.stringify(ack)}`)
|
||||
ids.C1 = ack.id
|
||||
})
|
||||
// #27/#30: a second player meets the same token of the same character, so the transcript carries
|
||||
// two encountered lists whose names can be compared independently (leakcheck runs once per
|
||||
// subject). The later rename and reveals must reach this player's snapshot too, and a reveal to
|
||||
// the *other* player must not.
|
||||
await group('GM walks the other player into the room again', async () => {
|
||||
expectAck(await move(gm, ids.B, [650, 700]), true)
|
||||
})
|
||||
// One mutation per group (the driver's contract): the checker evaluates a group as one state, so
|
||||
// two prop writes in one group would compare an intermediate frame against the final truth.
|
||||
for (const [bucket, value] of [['public', 'noble'], ['owner', 4], ['gm', 'the duke']]) {
|
||||
@@ -338,6 +345,9 @@ export async function runLeak({ origin, gmKey, stateDir, transcript, keepOpen =
|
||||
const ack = await gm.request({ t: 'name.reveal_all', subject: 'token', id: ids.N2 })
|
||||
if (ack.ok !== true) throw new Error(`name.reveal_all refused: ${JSON.stringify(ack)}`)
|
||||
})
|
||||
await group('GM walks the other player out again', async () => {
|
||||
expectAck(await move(gm, ids.B, [420, 1500]), true)
|
||||
})
|
||||
await group('GM hides the character token (the encountered entry stays, greyed)', async () => {
|
||||
const ack = await gm.request({ t: 'token.update', id: ids.C1, gm_only: true })
|
||||
if (ack.ok !== true) throw new Error(`token.update refused: ${JSON.stringify(ack)}`)
|
||||
|
||||
+13
-5
@@ -20,6 +20,8 @@
|
||||
## GM_PROP the `gm` property bucket reached a player at all
|
||||
## STATUS_SOURCE a player frame carried the `source` of a status (5.5, 6.1 note 6)
|
||||
## ACTUAL_NAME an actual name the subject was never told reached it (8)
|
||||
## STALE_NAME an encountered entry names a subject something other than the name that player
|
||||
## knows (8): a stale snapshot the old "not the unrevealed actual" check let pass
|
||||
## CHAR_LIST another player's character record or encountered entry reached the subject
|
||||
## CONTROLLER_CHANGE a token changed controller in a group that asked for no such thing (8)
|
||||
## CHAR_OWNER_PROP a character's owner bucket reached a player who does not own it (8)
|
||||
@@ -487,11 +489,17 @@ proc main() =
|
||||
fail "CHAR_LIST", "encountered entry the subject never saw: " & id
|
||||
elif truth.tokens.hasKey(id):
|
||||
let (dname, aname) = subjectNames(truth, truth.tokens[id])
|
||||
# Only a *different* name leaks: a subject whose two names are the same string has no
|
||||
# secret to keep.
|
||||
if aname != dname and e["name"].getStr == aname and
|
||||
not subjectKnewActual(truth, truth.tokens[id], me):
|
||||
fail "ACTUAL_NAME", "encountered " & id & " carries an unrevealed actual name"
|
||||
let knows = subjectKnewActual(truth, truth.tokens[id], me)
|
||||
# The expected name, not merely "not the unrevealed actual": a stale snapshot that still
|
||||
# carries an old descriptive name is a bug the old check could not see (#30).
|
||||
let want = if knows: aname else: dname
|
||||
let got = e["name"].getStr
|
||||
if got != want:
|
||||
if aname != dname and got == aname:
|
||||
fail "ACTUAL_NAME", "encountered " & id & " carries an unrevealed actual name"
|
||||
else:
|
||||
fail "STALE_NAME", "encountered " & id & " has name " & got & " not " & want &
|
||||
" (group " & g.label & ")"
|
||||
if m.hasKey("del"):
|
||||
for d in m["del"]:
|
||||
if d.getStr notin encounterable:
|
||||
|
||||
+10
-3
@@ -22,6 +22,10 @@
|
||||
# az: in-use not enforced | b1: player.remove forgets the characters | b2: an encountered rename
|
||||
# overwrites a name the player never knew | b3: cprops carries the character owner bucket |
|
||||
# b4: a player is sent every character
|
||||
# #27/#29/#30: the per-player reveal scope, the link patch's name handover and the live
|
||||
# snapshot update. e1: a reveal rewrites every player's snapshot (the old subject-only
|
||||
# SQL) | e2: the encountered rewrite updates the DB but not memory (live list stale) |
|
||||
# e3: a link patch keeps the plain token's own names (every link attempt is invalid)
|
||||
# slice 9 (#24): the canonical tile URL and the "any explored cell allows" gate
|
||||
# c1: a tile is allowed without any explored cell | c2: every overlapping cell is required
|
||||
# c3: an out-of-grid coordinate is accepted | c4: a leading-zero coordinate is canonical
|
||||
@@ -139,7 +143,7 @@ run y "wall endpoints unchecked against the world bounds" server/world.nim 's/p\
|
||||
run z "door.set accepted on a plain wall" server/world.nim '/if sc.walls\[idx.get\].kind != wkDoor: return waNotDoor/d' "wrong kinds are their own"
|
||||
run z2 "persisted open state loaded inverted" server/world.nim 's/open: row\[6\] == "1",/open: row[6] == "0",/' "round-trip through a reload"
|
||||
# slice 5 (#20): the view rule, the egress diff and the explored set
|
||||
run aa "the egress ignores LOS, gm_only and ownership" server/views.nim 's/^ if not pv\.visibleTo(t, playerId): continue$/ if false: continue/' "$AA"
|
||||
run aa "the egress ignores LOS, gm_only and ownership" server/views.nim 's/^ if not pv\[\]\.visibleTo(t, playerId): continue$/ if false: continue/' "$AA"
|
||||
run ab "gm_only ignored by the view rule" server/vision.nim '/^ if t\.gmOnly: return false$/d' "$AB"
|
||||
run ac "a controlled token is not always visible" server/vision.nim '/^ if t\.owner == playerId: return true$/d' "$AC"
|
||||
run ad "no token_gone when a token leaves the view" server/views.nim 's/^ result\.add tokenGoneFrame(id)$/ discard/' "$AD"
|
||||
@@ -170,9 +174,12 @@ run ax "a rename tells a player an actual name it never knew" server/characters.
|
||||
run ay "a linked token may keep its own names" server/tokens.nim 's/ if t\.characterId\.isSome: t\.descriptiveName\.len == 0/ if false: t.descriptiveName.len == 0/' "a linked token carries no names"
|
||||
run az "in-use is not enforced" server/characters.nim 's/^ if t\.characterId\.isSome and t\.characterId\.get == characterId: inc result$/ discard/' "a character still figured by a token is in use"
|
||||
run b1 "player.remove forgets the characters" server/characters.nim 's/WHERE owner_player_id = ?", gmId, playerId)/WHERE owner_player_id = ? AND 0", gmId, playerId)/' "hands the player"
|
||||
run b2 "an encountered rename overwrites a secret" server/characters.nim 's/AND actual_name <> .."""/AND 1 = 1"""/' "a character rename follows the snapshots"
|
||||
run b2 "an encountered rename overwrites a secret" server/characters.nim 's/actualName: (if knows: some(n\.actual) else: none(string))/actualName: some(n.actual)/' "a character rename"
|
||||
run b3 "cprops carries the character owner bucket" server/views.nim 's/cpropsPublic: objectOf(if ch\.isSome and not ownsCharacter: ch\.get\.props\[pbPublic\]/cpropsPublic: objectOf(if ch.isSome and not ownsCharacter: ch.get.props[pbOwner]/' "cprops carries the character"
|
||||
run b4 "a player is sent every character" server/views.nim 's/ if c\.owner != playerId: continue/ if false: continue/' "a player gets only its own characters"
|
||||
run e1 "an encountered rewrite ignores what the player knows" server/characters.nim 's/let updated = e\.refreshed(n, knowsActual(/let updated = e.refreshed(n, true or knowsActual(/' "a link change moves the snapshot"
|
||||
run e2 "an encountered rewrite updates the DB only" server/characters.nim 's/^ enc\[pid\]\[e\.entityId\] = e$/ discard/' "a character rename rewrites the live snapshots"
|
||||
run e3 "a link patch keeps the token's own names" server/tokens.nim 's/^ if result\.characterId\.isSome:$/ if false:/' "linking through a patch hands the names over"
|
||||
# slice 9 (#24): the tile URL grammar and the access rule (server/tiles.nim)
|
||||
TILE_GATE="an unexplored tile is refused|a tile is allowed when any overlapping cell"
|
||||
TILE_ZERO="leading zero tile coordinates"
|
||||
@@ -186,7 +193,7 @@ run d3 "scene.set accepts a patch with no field" server/world.nim 's/^ if not a
|
||||
wait
|
||||
|
||||
rc=0; : > "$LOG"
|
||||
for id in control a b c d1 d2 d3 e f g h i j k l m n o p q r s t u v w x y z z2 aa ab ac ad ae af ag ah ai aj ak al am an ao ap aq ar as at au av aw ax ay az b1 b2 b3 b4 c1 c2 c3 c4; do
|
||||
for id in control a b c d1 d2 d3 e e1 e2 e3 f g h i j k l m n o p q r s t u v w x y z z2 aa ab ac ad ae af ag ah ai aj ak al am an ao ap aq ar as at au av aw ax ay az b1 b2 b3 b4 c1 c2 c3 c4; do
|
||||
[ -f "$T/$id/res" ] && cat "$T/$id/res" >> "$LOG"
|
||||
if [ -f "$T/$id/verdict" ]; then tee -a "$LOG" < "$T/$id/verdict"; [ "$(cat "$T/$id/rc")" = 0 ] || rc=1
|
||||
else echo "$id: NO RESULT (job killed?)" | tee -a "$LOG"; rc=1; fi
|
||||
|
||||
+194
-25
@@ -6,9 +6,17 @@
|
||||
// sighting in the encountered list, explored cells, players and their names), then compares what a
|
||||
// fresh GM connection and a fresh player connection receive as their full 6.3 resync before and
|
||||
// after the restart. SIGTERM and SIGKILL are both checked: every acked commit is durable (4.2).
|
||||
//
|
||||
// #27: three named players meet the same subjects, and the reveals are per player, so the restart
|
||||
// group also pins that the persisted `encountered` snapshot is the name *that* player knows. Before
|
||||
// the fix the reveal SQL had no player predicate: the non-revealed players saw the actual name only
|
||||
// after the restart. Each player's encountered list is compared before/after AND against the name
|
||||
// that player may know (from the GM truth), so a leak cannot hide behind two equally wrong snapshots.
|
||||
import test from 'node:test'
|
||||
import assert from 'node:assert/strict'
|
||||
import { spawnSync } from 'node:child_process'
|
||||
import fs from 'node:fs'
|
||||
import path from 'node:path'
|
||||
import { createPlayer, namePlayer, sleep } from './lib.mjs'
|
||||
import { gmKeyOf, makeStateDir, openAs, startServer, stopServer } from './withserver.mjs'
|
||||
|
||||
@@ -25,11 +33,20 @@ async function buildWorld(origin, gmKey) {
|
||||
const gm = await gmConnect(origin, gmKey)
|
||||
const saved = {}
|
||||
try {
|
||||
// Players: a GM plus one named player with a key (its own CRUD path is key.* + player.setname).
|
||||
// Players: a GM plus three named players with keys (their CRUD path is key.* + player.setname).
|
||||
// Three, because the per-player reveal bug (#27) needs a second and a third encountered list to
|
||||
// be wrong: one player is revealed to, the others must keep the descriptive name across a
|
||||
// restart. Their tokens all stand west of the x=700 wall and see the same two subjects.
|
||||
const alice = await createPlayer(gm.ws)
|
||||
await namePlayer(origin, alice.key, 'Alice')
|
||||
saved.alice = alice
|
||||
// A second, still unnamed player: the GM roster carries it (`name:null`), players never see it.
|
||||
const bob = await createPlayer(gm.ws)
|
||||
await namePlayer(origin, bob.key, 'Bob')
|
||||
saved.bob = bob
|
||||
const carol = await createPlayer(gm.ws)
|
||||
await namePlayer(origin, carol.key, 'Carol')
|
||||
saved.carol = carol
|
||||
// A fourth, still unnamed player: the GM roster carries it (`name:null`), players never see it.
|
||||
await createPlayer(gm.ws)
|
||||
// A wall and a door; the door is opened (door.set is its own write path).
|
||||
const wall = await gm.ws.request({ t: 'wall.add', scene: gm.sceneId, a: [700, 0],
|
||||
@@ -49,6 +66,17 @@ async function buildWorld(origin, gmKey) {
|
||||
props: { public: { aura: 'cold' }, owner: { hp: 5 },
|
||||
gm: { secret: 1 } } })
|
||||
assert.equal(pc.ok, true, JSON.stringify(pc))
|
||||
// Bob and Carol: sight of their own, at the same subjects. They never own a character, so only a
|
||||
// reveal can tell them an actual name.
|
||||
for (const [p, pos] of [[bob, [250, 500]], [carol, [300, 650]]]) {
|
||||
const sight = await gm.ws.request({ t: 'token.create', scene: gm.sceneId, kind: 'pc',
|
||||
pos, descriptive_name: 'sight', actual_name: 'Sight',
|
||||
controller: p.id, flags: { vision: true },
|
||||
radius: 35 })
|
||||
assert.equal(sight.ok, true, JSON.stringify(sight))
|
||||
if (p === bob) saved.ids = { ...(saved.ids ?? {}), bobTok: sight.id }
|
||||
else saved.ids = { ...(saved.ids ?? {}), carolTok: sight.id }
|
||||
}
|
||||
// A monster right next to it: Alice meets it (one encountered entry, grey after it moves away).
|
||||
const monster = await gm.ws.request({ t: 'token.create', scene: gm.sceneId, kind: 'npc',
|
||||
pos: [500, 400], descriptive_name: 'a goblin',
|
||||
@@ -77,13 +105,17 @@ async function buildWorld(origin, gmKey) {
|
||||
assert.equal((await gm.ws.request({ t: 'status.set', id: monster.id,
|
||||
status: 'stuck' })).ok, true)
|
||||
assert.equal((await gm.ws.request({ t: 'token.resize', id: monster.id, radius: 28 })).ok, true)
|
||||
// A reveal for one player, reveal-all for a plain token, one encountered entry removed later.
|
||||
// Per-player reveals: the character's actual name to Alice only, the monster's to Bob only.
|
||||
// Alice must then keep "a goblin" and Bob "a cloaked figure" - before the fix both flipped to
|
||||
// the actual name after a restart (#27). reveal-all for a plain token stays all-players.
|
||||
assert.equal((await gm.ws.request({ t: 'name.reveal', subject: 'character',
|
||||
id: character.id, player: alice.id })).ok, true)
|
||||
assert.equal((await gm.ws.request({ t: 'name.reveal', subject: 'token',
|
||||
id: monster.id, player: bob.id })).ok, true)
|
||||
assert.equal((await gm.ws.request({ t: 'name.reveal_all', subject: 'token',
|
||||
id: pc.id })).ok, true)
|
||||
saved.ids = { pc: pc.id, monster: monster.id, character: character.id, linked: linked.id,
|
||||
wall: wall.id, door: door.id }
|
||||
saved.ids = { ...saved.ids, pc: pc.id, monster: monster.id, character: character.id,
|
||||
linked: linked.id, wall: wall.id, door: door.id }
|
||||
gm.ws.close()
|
||||
} finally {
|
||||
try { gm.ws.close() } catch {}
|
||||
@@ -150,29 +182,50 @@ async function snapshot(origin, key, expected, isGm) {
|
||||
}
|
||||
}
|
||||
|
||||
// The full before/after comparison: one fresh GM connection and one fresh Alice connection.
|
||||
async function compareAcross(origin, gmKey, aliceKey, ids, expected) {
|
||||
// The name each player may know for a subject, from the fixture's own truth: the character reveal
|
||||
// went to Alice only, the monster's to Bob only, and the pc's reveal_all covers everyone.
|
||||
function expectedEncounteredName(who, ids) {
|
||||
if (who === 'alice') return { [ids.monster]: 'a goblin', [ids.linked]: 'Lady Vex',
|
||||
[ids.pc]: 'Wanderer' }
|
||||
return { [ids.monster]: who === 'bob' ? 'Grib' : 'a goblin',
|
||||
[ids.linked]: 'a cloaked figure', [ids.pc]: 'Wanderer' }
|
||||
}
|
||||
|
||||
// The full before/after comparison: one fresh GM connection and one fresh connection per player.
|
||||
async function compareAcross(origin, gmKey, players, ids, expected) {
|
||||
const beforeGm = await snapshot(origin, gmKey, expected, true)
|
||||
const beforeAlice = await snapshot(origin, aliceKey, expected, false)
|
||||
const before = { gm: beforeGm }
|
||||
for (const [who, p] of Object.entries(players)) {
|
||||
// Only the character's owner is sent a `character` record (6.1).
|
||||
before[who] = await snapshot(origin, p.key,
|
||||
{ ...expected, characters: who === 'alice' ? 1 : 0 }, false)
|
||||
}
|
||||
// The CRUD inventory is not empty: a restart test over an empty world would prove nothing.
|
||||
assert.ok(beforeGm.walls.walls.length >= 2, 'the wall and the door are there')
|
||||
assert.equal(beforeGm.walls.walls.find(w => w.id === ids.door).open, true, 'the door is open')
|
||||
assert.ok(beforeGm.tokens.length >= 3, 'plain, monster, player and linked tokens are there')
|
||||
assert.ok(beforeGm.tokens.length >= 5, 'plain, monster, player, linked and sight tokens are there')
|
||||
assert.ok(beforeGm.characters.length >= 1, 'the character is there')
|
||||
assert.equal(beforeGm.characters.find(c => c.id === ids.character).props.public.banner, 'red')
|
||||
assert.equal(beforeGm.tokens.find(t => t.id === ids.monster).radius, 28,
|
||||
'the resize survived')
|
||||
assert.ok(beforeGm.tokens.find(t => t.id === ids.monster).statuses
|
||||
.some(s => s.status === 'stuck' && s.source === 'GM'), 'the GM status survived')
|
||||
assert.equal(beforeAlice.encountered.set.find(e => e.id === ids.monster)?.name, 'a goblin')
|
||||
assert.ok(beforeAlice.vision.polys.length >= 1, 'the player has sight')
|
||||
assert.ok(beforeAlice.explored.rle.length >= 1, 'the player explored something')
|
||||
return { beforeGm, beforeAlice }
|
||||
// #27: each player's encountered list holds the name *that* player knows, live.
|
||||
for (const [who, snap] of Object.entries(before)) {
|
||||
if (who === 'gm') continue
|
||||
for (const [id, name] of Object.entries(expectedEncounteredName(who, ids))) {
|
||||
assert.equal(snap.encountered.set.find(e => e.id === id)?.name, name,
|
||||
`${who}'s encountered entry for ${id} live`)
|
||||
}
|
||||
assert.ok(snap.vision.polys.length >= 1, `${who} has sight`)
|
||||
assert.ok(snap.explored.rle.length >= 1, `${who} explored something`)
|
||||
}
|
||||
return before
|
||||
}
|
||||
|
||||
async function restartAndCompare(stateDir, gmKey, aliceKey, ids, expected, hard) {
|
||||
async function restartAndCompare(stateDir, gmKey, players, ids, expected, hard) {
|
||||
let s = await startServer({ stateDir })
|
||||
const before = await compareAcross(s.origin, gmKey, aliceKey, ids, expected)
|
||||
const before = await compareAcross(s.origin, gmKey, players, ids, expected)
|
||||
if (hard) {
|
||||
// Every ack is sent after COMMIT (4.2), so a hard kill cannot lose an acked mutation.
|
||||
s.child.kill('SIGKILL')
|
||||
@@ -186,47 +239,163 @@ async function restartAndCompare(stateDir, gmKey, aliceKey, ids, expected, hard)
|
||||
s = await startServer({ stateDir })
|
||||
try {
|
||||
const afterGm = await snapshot(s.origin, gmKey, expected, true)
|
||||
const afterAlice = await snapshot(s.origin, aliceKey, expected, false)
|
||||
assert.deepEqual(afterGm, before.beforeGm, 'the GM resync after the restart is identical')
|
||||
assert.deepEqual(afterAlice, before.beforeAlice,
|
||||
'the player resync after the restart is identical')
|
||||
assert.deepEqual(afterGm, before.gm, 'the GM resync after the restart is identical')
|
||||
for (const [who, p] of Object.entries(players)) {
|
||||
const after = await snapshot(s.origin, p.key,
|
||||
{ ...expected, characters: who === 'alice' ? 1 : 0 }, false)
|
||||
assert.deepEqual(after, before[who],
|
||||
`${who}'s resync after the restart is identical`)
|
||||
// ... and the absolute truth, so two equally wrong snapshots cannot pass (#27).
|
||||
for (const [id, name] of Object.entries(expectedEncounteredName(who, ids))) {
|
||||
assert.equal(after.encountered.set.find(e => e.id === id)?.name, name,
|
||||
`${who}'s encountered entry for ${id} after the restart`)
|
||||
}
|
||||
}
|
||||
} finally {
|
||||
await stopServer(s)
|
||||
}
|
||||
}
|
||||
|
||||
const EXPECTED = { tokens: 3, characters: 1 }
|
||||
// The persisted snapshot is where #27 lived: assert directly that the reveal changed exactly one
|
||||
// player's row for the revealed subject, and nobody else's.
|
||||
function dbEncounteredNames(stateDir, entityId) {
|
||||
const r = spawnSync('sqlite3', [path.join(stateDir, 'state.db'),
|
||||
`SELECT player_id || '=' || IFNULL(actual_name, '') FROM encountered ` +
|
||||
`WHERE entity_id = '${entityId}' ORDER BY player_id`], { encoding: 'utf8' })
|
||||
assert.equal(r.status, 0, r.stderr)
|
||||
return Object.fromEntries(r.stdout.trim().split('\n').filter(Boolean)
|
||||
.map(line => { const [id, name] = line.split('='); return [id, name] }))
|
||||
}
|
||||
|
||||
test('restart: full state, all CRUD, before and after SIGTERM', async () => {
|
||||
const EXPECTED = { tokens: 5, characters: 1 }
|
||||
|
||||
async function restartGroup(hard) {
|
||||
const stateDir = makeStateDir()
|
||||
const gmKey = gmKeyOf(stateDir)
|
||||
const players = {}
|
||||
let s = await startServer({ stateDir })
|
||||
let saved
|
||||
try {
|
||||
saved = await buildWorld(s.origin, gmKey)
|
||||
players.alice = saved.alice
|
||||
players.bob = saved.bob
|
||||
players.carol = saved.carol
|
||||
// #27, asserted against the rows while the server is still up: the monster's actual name was
|
||||
// revealed to Bob only, so exactly one of the three rows carries it.
|
||||
const monsterRows = dbEncounteredNames(stateDir, saved.ids.monster)
|
||||
assert.equal(monsterRows[String(saved.bob.id)], 'Grib')
|
||||
assert.equal(monsterRows[String(saved.alice.id)], '')
|
||||
assert.equal(monsterRows[String(saved.carol.id)], '',
|
||||
'the reveal to Bob did not write Alice\'s or Carol\'s row (#27)')
|
||||
const linkedRows = dbEncounteredNames(stateDir, saved.ids.linked)
|
||||
assert.equal(linkedRows[String(saved.alice.id)], 'Lady Vex')
|
||||
assert.equal(linkedRows[String(saved.bob.id)], '')
|
||||
assert.equal(linkedRows[String(saved.carol.id)], '',
|
||||
'the reveal to Alice did not write Bob\'s or Carol\'s row (#27)')
|
||||
} finally {
|
||||
await stopServer(s)
|
||||
}
|
||||
try {
|
||||
await restartAndCompare(stateDir, gmKey, saved.alice.key, saved.ids, EXPECTED, false)
|
||||
await restartAndCompare(stateDir, gmKey, players, saved.ids, EXPECTED, hard)
|
||||
} finally {
|
||||
fs.rmSync(stateDir, { recursive: true, force: true })
|
||||
}
|
||||
}
|
||||
|
||||
test('restart: full state, all CRUD, before and after SIGTERM', async () => {
|
||||
await restartGroup(false)
|
||||
})
|
||||
|
||||
test('restart: full state, all CRUD, before and after SIGKILL', async () => {
|
||||
await restartGroup(true)
|
||||
})
|
||||
|
||||
// #30: a rename must reach the live encountered list, not just the row a restart loads. The wire
|
||||
// and the DB are asserted to agree before and after a restart, for a plain token and a character,
|
||||
// and for a player who was never told the actual name.
|
||||
test('restart: a mid-session rename reaches the live encountered list (#30)', async () => {
|
||||
const stateDir = makeStateDir()
|
||||
const gmKey = gmKeyOf(stateDir)
|
||||
let s = await startServer({ stateDir })
|
||||
let saved
|
||||
let ids = {}
|
||||
let alice, bob
|
||||
try {
|
||||
saved = await buildWorld(s.origin, gmKey)
|
||||
const gm = await gmConnect(s.origin, gmKey)
|
||||
alice = await createPlayer(gm.ws)
|
||||
await namePlayer(s.origin, alice.key, 'Alice')
|
||||
bob = await createPlayer(gm.ws)
|
||||
await namePlayer(s.origin, bob.key, 'Bob')
|
||||
for (const [p, pos] of [[alice, [350, 350]], [bob, [300, 650]]]) {
|
||||
const t = await gm.ws.request({ t: 'token.create', scene: gm.sceneId, kind: 'pc', pos,
|
||||
descriptive_name: 'sight', actual_name: 'Sight',
|
||||
controller: p.id, flags: { vision: true } })
|
||||
assert.equal(t.ok, true, JSON.stringify(t))
|
||||
}
|
||||
const wolf = await gm.ws.request({ t: 'token.create', scene: gm.sceneId, kind: 'npc',
|
||||
pos: [450, 450], descriptive_name: 'a wolf',
|
||||
actual_name: 'Fang' })
|
||||
const char = await gm.ws.request({ t: 'character.create',
|
||||
descriptive_name: 'a cloaked figure',
|
||||
actual_name: 'Lady Vex', owner: alice.id })
|
||||
const linked = await gm.ws.request({ t: 'token.create', scene: gm.sceneId, kind: 'npc',
|
||||
pos: [500, 400], character_id: char.id })
|
||||
assert.ok(wolf.ok && char.ok && linked.ok)
|
||||
ids = { wolf: wolf.id, char: char.id, linked: linked.id }
|
||||
|
||||
// Both players are connected while the rename happens, so the live frame is what is checked.
|
||||
const a = await openAs(s.origin, alice.key)
|
||||
await a.ws.waitText('hello')
|
||||
const b = await openAs(s.origin, bob.key)
|
||||
await b.ws.waitText('hello')
|
||||
await a.ws.waitFor(m => m.t === 'encountered' &&
|
||||
m.set.some(e => e.id === wolf.id && e.name === 'a wolf'))
|
||||
await b.ws.waitFor(m => m.t === 'encountered' &&
|
||||
m.set.some(e => e.id === wolf.id && e.name === 'a wolf'))
|
||||
|
||||
let from = a.ws.messages.length
|
||||
const w = await gm.ws.request({ t: 'token.update', id: wolf.id,
|
||||
descriptive_name: 'a huge wolf' })
|
||||
assert.equal(w.ok, true, JSON.stringify(w))
|
||||
const renamed = await a.ws.waitFor((m, i) => i >= from && m.t === 'encountered' &&
|
||||
m.set.some(e => e.id === wolf.id))
|
||||
assert.equal(renamed.set.find(e => e.id === wolf.id).name, 'a huge wolf',
|
||||
'the live list follows a plain-token rename (#30)')
|
||||
await b.ws.waitFor(m => m.t === 'encountered' &&
|
||||
m.set.some(e => e.id === wolf.id && e.name === 'a huge wolf'))
|
||||
|
||||
const bFrom = b.ws.messages.length
|
||||
const c = await gm.ws.request({ t: 'character.update', id: char.id,
|
||||
descriptive_name: 'a tall stranger' })
|
||||
assert.equal(c.ok, true, JSON.stringify(c))
|
||||
// Alice owns the character, so she sees the actual name and the descriptive rename is invisible
|
||||
// to her; Bob (never told) must see the new descriptive name on the wire, with no restart.
|
||||
const moved = await b.ws.waitFor((m, i) => i >= bFrom && m.t === 'encountered' &&
|
||||
m.set.some(e => e.id === linked.id))
|
||||
assert.equal(moved.set.find(e => e.id === linked.id).name, 'a tall stranger',
|
||||
'the live list follows a character rename for a player who was not told (#30)')
|
||||
assert.equal(renamed.set.find(e => e.id === wolf.id).name, 'a huge wolf')
|
||||
a.ws.close(); b.ws.close(); gm.ws.close()
|
||||
} finally {
|
||||
await stopServer(s)
|
||||
}
|
||||
try {
|
||||
await restartAndCompare(stateDir, gmKey, saved.alice.key, saved.ids, EXPECTED, true)
|
||||
s = await startServer({ stateDir })
|
||||
for (const [key, who] of [[alice.key, 'alice'], [bob.key, 'bob']]) {
|
||||
const c = await openAs(s.origin, key)
|
||||
try {
|
||||
await c.ws.waitFor(m => m.t === 'encountered' && m.full === true)
|
||||
const e = c.ws.messages.filter(m => m.t === 'encountered').pop()
|
||||
assert.equal(e.set.find(x => x.id === ids.wolf)?.name, 'a huge wolf',
|
||||
`${who}: the plain-token rename survives the restart`)
|
||||
assert.equal(e.set.find(x => x.id === ids.linked)?.name,
|
||||
who === 'alice' ? 'Lady Vex' : 'a tall stranger',
|
||||
`${who}: the character rename survives the restart, per player`)
|
||||
} finally {
|
||||
c.ws.close()
|
||||
}
|
||||
}
|
||||
} finally {
|
||||
await stopServer(s)
|
||||
fs.rmSync(stateDir, { recursive: true, force: true })
|
||||
}
|
||||
})
|
||||
|
||||
Reference in New Issue
Block a user