Files
station-master/test/remote-session.test.ts
T
Jesse.MarkowitzandClaude Fable 5.1 e47cd3d400 v0.8.4 — the multiplayer transport: server and browser
The second release from the audit. Every fault here was invisible in solitaire, and four of
the five server faults were in the one file no test had ever stood up; `http.ts` now has an
end-to-end suite on a real port. CHANGELOG has the reasoning.

SERVER. Leaving a lobby freed the chair and kept the token, so a leaver could stream and
move for whoever took the seat next — revoked now, in memory and on disk. The browser
numbered intents from 1 per page load while the server remembered the seat's last number,
so the first move after a reload was swallowed as a resend — the connect push carries the
count and the client continues from it. Nothing serialised moves within a game and every
write shared one `.tmp` name, so two moves at once tore `game.json` (measured: 6 of 200),
and the boot's bare `JSON.parse` then took every game down — per-path write queues, a
per-game move queue, and a boot that skips one bad file. An error after the SSE head was
sent crashed the process. Bodies were unbounded before any secret check.

BROWSER. A double-click did the thing twice: one submit in flight at a time. A failed
submit is `false`, not an unhandled rejection. The documentation renderer flattened nested
bullets into a literal "- " mid-sentence on the published home-deck page. The make-up panel
promised cars the engine refuses; it asks `acceptsCar` now.

Co-Authored-By: Claude Fable 5.1 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01FrCWubm9GAftYCm2hWdKwK
2026-09-29 17:02:32 -04:00

82 lines
3.4 KiB
TypeScript

/**
* The browser's half of the multiplayer transport, driven with a fake `EventSource` and `fetch`.
* `createRemoteSession` is pure otherwise — no DOM — so it runs here as it does in the page.
*/
import { describe, it } from 'node:test';
import assert from 'node:assert/strict';
import { createRemoteSession } from '../src/web/session.ts';
type Fake = { onmessage: ((ev: { data: string }) => void) | null; emit(data: unknown): void; close(): void };
function fakeTransport(): { source: () => Fake; bodies: () => { seq: number }[]; fail: (on: boolean) => void } {
let last: Fake | null = null;
const bodies: { seq: number }[] = [];
let failing = false;
const g = globalThis as unknown as Record<string, unknown>;
g['EventSource'] = class {
onmessage: ((ev: { data: string }) => void) | null = null;
onerror: (() => void) | null = null;
constructor() {
last = this;
}
emit(data: unknown): void {
this.onmessage?.({ data: JSON.stringify(data) });
}
close(): void {}
};
g['fetch'] = async (_url: string, init?: { body?: string }) => {
if (init?.body) bodies.push(JSON.parse(init.body) as { seq: number });
await new Promise((r) => setTimeout(r, 5));
if (failing) throw new Error('network down');
return { ok: true, status: 200, json: async () => ({ ok: true }) };
};
return { source: () => last!, bodies: () => bodies, fail: (on) => (failing = on) };
}
const connectPush = (lastSeq: number): unknown => ({ frame: null, menu: null, lines: [], lastSeq });
describe('the remote session (v0.8.4)', () => {
it('continues the intent count from where the server says, not from 1', async () => {
const t = fakeTransport();
const s = createRemoteSession('tok', 0);
t.source().emit(connectPush(7));
await s.submit({ type: 'draw.end' });
assert.deepEqual(t.bodies().map((b) => b.seq), [8], 'the first intent after a reload re-used a number the server had already applied');
// A later reconnect never moves the count backwards.
t.source().emit(connectPush(3));
await s.submit({ type: 'draw.end' });
assert.deepEqual(t.bodies().map((b) => b.seq), [8, 9]);
});
it('drops a second submit while the first is still in flight', async () => {
const t = fakeTransport();
const s = createRemoteSession('tok', 0);
t.source().emit(connectPush(0));
const [a, b] = await Promise.all([s.submit({ type: 'draw.end' }), s.submit({ type: 'draw.end' })]);
assert.equal(a, true);
assert.equal(b, false, 'a double-click posted twice');
assert.equal(t.bodies().length, 1, 'two intents went over the wire for one click');
// And the next one, after the round trip, goes through as normal.
assert.equal(await s.submit({ type: 'draw.end' }), true);
assert.equal(t.bodies().length, 2);
});
it('answers false, not an unhandled rejection, when the network fails', async () => {
const t = fakeTransport();
const s = createRemoteSession('tok', 0);
t.source().emit(connectPush(0));
t.fail(true);
assert.equal(await s.submit({ type: 'draw.end' }), false);
t.fail(false);
assert.equal(await s.submit({ type: 'draw.end' }), true, 'the session did not recover after a failed submit');
});
it('drops a push that is not JSON instead of throwing out of the handler', () => {
const t = fakeTransport();
createRemoteSession('tok', 0);
assert.doesNotThrow(() => t.source().onmessage?.({ data: '{not json' }));
});
});