Files
noitu/web/tests/history-export.test.js
T
tiennm99 4e2e9e1433 feat(online)!: seat two to four players in a room
A room holds up to four people and needs two to start. Both numbers are
server constants sent to the client in RoomState, so the lobby draws
whatever the server allows and widening a room is a server change alone.

Failing a turn eliminates that player rather than ending the game. The
syllable and the used words survive them, the turn passes to whoever is
next, and the last player standing wins. Two seats is that same rule seen
from close up, which is why there is one implementation of it and not two.

A dead end still costs the first player to face it their own clock, as
before: they get their turn, and lose it. Everyone behind them has already
seen that board, so they go out together rather than each sitting out a turn
limit they cannot use — which leaves the player who closed the position
standing, the same outcome two players get.

A player who is knocked out keeps their seat. They watch the rest of the
game, chat included, with only the word input gone, and everybody lands back
in the same lobby when it ends. The result screen is the whole table, ranked
by who outlasted whom, with each score reported beside the place rather than
deciding it.

The turn clock is deliberately not paused for a seat that has dropped, so a
player who loses their connection on their own turn loses it the way anybody
else would. Their reconnect window decides only whether they are still in the
game afterwards. Any number of windows can be open at once, settled by one
timer armed for the nearest of them.

Starting waits for every guest, not merely the first: a room of four that
began on one yes would have dealt three people a turn they never agreed to.
Kicking names a seat and is still refused on a player who is ready, and on
the owner's own — leaving is what an owner who wants out does, and it hands
the room on. Joining stays a lobby thing: a room with a game running turns a
latecomer away even with seats going spare, because there is no way to hand
somebody a game already in progress.

BREAKING CHANGE: RoomState, TurnUpdate and GameOver lose the fields that
could only ever describe a second player, OpponentLeft is retired in favour
of presence on RoomState, and suggestions move to the new PlayerEliminated —
they describe the position that beat a player, which by the end of a longer
game is nobody else's position. ProtocolVersion goes to 2, so a client built
against 1 is refused with a readable error rather than decoding a frame that
now means something else.
2026-09-08 10:56:14 +07:00

100 lines
3.1 KiB
JavaScript

// The transcript is the only artefact of a game that outlives the tab, so what
// it contains is worth pinning: the chain in playing order, who played what,
// and a filename that survives every filesystem.
import { describe, expect, it } from 'vitest';
import { chainToText, historyFilename } from '../src/lib/history-export.js';
/**
* @param {Partial<import('../src/lib/stores/game.svelte.js').ChainEntry>} entry
*/
function link(entry) {
return {
word: '',
typed: '',
byMe: false,
playerId: '',
points: 0,
syllables: 2,
opening: false,
...entry
};
}
const chain = [
link({ word: 'học sinh', opening: true, syllables: 0 }),
link({ word: 'sinh viên', byMe: true, playerId: 'p1', points: 2 }),
link({ word: 'viên chức', playerId: 'p2', points: 2 })
];
/** Resolves a seat to the name that seat was playing under. */
const nameOf = (/** @type {string} */ id) => (id === 'p2' ? 'Minh' : '');
const at = new Date(2026, 8, 7, 15, 25);
describe('chainToText', () => {
it('keeps the chain in playing order, opening word first', () => {
const lines = chainToText({ chain, at }).trim().split('\n');
const words = lines.filter((line) => /^\d+\./.test(line));
expect(words).toHaveLength(3);
expect(words[0]).toContain('học sinh');
expect(words[1]).toContain('sinh viên');
expect(words[2]).toContain('viên chức');
});
it('marks the opening word as played by neither side', () => {
const text = chainToText({ chain, at });
expect(text).toContain('1. học sinh (từ mở đầu)');
expect(text).not.toContain('học sinh — ');
});
it('names who played each word and what it scored', () => {
const text = chainToText({ chain, at, nameOf });
expect(text).toContain('2. sinh viên — Bạn +2');
expect(text).toContain('3. viên chức — Minh +2');
});
it('falls back to a generic label for a seat whose name is gone', () => {
// A player who left takes their name with them, and the transcript is
// written long after. The line still has to say it was not this
// player's word.
const text = chainToText({ chain, at });
expect(text).toContain('3. viên chức — Đối thủ +2');
});
it('heads the file with the result when there is one', () => {
const text = chainToText({
chain,
at,
result: { iWon: true, myScore: 4, chainLength: 3 }
});
expect(text).toContain('2026-09-07 15:25');
expect(text).toContain('Bạn thắng!');
expect(text).toContain('Điểm cuối cùng: 4');
expect(text).toContain('Số từ trong chuỗi: 3');
});
it('writes a chain with no result at all', () => {
const text = chainToText({ chain: [], at });
expect(text).toContain('2026-09-07 15:25');
expect(text).not.toContain('Bạn thắng!');
expect(text).not.toContain('Bạn thua.');
});
});
describe('historyFilename', () => {
it('stamps the local date and time without a colon', () => {
expect(historyFilename(at)).toBe('noi-tu-2026-09-07-1525.txt');
});
it('pads every field so names sort chronologically', () => {
expect(historyFilename(new Date(2026, 0, 2, 3, 4))).toBe('noi-tu-2026-01-02-0304.txt');
});
});