All source files

tests/practice.test.mjs

Download raw file

import test from 'node:test';
import assert from 'node:assert/strict';
import {readFile} from 'node:fs/promises';
import {PRACTICE_POOL} from '../src/practice-data.mjs';
import {ANSWER_SCHEDULE, ALLOWED_WORDS} from '../src/word-data.mjs';

const CJK = /[\u4e00-\u9fff]/;
const LATIN = /[a-z]/i;

test('practice pool has at least 120 unique five-letter lowercase words', () => {
  assert.ok(PRACTICE_POOL.length >= 120, `practice pool has only ${PRACTICE_POOL.length} entries`);
  const seen = new Set();
  for (const entry of PRACTICE_POOL) {
    assert.match(entry.word, /^[a-z]{5}$/, `${entry.word} must be five lowercase letters`);
    assert.ok(!seen.has(entry.word), `${entry.word} appears more than once`);
    seen.add(entry.word);
  }
  assert.equal(seen.size, PRACTICE_POOL.length);
});

test('practice pool covers every initial a-z with at least three entries', () => {
  assert.ok(PRACTICE_POOL.length >= 200, `practice pool has only ${PRACTICE_POOL.length} entries`);
  const counts = new Map();
  for (const entry of PRACTICE_POOL) {
    const initial = entry.word[0];
    counts.set(initial, (counts.get(initial) || 0) + 1);
  }
  const short = [];
  for (let code = 'a'.charCodeAt(0); code <= 'z'.charCodeAt(0); code += 1) {
    const letter = String.fromCharCode(code);
    const count = counts.get(letter) || 0;
    if (count < 3) short.push(`${letter}:${count}`);
  }
  assert.deepEqual(short, [], `practice pool needs at least 3 entries per initial a-z; short: ${short.join(', ')}`);
});

test('practice words never intersect the daily answer schedule', () => {
  const answers = new Set(ANSWER_SCHEDULE.map(entry => entry.word));
  const overlap = PRACTICE_POOL.map(entry => entry.word).filter(word => answers.has(word));
  assert.deepEqual(overlap, [], `practice leaks scheduled daily answers: ${overlap.join(', ')}`);
});

test('every practice word is already accepted by the guess list', () => {
  const allowed = new Set(ALLOWED_WORDS);
  const missing = PRACTICE_POOL.map(entry => entry.word).filter(word => !allowed.has(word));
  assert.deepEqual(missing, [], `practice words rejected by the guess list: ${missing.join(', ')}`);
});

test('every practice entry carries non-empty bilingual category and clue text', () => {
  for (const entry of PRACTICE_POOL) {
    for (const field of ['word', 'categoryEn', 'categoryZh', 'clueEn', 'clueZh']) {
      assert.equal(typeof entry[field], 'string', `${entry.word}.${field} must be a string`);
      assert.ok(entry[field].trim().length > 0, `${entry.word}.${field} must not be empty`);
    }
    assert.match(entry.categoryEn, LATIN, `${entry.word}.categoryEn must be English text`);
    assert.match(entry.clueEn, LATIN, `${entry.word}.clueEn must be English text`);
    assert.match(entry.categoryZh, CJK, `${entry.word}.categoryZh must include Chinese text`);
    assert.match(entry.clueZh, CJK, `${entry.word}.clueZh must include Chinese text`);
  }
});

test('no English clue contains its own answer', () => {
  const leaks = PRACTICE_POOL
    .filter(entry => entry.clueEn.toLowerCase().includes(entry.word.toLowerCase()))
    .map(entry => `${entry.word}: ${entry.clueEn}`);
  assert.deepEqual(leaks, [], `clues leak their own answer: ${leaks.join('; ')}`);
});

test('no English clue contains a four-letter stem of its own answer', () => {
  const leaks = PRACTICE_POOL
    .filter(entry => entry.clueEn.toLowerCase().includes(entry.word.slice(0, 4)))
    .map(entry => `${entry.word}: ${entry.clueEn}`);
  assert.deepEqual(leaks, [], `clues leak a four-letter stem of their answer: ${leaks.join('; ')}`);
});

test('no category field contains its own answer', () => {
  const leaks = [];
  for (const entry of PRACTICE_POOL) {
    for (const field of ['categoryEn', 'categoryZh']) {
      if (entry[field].toLowerCase().includes(entry.word.toLowerCase())) {
        leaks.push(`${entry.word}.${field}: ${entry[field]}`);
      }
    }
  }
  assert.deepEqual(leaks, [], `categories leak their own answer: ${leaks.join('; ')}`);
});

test('no category field contains a four-letter stem of its own answer', () => {
  const leaks = [];
  for (const entry of PRACTICE_POOL) {
    for (const field of ['categoryEn', 'categoryZh']) {
      if (entry[field].toLowerCase().includes(entry.word.slice(0, 4))) {
        leaks.push(`${entry.word}.${field}: ${entry[field]}`);
      }
    }
  }
  assert.deepEqual(leaks, [], `categories leak a four-letter stem of their answer: ${leaks.join('; ')}`);
});

test('practice mode in src/app.mjs never reads the daily answer schedule', async () => {
  const source = await readFile(new URL('../src/app.mjs', import.meta.url), 'utf8');
  assert.ok(!source.includes('ANSWER_SCHEDULE'), 'app.mjs must not import or reference ANSWER_SCHEDULE');
  assert.ok(source.includes("from './practice-data.mjs'"), 'app.mjs must load the practice pool');
  assert.ok(source.includes('PRACTICE_POOL'), 'app.mjs must draw practice words from PRACTICE_POOL');
});