// M3 slice 7 (#22): token details over the real wire - `token.resize` (5.6 grow push / squeeze / // clear to the default), `status.set`/`status.clear` with the `walls`|`GM` sources (5.5, the source // is GM-only), `prop.set`/`prop.unset` under the 6.1 caps, the `stuck` a GM placement or a new wall // gives, the shrink that clears it, and a restart that brings sizes, props and statuses back. import test from 'node:test' import assert from 'node:assert/strict' import fs from 'node:fs' import { createPlayer, namePlayer, sleep } from './lib.mjs' import { gmKeyOf, makeStateDir, openAs, startServer, stopServer, withGmServer } from './withserver.mjs' async function gmConnect(origin, gmKey) { const conn = await openAs(origin, gmKey) const hello = await conn.ws.waitText('hello') await conn.ws.waitText('walls') // `scene` is the hello block (defaults and sizes); `sceneId` is the 6.1 `scene` field of an intent return { ...conn, scene: hello.scene, sceneId: hello.scene.id } } // A mutation plus "the frames it caused": the cursor is taken BEFORE the intent is sent, so a wait // can never be satisfied by a frame an earlier mutation produced (these tests repeat statuses and // props, so "the first frame with length 1" would be the wrong one). 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) } } async function namedPlayer(origin, gm, name) { const player = await createPlayer(gm.ws) await namePlayer(origin, player.key, name) return player } const tokenFrames = ws => ws.messages.filter(m => m.t === 'token') const frameOf = (ws, id) => [...ws.messages].reverse().find(m => m.t === 'token' && m.id === id) test('token.resize: a per-token size, null back to the scene default, and the codes of 6.1', async () => { await withGmServer(async (s, gmKey) => { const gm = await gmConnect(s.origin, gmKey) const alice = await namedPlayer(s.origin, gm, '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 } }) const a = await openAs(s.origin, alice.key) await a.ws.waitText('hello') const seen = await a.ws.waitFor(m => m.t === 'token' && m.id === tok.id) assert.equal(seen.radius, gm.scene.token_radius_default, 'the scene default (#4)') // a size reaches every viewer as the effective radius, and only the GM gets radius_own const grow = await mutate(gm.ws, { t: 'token.resize', id: tok.id, radius: 45 }) assert.equal(grow.ack.ok, true) const gmView = await grow.wait(m => m.t === 'token' && m.id === tok.id && m.radius === 45) assert.equal(gmView.radius_own, 45) const grown = await a.ws.waitFor(m => m.t === 'token' && m.id === tok.id && m.radius === 45) assert.equal(grown.radius_own, undefined, 'the own radius is a GM-only field') // null clears back to the scene default const clear = await mutate(gm.ws, { t: 'token.resize', id: tok.id, radius: null }) assert.equal(clear.ack.ok, true) const back = await clear.wait(m => m.t === 'token' && m.id === tok.id && m.radius === gm.scene.token_radius_default) assert.equal(back.radius, gm.scene.token_radius_default) assert.equal((await grow.wait(m => m.t === 'token' && m.id === tok.id && m.radius === 45)) !== null, true) // codes: a missing field, a non-positive size, a bad type, an unknown target for (const patch of [{}, { radius: 0 }, { radius: -3 }, { radius: 'big' }, { radius: [1] }]) { const ack = await gm.ws.request({ t: 'token.resize', id: tok.id, ...patch }) assert.equal(ack.ok, false, JSON.stringify(patch)) assert.equal(ack.code, 'invalid', JSON.stringify(patch)) } assert.equal((await gm.ws.request({ t: 'token.resize', id: 'nope', radius: 20 })).code, 'not-found') // a player may not resize anything (G-only, 6.5 step 6) assert.equal((await a.ws.request({ t: 'token.resize', id: tok.id, radius: 5 })).code, 'not-allowed') a.ws.close(); gm.ws.close() }) }) test('grow push and squeeze (5.6): pushed clear of a wall, or flagged squeezed when there is no room', async () => { await withGmServer(async (s, gmKey) => { const gm = await gmConnect(s.origin, gmKey) const alice = await namedPlayer(s.origin, gm, 'Alice') // a 40 px corridor at x in [500, 540]: a token of r=15 fits, r=25 does not const left = await gm.ws.request({ t: 'wall.add', scene: gm.sceneId, a: [500, 0], b: [500, 900], kind: 'wall' }) await gm.ws.request({ t: 'wall.add', scene: gm.sceneId, a: [540, 0], b: [540, 900], kind: 'wall' }) // the token sits mid-corridor: r=15 fits between the walls, so it starts unlocked const pushee = 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 }, radius: 15 }) assert.ok(left.id > 0) const centred = await mutate(gm.ws, { t: 'token.move', id: pushee.id, pos: [525, 400] }) assert.equal(centred.ack.ok, true) const start = await centred.wait(m => m.t === 'token' && m.id === pushee.id && m.pos[0] === 525) assert.deepEqual(start.statuses, [], 'mid-corridor at r=15: nothing overlaps') // r=15 -> 40 does not fit the 40 px corridor, so the grow is squeezed const squeezed = await mutate(gm.ws, { t: 'token.resize', id: pushee.id, radius: 40 }) assert.equal(squeezed.ack.ok, true, 'a grow that fails is still applied (squeezed is the outcome)') let frame = await squeezed.wait(m => m.t === 'token' && m.id === pushee.id && m.radius === 40) assert.deepEqual(frame.statuses, [{ status: 'squeezed', source: 'walls' }], 'no room either side: squeezed, source walls (5.6)') assert.deepEqual(frame.pos, [525, 400], 'a failed grow does not move the token') const a = await openAs(s.origin, alice.key) await a.ws.waitText('hello') const playerFrame = await a.ws.waitFor(m => m.t === 'token' && m.id === pushee.id) assert.deepEqual(playerFrame.statuses, [{ status: 'squeezed' }], 'a player sees the status, never its source (5.5, 6.1 note 6)') // the player cannot move a squeezed token, and the GM can const nack = await a.ws.request({ t: 'token.move', id: pushee.id, pos: [530, 400] }) assert.equal(nack.ok, false) assert.deepEqual(nack.pos, [525, 400]) const away = await mutate(gm.ws, { t: 'token.move', id: pushee.id, pos: [2000, 2000] }) assert.equal(away.ack.ok, true) frame = await away.wait(m => m.t === 'token' && m.id === pushee.id && m.statuses.length === 0 && m.pos[0] === 2000) assert.deepEqual(frame.statuses, [], 'the GM move to a free spot clears the walls entries') // a grow in the open is pushed away with the minimal displacement const free = await gm.ws.request({ t: 'token.create', scene: gm.sceneId, kind: 'pc', pos: [1000, 900], descriptive_name: 'Bob', actual_name: 'Bob', radius: 10 }) await gm.ws.request({ t: 'wall.add', scene: gm.sceneId, a: [1010, 700], b: [1010, 1100], kind: 'wall' }) const grown = await mutate(gm.ws, { t: 'token.resize', id: free.id, radius: 60 }) assert.equal(grown.ack.ok, true) const pushed = await grown.wait(m => m.t === 'token' && m.id === free.id && m.radius === 60) assert.ok(pushed.pos[0] <= 1010 - 60, `pushed west of the wall: ${JSON.stringify(pushed.pos)}`) assert.equal(Math.abs(pushed.pos[1] - 900) < 1e-6, true, 'the push is perpendicular') assert.deepEqual(pushed.statuses, [], 'a pushed token is clear of every wall') a.ws.close(); gm.ws.close() }) }) test('stuck from a wall and from a GM placement, cleared by the removal and by a shrink', async () => { await withGmServer(async (s, gmKey) => { const gm = await gmConnect(s.origin, gmKey) const alice = await namedPlayer(s.origin, gm, 'Alice') const tok = await gm.ws.request({ t: 'token.create', scene: gm.sceneId, kind: 'pc', pos: [1000, 1000], descriptive_name: 'Alice', actual_name: 'Alice', controller: alice.id, flags: { vision: true } }) const a = await openAs(s.origin, alice.key) await a.ws.waitText('hello') // a wall added over the token gives it stuck(walls) (5.5 SET) const add = await mutate(gm.ws, { t: 'wall.add', scene: gm.sceneId, a: [990, 800], b: [990, 1200], kind: 'wall' }) const wall = add.ack let frame = await add.wait(m => m.t === 'token' && m.id === tok.id && m.statuses.length > 0) assert.deepEqual(frame.statuses, [{ status: 'stuck', source: 'walls' }]) let playerFrame = await a.ws.waitFor(m => m.t === 'token' && m.id === tok.id && m.statuses.length > 0) assert.deepEqual(playerFrame.statuses, [{ status: 'stuck' }]) // the player cannot step out (the plain escape rule still applies: it may, sideways) assert.equal((await a.ws.request({ t: 'token.move', id: tok.id, pos: [1040, 1000] })).ok, false) // shrinking below the overlap clears the walls entry (5.5 CLEAR, 5.6) const shrink = await mutate(gm.ws, { t: 'token.resize', id: tok.id, radius: 5 }) assert.equal(shrink.ack.ok, true) frame = await shrink.wait(m => m.t === 'token' && m.id === tok.id && m.statuses.length === 0) assert.deepEqual(frame.statuses, []) // and now the player can step away from the wall assert.equal((await a.ws.request({ t: 'token.move', id: tok.id, pos: [1030, 1000] })).ok, true) // removing the wall does not resurrect the entry const remove = await mutate(gm.ws, { t: 'wall.remove', scene: gm.sceneId, id: wall.id }) assert.equal(remove.ack.ok, true) await sleep(80) assert.deepEqual(frameOf(gm.ws, tok.id).statuses, []) // a GM placement on a wall is stuck too (5.5 SET) await gm.ws.request({ t: 'wall.add', scene: gm.sceneId, a: [990, 800], b: [990, 1200], kind: 'wall' }) const placed = await gm.ws.request({ t: 'token.create', scene: gm.sceneId, kind: 'npc', pos: [990, 1000], descriptive_name: 'a guard', actual_name: 'Vex' }) const placedFrame = await gm.ws.waitFor(m => m.t === 'token' && m.id === placed.id) assert.deepEqual(placedFrame.statuses, [{ status: 'stuck', source: 'walls' }], 'the create ack carries the id and the very first frame already has the status') a.ws.close(); gm.ws.close() }) }) test('status.set and status.clear: the GM source, single entries, and the codes of 6.1', async () => { await withGmServer(async (s, gmKey) => { const gm = await gmConnect(s.origin, gmKey) const alice = await namedPlayer(s.origin, gm, '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 } }) const a = await openAs(s.origin, alice.key) await a.ws.waitText('hello') await a.ws.waitFor(m => m.t === 'token' && m.id === tok.id) // a GM-sourced stuck: the player is locked, the GM sees the source let op = await mutate(gm.ws, { t: 'status.set', id: tok.id, status: 'stuck' }) assert.equal(op.ack.ok, true) let frame = await op.wait(m => m.t === 'token' && m.id === tok.id && m.statuses.length > 0) assert.deepEqual(frame.statuses, [{ status: 'stuck', source: 'GM' }]) frame = await a.ws.waitFor(m => m.t === 'token' && m.id === tok.id && m.statuses.length > 0) assert.deepEqual(frame.statuses, [{ status: 'stuck' }]) const nack = await a.ws.request({ t: 'token.move', id: tok.id, pos: [440, 400] }) assert.equal(nack.ok, false) assert.deepEqual(nack.pos, [400, 400]) // both statuses show at once; the same entry twice is a no-op const squeeze = await mutate(gm.ws, { t: 'status.set', id: tok.id, status: 'squeezed' }) assert.equal(squeeze.ack.ok, true) frame = await squeeze.wait(m => m.t === 'token' && m.id === tok.id && m.statuses.length === 2) assert.equal((await gm.ws.request({ t: 'status.set', id: tok.id, status: 'stuck' })).ok, true) assert.deepEqual(frame.statuses, [{ status: 'stuck', source: 'GM' }, { status: 'squeezed', source: 'GM' }]) // clearing one entry leaves the other const cleared = await mutate(gm.ws, { t: 'status.clear', id: tok.id, status: 'stuck', source: 'GM' }) assert.equal(cleared.ack.ok, true) frame = await cleared.wait(m => m.t === 'token' && m.id === tok.id && m.statuses.length === 1) assert.deepEqual(frame.statuses, [{ status: 'squeezed', source: 'GM' }]) // clearing it again is not-found (the entry is gone), and so is a bad source assert.equal((await gm.ws.request({ t: 'status.clear', id: tok.id, status: 'stuck', source: 'GM' })).code, 'not-found') assert.equal((await gm.ws.request({ t: 'status.clear', id: tok.id, status: 'stuck', source: 'wall' })).code, 'invalid') assert.equal((await gm.ws.request({ t: 'status.clear', id: 'nope', status: 'stuck', source: 'GM' })).code, 'not-found') for (const patch of [{}, { status: 'blocked' }, { status: 3 }]) { assert.equal((await gm.ws.request({ t: 'status.set', id: tok.id, ...patch })).code, 'invalid', JSON.stringify(patch)) } assert.equal((await gm.ws.request({ t: 'status.set', id: 'nope', status: 'stuck' })).code, 'not-found') // a player may not set a status (G-only) assert.equal((await a.ws.request({ t: 'status.set', id: tok.id, status: 'stuck' })).code, 'not-allowed') // clearing the last entry frees the token for its player const free = await mutate(gm.ws, { t: 'status.clear', id: tok.id, status: 'squeezed', source: 'GM' }) assert.equal(free.ack.ok, true) await free.wait(m => m.t === 'token' && m.id === tok.id && m.statuses.length === 0) assert.equal((await a.ws.request({ t: 'token.move', id: tok.id, pos: [440, 400] })).ok, true) a.ws.close(); gm.ws.close() }) }) test('prop.set / prop.unset: three buckets, the caps, and who may read which bucket', async () => { await withGmServer(async (s, gmKey) => { const gm = await gmConnect(s.origin, gmKey) const alice = await namedPlayer(s.origin, gm, '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 } }) const a = await openAs(s.origin, alice.key) await a.ws.waitText('hello') const seen = await a.ws.waitFor(m => m.t === 'token' && m.id === tok.id) assert.deepEqual(seen.props, { public: {}, owner: {} }, 'a token starts with empty buckets; the controller gets public + owner') // one write per bucket for (const [bucket, value] of [['public', 'cold'], ['owner', 7], ['gm', 'the duke']]) { assert.equal((await gm.ws.request({ t: 'prop.set', target: 'token', id: tok.id, bucket, key: 'aura', value })).ok, true, bucket) } const gmFrame = await gm.ws.waitFor(m => m.t === 'token' && m.id === tok.id && m.props.gm && m.props.gm.aura === 'the duke') assert.deepEqual(gmFrame.props, { public: { aura: 'cold' }, owner: { aura: 7 }, gm: { aura: 'the duke' } }) const playerFrame = await a.ws.waitFor(m => m.t === 'token' && m.id === tok.id && m.props.owner && m.props.owner.aura === 7) assert.deepEqual(playerFrame.props, { public: { aura: 'cold' }, owner: { aura: 7 } }, 'a controller never gets the gm bucket') assert.equal(playerFrame.props.gm, undefined) // a bystander sees the public bucket only const bob = await namedPlayer(s.origin, gm, 'Bob') await gm.ws.request({ t: 'token.create', scene: gm.sceneId, kind: 'pc', pos: [420, 400], descriptive_name: 'Bob', actual_name: 'Bob', controller: bob.id, flags: { vision: true } }) const b = await openAs(s.origin, bob.key) await b.ws.waitText('hello') const theirs = await b.ws.waitFor(m => m.t === 'token' && m.id === tok.id) assert.deepEqual(theirs.props, { public: { aura: 'cold' } }, 'a non-controller gets the public bucket and nothing else') assert.equal(theirs.mine, false) // unset removes one key from one bucket; unsetting an absent key is ok assert.equal((await gm.ws.request({ t: 'prop.unset', target: 'token', id: tok.id, bucket: 'public', key: 'aura' })).ok, true) await gm.ws.waitFor(m => m.t === 'token' && m.id === tok.id && Object.keys(m.props.public).length === 0) assert.equal((await gm.ws.request({ t: 'prop.unset', target: 'token', id: tok.id, bucket: 'public', key: 'aura' })).ok, true) assert.equal((await gm.ws.request({ t: 'prop.unset', target: 'token', id: tok.id, bucket: 'gm', key: 'never-there' })).ok, true) // the owner and gm buckets survived the public unset const after = await gm.ws.waitFor(m => m.t === 'token' && m.id === tok.id && m.props.owner.aura === 7 && m.props.gm.aura === 'the duke') assert.equal(after.props.owner.aura, 7) // codes assert.equal((await gm.ws.request({ t: 'prop.set', target: 'token', id: tok.id, bucket: 'secret', key: 'k', value: 1 })).code, 'invalid') assert.equal((await gm.ws.request({ t: 'prop.set', target: 'nope', id: tok.id, bucket: 'public', key: 'k', value: 1 })).code, 'invalid') assert.equal((await gm.ws.request({ t: 'prop.set', target: 'token', id: tok.id, bucket: 'public', key: '', value: 1 })).code, 'invalid') assert.equal((await gm.ws.request({ t: 'prop.set', target: 'token', id: tok.id, bucket: 'public', key: 'x'.repeat(65), value: 1 })).code, 'invalid') assert.equal((await gm.ws.request({ t: 'prop.set', target: 'token', id: tok.id, bucket: 'public', key: 'k', value: 'y'.repeat(8193) })).code, 'invalid', 'a value over 8 KB is refused (6.1)') assert.equal((await gm.ws.request({ t: 'prop.set', target: 'token', id: tok.id, bucket: 'public', key: 'k' })).code, 'invalid', 'a missing value is invalid') assert.equal((await gm.ws.request({ t: 'prop.set', target: 'token', id: 'nope', bucket: 'public', key: 'k', value: 1 })).code, 'not-found') assert.equal((await gm.ws.request({ t: 'prop.set', target: 'character', id: 'abc', bucket: 'public', key: 'k', value: 1 })).code, 'not-found', 'characters do not exist before slice 8: an unknown character id') // a player may not write props in v1 (G-only) assert.equal((await a.ws.request({ t: 'prop.set', target: 'token', id: tok.id, bucket: 'owner', key: 'k', value: 1 })).code, 'not-allowed') // the cap: 64 keys in one bucket, the 65th refused and nothing applied for (let i = 0; i < 64; i++) { assert.equal((await gm.ws.request({ t: 'prop.set', target: 'token', id: tok.id, bucket: 'public', key: `k${i}`, value: i })).ok, true) } assert.equal((await gm.ws.request({ t: 'prop.set', target: 'token', id: tok.id, bucket: 'public', key: 'one-too-many', value: 1 })).code, 'invalid') const capped = await gm.ws.waitFor(m => m.t === 'token' && m.id === tok.id && Object.keys(m.props.public).length === 64) assert.equal(Object.keys(capped.props.public).includes('one-too-many'), false) assert.equal((await gm.ws.request({ t: 'prop.set', target: 'token', id: tok.id, bucket: 'gm', key: 'still-room', value: 1 })).ok, true, 'the cap is per bucket') // token.create accepts props in all three buckets const withProps = await gm.ws.request({ t: 'token.create', scene: gm.sceneId, kind: 'item', pos: [460, 400], descriptive_name: 'a lamp', actual_name: 'Lantern', props: { public: { light: 30 }, gm: { trap: true } } }) assert.equal(withProps.ok, true) const created = await gm.ws.waitFor(m => m.t === 'token' && m.id === withProps.id) assert.deepEqual(created.props, { public: { light: 30 }, owner: {}, gm: { trap: true } }) a.ws.close(); b.ws.close(); gm.ws.close() }) }) test('restart: sizes, props and statuses come back with the same view', async () => { 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 namedPlayer(s.origin, gm, '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 }, radius: 35, props: { public: { aura: 'cold' }, gm: { secret: 1 } } }) await gm.ws.request({ t: 'status.set', id: tok.id, status: 'stuck' }) await gm.ws.request({ t: 'status.set', id: tok.id, status: 'squeezed' }) const frame = await gm.ws.waitFor(m => m.t === 'token' && m.id === tok.id && m.statuses.length === 2) saved = { id: tok.id, radius: frame.radius, props: frame.props, statuses: frame.statuses, alice } gm.ws.close() } finally { await stopServer(s) } s = await startServer({ stateDir }) try { const gm = await gmConnect(s.origin, gmKey) const frame = await gm.ws.waitFor(m => m.t === 'token' && m.id === saved.id) assert.equal(frame.radius, saved.radius) assert.deepEqual(frame.props, saved.props) assert.deepEqual(frame.statuses, saved.statuses) assert.equal(frame.radius_own, 35) // the lock survived the restart: the controller still cannot move it const a = await openAs(s.origin, saved.alice.key) await a.ws.waitText('hello') await a.ws.waitFor(m => m.t === 'token' && m.id === saved.id) assert.equal((await a.ws.request({ t: 'token.move', id: saved.id, pos: [430, 400] })).ok, false) a.ws.close(); gm.ws.close() } finally { await stopServer(s) } } finally { fs.rmSync(stateDir, { recursive: true, force: true }) } })