NT-Connections/test.js
Aidan Dona 4a2309b589 Add Connections-style word game with puzzle editor
Express + node:sqlite server, vanilla HTML/JS frontend. Daily puzzle by
assigned date, puzzle list, shuffle, one-away hint, shareable emoji
results, and a password-protected admin page to create and edit puzzles.
Answers are checked server-side and never sent before they are solved.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-10-08 16:56:34 -05:00

47 lines
1.9 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);
});