162 lines
7.6 KiB
JavaScript
162 lines
7.6 KiB
JavaScript
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))};
|
||
}
|
||
test('entrance is always x1 y1 and exits vary only on the allowed edges', () => {
|
||
assert.equal(START, 0);
|
||
assert.equal(coordinate(START), 'x1 y1');
|
||
assert.equal(coordinate(59), 'x10 y6');
|
||
const exits = new Set();
|
||
const allowed = [19,29,39,49,55,56,57,58,59];
|
||
for (let i = 0; i < allowed.length; i++) {
|
||
const random = () => (i + .5) / allowed.length;
|
||
const field = generateField(options, random);
|
||
assert.equal(field.start, 0);
|
||
assert(allowed.includes(field.exit));
|
||
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.deepEqual([...exits].sort((a,b)=>a-b), allowed);
|
||
});
|
||
|
||
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 (next !== field.exit && !field.hazards.has(next)) reached.add(next);
|
||
}
|
||
assert(neighbors(field.exit).some(id => reached.has(id)));
|
||
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 the current position’s neighbors are available, even for safe or opened cells', () => {
|
||
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.jump(2).code, 'unreachable'); // safe upper-row cell, two columns away
|
||
assert.deepEqual(game.view().cells.filter(c => c.available).map(c => c.id), [1,10,11]);
|
||
assert.equal(game.mark(START).code, 'transition');
|
||
assert.equal(game.jump(1).code, 'opened');
|
||
assert.equal(game.jump(1).code, 'unreachable');
|
||
assert(game.view().cells[10].available);
|
||
assert.equal(game.jump(2).code, 'opened');
|
||
assert(!game.view().cells[10].available); // still next to opened cell 1
|
||
assert.equal(game.jump(10).code, 'unreachable');
|
||
assert.equal(game.jump(3).code, 'opened');
|
||
assert.equal(game.jump(1).code, 'unreachable'); // previously opened
|
||
assert.equal(game.view().position, 3);
|
||
assert.equal(game.jump(2).code, 'revisited');
|
||
const diagonal = fixture();
|
||
assert(!diagonal.field.hazards.has(11));
|
||
assert.equal(diagonal.game.jump(11).code, 'opened');
|
||
});
|
||
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();
|
||
function explore(position) {
|
||
for (const id of neighbors(position)) {
|
||
if (game.view().status === 'won') return;
|
||
const cell = game.view().cells[id];
|
||
if (cell.opened || cell.transition || field.hazards.has(id)) continue;
|
||
assert.equal(game.jump(id).code, 'opened');
|
||
explore(id);
|
||
if (game.view().status !== 'won' && position !== START) assert.equal(game.jump(position).code, 'revisited');
|
||
}
|
||
}
|
||
explore(START);
|
||
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 active = fixture(), noHints = fixture({...options, lu:0});
|
||
const route = [1,2,3,4,5,4,5];
|
||
for (const id of route.slice(0,-1)) {
|
||
active.game.jump(id, () => 1);
|
||
noHints.game.jump(id, () => 0);
|
||
}
|
||
const revisit = active.game.jump(5, () => 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, 5);
|
||
assert.equal(active.game.view().history.length, route.length);
|
||
assert.equal(noHints.game.jump(5, () => 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);
|
||
});
|