All source filestests/game-core.test.mjs
Download raw file
import test from 'node:test';
import assert from 'node:assert/strict';
import {
normalizeWord, scoreGuess, keyboardStates, utcDayKey, createGame,
isHintOffered, applyAction, buildShareText, createStats, settleGame
} from '../src/game-core.mjs';
const context = () => ({ answer: 'apple', allowedWords: ['apple','alley','crane','sloth','plant','bloom','crisp','honey','sheep','brave'], dayKey: '2026-12-05' });
function play(word, state = createGame('2026-12-05')) {
const typed = [...word].reduce((s, letter) => applyAction(s, {type:'input',letter}, context()).state, state);
return applyAction(typed, {type:'submit'}, context());
}
test('normalizes ASCII words and rejects other characters', () => {
assert.equal(normalizeWord(' Apple '), 'apple');
assert.equal(normalizeWord('applé'), 'appl');
assert.equal(normalizeWord(null), '');
});
test('scores repeated letters with exact first and present quota second', () => {
assert.deepEqual(scoreGuess('ALLEY','APPLE'), ['correct','present','absent','present','absent']);
assert.deepEqual(scoreGuess('APPLE','ALLEY'), ['correct','absent','absent','present','present']);
assert.deepEqual(scoreGuess('BLOOM','BLOBS'), ['correct','correct','correct','absent','absent']);
assert.deepEqual(scoreGuess('CRANE','PLANT'), ['absent','absent','correct','correct','absent']);
});
test('rejects malformed scoring inputs', () => {
assert.throws(() => scoreGuess('APP','APPLE'), /five letters/);
assert.throws(() => scoreGuess('APPLE',null), /five letters/);
});
test('keyboard status never downgrades', () => {
const states = keyboardStates(['alley','crane'], 'apple');
assert.equal(states.a, 'correct');
assert.equal(states.l, 'present');
assert.equal(states.e, 'correct');
assert.equal(states.c, 'absent');
});
test('UTC day key is stable across local timezones', () => {
assert.equal(utcDayKey(new Date('2026-12-05T23:59:59-08:00')), '2026-12-06');
assert.equal(utcDayKey(new Date('2026-12-05T00:00:00Z')), '2026-12-05');
});
test('invalid length and off-list words do not consume a guess', () => {
const initial = createGame('2026-12-05');
const short = play('app', initial);
assert.equal(short.outcome.type, 'invalid_length');
assert.equal(short.state.guesses.length, 0);
const unknown = play('xxxxx', initial);
assert.equal(unknown.outcome.type, 'invalid_word');
assert.equal(unknown.state.guesses.length, 0);
});
test('valid submission starts the game and preserves no draft', () => {
const result = play('crane');
assert.equal(result.outcome.type, 'guess');
assert.equal(result.outcome.started, true);
assert.equal(result.state.status, 'playing');
assert.deepEqual(result.state.guesses, ['crane']);
assert.equal(result.state.draft, '');
});
test('six wrong guesses end the game and a seventh is ignored', () => {
let state = createGame('2026-12-05');
for (const guess of ['crane','sloth','plant','bloom','crisp','brave']) state = play(guess,state).state;
assert.equal(state.status, 'lost');
const seventh = play('honey', state);
assert.equal(seventh.outcome.type, 'ignored');
assert.equal(seventh.state.guesses.length, 6);
});
test('solving on the final guess is solved, not lost', () => {
let state = createGame('2026-12-05');
for (const guess of ['crane','sloth','plant','bloom','crisp']) state = play(guess,state).state;
const result = play('apple',state);
assert.equal(result.state.status, 'solved');
assert.equal(result.outcome.type, 'solved');
});
test('hints are manual, ordered and capped at three', () => {
let state = createGame('2026-12-05');
assert.equal(state.hintStep, 1);
state = play('crane',state).state;
state = play('sloth',state).state;
assert.equal(isHintOffered(state), true);
assert.equal(state.hintStep, 1);
for (const expected of [2,3,3]) {
const result = applyAction(state,{type:'reveal_hint'},context());
state = result.state;
assert.equal(state.hintStep, expected);
}
});
test('reveal answer is a separate terminal state', () => {
const result = applyAction(createGame('2026-12-05'),{type:'reveal_answer'},context());
assert.equal(result.state.status, 'revealed');
assert.equal(result.outcome.type, 'revealed');
});
test('input and backspace are bounded to five letters', () => {
let state = createGame('2026-12-05');
for (const letter of 'abcdefg') state = applyAction(state,{type:'input',letter},context()).state;
assert.equal(state.draft, 'abcde');
state = applyAction(state,{type:'backspace'},context()).state;
assert.equal(state.draft, 'abcd');
});
test('share text contains result grid but no answer or guesses', () => {
const result = play('alley');
const solved = play('apple',result.state).state;
const text = buildShareText(solved, 42, 'apple');
assert.match(text, /#42 2\/6/);
assert.ok(!text.toLowerCase().includes('apple'));
assert.ok(!text.toLowerCase().includes('alley'));
assert.equal((text.match(/[🟩🟨⬜]/gu) || []).length, 10);
});
test('stats are idempotent and solved streak follows UTC days', () => {
let stats = createStats();
const solved = play('apple').state;
stats = settleGame(stats, solved);
stats = settleGame(stats, solved);
assert.equal(stats.played, 1);
assert.equal(stats.wins, 1);
assert.equal(stats.currentStreak, 1);
assert.equal(stats.distribution[1], 1);
const next = {...solved, dayKey:'2026-12-06'};
stats = settleGame(stats,next);
assert.equal(stats.currentStreak, 2);
assert.equal(stats.bestStreak, 2);
});
test('revealed game preserves best streak but not current streak', () => {
const stats = {...createStats(), currentStreak:4, bestStreak:4};
const revealed = applyAction(createGame('2026-12-05'),{type:'reveal_answer'},context()).state;
const next = settleGame(stats,revealed);
assert.equal(next.currentStreak, 0);
assert.equal(next.bestStreak, 4);
assert.equal(next.wins, 0);
});