// Walls r1 (#58, DESIGN 5.8, TESTING 10.3): one-way sight over the real binary and the test nginx. // One scene, one full-height one-way wall and two players on its two sides: // - the player on the blind side gets no token, explored cell or tile across the wall; // - the player on the seeing side does (the positive control, so the oracle can fail); // - flipping `sight_dir` swaps the blind side without moving the endpoints and with no // normalisation: explored cells and allowed tiles only ever grow, never shrink; // - after a restart the direction and the walls are identical and nothing is revoked. // The oracle below is this file's OWN: it reads the GM's wall truth, applies its own side test // (cross product with the (-dy, dx) normal of `a -> b`) and samples clear segments per cell. It // never calls the server's vision or explored code. import test from 'node:test' import assert from 'node:assert/strict' import fs from 'node:fs' import { createPlayer, freePort, httpsBaseUrl, namePlayer, sleep } from './lib.mjs' import { gmKeyOf, makeStateDir, openAs, startServer, stopServer } from './withserver.mjs' import { startTestNginx, stopTestNginx } from './nginx.mjs' import { writeTileGrid } from './tilefixture.mjs' const ALICE = 'Alice' const BOB = 'Bob' const CELL = 32 // v1 pins tile_size = cell (Q4); the oracle and the tile sweep both use it const WALL_X = 400 const WALL = { a: [WALL_X, 0], b: [WALL_X, 4096], preset: 'one_way' } // sight_dir defaults to left // The first cell wholly east of the drawn line: the wall at x=400 sits inside cell 12. const EAST_EDGE = Math.ceil(WALL_X / CELL) * CELL const REGION = { x0: 5, x1: 17, y0: 5, y1: 12 } const Tol = 0.5 // --------------------------------------------------------------------------------------------- // A stack: real server + test nginx in front (the production shape of 11.1). async function withOneWayStack(fn) { const stateDir = makeStateDir() const gmKey = gmKeyOf(stateDir) const stack = { stateDir, gmKey } const boot = async () => { stack.nginxPort = await freePort() stack.server = await startServer({ stateDir, publicOrigin: httpsBaseUrl(stack.nginxPort) }) stack.nginx = await startTestNginx({ backendPort: stack.server.port, stateDir, port: stack.nginxPort }) stack.origin = stack.nginx.origin } const down = async () => { await stopTestNginx(stack.nginx) await stopServer(stack.server) } await boot() const api = { stateDir, gmKey, boot, down, get origin() { return stack.origin }, } try { return await fn(api) } finally { await down() fs.rmSync(stateDir, { recursive: true, force: true }) } } async function gmConnect(origin, gmKey) { const conn = await openAs(origin, gmKey) const hello = await conn.ws.waitText('hello') const walls = await conn.ws.waitText('walls') return { ...conn, scene: walls.scene, walls, art: hello.scene.art_version } } // --------------------------------------------------------------------------------------------- // The independent oracle: the side test, the sight wall list and the per-cell clear-segment sample. function blindTo(w, o) { // DESIGN 5.8: `sight_dir` names the blind side of `a -> b`; `left` is the (-dy, dx) normal. The // cross product of the a->b direction with (o - a) is the signed distance along that normal. if (w.sight_dir !== 'left' && w.sight_dir !== 'right') return false const dx = w.b[0] - w.a[0], dy = w.b[1] - w.a[1] const s = dx * (o.y - w.a[1]) - dy * (o.x - w.a[0]) return w.sight_dir === 'left' ? s >= -1e-9 : s <= 1e-9 } function sightBlocking(wallsFrame, viewer) { // Today's blocking rule is `not (door and open)`, plus the one-way side test. A one-way wall is // in the viewer's fan only while the viewer stands on the blind side (a wall it sees across is // absent from the fan). This is the file's own copy, never server vision code. return wallsFrame.walls .filter(w => !(w.door && w.open) && !((w.sight_dir === 'left' || w.sight_dir === 'right') && !blindTo(w, viewer))) .map(w => ({ a: { x: w.a[0], y: w.a[1] }, b: { x: w.b[0], y: w.b[1] } })) } function segDist(p, a, b) { const ex = b.x - a.x const ey = b.y - a.y const l2 = ex * ex + ey * ey const t = l2 === 0 ? 0 : Math.max(0, Math.min(1, ((p.x - a.x) * ex + (p.y - a.y) * ey) / l2)) return Math.hypot(p.x - a.x - t * ex, p.y - a.y - t * ey) } function orient(a, b, c) { return (b.x - a.x) * (c.y - a.y) - (b.y - a.y) * (c.x - a.x) } function properCross(p, q, a, b) { const d1 = orient(p, q, a), d2 = orient(p, q, b), d3 = orient(a, b, p), d4 = orient(a, b, q) return d1 * d2 < 0 && d3 * d4 < 0 } function segGap(p, q, a, b) { if (properCross(p, q, a, b)) return 0 return Math.min(segDist(p, a, b), segDist(q, a, b), segDist(a, p, q), segDist(b, p, q)) } // sure = a lattice point inside the inscribed 64-gon whose LOS is > Tol from every blocking wall; // maybe = a point within radius + Tol whose LOS no blocking wall properly crosses. function oracleCell({ o, radius, walls, cell, x, y }) { const rin = radius * Math.cos(Math.PI / 64) - Tol const x0 = x * cell, y0 = y * cell let sure = false, maybe = false const n = 16 for (let j = 0; j <= n && !(sure && maybe); j++) { for (let i = 0; i <= n; i++) { const p = { x: x0 + (cell * i) / n, y: y0 + (cell * j) / n } const d = Math.hypot(p.x - o.x, p.y - o.y) if (!maybe && d <= radius + Tol) { let clear = true for (const w of walls) if (properCross(o, p, w.a, w.b)) { clear = false; break } if (clear) maybe = true } if (!sure && d <= rin) { let clear = true for (const w of walls) if (segGap(o, p, w.a, w.b) <= Tol) { clear = false; break } if (clear) sure = true } if (sure && maybe) break } } return { sure, maybe } } // --------------------------------------------------------------------------------------------- function collectExplored(ws) { const cells = new Set() for (const m of ws.messages) { if (m.t !== 'explored') continue if (m.full) { for (const row of m.rle) { const cy = row[0] for (let i = 1; i < row.length; i += 2) { for (let k = 0; k < row[i + 1]; k++) cells.add(`${row[i] + k},${cy}`) } } } else { for (const [cx, cy] of m.cells) cells.add(`${cx},${cy}`) } } return cells } function assertSuperset(after, before, what) { for (const k of before) assert.ok(after.has(k), `${what} lost ${k} (explored/tiles never shrink)`) } async function sweepTiles({ origin, cookie, scene, art, wallsFrame, o, radius, cell, strictMaybes = true }) { const walls = sightBlocking(wallsFrame, o) const allowed = new Set() let sure = 0 for (let y = REGION.y0; y <= REGION.y1; y++) { for (let x = REGION.x0; x <= REGION.x1; x++) { const vis = oracleCell({ o, radius, walls, cell, x, y }) if (vis.sure) sure++ const r = await fetch(`${origin}/tiles/${scene}/v${art}/${x}_${y}.png`, { headers: { Cookie: cookie } }) const at = `tile ${x},${y} (sure=${vis.sure} maybe=${vis.maybe})` if (r.status === 200) { allowed.add(`${x},${y}`) // Explored only grows: once a cell was seen, its tile stays allowed even when a later flip // puts it on the blind side again. Only a fresh region may be checked for invented cells. if (strictMaybes) { assert.ok(vis.maybe, `${at}: allowed although the oracle cannot see any point in the cell`) } } else { assert.equal(r.status, 403, `${at}: unexpected status ${r.status}`) assert.ok(!vis.sure, `${at}: refused although the oracle is sure the cell is visible`) } } } return { allowed, sure } } function eastOfWall(cells) { const out = [] for (const k of cells) { const [cx, cy] = k.split(',').map(Number) if (cx * CELL >= EAST_EDGE) out.push(k) } return out } // --------------------------------------------------------------------------------------------- test('one-way sight: the blind side sees nothing across, the seeing side does (#58)', async () => { await withOneWayStack(async ({ origin, gmKey, stateDir }) => { const gm = await gmConnect(origin, gmKey) const ack = await gm.ws.request({ t: 'wall.add', scene: gm.scene, ...WALL }) assert.equal(ack.ok, true, JSON.stringify(ack)) const wall = await gm.ws.waitFor(m => m.t === 'wall' && m.id === ack.id) assert.equal(wall.sight_dir, 'left') assert.equal(wall.kind, 'one_way') assert.deepEqual(wall.a, WALL.a) assert.deepEqual(wall.b, WALL.b) const wallsFrame = { walls: [wall] } const alice = await createPlayer(gm.ws) const bob = await createPlayer(gm.ws) await namePlayer(origin, alice.key, ALICE) await namePlayer(origin, bob.key, BOB) const mkToken = async (player, pos, name) => { const a = await gm.ws.request({ t: 'token.create', scene: gm.scene, kind: 'pc', pos, flags: { vision: true }, vision_radius: 400, controller: player.id, descriptive_name: name, actual_name: name }) assert.equal(a.ok, true, JSON.stringify(a)) return (await gm.ws.waitFor(m => m.t === 'token' && m.id === a.id)).id } const aliceToken = await mkToken(alice, [300, 300], ALICE) // west, the blind (left) side const bobToken = await mkToken(bob, [500, 400], BOB) // east, the seeing side const a = await openAs(origin, alice.key) await a.ws.waitText('hello') await a.ws.waitFor(m => m.t === 'explored' && m.full === true) const b = await openAs(origin, bob.key) await b.ws.waitText('hello') await b.ws.waitFor(m => m.t === 'explored' && m.full === true) await sleep(100) // Blind side: no token frame, no encountered entry, and no explored cell east of the wall. assert.equal(a.ws.messages.some(m => m.t === 'token' && m.id === bobToken), false, 'the blind side received a token across the one-way wall') const encountered = a.ws.messages.filter(m => m.t === 'encountered') .flatMap(m => (m.set ?? []).map(e => e.id)) assert.equal(encountered.includes(bobToken), false, 'the blind side encountered a token across the one-way wall') const aliceExplored = collectExplored(a.ws) assert.deepEqual(eastOfWall(aliceExplored), [], 'the blind side explored a cell east of the wall') assert.ok(aliceExplored.has('9,9'), 'the blind side did not explore its own cell (control)') // Seeing side (positive control): the same wall lets Bob see Alice's token and west cells. assert.ok(b.ws.messages.some(m => m.t === 'token' && m.id === aliceToken), 'the seeing side did not receive the token across the one-way wall') const bobExplored = collectExplored(b.ws) assert.ok(bobExplored.has('9,9'), 'the seeing side did not explore a cell west of the wall') // Direction is sight only: the wall blocks movement both ways (story 22). const crossFromBlind = await a.ws.request({ t: 'token.move', id: aliceToken, pos: [450, 300] }) assert.equal(crossFromBlind.ok, false, 'the blind side walked through the one-way wall') const crossFromSeeing = await b.ws.request({ t: 'token.move', id: bobToken, pos: [350, 400] }) assert.equal(crossFromSeeing.ok, false, 'the seeing side walked through the one-way wall') // Tiles, blind side: every allowed tile is oracle-maybe, every oracle-sure tile is allowed, and // the east shadow (full-height wall) is refused. const scene = gm.scene writeTileGrid(stateDir, { scene, artVersion: gm.art, size: CELL, ...REGION }) const blind = await sweepTiles({ origin, cookie: a.cookie, scene, art: gm.art, wallsFrame, o: { x: 300, y: 300 }, radius: 400, cell: CELL }) assert.ok(blind.sure > 15, `the blind-side oracle sampled too little (sure=${blind.sure})`) assert.ok(blind.allowed.size > 10, `the gate allowed too few blind-side tiles (${blind.allowed.size})`) for (let y = REGION.y0; y <= REGION.y1; y++) { for (let x = EAST_EDGE / CELL; x <= REGION.x1; x++) { const shadow = oracleCell({ o: { x: 300, y: 300 }, radius: 400, walls: sightBlocking(wallsFrame, { x: 300, y: 300 }), cell: CELL, x, y }) assert.equal(shadow.maybe, false, `oracle says cell ${x},${y} east of the wall is visible`) assert.equal(blind.allowed.has(`${x},${y}`), false, `the blind side was allowed tile ${x},${y} behind the wall`) } } // --- flip: same endpoints, other blind side ------------------------------------------------ const upd = await gm.ws.request({ t: 'wall.update', scene, id: wall.id, sight_dir: 'right' }) assert.equal(upd.ok, true, JSON.stringify(upd)) const flipped = await gm.ws.waitFor(m => m.t === 'wall' && m.id === wall.id && m.sight_dir === 'right') assert.deepEqual(flipped.a, WALL.a, 'the flip moved endpoint a') assert.deepEqual(flipped.b, WALL.b, 'the flip moved endpoint b') assert.equal(flipped.kind, 'one_way') await a.ws.waitFor(m => m.t === 'token' && m.id === bobToken) await b.ws.waitFor(m => m.t === 'token_gone' && m.id === aliceToken) const aliceSaw = await a.ws.waitFor(m => m.t === 'explored' && m.full === false) assert.ok(aliceSaw.cells.length > 0, 'the newly seeing side explored no cell after the flip') const aliceAfter = collectExplored(a.ws) assertSuperset(aliceAfter, aliceExplored, 'the flipped side') assert.ok(eastOfWall(aliceAfter).length > 0, 'the seeing side never explored across after the flip') const bobAfter = collectExplored(b.ws) assertSuperset(bobAfter, bobExplored, 'the side that lost sight') const flippedFrame = { walls: [flipped] } // Tiles after the flip: previously allowed blind-side tiles stay allowed (no revoke) and the // newly seeing side gains at least one eastern tile (positive control). const seen = await sweepTiles({ origin, cookie: a.cookie, scene, art: gm.art, wallsFrame: flippedFrame, o: { x: 300, y: 300 }, radius: 400, cell: CELL }) assertSuperset(seen.allowed, blind.allowed, 'tiles after the flip') assert.ok([...seen.allowed].some(k => Number(k.split(',')[0]) >= 13), 'the seeing side gained no tile east of the wall after the flip') // --- flip back: nothing is ever revoked ---------------------------------------------------- const back = await gm.ws.request({ t: 'wall.update', scene, id: wall.id, sight_dir: 'left' }) assert.equal(back.ok, true, JSON.stringify(back)) const again = await gm.ws.waitFor(m => m.t === 'wall' && m.id === wall.id && m.sight_dir === 'left') assert.deepEqual(again.a, WALL.a) assert.deepEqual(again.b, WALL.b) const blindAgain = await sweepTiles({ origin, cookie: a.cookie, scene, art: gm.art, wallsFrame: { walls: [again] }, o: { x: 300, y: 300 }, radius: 400, cell: CELL, strictMaybes: false }) assertSuperset(blindAgain.allowed, seen.allowed, 'tiles after the flip back') assertSuperset(collectExplored(a.ws), aliceAfter, 'explored after the flip back') a.ws.close(); b.ws.close(); gm.ws.close() }) }) test('one-way sight: the direction and the walls survive a restart (#58)', async () => { await withOneWayStack(async ctx => { let origin = ctx.origin const { gmKey, stateDir, boot, down } = ctx let gm = await gmConnect(origin, gmKey) const ack = await gm.ws.request({ t: 'wall.add', scene: gm.scene, ...WALL }) assert.equal(ack.ok, true, JSON.stringify(ack)) await gm.ws.waitFor(m => m.t === 'wall' && m.id === ack.id) const alice = await createPlayer(gm.ws) await namePlayer(origin, alice.key, ALICE) const mk = await gm.ws.request({ t: 'token.create', scene: gm.scene, kind: 'pc', pos: [300, 300], flags: { vision: true }, vision_radius: 400, controller: alice.id, descriptive_name: ALICE, actual_name: ALICE }) assert.equal(mk.ok, true, JSON.stringify(mk)) const bob = await gm.ws.request({ t: 'token.create', scene: gm.scene, kind: 'pc', pos: [500, 400], flags: { vision: true }, vision_radius: 400, descriptive_name: BOB, actual_name: BOB }) assert.equal(bob.ok, true, JSON.stringify(bob)) const scene = gm.scene const art = gm.art writeTileGrid(stateDir, { scene, artVersion: art, size: CELL, ...REGION }) const wallsFrame = { walls: [await gm.ws.waitFor(m => m.t === 'wall' && m.id === ack.id)] } let a = await openAs(origin, alice.key) await a.ws.waitText('hello') await a.ws.waitFor(m => m.t === 'explored' && m.full === true) await sleep(100) assert.equal(a.ws.messages.some(m => m.t === 'token' && m.id === bob.id), false) const before = collectExplored(a.ws) const beforeTiles = await sweepTiles({ origin, cookie: a.cookie, scene, art, wallsFrame, o: { x: 300, y: 300 }, radius: 400, cell: CELL }) assert.ok(beforeTiles.allowed.size > 10) a.ws.close(); gm.ws.close() // Restart in place: same state dir, real binary migrates/reopens, nothing is rebuilt by hand. await down() await boot() origin = ctx.origin gm = await gmConnect(origin, gmKey) const snap = gm.walls assert.equal(snap.walls.length, 1) assert.equal(snap.walls[0].sight_dir, 'left') assert.equal(snap.walls[0].kind, 'one_way') assert.deepEqual(snap.walls[0].a, WALL.a) assert.deepEqual(snap.walls[0].b, WALL.b) a = await openAs(origin, alice.key) await a.ws.waitText('hello') await a.ws.waitFor(m => m.t === 'explored' && m.full === true) await sleep(100) assertSuperset(collectExplored(a.ws), before, 'explored across the restart') assert.equal(a.ws.messages.some(m => m.t === 'token' && m.id === bob.id), false, 'the restarted server let the blind side see across') const afterTiles = await sweepTiles({ origin, cookie: a.cookie, scene, art: art, wallsFrame: { walls: snap.walls }, o: { x: 300, y: 300 }, radius: 400, cell: CELL, strictMaybes: false }) assertSuperset(afterTiles.allowed, beforeTiles.allowed, 'tiles across the restart') a.ws.close(); gm.ws.close() }) })