Files
FFF_vtt_frozen/client/e2e/stack.mjs
T
SirStone 3c0258ea31 feat: client shell, sign-in and protocol client (#67)
New `client/` package (Vite + TypeScript + PixiJS v8, plain DOM overlay):
the pure reducer seam (no Pixi import), an append-only slice registry
build-time glob, the `proto` 2 WS client with envelope/ack/closing mapping,
the sign-in door (`POST /join`, one "Key not recognised." for wrong and
unknown), the DOM shell/HUD and a minimal Pixi stage created only after a
live `hello`. `hello` resets every slice so a reconnect cannot show stale
state; `proto-mismatch` shows reload; `revoked` returns to the sign-in page
and stops.

The browser e2e moves into the client package (`client/e2e/`, owning
playwright-core) and runs the real Vite build through the TLS/HTTP2 test
nginx: join -> canvas -> hello/roster for GM and player, no data before
join, identical wrong/unknown key refusal, proto mismatch, revoked.
`devbox run client-test` adds the Vitest seam; `devbox run e2e` builds the
client first. The #66 stub dist stays local to `tools/vhost.test.mjs`, so
`devbox run test` still needs no npm install.

Refs #67
2026-11-20 23:57:16 +01:00

97 lines
3.4 KiB
JavaScript

// Shared stack for the client e2e (TESTING 10.3): the real client build served by the TLS/HTTP2
// test nginx in front of the real server binary. Never a fixture, never a mocked protocol.
import assert from 'node:assert/strict'
import fs from 'node:fs'
import path from 'node:path'
import { chromium } from 'playwright-core'
import { createPlayer, freePort, httpsBaseUrl, namePlayer, projectRoot, sleep } from '../../tools/lib.mjs'
import { gmKeyOf, makeStateDir, openAs, startServer, stopServer } from '../../tools/withserver.mjs'
import { startTestNginx, stopTestNginx } from '../../tools/nginx.mjs'
export const CHROMIUM = process.env.CHROMIUM ?? 'chromium'
export const LAUNCH_ARGS = [
'--no-sandbox',
'--use-angle=swiftshader',
'--enable-unsafe-swiftshader',
'--ignore-gpu-blocklist',
]
export const CLIENT_DIST = path.join(projectRoot, 'client', 'dist')
export function assertClientBuild() {
assert.ok(
fs.existsSync(path.join(CLIENT_DIST, 'index.html')),
`client build missing at ${CLIENT_DIST}: run \`npm run build\` in client/ (client/e2e/run.sh does)`,
)
}
export async function withStack(fn) {
assertClientBuild()
const stateDir = makeStateDir()
const gmKey = gmKeyOf(stateDir)
const nginxPort = await freePort()
const server = await startServer({ stateDir, publicOrigin: httpsBaseUrl(nginxPort) })
let nginx
try {
nginx = await startTestNginx({
backendPort: server.port,
stateDir,
port: nginxPort,
distDir: CLIENT_DIST,
})
return await fn({ stateDir, server, nginx, origin: nginx.origin, gmKey })
} finally {
if (nginx) await stopTestNginx(nginx)
await stopServer(server)
fs.rmSync(stateDir, { recursive: true, force: true })
}
}
export async function launchBrowser() {
return chromium.launch({ executablePath: CHROMIUM, args: LAUNCH_ARGS })
}
export async function newPage(browser) {
// The certificate is generated per run by tools/tlsdev.mjs; trusting it in the browser is the
// one concession, the rest of the page is the real https/h2 path.
const context = await browser.newContext({ ignoreHTTPSErrors: true, viewport: { width: 1000, height: 700 } })
return { context, page: await context.newPage() }
}
/** A named player created the way the product creates one: a GM key.create, then player.setname. */
export async function makePlayer(origin, gmKey, name) {
const gm = await openAs(origin, gmKey)
await gm.ws.waitText('hello')
const created = await createPlayer(gm.ws)
await namePlayer(origin, created.key, name)
gm.ws.close()
await gm.ws.closed
return { id: created.id, key: created.key, name }
}
export async function signIn(page, key) {
await page.fill('#key', key)
await page.click('#signin-submit')
}
export async function waitForVisible(page, selector, timeout = 20000) {
await page.locator(selector).waitFor({ state: 'visible', timeout })
}
export async function waitForHidden(page, selector, timeout = 20000) {
await page.locator(selector).waitFor({ state: 'hidden', timeout })
}
export async function waitForText(page, selector, pattern, timeout = 20000) {
const deadline = Date.now() + timeout
let last = ''
while (Date.now() < deadline) {
last = (await page.locator(selector).textContent()) ?? ''
if (pattern.test(last)) return last
await sleep(50)
}
throw new Error(`${selector} never matched ${pattern}: last was ${JSON.stringify(last)}`)
}
export { sleep }