// Behaviour preservation for the proto-2 wall record (#54, TESTING 10.3 "Wall fields, storage and // migration"). One real server migrates the shared schema-7 fixture and keeps its legacy walls; a // second server migrates the identical fixture and rebuilds the *same* walls through the proto-2 // `wall.add` raw-switch path before any player connects. The same script then runs against both, // and each player's complete frame sequence (including every ack/nack) and the per-tile HTTP status // sweep must be byte-identical after canonicalisation. No normalisation: token ids, player ids and // the per-connection `n` are identical because both runs start from the same fixture. import test from 'node:test' import assert from 'node:assert/strict' import fs from 'node:fs' import { DatabaseSync } from 'node:sqlite' import { baseUrl, freePort, refuseLivePort } from './lib.mjs' import { gmKeyOf, makeStateDir, openAs, startServer, stopServer } from './withserver.mjs' import { startTestNginx, stopTestNginx } from './nginx.mjs' import { writeTileGrid } from './tilefixture.mjs' import { V7_DDL } from './schema7fixture.mjs' const ALICE = 'ALIC-ALIC-ALIC-ALIC-ALIC' // The four walls the schema-7 fixture holds, as the proto-2 raw-switch create spells them. const AUTHORED_WALLS = [ { a: [0, 0], b: [4096, 0], flags: { note: 'north' } }, { a: [10, 10], b: [100, 10] }, { a: [100, 100], b: [400, 100], door: true, open: true, flags: { a: 1 } }, { a: [500, 500], b: [900, 500], door: true }, ] const canonical = value => { if (Array.isArray(value)) return value.map(canonical) if (value && typeof value === 'object') { return Object.fromEntries(Object.keys(value).sort().map(k => [k, canonical(value[k])])) } return value } function seededStateDir() { const stateDir = makeStateDir() const db = new DatabaseSync(`${stateDir}/state.db`) try { db.exec(V7_DDL) } finally { db.close() } return stateDir } async function stack(stateDir) { const gmKey = gmKeyOf(stateDir) const nginxPort = await freePort() refuseLivePort(nginxPort) const server = await startServer({ stateDir, publicOrigin: baseUrl(nginxPort) }) const nginx = await startTestNginx({ backendPort: server.port, stateDir, port: nginxPort }) return { stateDir, gmKey, server, nginx, origin: nginx.origin } } // Rebuild the migrated scene's walls through the wire, before any player connects. async function authorWalls(gm, scene) { const snap = await gm.ws.waitText('walls') for (const w of snap.walls) { assert.equal((await gm.ws.request({ t: 'wall.remove', scene, id: w.id })).ok, true) } for (const wall of AUTHORED_WALLS) { const ack = await gm.ws.request({ t: 'wall.add', scene, ...wall }) assert.equal(ack.ok, true, JSON.stringify(ack)) await gm.ws.waitFor(m => m.t === 'wall' && m.id === ack.id) } } // The wall whose drawn segment is the given one, however its id was assigned in this run. function wallBySegment(gm, a, b) { const from = gm.ws.messages.filter(m => m.t === 'wall' && m.a[0] === a[0] && m.a[1] === a[1] && m.b[0] === b[0] && m.b[1] === b[1]) if (from.length > 0) return from[from.length - 1] const snap = gm.ws.messages.filter(m => m.t === 'walls').pop() const hit = snap.walls.find(w => w.a[0] === a[0] && w.a[1] === a[1] && w.b[0] === b[0] && w.b[1] === b[1]) assert.ok(hit, `no wall from ${a} to ${b}`) return hit } const TOKEN = 'tok000000001' async function position(gm, x, y) { const before = gm.ws.messages.length const ack = await gm.ws.request({ t: 'token.move', id: TOKEN, pos: [x, y] }) assert.equal(ack.ok, true, JSON.stringify(ack)) await gm.ws.waitFor((m, i) => i >= before && m.t === 'token' && m.id === TOKEN && m.pos[0] === x && m.pos[1] === y) } // One scripted sequence, identical in both stacks except for how the migrated walls got there. async function runScript(ctx) { const gm = await openAs(ctx.origin, ctx.gmKey) await gm.ws.waitText('hello') const scene = (await gm.ws.waitText('walls')).scene if (ctx.author) await authorWalls(gm, scene) const door = wallBySegment(gm, [500, 500], [900, 500]) assert.equal(door.kind, 'door') // The player connects only after the wall set exists in both runs, so the compared sequence starts // from the same resync. const player = await openAs(ctx.origin, ALICE) await player.ws.waitText('hello') await player.ws.waitFor(m => m.t === 'explored' && m.full === true) // Start 20 px above the closed door, then attempt to cross it: the uniform move nack (5.5). await position(gm, 600, 480) let ack = await player.ws.request({ t: 'token.move', id: TOKEN, pos: [600, 520] }) assert.equal(ack.ok, false) assert.deepEqual(ack.pos, [600, 480]) ack = await player.ws.request({ t: 'token.move', id: TOKEN, pos: [600, 440] }) assert.equal(ack.ok, true) await player.ws.waitFor(m => m.t === 'token' && m.id === TOKEN && m.pos[1] === 440) // Open the door: the way is clear and the player crosses. assert.equal((await gm.ws.request({ t: 'door.set', scene, id: door.id, open: true })).ok, true) await gm.ws.waitFor(m => m.t === 'wall' && m.id === door.id && m.open === true) await position(player, 600, 520) assert.equal((await player.ws.request({ t: 'token.move', id: TOKEN, pos: [600, 440] })).ok, true) // Move clear, close the door, and the same crossing is refused again. await position(gm, 600, 560) assert.equal((await gm.ws.request({ t: 'door.set', scene, id: door.id, open: false })).ok, true) await gm.ws.waitFor(m => m.t === 'wall' && m.id === door.id && m.open === false) ack = await player.ws.request({ t: 'token.move', id: TOKEN, pos: [600, 480] }) assert.equal(ack.ok, false) // Two refusals of different shapes: over the step cap, and out of bounds. ack = await player.ws.request({ t: 'token.move', id: TOKEN, pos: [2000, 560] }) assert.equal(ack.ok, false) assert.deepEqual(ack.pos, [600, 560]) ack = await player.ws.request({ t: 'token.move', id: TOKEN, pos: [-10, 560] }) assert.equal(ack.ok, false) await new Promise(r => setTimeout(r, 80)) const frames = player.ws.messages.map(canonical) const cookie = player.cookie player.ws.close(); gm.ws.close() await player.ws.closed return { scene, frames, cookie } } // Every tile of the checked rectangle, as a status string; the oracle is the server's own explored // gate in both runs, so any difference is a real behaviour difference. async function tileStatuses(ctx, scene, cookie) { const statuses = [] for (let y = 0; y <= 31; y++) { for (let x = 0; x <= 31; x++) { const r = await fetch(`${ctx.origin}/tiles/${scene}/v1/${x}_${y}.png`, { headers: { Cookie: cookie } }) statuses.push(`${x},${y}:${r.status}`) await r.arrayBuffer() } } return statuses } test('behaviour preservation: a migrated schema-7 scene and the same scene authored via raw switches are byte-identical to a player', async () => { const migratedDir = seededStateDir() const authoredDir = seededStateDir() let migrated let authored try { migrated = await stack(migratedDir) authored = await stack(authoredDir) // Fixture tiles for both stacks: every tile the gate may allow has bytes behind the alias. for (const dir of [migratedDir, authoredDir]) { writeTileGrid(dir, { scene: 1, artVersion: 1, size: 32, x0: 0, x1: 31, y0: 0, y1: 31 }) } const a = await runScript({ ...migrated, author: false }) const b = await runScript({ ...authored, author: true }) assert.ok(a.frames.length > 20, 'the script produced real coverage') assert.deepEqual(b.frames, a.frames, 'the migrated scene and the same scene authored through wall.add must give identical player frames') const ta = await tileStatuses(migrated, a.scene, a.cookie) const tb = await tileStatuses(authored, b.scene, b.cookie) assert.deepEqual(tb, ta, 'per-tile statuses must be identical between the two scenes') assert.ok(ta.some(s => s.endsWith(':200')), 'the sweep allowed some tiles') assert.ok(ta.some(s => s.endsWith(':403')), 'the sweep refused some tiles') } finally { await stopTestNginx(authored?.nginx) await stopTestNginx(migrated?.nginx) await stopServer(authored?.server) await stopServer(migrated?.server) fs.rmSync(migratedDir, { recursive: true, force: true }) fs.rmSync(authoredDir, { recursive: true, force: true }) } })