tools: raw WS client and the protocol test group (#18)
lib.mjs gains a node:net WebSocket client (frames, opcodes, masked sends, a close handshake that mirrors a browser) so the harness can send what the global WebSocket cannot: oversize, binary, unmasked, bad-opcode and garbage frames, and read Ping/Pong and the empty close frame. wsUrl/rawUpgrade now carry the required proto query. withserver.mjs gains the GM-server helpers (gm-key, withGmServer, signIn, openAs) that auth.test.mjs used to keep privately, so both harnesses share one copy. protocol.test.mjs is the protocol group of DESIGN 13 M3: hello/n, catalogue types, proto, unknown type and ignored fields, exactly one ack per reqId, unnamed player, malformed + binary frames, the flood close, the intent bucket, the 64 KB boundary, unmasked/bad opcode, server Ping and Pong, the connection cap, and closing-then-1005. auth.test.mjs now also asserts the closing reason on eviction and revocation.
This commit is contained in:
+56
-1
@@ -5,7 +5,7 @@ import { spawn, spawnSync } from 'node:child_process'
|
||||
import fs from 'node:fs'
|
||||
import os from 'node:os'
|
||||
import path from 'node:path'
|
||||
import { baseUrl, freePort, projectRoot, refuseLivePort, sleep } from './lib.mjs'
|
||||
import { baseUrl, freePort, join, openWs, projectRoot, refuseLivePort, sleep } from './lib.mjs'
|
||||
|
||||
const SERVER_BIN = path.join(projectRoot, 'run', 'test', 'server')
|
||||
|
||||
@@ -103,3 +103,58 @@ export async function withServer(fn, opts = {}) {
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// ---------------------------------------------------------------------------------------------
|
||||
// A server with the GM key already made (#17/#18 harnesses). Kept here, next to the lifecycle,
|
||||
// so the black-box test files share one copy.
|
||||
|
||||
const KEY_LINE = /GM key(?: \(new\))?: ([A-Z0-9-]+)/
|
||||
|
||||
// Runs `fff-vtt gm-key` against a state dir (the CLI is part of the product, so it is the source
|
||||
// of the GM key in every harness).
|
||||
export function runGmKey(stateDir, extra = []) {
|
||||
const r = spawnSync(buildServer(), ['gm-key', `--state-dir=${stateDir}`, ...extra], { encoding: 'utf8' })
|
||||
return { status: r.status, stdout: r.stdout, stderr: r.stderr }
|
||||
}
|
||||
|
||||
// The key line of a `gm-key` run: "GM key: XXXX-..." or "GM key (new): XXXX-...".
|
||||
export function parseGmKey(stdout) {
|
||||
const m = KEY_LINE.exec(stdout)
|
||||
if (!m) throw new Error(`no GM key in output: ${stdout}`)
|
||||
return m[1]
|
||||
}
|
||||
|
||||
// The GM key of a state dir, created on first call and stable afterwards.
|
||||
export function gmKeyOf(stateDir) {
|
||||
const r = runGmKey(stateDir)
|
||||
if (r.status !== 0) throw new Error(`gm-key failed: ${r.stderr}`)
|
||||
return parseGmKey(r.stdout)
|
||||
}
|
||||
|
||||
// A fresh state dir, its GM key and a running server; the dir is removed afterwards.
|
||||
export async function withGmServer(fn) {
|
||||
const stateDir = makeStateDir()
|
||||
const gmKey = gmKeyOf(stateDir)
|
||||
const s = await startServer({ stateDir })
|
||||
try {
|
||||
return await fn(s, gmKey, stateDir)
|
||||
} finally {
|
||||
await stopServer(s)
|
||||
fs.rmSync(stateDir, { recursive: true, force: true })
|
||||
}
|
||||
}
|
||||
|
||||
// POST /join with a key that must work, returning the cookie.
|
||||
export async function signIn(origin, key) {
|
||||
const r = await join(origin, key)
|
||||
if (r.status !== 303) throw new Error(`join with a valid key: ${r.status}`)
|
||||
if (!r.cookie) throw new Error('no session cookie')
|
||||
return r
|
||||
}
|
||||
|
||||
// {cookie, ws}: a signed-in WebSocket client of that key.
|
||||
export async function openAs(origin, key) {
|
||||
const { cookie } = await signIn(origin, key)
|
||||
const ws = await openWs(origin, cookie)
|
||||
return { cookie, ws }
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user