A room whose ledger cannot be read is logged and left unloaded, not the server down; the server has tests of its own

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01Jm2auWk6RP71CjaAb4FMoG
This commit is contained in:
Eric WagonerandClaude Opus 5.5 committed 2026-09-29 12:13:11 -04:00
1 parent 2810076677
commit faa2845e70
5 files changed
+69 -6

No files matched your search

+46
View File
@@ -0,0 +1,46 @@
import { appendFileSync, mkdtempSync, rmSync } from 'node:fs';
import { tmpdir } from 'node:os';
import { join } from 'node:path';
import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest';
import { game, type Input, type State } from '../../src/lib/game';
import type { GameSpec } from '../../src/lib/game/spec';
import { Rooms } from './rooms';
import { Store } from './store';
let dir: string;
beforeEach(() => {
dir = mkdtempSync(join(tmpdir(), 'rooms-'));
});
afterEach(() => {
rmSync(dir, { recursive: true, force: true });
});
/** A table of the demo game: the host and a second person, the game begun. */
function table(spec: GameSpec<State, Input> = game) {
const rooms = new Rooms(spec, new Store(dir));
const host = rooms.create('Ann', 2);
const guest = rooms.join(host.room, 'Bo');
rooms.begin(host.room, host.token);
return { rooms, room: host.room, ann: host.token, bo: guest.token };
}
describe('rooms', () => {
it('resolves a round once every seat has moved, and refuses a move for a round already played', () => {
const { rooms, room, ann, bo } = table();
rooms.submit(room, ann, { pick: 5 }, 0);
expect(rooms.waitingOn(room)).toEqual(['B']);
rooms.submit(room, bo, { pick: 2 }, 0);
expect(room.resolved).toBe(1);
expect(() => rooms.submit(room, ann, { pick: 4 }, 0)).toThrow(/already been played/);
});
it('leaves a room with an unreadable ledger unloaded and opens the rest', () => {
const { room } = table();
appendFileSync(join(dir, 'ZZZZ.jsonl'), '{"t":"room","id":"ZZZZ","seats":2,"createdAt":1,"options":{}}\n{"t":"seat", torn');
const errors = vi.spyOn(console, 'error').mockImplementation(() => {});
const reopened = new Rooms(game, new Store(dir));
expect(errors).toHaveBeenCalledWith(expect.stringMatching(/ZZZZ: ledger unreadable/));
expect(reopened.get(room.id).state).not.toBeNull();
errors.mockRestore();
});
});