Files
noitu/web/tests/error-codes.test.js
T
tiennm99 64106735ec feat(web): add SvelteKit vs-bot frontend
The board, the home screen and the game-over panel, in Vietnamese, served by
the Go binary as a static single-page app.

The store is a reducer over ServerMessage and computes nothing. Validity, turn
order, scores and the result are read from the wire, which is what lets one
screen serve the bot now and online play later. Every Vietnamese string lives in
one module, including the map from RejectReason to a message, so the server can
send UI keys instead of prose.

The word field is uncontrolled. A Telex or VNI input method composes a diacritic
across several keystrokes, and writing the value back on each one cancels the
composition. It is read on submit and cleared only there.

The countdown is drawn against the server's clock, estimated from the ping round
trip, and settles 300ms early so the ring never claims more time than the server
allows.

The screen owns the socket and the game while it is mounted, and gives the
current game up on the way out. Asking for a game is stored intent rather than a
condition inferred from the board being empty: clearing the board for a rematch
is that same condition, so the inference sent a second StartBotGame and the
server built two rooms that then destroyed each other.

A test greps the built bundle for dictionary words and holds a size budget, so
the wordlist cannot reach the browser unnoticed. Another reads the error codes
out of the Go transport and fails when one has no Vietnamese message.
2026-09-05 12:55:02 +07:00

56 lines
2.1 KiB
JavaScript

// ServerError.code is a UI key, so the server decides the vocabulary and this
// file has to speak all of it. The proto schema cannot help here — the codes
// are string literals in the Go transport, not an enum — so the guard reads
// them out of the source that emits them.
//
// Without this, adding a code on the server degrades silently to the generic
// fallback: the player is told "something went wrong" for a situation the
// server described precisely.
import { readFileSync, readdirSync } from 'node:fs';
import { join } from 'node:path';
import { fileURLToPath } from 'node:url';
import { describe, expect, it } from 'vitest';
import { errorMessages } from '../src/lib/i18n/vi.js';
const wsapiDir = fileURLToPath(new URL('../../server/internal/wsapi', import.meta.url));
/** Every `errorMsg("…")` literal in the transport, excluding its own tests. */
function serverErrorCodes() {
const codes = new Set();
const files = readdirSync(wsapiDir).filter((f) => f.endsWith('.go') && !f.endsWith('_test.go'));
for (const file of files) {
const source = readFileSync(join(wsapiDir, file), 'utf8');
for (const [, code] of source.matchAll(/errorMsg\("([a-z_]+)"\)/g)) {
codes.add(code);
}
}
return codes;
}
describe('server error codes', () => {
const codes = serverErrorCodes();
it('finds the call sites at all, so an empty match cannot pass as agreement', () => {
// A rename of errorMsg would otherwise turn this whole file green by
// finding nothing to check.
expect(codes.size).toBeGreaterThan(10);
expect(codes.has('room_not_found')).toBe(true);
});
it('has a Vietnamese message for every code the server can send', () => {
const missing = [...codes].filter((code) => !(code in errorMessages)).sort();
expect(missing).toEqual([]);
});
it('carries no message for a code the server cannot send', () => {
// Dead copy is a smaller problem than a missing message, but it is still
// a claim about the server that has stopped being true.
const stale = Object.keys(errorMessages)
.filter((code) => !codes.has(code))
.sort();
expect(stale).toEqual([]);
});
});