feat(m4): playtest friction: perf overlay, fresh import, token seed (#82)
Opt-in ?perf=1 overlay (fps, frame p50/p95, JS p50/p95, tokens, zoom; no node or ticker work when off). fff-vtt import creates and migrates a fresh/empty state dir through the shared server startup path; devbox run serve gains --image. devbox run seed creates deterministic labelled NPC tokens over the real WS (idempotent rerun, --replace), and the GM token form defaults X/Y to the viewport centre.
This commit is contained in:
@@ -0,0 +1,98 @@
|
||||
// #82: `serve --image` is an end-to-end smoke of the playtest command: import runs first into a
|
||||
// fresh state dir, then the real server + TLS/HTTP2 nginx start and the GM sees the imported
|
||||
// scene. The internal probe is a child node with NODE_EXTRA_CA_CERTS set to the generated cert.
|
||||
import test from 'node:test'
|
||||
import assert from 'node:assert/strict'
|
||||
import fs from 'node:fs'
|
||||
import path from 'node:path'
|
||||
import { spawn, spawnSync } from 'node:child_process'
|
||||
import { freePort, projectRoot, sleep } from './lib.mjs'
|
||||
import { buildServer, makeStateDir } from './withserver.mjs'
|
||||
import { pngBytesWH } from './tilefixture.mjs'
|
||||
|
||||
const PROBE = `
|
||||
import { openAs } from './tools/withserver.mjs'
|
||||
const gm = await openAs(process.env.PROBE_URL, process.env.PROBE_KEY)
|
||||
const hello = await gm.ws.waitText('hello')
|
||||
process.stdout.write(JSON.stringify(hello.scene) + '\\n')
|
||||
gm.ws.close()
|
||||
process.exit(0)
|
||||
`
|
||||
|
||||
function waitFor(child, pattern, timeoutMs = 60000) {
|
||||
return new Promise((resolve, reject) => {
|
||||
let out = ''
|
||||
let err = ''
|
||||
const timer = setTimeout(() => {
|
||||
cleanup()
|
||||
reject(new Error(`timed out waiting for ${pattern}\n--- stdout ---\n${out}\n--- stderr ---\n${err}`))
|
||||
}, timeoutMs)
|
||||
const onData = chunk => {
|
||||
out += chunk
|
||||
const match = pattern.exec(out)
|
||||
if (match) { cleanup(); resolve({ match, out }) }
|
||||
}
|
||||
const onErr = chunk => { err += chunk }
|
||||
const onExit = code => {
|
||||
cleanup()
|
||||
reject(new Error(`serve exited (code=${code}) before ${pattern}\n--- stdout ---\n${out}\n--- stderr ---\n${err}`))
|
||||
}
|
||||
function cleanup() {
|
||||
clearTimeout(timer)
|
||||
child.stdout.off('data', onData)
|
||||
child.stderr.off('data', onErr)
|
||||
child.off('exit', onExit)
|
||||
}
|
||||
child.stdout.on('data', onData)
|
||||
child.stderr.on('data', onErr)
|
||||
child.on('exit', onExit)
|
||||
})
|
||||
}
|
||||
|
||||
test('serve --image: imports into a fresh dir, then serves the scene (#82)', { timeout: 120000 },
|
||||
async () => {
|
||||
const root = makeStateDir()
|
||||
const stateDir = path.join(root, 'lan')
|
||||
const image = path.join(root, 'map.png')
|
||||
fs.writeFileSync(image, pngBytesWH(256, 192, (x, y) => [x & 0xff, y & 0xff, 128]))
|
||||
const port = await freePort()
|
||||
const bin = buildServer()
|
||||
const child = spawn(process.execPath, [
|
||||
path.join(projectRoot, 'tools', 'serve.mjs'),
|
||||
'--host=127.0.0.1', `--port=${port}`, `--state-dir=${stateDir}`,
|
||||
`--image=${image}`, '--tile-size=64', `--server-bin=${bin}`,
|
||||
], { cwd: projectRoot, stdio: ['ignore', 'pipe', 'pipe'] })
|
||||
try {
|
||||
const { out } = await waitFor(child, /GM key:\s+([A-Z0-9-]+)/)
|
||||
assert.match(out, /importing .*map\.png before starting the server/)
|
||||
assert.match(out, /URL:\s+https:\/\/127\.0\.0\.1:\d+/)
|
||||
assert.equal(fs.readdirSync(path.join(stateDir, 'tiles', '1', 'v1')).filter(n => n.endsWith('.png')).length,
|
||||
12, 'the import ran before the server and published v1 tiles')
|
||||
const url = /URL:\s+(https:\/\/\S+)/.exec(out)[1]
|
||||
const key = /GM key:\s+([A-Z0-9-]+)/.exec(out)[1]
|
||||
|
||||
const probe = spawnSync(process.execPath, ['--input-type=module', '-e', PROBE], {
|
||||
cwd: projectRoot,
|
||||
encoding: 'utf8',
|
||||
env: {
|
||||
...process.env,
|
||||
NODE_EXTRA_CA_CERTS: path.join(stateDir, 'tls', 'cert.pem'),
|
||||
PROBE_URL: url,
|
||||
PROBE_KEY: key,
|
||||
},
|
||||
})
|
||||
assert.equal(probe.status, 0, probe.stderr)
|
||||
const scene = JSON.parse(probe.stdout.trim())
|
||||
assert.equal(scene.w, 256)
|
||||
assert.equal(scene.h, 192)
|
||||
assert.equal(scene.tile_size, 64)
|
||||
} finally {
|
||||
child.kill('SIGINT')
|
||||
const exited = await Promise.race([
|
||||
new Promise(resolve => child.once('exit', code => resolve(code))),
|
||||
sleep(15000).then(() => 'timeout'),
|
||||
])
|
||||
assert.equal(exited, 0, 'SIGINT stops nginx and the server')
|
||||
fs.rmSync(root, { recursive: true, force: true })
|
||||
}
|
||||
})
|
||||
Reference in New Issue
Block a user