mirror of
https://github.com/tiennm99/noitu.git
synced 2026-10-05 22:14:27 +00:00
Create a room and share a six-character code or an invite link; join by either. The waiting room shows the code until an opponent arrives. During a game the opponent's disconnection raises a banner with the grace countdown, and their return clears it. When the game ends, the rematch prompt appears only while an offer is actually open, so there is never a button that can only produce an error. The board is now one component both modes render, which is what makes online play the same screen as the bot game rather than a second implementation of it. Room codes are normalized by stripping separators and reading the code out of a pasted invite link, and nothing else. Filtering out every character outside the alphabet would turn any sentence into six plausible letters and send the player to a room that was never theirs. The socket now opens when the player asks for a room, not on arrival: the handshake carries the nickname once, and a socket opened before they typed it introduced them under whatever was stored beforehand. A tab that already holds a session reconnects immediately instead, so refreshing mid-game returns to the game. A resume the server cannot honour is dropped quietly rather than opening the lobby with an error the player did nothing to cause. The client also notices a socket that fails without closing. Nothing did before, so the page kept showing a live connection and a running countdown over a socket nothing could reach. A tick that fired late is ignored, because a backgrounded tab has its timers throttled and would otherwise close a healthy connection every time the player switched away.
96 lines
3.3 KiB
JavaScript
96 lines
3.3 KiB
JavaScript
// Room codes cross a gap the type system cannot see: the server generates them
|
|
// from a Go constant and the client validates them against a JavaScript copy.
|
|
// The first test here reads the Go source so the copy cannot quietly drift.
|
|
|
|
import { readFileSync } from 'node:fs';
|
|
import { fileURLToPath } from 'node:url';
|
|
import { describe, expect, it } from 'vitest';
|
|
import {
|
|
ROOM_CODE_ALPHABET,
|
|
ROOM_CODE_LENGTH,
|
|
isRoomCode,
|
|
normalizeRoomCode
|
|
} from '../src/lib/room-code.js';
|
|
|
|
const hubSource = fileURLToPath(new URL('../../server/internal/wsapi/hub.go', import.meta.url));
|
|
|
|
describe('the alphabet', () => {
|
|
const go = readFileSync(hubSource, 'utf8');
|
|
|
|
it('matches the one the server generates codes from', () => {
|
|
const match = go.match(/roomCodeAlphabet\s*=\s*"([^"]+)"/);
|
|
expect(match, 'roomCodeAlphabet not found in hub.go').not.toBeNull();
|
|
expect(ROOM_CODE_ALPHABET).toBe(match?.[1]);
|
|
});
|
|
|
|
it('matches the length the server generates', () => {
|
|
const match = go.match(/roomCodeLen\s*=\s*(\d+)/);
|
|
expect(match, 'roomCodeLen not found in hub.go').not.toBeNull();
|
|
expect(ROOM_CODE_LENGTH).toBe(Number(match?.[1]));
|
|
});
|
|
|
|
it('omits the characters people confuse when reading a code aloud', () => {
|
|
for (const ch of ['0', 'O', '1', 'I', 'L']) {
|
|
expect(ROOM_CODE_ALPHABET.includes(ch), `${ch} should not be in the alphabet`).toBe(false);
|
|
}
|
|
});
|
|
});
|
|
|
|
describe('normalizeRoomCode', () => {
|
|
it('upper-cases what was typed in lower case', () => {
|
|
expect(normalizeRoomCode('k7m2qp')).toBe('K7M2QP');
|
|
});
|
|
|
|
it('accepts a code with punctuation or spacing around it', () => {
|
|
expect(normalizeRoomCode(' K7M2-QP ')).toBe('K7M2QP');
|
|
});
|
|
|
|
it('takes the code out of a pasted invite link', () => {
|
|
expect(normalizeRoomCode('https://noitu.example/online?code=K7M2QP')).toBe('K7M2QP');
|
|
});
|
|
|
|
it('reads the code from a link that carries other parameters too', () => {
|
|
expect(normalizeRoomCode('https://noitu.example/online?ref=chat&code=K7M2QP#top')).toBe(
|
|
'K7M2QP'
|
|
);
|
|
});
|
|
|
|
it('does not try to find a code buried in a sentence', () => {
|
|
// Guessing would occasionally produce a plausible code for the wrong
|
|
// room, which is worse than asking the player to paste the code itself.
|
|
expect(isRoomCode(normalizeRoomCode('vào phòng của tôi nhé'))).toBe(false);
|
|
});
|
|
|
|
it('keeps characters outside the alphabet so the code can be refused', () => {
|
|
// A code misread as O for 0 should be reported as malformed, not
|
|
// quietly rewritten into a different six-character code.
|
|
expect(normalizeRoomCode('K0MOQP')).toBe('K0MOQP');
|
|
expect(isRoomCode(normalizeRoomCode('K0MOQP'))).toBe(false);
|
|
});
|
|
|
|
it('does not truncate a long paste into a different code', () => {
|
|
expect(isRoomCode(normalizeRoomCode('K7M2QPZZZZ'))).toBe(false);
|
|
});
|
|
|
|
it('handles nothing at all', () => {
|
|
expect(normalizeRoomCode('')).toBe('');
|
|
expect(normalizeRoomCode(/** @type {any} */ (undefined))).toBe('');
|
|
});
|
|
});
|
|
|
|
describe('isRoomCode', () => {
|
|
it('accepts a well-formed code', () => {
|
|
expect(isRoomCode('K7M2QP')).toBe(true);
|
|
});
|
|
|
|
it('rejects the wrong length', () => {
|
|
expect(isRoomCode('K7M2Q')).toBe(false);
|
|
expect(isRoomCode('K7M2QPZ')).toBe(false);
|
|
});
|
|
|
|
it('rejects characters outside the alphabet', () => {
|
|
expect(isRoomCode('K7M2Q0')).toBe(false);
|
|
expect(isRoomCode('k7m2qp')).toBe(false);
|
|
});
|
|
});
|