feat(walls): presets, label rule and one switch-set validation path (#55)

wall.add/wall.update now take a preset or raw switches, never both:
resolveWall expands the four 5.8 presets to switches before validating, so
a preset never touches geometry and preset+switch or preset+open is invalid.
A raw create starts from the wall defaults; a raw update patches only the
switches it names and {door:false} closes and de-secrets.

The switch-set invariants are enforced by that one path for add and update:
open/secret/locked without a door, a secret door that is open/one-way/not
opaque, open with secret or locked, and a direction with no sight to block
are all invalid. The DB gains the missing open+locked CHECK as the backstop,
and the now-redundant open/door parity test is gone from the server path.

A wall label is now the 6.5 text rule: trimmed, 1..64 code points, no control
characters, null clears, stored verbatim and GM-only. window/one_way presets
are accepted and expanded to their switch sets per DESIGN 5.8/6.1; their
player-facing behaviour stays with #57/#58.

Tests: WS black-box presets/patch semantics/every refusal/label round-trip;
Nim shape pins for expansion, invariants and the label rule.
Nim 385 / Node 117 green.
This commit is contained in:
2026-11-19 22:35:57 +01:00
parent 77fa2dfe4c
commit 484275e8bb
5 changed files with 504 additions and 107 deletions
+243
View File
@@ -193,6 +193,249 @@ test('wall.update: patches only what it names; open and kind are unknown fields'
})
})
test('presets expand server-side to switches only; preset xor raw switches (#55)', async () => {
await withGmServer(async (s, gmKey) => {
const gm = await gmConnect(s.origin, gmKey)
// The four 5.8 presets: each sets only switches, and the derived kind follows the expansion.
const cases = [
['wall', { blocks_sight: true, blocks_movement: true, sight_dir: 'both',
player_marker: false, door: false, open: false, secret: false, locked: false },
'wall'],
['door', { blocks_sight: true, blocks_movement: true, sight_dir: 'both',
player_marker: false, door: true, open: false, secret: false, locked: false },
'door'],
['window', { blocks_sight: false, blocks_movement: true, sight_dir: 'both',
player_marker: true, door: true, open: false, secret: false, locked: false },
'window'],
['one_way', { blocks_sight: true, blocks_movement: true, sight_dir: 'left',
player_marker: false, door: false, open: false, secret: false, locked: false },
'one_way'],
]
for (const [preset, switches, kind] of cases) {
const ack = await addWall(gm, { a: [10, 10], b: [300, 10], preset })
assert.equal(ack.ok, true, preset)
const wall = await wallEffect(gm.ws, m => m.t === 'wall' && m.id === ack.id)
for (const [key, value] of Object.entries(switches)) {
assert.equal(wall[key], value, `${preset}: ${key}`)
}
assert.equal(wall.kind, kind, preset)
}
// a preset never implies or resets thickness, position or height
const thick = await addWall(gm, { a: [10, 20], b: [300, 20], preset: 'door',
thickness: 20, position: 'left', height: 3.5,
label: 'west door' })
assert.equal(thick.ok, true)
const thickWall = await wallEffect(gm.ws, m => m.t === 'wall' && m.id === thick.id)
assert.equal(thickWall.thickness, 20)
assert.equal(thickWall.position, 'left')
assert.equal(thickWall.height, 3.5)
assert.equal(thickWall.label, 'west door')
// preset or raw switches, never both (open counts as a raw switch)
const both = [
{ preset: 'door', blocks_sight: false },
{ preset: 'wall', player_marker: true },
{ preset: 'door', open: true },
{ preset: 'door', open: false },
{ preset: 'window', sight_dir: 'left' },
]
for (const fields of both) {
const ack = await addWall(gm, { a: [1, 1], b: [2, 2], ...fields })
assert.equal(ack.ok, false, JSON.stringify(fields))
assert.equal(ack.code, 'invalid', JSON.stringify(fields))
}
assert.equal((await addWall(gm, { a: [1, 1], b: [2, 2], preset: 'terrain' })).code,
'invalid', 'an unknown preset name is invalid')
// a raw create starts from the `wall` preset's switch defaults
const raw = await addWall(gm, { a: [10, 40], b: [300, 40], blocks_sight: false })
const rawWall = await wallEffect(gm.ws, m => m.t === 'wall' && m.id === raw.id)
assert.equal(rawWall.kind, 'window')
assert.equal(rawWall.door, false)
assert.equal(rawWall.player_marker, false, 'only the window preset turns the marker on')
assert.equal(rawWall.sight_dir, 'both')
assert.equal(rawWall.blocks_movement, true)
gm.ws.close()
})
})
test('wall.update presets replace the whole switch set and raw patches only named switches (#55)',
async () => {
await withGmServer(async (s, gmKey) => {
const gm = await gmConnect(s.origin, gmKey)
const { id } = await addWall(gm, { a: [10, 10], b: [300, 10], thickness: 16,
position: 'right', height: 2 })
await wallEffect(gm.ws, m => m.t === 'wall' && m.id === id)
// a raw patch names one switch and leaves the rest alone
assert.equal((await gm.ws.request({ t: 'wall.update', scene: gm.scene, id,
blocks_sight: false })).ok, true)
const rawPatched = await wallEffect(gm.ws, m => m.t === 'wall' && m.id === id &&
m.blocks_sight === false)
assert.equal(rawPatched.blocks_movement, true)
assert.equal(rawPatched.door, false)
assert.equal(rawPatched.kind, 'window')
// a preset replaces the whole switch set but keeps geometry
assert.equal((await gm.ws.request({ t: 'wall.update', scene: gm.scene, id,
preset: 'door' })).ok, true)
const asDoor = await wallEffect(gm.ws, m => m.t === 'wall' && m.id === id && m.door === true)
assert.equal(asDoor.blocks_sight, true)
assert.equal(asDoor.blocks_movement, true)
assert.equal(asDoor.player_marker, false)
assert.equal(asDoor.open, false)
assert.equal(asDoor.kind, 'door')
assert.equal(asDoor.thickness, 16, 'a preset never resets geometry')
assert.equal(asDoor.position, 'right')
assert.equal(asDoor.height, 2)
// the window preset turns its marker on and is a closed opening window
assert.equal((await gm.ws.request({ t: 'wall.update', scene: gm.scene, id,
preset: 'window' })).ok, true)
const asWindow = await wallEffect(gm.ws, m => m.t === 'wall' && m.id === id &&
m.blocks_sight === false &&
m.player_marker === true)
assert.equal(asWindow.door, true)
assert.equal(asWindow.player_marker, true)
assert.equal(asWindow.kind, 'window')
// preset xor raw switches in an update too
for (const fields of [{ preset: 'wall', door: true },
{ preset: 'one_way', blocks_sight: false },
{ preset: 'door', open: true },
{ preset: 'door', open: false }]) {
const ack = await gm.ws.request({ t: 'wall.update', scene: gm.scene, id, ...fields })
assert.equal(ack.ok, false, JSON.stringify(fields))
assert.equal(ack.code, 'invalid', JSON.stringify(fields))
}
// a fieldless update is invalid whatever the id, before the lookup (6.1, 6.5 steps 7 then 8)
assert.equal((await gm.ws.request({ t: 'wall.update', scene: gm.scene, id: 99 })).code,
'invalid')
gm.ws.close()
})
})
test('switch-set invariants: door-only settings need a door, and open excludes secret/locked (#55)',
async () => {
await withGmServer(async (s, gmKey) => {
const gm = await gmConnect(s.origin, gmKey)
const invalid = [
{ open: true }, // open without door
{ secret: true }, // secret without door
{ locked: true }, // locked without door
{ open: true, secret: true },
{ open: true, locked: true, door: true }, // an open door is never locked
{ door: true, secret: true, open: true },
{ door: true, secret: true, sight_dir: 'left' }, // a secret door is two-way
{ door: true, secret: true, blocks_sight: false }, // a secret door is opaque
{ door: true, secret: true, blocks_movement: false },
{ door: true, locked: true, open: true },
]
for (const fields of invalid) {
const ack = await addWall(gm, { a: [10, 10], b: [20, 20], ...fields })
assert.equal(ack.ok, false, JSON.stringify(fields))
assert.equal(ack.code, 'invalid', JSON.stringify(fields))
}
// valid door settings: a closed secret+locked door is allowed; so is a locked window
const secret = await addWall(gm, { a: [40, 40], b: [80, 40], door: true, secret: true,
locked: true })
assert.equal(secret.ok, true)
await wallEffect(gm.ws, m => m.t === 'wall' && m.id === secret.id)
const windowLocked = await addWall(gm, { a: [40, 60], b: [80, 60], preset: 'window',
locked: true })
assert.equal(windowLocked.ok, false, 'preset xor raw: locked alongside window is refused')
const windowLockedRaw = await addWall(gm, { a: [40, 60], b: [80, 60],
blocks_sight: false, door: true,
locked: true })
assert.equal(windowLockedRaw.ok, true)
const lockedWindow = await wallEffect(gm.ws, m => m.t === 'wall' && m.id === windowLockedRaw.id)
assert.equal(lockedWindow.kind, 'window')
assert.equal(lockedWindow.locked, true)
// raw switches on a non-door are refused, not silently dropped
const plain = await addWall(gm, { a: [100, 100], b: [200, 100] })
await wallEffect(gm.ws, m => m.t === 'wall' && m.id === plain.id)
for (const fields of [{ secret: true }, { locked: true }]) {
const ack = await gm.ws.request({ t: 'wall.update', scene: gm.scene, id: plain.id,
...fields })
assert.equal(ack.ok, false, JSON.stringify(fields))
assert.equal(ack.code, 'invalid', JSON.stringify(fields))
}
// clearing door closes and de-secrets; naming a true door-only switch with it is invalid
assert.equal((await gm.ws.request({ t: 'wall.update', scene: gm.scene, id: secret.id,
door: false })).ok, true)
const cleared = await wallEffect(gm.ws, m => m.t === 'wall' && m.id === secret.id &&
m.door === false)
assert.equal(cleared.open, false)
assert.equal(cleared.secret, false)
assert.equal(cleared.locked, false)
assert.equal((await gm.ws.request({ t: 'wall.update', scene: gm.scene, id: secret.id,
door: true, secret: true, blocks_sight: false })).code,
'invalid')
gm.ws.close()
})
})
test('wall label: set, change, clear and the 6.5 text rule (#55)', async () => {
await withGmServer(async (s, gmKey) => {
const gm = await gmConnect(s.origin, gmKey)
const { id } = await addWall(gm, { a: [10, 10], b: [200, 10], label: 'railing' })
const added = await wallEffect(gm.ws, m => m.t === 'wall' && m.id === id)
assert.equal(added.label, 'railing')
assert.equal((await gm.ws.request({ t: 'wall.update', scene: gm.scene, id,
label: "the king's window" })).ok, true)
const changed = await wallEffect(gm.ws, m => m.t === 'wall' && m.id === id &&
m.label === "the king's window")
assert.equal(changed.label, "the king's window")
// one code point and 64 code points are legal; unicode counts code points, not bytes
const one = await addWall(gm, { a: [10, 20], b: [200, 20], label: 'x' })
assert.equal(one.ok, true)
const exactly64 = await addWall(gm, { a: [10, 30], b: [200, 30], label: 'é'.repeat(64) })
assert.equal(exactly64.ok, true)
await wallEffect(gm.ws, m => m.t === 'wall' && m.id === exactly64.id)
const invalid = [
'', // 0 code points
'x'.repeat(65), // 65 code points
' railing', // untrimmed
'railing ', // untrimmed
'rail\ting', // control character
'rail\u007fing', // DEL is a control character
' ', // whitespace is not a label
42, // not a string
true,
]
for (const label of invalid) {
const ack = await addWall(gm, { a: [10, 40], b: [200, 40], label })
assert.equal(ack.ok, false, JSON.stringify(label))
assert.equal(ack.code, 'invalid', JSON.stringify(label))
const upd = await gm.ws.request({ t: 'wall.update', scene: gm.scene, id, label })
assert.equal(upd.ok, false, JSON.stringify(label))
assert.equal(upd.code, 'invalid', JSON.stringify(label))
}
// a refused update left the stored label alone
const stillChanged = gm.ws.messages.filter(m => m.t === 'wall' && m.id === id).pop()
assert.equal(stillChanged.label, "the king's window")
// null clears it, and the clear survives a fresh snapshot
assert.equal((await gm.ws.request({ t: 'wall.update', scene: gm.scene, id,
label: null })).ok, true)
const cleared = await wallEffect(gm.ws, m => m.t === 'wall' && m.id === id &&
m.label === null)
assert.equal(cleared.label, null)
const gm2 = await openAs(s.origin, gmKey)
await gm2.ws.waitText('hello')
const snap = await gm2.ws.waitText('walls')
assert.equal(snap.walls.find(w => w.id === id).label, null)
gm.ws.close(); gm2.ws.close()
})
})
test('wall.update {door:true} on a see-through fixed window yields a closed opening window', async () => {
await withGmServer(async (s, gmKey) => {
const gm = await gmConnect(s.origin, gmKey)