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:
2026-11-15 08:33:12 +01:00
parent 88dd27e2cc
commit 5e43ffa545
4 changed files with 630 additions and 55 deletions
+19 -50
View File
@@ -3,55 +3,10 @@
import test from 'node:test'
import assert from 'node:assert/strict'
import fs from 'node:fs'
import { spawnSync } from 'node:child_process'
import { buildServer, makeStateDir, startServer, stopServer } from './withserver.mjs'
import { join, openWs, rawUpgrade } from './lib.mjs'
import { gmKeyOf, makeStateDir, parseGmKey, runGmKey, startServer, stopServer,
withGmServer, signIn, openAs } from './withserver.mjs'
import { createPlayer, join, openWs, rawUpgrade } from './lib.mjs'
const KEY_LINE = /GM key(?: \(new\))?: ([A-Z0-9-]+)/
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 }
}
function gmKeyOf(stateDir) {
const r = runGmKey(stateDir)
assert.equal(r.status, 0, `gm-key failed: ${r.stderr}`)
const m = KEY_LINE.exec(r.stdout)
assert.ok(m, `no GM key in output: ${r.stdout}`)
return m[1]
}
async function withGmServer(fn) {
const stateDir = makeStateDir()
const gmKey = gmKeyOf(stateDir)
const s = await startServer({ stateDir })
try {
await fn(s, gmKey, stateDir)
} finally {
await stopServer(s)
fs.rmSync(stateDir, { recursive: true, force: true })
}
}
async function signIn(origin, key) {
const r = await join(origin, key)
assert.equal(r.status, 303, `join with a valid key: ${r.status}`)
assert.ok(r.cookie, 'no session cookie')
return r
}
async function createPlayer(gm) {
const ack = await gm.request({ t: 'key.create' })
assert.equal(ack.ok, true, JSON.stringify(ack))
return { id: ack.id, key: ack.key }
}
async function openAs(origin, key) {
const { cookie } = await signIn(origin, key)
const ws = await openWs(origin, cookie)
return { cookie, ws }
}
test('POST /join: cookie flags, and every refusal is a bare status', async () => {
await withGmServer(async (s, gmKey) => {
@@ -123,7 +78,13 @@ test('one key on several devices: two cookies, one player, one per-player cap',
// the fifth connection of the same player (device B) evicts the oldest (wsA)
const fifth = await openWs(s.origin, b.cookie)
const evicted = await wsA.closed
assert.equal(evicted.code, 1005) // mummy 0.4.8 sends an empty close frame (see DESIGN 3.2 note)
assert.equal(evicted.code, 1005) // mummy 0.4.8 sends an empty close frame (DESIGN 6.5, #18)
// ... so the reason rides the last app message before the close (FIXED, #18)
const last = wsA.messages[wsA.messages.length - 1]
assert.equal(last.t, 'closing')
assert.equal(last.reason, 'evicted')
assert.equal(last.code, 4002)
assert.equal(typeof last.n, 'number')
assert.equal(fifth.closeInfo, null)
assert.equal(wsB.closeInfo, null)
for (const w of [wsB, ...extra, fifth]) w.close()
@@ -162,6 +123,13 @@ test('key.replace: old key and old sessions die, live connections close, the new
// both live connections of that player are closed, the GM stays
await first.ws.closed
await second.ws.closed
for (const ws of [first.ws, second.ws]) {
const last = ws.messages[ws.messages.length - 1]
assert.equal(last.t, 'closing')
assert.equal(last.reason, 'revoked')
assert.equal(last.code, 4001)
assert.equal(ws.closeInfo.code, 1005) // the close frame itself carries no code
}
assert.equal(gm.ws.closeInfo, null)
// the old key and the old cookies are dead; the new key signs in
assert.equal((await join(s.origin, player.key)).status, 401)
@@ -181,6 +149,7 @@ test('player.remove: key, sessions and connections gone; slot freed; the GM is n
const ack = await gm.ws.request({ t: 'player.remove', id: player.id })
assert.equal(ack.ok, true)
await session.ws.closed
assert.equal(session.ws.messages[session.ws.messages.length - 1].reason, 'revoked')
assert.equal((await join(s.origin, player.key)).status, 401)
assert.equal((await rawUpgrade(s.origin, { cookie: session.cookie })).status, 401)
assert.equal((await gm.ws.request({ t: 'player.remove', id: player.id })).code, 'not-found')
@@ -236,7 +205,7 @@ test('gm-key: same key on re-run (also while the server runs); --new refuses whi
const rotated = runGmKey(stateDir, ['--new'])
assert.equal(rotated.status, 0, rotated.stderr)
const key2 = KEY_LINE.exec(rotated.stdout)[1]
const key2 = parseGmKey(rotated.stdout)
assert.notEqual(key2, key1)
assert.equal(gmKeyOf(stateDir), key2)