NT-Connections/test.js
Aidan Dona b2f8e879ab Add puzzle hints and per-word descriptions with sources
Hint is revealed on demand below the board while playing. After the game
ends, each word's description and source link are listed below the
puzzle, and hovering a solved word highlights its entry.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-10-13 16:44:07 -05:00

63 lines
2.6 KiB
JavaScript

const test = require('node:test');
const assert = require('node:assert');
const { validateGame, checkGuess } = require('./lib');
const good = () => ({
title: 'Sample',
date: '2026-10-08',
groups: [
{ name: 'Fish', color: 'yellow', words: ['bass', 'pike', 'carp', 'sole'] },
{ name: 'Planets', color: 'green', words: ['mars', 'venus', 'earth', 'saturn'] },
{ name: 'Keys', color: 'blue', words: ['shift', 'tab', 'enter', 'escape'] },
{ name: 'Tools', color: 'purple', words: ['saw', 'drill', 'level', 'file'] },
],
});
test('valid game is cleaned and uppercased', () => {
const { game, errors } = validateGame(good());
assert.equal(errors, undefined);
assert.deepEqual(game.groups[0].words, ['BASS', 'PIKE', 'CARP', 'SOLE']);
});
test('rejects duplicates, bad dates, repeated colors, missing words', () => {
const g = good();
g.groups[1].words[0] = ' Bass ';
g.groups[2].color = 'yellow';
g.groups[3].words = ['a', 'b', 'c'];
g.date = '2026-02-30';
const { errors } = validateGame(g);
assert.ok(errors.some((e) => e.includes('appears more than once')));
assert.ok(errors.some((e) => e.includes('used more than once')));
assert.ok(errors.some((e) => e.includes('needs 4 words')));
assert.ok(errors.some((e) => e.includes('real date')));
assert.ok(validateGame({}).errors.length);
});
test('undated game is allowed', () => {
assert.equal(validateGame({ ...good(), date: '' }).game.date, null);
});
test('checkGuess: correct, one away, wrong, invalid', () => {
const { groups } = validateGame(good()).game;
assert.equal(checkGuess(groups, ['sole', 'carp', 'PIKE', 'bass']).group.name, 'Fish');
assert.equal(checkGuess(groups, ['bass', 'pike', 'carp', 'mars']).result, 'one-away');
assert.equal(checkGuess(groups, ['bass', 'pike', 'mars', 'venus']).result, 'wrong');
assert.ok(checkGuess(groups, ['bass', 'bass', 'pike', 'carp']).error);
assert.ok(checkGuess(groups, 'nope').error);
});
test('hint and per-word details are validated', () => {
const g = good();
g.hint = ' Think water ';
g.groups[0].details = [{ description: 'A fish', source: 'https://example.com/bass' }];
const { game } = validateGame(g);
assert.equal(game.hint, 'Think water');
assert.deepEqual(game.groups[0].details[0], { description: 'A fish', source: 'https://example.com/bass' });
assert.deepEqual(game.groups[0].details[1], { description: '', source: '' });
g.groups[0].details = [{ source: 'javascript:alert(1)' }];
g.hint = 'x'.repeat(201);
const { errors } = validateGame(g);
assert.ok(errors.some((e) => e.includes('http(s) link')));
assert.ok(errors.some((e) => e.includes('Hint')));
});