The test nginx is now the production shape of DESIGN 11.1 and the browser gate's real path (#66, spec #63 decision 6): TLS-only listener with http2 on, a self-signed certificate generated per run (tools/tlsdev.mjs, added to the process's default CA list so verification stays on), the client dist at / with SPA fallback, index no-cache and a year-immutable /assets/ cache, and the tile gate's internal /_tiles/ still unreachable. Existing tile/doors/oneway/wallbehaviour harnesses point their server's public origin at the https vhost, so they prove the same listener the browser uses. tools/vhost.test.mjs is the independent node:http2 client (cert SANs, ALPN h2, cache headers, SPA fallback, direct /_tiles/ 404); e2e/h2.test.mjs drives devbox chromium via playwright-core and asserts the page reports h2 (`devbox run e2e`, npm deps outside tools/). Root devbox gains chromium + openssl. Harness builds use a worktree-local nimcache so parallel worktrees stop corrupting each other's objects. Refs #66
203 lines
9.4 KiB
JavaScript
203 lines
9.4 KiB
JavaScript
// 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 { freePort, httpsBaseUrl, 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. Two of
|
|
// them (the north wall and the closed door) carry non-default but inert geometry: thickness 256,
|
|
// placement left, height 1. The migrated fixture has the defaults (0/center/null), so the
|
|
// byte-identical comparison below is also the proof that a stored thickness, placement or height
|
|
// changes no sight, movement, status or tile (stories 29/30, #52).
|
|
const INERT_GEOMETRY = { thickness: 256, position: 'left', height: 1 }
|
|
const AUTHORED_WALLS = [
|
|
{ a: [0, 0], b: [4096, 0], flags: { note: 'north' }, ...INERT_GEOMETRY },
|
|
{ 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, ...INERT_GEOMETRY },
|
|
]
|
|
|
|
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: httpsBaseUrl(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')
|
|
if (ctx.author) {
|
|
// The authored stack really stored the non-default geometry (the migrated one keeps defaults),
|
|
// so the byte-identical player view below cannot pass by both runs having ignored the fields.
|
|
const north = wallBySegment(gm, [0, 0], [4096, 0])
|
|
for (const [label, wall] of [['closed door', door], ['north wall', north]]) {
|
|
assert.deepEqual(
|
|
{ thickness: wall.thickness, position: wall.position, height: wall.height },
|
|
INERT_GEOMETRY, `${label}: the non-default geometry is stored and echoed on the GM frame`)
|
|
}
|
|
}
|
|
|
|
// 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 })
|
|
}
|
|
})
|