Files
hnefatafl/server/src/rooms.test.ts
T

179 lines
8.6 KiB
TypeScript

import { mkdtempSync, rmSync } from 'node:fs';
import { tmpdir } from 'node:os';
import { join } from 'node:path';
import { afterEach, beforeEach, describe, expect, it } from 'vitest';
import { game } from '../../src/lib/game';
import { Rooms } from './rooms';
import { Store } from './store';
import { SPECTATOR } from '../../src/lib/game/spec';
let dir: string;
beforeEach(() => {
dir = mkdtempSync(join(tmpdir(), 'rooms-'));
});
afterEach(() => {
rmSync(dir, { recursive: true, force: true });
});
/** A table of two: Ann, who opened it, and Bo, the game begun. */
function table() {
const rooms = new Rooms(game, 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("puts the keeper's word in the table's talk though the gallery may not talk, and a restart keeps it", () => {
const { rooms, room } = table();
expect(room.galleryTalk).toBe(false);
rooms.keeperSays(room, 'I came too late for the seat; call me again.');
const said = { id: SPECTATOR, name: 'the keeper', keeper: true, text: 'I came too late for the seat; call me again.' };
expect(room.chat.at(-1)).toMatchObject(said);
expect(new Rooms(game, new Store(dir)).get(room.id).chat.at(-1)).toMatchObject(said);
});
it("gives a seat a new token at the keeper's word: the old one opens nothing, and a restart keeps the change", () => {
const { rooms, room, bo } = table();
const phrase = rooms.transfer(room, bo).phrase;
const { seat, token } = rooms.reseat(room, 'B');
expect(seat.name).toBe('Bo');
expect(rooms.seatOf(room, token).id).toBe('B');
expect(() => rooms.seatOf(room, bo)).toThrow(/opens no seat/);
// A phrase minted with the old token carries nothing now.
expect(() => rooms.claim(phrase)).toThrow(/unknown or has expired/);
const reopened = new Rooms(game, new Store(dir)).get(room.id);
expect(rooms.seatOf(reopened, token).id).toBe('B');
expect(() => rooms.seatOf(reopened, bo)).toThrow(/opens no seat/);
});
it('re-seats only a person, and says when each person last acted', () => {
const { rooms, room, ann, bo } = table();
expect(() => rooms.reseat(room, 'Z')).toThrow(/No person sits/);
const mover = rooms.waitingOn(room)[0];
rooms.submit(room, mover === 'A' ? ann : bo, game.botInput(room.state!, mover), 0);
const seats = rooms.seatsForKeeper(room);
expect(seats.map((s) => [s.id, s.name])).toEqual([['A', 'Ann'], ['B', 'Bo']]);
expect(seats.find((s) => s.id === mover)!.lastActed).toBeGreaterThan(0);
expect(seats.find((s) => s.id !== mover)!.lastActed).toBeNull();
});
});
describe('the host and the other seat', () => {
it("gives a person's seat to a housecarl, who plays it whenever it owes the move, and the person's token opens nothing", () => {
const { rooms, room, ann, bo } = table();
rooms.standIn(room, ann, 'B');
expect(() => rooms.seatOf(room, bo)).toThrow(/gave your seat to a housecarl/);
expect(rooms.view(room, 'A').seats[1]).toEqual({ id: 'B', name: 'Bo', bot: true, standIn: true });
// Whoever moved first, the housecarl has answered, and the move is Ann's.
expect(rooms.waitingOn(room)).toEqual(['A']);
const before = room.resolved;
rooms.submit(room, ann, game.botInput(room.state!, 'A'), room.resolved);
expect(room.resolved).toBe(before + 2);
expect(rooms.waitingOn(room)).toEqual(['A']);
});
it("is the host's call alone, in a game under way, for the other person", () => {
const { rooms, room, ann, bo } = table();
expect(() => rooms.standIn(room, bo, 'A')).toThrow(/Only the player who opened/);
expect(() => rooms.standIn(room, ann, 'A')).toThrow(/keeps their own seat/);
expect(() => rooms.standIn(room, ann, 'Z')).toThrow(/No person sits/);
const lobby = rooms.create('Ann', 2);
rooms.join(lobby.room, 'Bo');
expect(() => rooms.standIn(lobby.room, lobby.token, 'B')).toThrow(/under way/);
});
it("gives the seat back by the host's phrase, with a new token, and a restart keeps both changes", () => {
const { rooms, room, ann, bo } = table();
rooms.standIn(room, ann, 'B');
expect(new Rooms(game, new Store(dir)).get(room.id).seats[1]).toMatchObject({ bot: true, standIn: true });
const claimed = rooms.claim(rooms.hostPhrase(room, ann, 'B').phrase);
expect(claimed.seat).toBe('B');
expect(rooms.seatOf(room, claimed.token).name).toBe('Bo');
expect(() => rooms.seatOf(room, bo)).toThrow(/opens no seat/);
const reopened = new Rooms(game, new Store(dir)).get(room.id);
expect(reopened.seats[1]).toEqual({ id: 'B', name: 'Bo', tokenHash: room.seats[1].tokenHash, bot: false });
});
it("re-seats a person whose browser lost the seat, by the host's phrase, once, and only the host makes one", () => {
const { rooms, room, ann, bo } = table();
expect(() => rooms.hostPhrase(room, bo, 'A')).toThrow(/Only the player who opened/);
expect(() => rooms.hostPhrase(room, ann, 'A')).toThrow(/No other person/);
const { phrase } = rooms.hostPhrase(room, ann, 'B');
const claimed = rooms.claim(phrase);
expect(claimed.token).not.toBe(bo);
expect(() => rooms.seatOf(room, bo)).toThrow(/opens no seat/);
expect(() => rooms.claim(phrase)).toThrow(/unknown or has expired/);
});
it("voids a phrase for the seat once a housecarl stands in", () => {
const { rooms, room, ann } = table();
const { phrase } = rooms.hostPhrase(room, ann, 'B');
rooms.standIn(room, ann, 'B');
expect(() => rooms.claim(phrase)).toThrow(/unknown or has expired/);
});
});
describe('a seat a browser lost', () => {
it('notes the address a seat is used from, once per change, without waking the table, and a restart keeps it', () => {
const { rooms, room, bo } = table();
const seq = room.seq;
rooms.seen(room, bo, 'home');
rooms.seen(room, bo, 'home');
expect(room.seats.find((s) => s.id === 'B')?.addr).toBe('home');
expect(room.seq).toBe(seq);
rooms.seen(room, 'no-such-token', 'elsewhere');
// A seat open on two networks at once: the second address is not written on its heels.
rooms.seen(room, bo, 'office');
expect(new Store(dir).read(room.id).filter((l) => l.t === 'seen')).toHaveLength(1);
expect(new Rooms(game, new Store(dir)).get(room.id).seats.find((s) => s.id === 'B')?.addr).toBe('home');
});
it('gives the seat back, under its name, to the address it was last used from, with a new token', () => {
const { rooms, room, bo } = table();
rooms.seen(room, bo, 'home');
const found = rooms.findSeats('bo', 'home');
expect(found.elsewhere).toBe(0);
expect(found.seated).toHaveLength(1);
const { roomId, seat, token } = found.seated[0];
expect([roomId, seat]).toEqual([room.id, 'B']);
expect(rooms.seatOf(room, token).name).toBe('Bo');
expect(() => rooms.seatOf(room, bo)).toThrow(/opens no seat/);
});
it('only counts the table when the address differs, or another person at it shares the address, or a bot stands in', async () => {
const { rooms, room, ann, bo } = table();
rooms.seen(room, bo, 'home');
expect(rooms.findSeats('Bo', 'away')).toEqual({ seated: [], elsewhere: 1 });
// Ann and Bo play from one address: a name alone cannot tell them apart, so neither is re-seated by it.
rooms.seen(room, ann, 'home');
expect(rooms.findSeats('Bo', 'home')).toEqual({ seated: [], elsewhere: 1 });
expect(rooms.findSeats('Dee', 'home')).toEqual({ seated: [], elsewhere: 0 });
expect(rooms.seatOf(room, bo).id).toBe('B');
// The host gave Bo's seat to a bot: it is the host's phrase that gives it back, not Bo's name.
rooms.seen(room, ann, 'office');
rooms.standIn(room, ann, 'B');
await new Promise<void>((r) => setImmediate(r));
expect(rooms.findSeats('Bo', 'home')).toEqual({ seated: [], elsewhere: 1 });
expect(rooms.tablesForKeeper('bo')).toEqual([{ roomId: room.id, seat: 'B', standIn: true, players: ['Ann'], updatedAt: room.updatedAt }]);
expect(rooms.tablesForKeeper('Dee')).toEqual([]);
});
it('looks only at the table named when one is, and passes over a table that is over', () => {
const { rooms, room, bo } = table();
rooms.seen(room, bo, 'home');
const other = rooms.create('Bo', 2);
rooms.seen(other.room, other.token, 'home');
const first = rooms.findSeats('Bo', 'home', other.room.id).seated;
expect(first.map((s) => s.roomId)).toEqual([other.room.id]);
// A seat given back is noted afresh from its next use; until then its old address claims nothing.
const second = rooms.findSeats('Bo', 'home').seated;
expect(second.map((s) => s.roomId)).toEqual([room.id]);
rooms.seen(other.room, first[0].token, 'home');
rooms.seen(room, second[0].token, 'home');
room.over = true;
expect(rooms.findSeats('Bo', 'home').seated.map((s) => s.roomId)).toEqual([other.room.id]);
});
});