import test from 'node:test'; import assert from 'node:assert/strict'; import { Training, generateField, neighbors, hintChance, START, coordinate } from '../../web/lu-trainer/engine.js'; function seeded(seed = 19) { return () => { seed = (Math.imul(seed, 1664525) + 1013904223) >>> 0; return seed / 2 ** 32; }; } const options = {height: 1, lu: 5, growth: 12, season: 0}; function fixture(settings = options, seed = 19) { return {field: generateField(settings, seeded(seed)), game: new Training(settings, seeded(seed))}; } function openReachableSafe(game, field) { let progress = true; while (progress && !['won', 'lost'].includes(game.view().status)) { progress = false; for (const cell of game.view().cells) { if (cell.available && !cell.opened && !cell.transition && !field.hazards.has(cell.id)) { game.jump(cell.id); progress = true; } } } } test('entrance is always x1 y1 and every other cell can host the exit', () => { assert.equal(START, 0); assert.equal(coordinate(START), 'x1 y1'); assert.equal(coordinate(59), 'x10 y6'); const exits = new Set(); for (let id = 1; id < 60; id++) { const random = () => (id - .5) / 59; const field = generateField(options, random); assert.equal(field.start, 0); assert.equal(field.exit, id); exits.add(field.exit); assert(!field.hazards.has(field.start)); assert(!field.hazards.has(field.exit)); assert(!field.base.includes(field.start)); assert(!field.base.includes(field.exit)); const game = new Training(options, random); const view = game.view(); assert.equal(view.position, 0); assert.equal(view.start, field.start); assert.equal(view.exit, field.exit); assert.equal(view.cells.filter(cell => cell.entrance).length, 1); assert.equal(view.cells.filter(cell => cell.exit).length, 1); assert.equal(view.cells.filter(cell => cell.transition).length, 2); assert.equal(game.jump(field.exit).code, 'transition'); assert.equal(game.mark(field.exit).code, 'transition'); } assert.equal(exits.size, 59); }); test('generation covers every settings combination with safe transitions, base and connecting route', () => { for (let height = 1; height <= 5; height++) for (const growth of [12,15,18,22,25]) for (const season of [-3,0,5]) { const field = generateField({...options, height, growth, season}, seeded(height * growth + season)); assert.equal(field.values.length, 60); assert(!field.hazards.has(START) && !field.hazards.has(field.exit)); for (const id of field.base) assert(!field.hazards.has(id)); assert.equal(field.base.length, height === 1 ? 5 : 3); assert.equal(field.hazards.size, Math.min(Math.max(5, growth + season + 2 * (height - 1)), 60 - field.reserved.size)); const reached = new Set([START]); for (const id of reached) for (const next of neighbors(id)) if (!field.hazards.has(next)) reached.add(next); assert(reached.has(field.exit)); for (const id of field.base) assert(reached.has(id)); for (let id = 0; id < 60; id++) assert.equal(field.values[id], neighbors(id).filter(n => field.hazards.has(n)).length); } }); test('edge neighborhoods do not wrap rows', () => { assert.deepEqual(neighbors(0).sort((a,b)=>a-b), [1,10,11]); assert.equal(neighbors(9).length, 3); assert.equal(neighbors(25).length, 8); assert.deepEqual(neighbors(59).sort((a,b)=>a-b), [48,49,58]); }); test('only base or neighbors of any opened cell are available; transitions never count', () => { const {game} = fixture(); assert.equal(game.jump(START).code, 'transition'); assert.equal(game.jump(game.view().exit).code, 'transition'); assert.equal(game.jump(25).code, 'unreachable'); assert.equal(game.mark(START).code, 'transition'); assert.equal(game.jump(1).code, 'opened'); game.jump(4); // last position differs: adjacency still belongs to any open cell assert(game.view().cells[10].available); assert.equal(game.view().position, 4); }); test('notes hide no facts, block jumps, clear independently and reject invalid IDs', () => { const {game} = fixture(); game.jump(1); game.mark(2); const before = game.view(); assert.equal(game.jump(2).code, 'marked'); assert.equal(before.cells[2].value, null); assert(!Object.hasOwn(before.cells[2], 'hazard')); assert.equal(game.mark(1).code, 'opened'); game.clearMarks(); assert(game.view().cells[1].opened); assert.equal(game.view().marks, 0); assert.equal(game.view().position, before.position); assert(game.view().history.length > before.history.length); for (const id of [-1,60,1.5,'1',null]) assert.equal(game.jump(id).code, 'invalid'); }); test('all and only hazards marked wins; terminal actions cannot change state', () => { const {game,field} = fixture(); game.mark(1); for (const id of field.hazards) game.mark(id); assert.notEqual(game.view().status, 'won'); game.mark(1); assert.equal(game.view().status, 'won'); const ended = game.view(); for (const cell of ended.cells) assert.equal(typeof cell.hazard, 'boolean'); assert.equal(game.jump(0).code, 'finished'); assert.equal(game.mark(1).code, 'finished'); assert.equal(game.clearMarks().code, 'finished'); assert.deepEqual(game.view(), ended); }); test('opening all safe cells wins without entering either transition', () => { const {game,field} = fixture(); openReachableSafe(game, field); assert.equal(game.view().status, 'won'); assert.equal(game.view().opened, 58 - field.hazards.size); }); test('hazard ends attempt, reveals wrong notes and preserves last safe position', () => { const {game,field} = fixture(); game.mark(4); game.jump(1); while (game.view().status !== 'lost') { const cell = game.view().cells.find(c => c.available && !c.opened && !c.transition && !c.marked); assert(cell); game.jump(cell.id); } const end = game.view(); assert(end.cells[4].wrong); assert(field.hazards.has(end.hit)); assert(!field.hazards.has(end.position)); }); test('LU hint probability, repeated safe visits and hidden information boundary', () => { assert.deepEqual(Array.from({length:10}, (_,lu)=>hintChance(lu)), [0,0,0,0,.1,.2,.3,.3,.3,.3]); const {game,field} = fixture(); openReachableSafe(game, field); // Separate active game: choose an available safe base cell with adjacent hazards. const active = fixture(); const id = active.field.base.find(n => active.field.values[n] > 0); assert.notEqual(id, undefined); active.game.jump(id, () => 0); const revisit = active.game.jump(id, () => 0); assert.equal(revisit.code, 'revisited'); assert(active.field.hazards.has(revisit.hint)); assert.equal(active.game.view().cells[revisit.hint].value, null); assert.equal(active.game.view().opened, 1); assert.equal(active.game.view().history.length, 2); assert.equal(new Training({...options,lu:0}, seeded()).jump(id, () => 0).hint, null); }); test('invalid settings are rejected rather than corrupting generation', () => { for (const bad of [{height:0},{lu:10},{growth:99},{season:7}]) assert.throws(()=>new Training({...options,...bad}), RangeError); });