Files
cwshed-clone/tests/lu-trainer/engine.test.mjs
T

247 lines
12 KiB
JavaScript
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
import test from 'node:test';
import assert from 'node:assert/strict';
import { Training, generateField, neighbors, hintChance, START, COLS, coordinate } from '../../web/lu-trainer/engine.js';
import { rustleSamples } from '../../web/lu-trainer/audio.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};
test('sound cue is silent at zero and has one distinct pulse per level', () => {
const sampleRate = 48000, step = 11040, audible = 4560;
assert.equal(rustleSamples(0, sampleRate).length, 0);
for (let level = 1; level <= 7; level++) {
const samples = rustleSamples(level, sampleRate);
assert.equal(samples.length, level * step);
for (let start = 0; start < samples.length; start += step) {
assert(samples.slice(start, start + audible).some(value => Math.abs(value) > .01));
assert(samples.slice(start + audible, start + step).every(value => value === 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('entrance-area limits apply to actual hazards without making either lower cell safe', () => {
const dense = {...options, height: 5, growth: 25, season: 5};
const limits = [[START, 2], [COLS, 3], [2 * COLS, 4]];
const levels = limits.map(() => new Set());
const edgeHazards = new Set();
for (let seed = 0; seed < 100; seed++) {
const field = generateField(dense, seeded(seed));
assert.equal(field.hazards.size, 38, `seed ${seed}`);
assert(!field.hazards.has(START));
limits.forEach(([id, max], i) => {
const actual = neighbors(id).filter(neighbor => field.hazards.has(neighbor)).length;
assert(actual <= max, `seed ${seed}: ${coordinate(id)} has ${actual} hazards, expected at most ${max}`);
assert.equal(field.values[id], actual);
levels[i].add(actual);
if (field.hazards.has(id)) edgeHazards.add(id);
});
}
assert.deepEqual(levels[0], new Set([0, 1, 2]));
assert(levels[1].has(3));
assert(levels[2].has(4));
assert.deepEqual(edgeHazards, new Set([COLS, 2 * COLS]));
});
test('the ready snapshot reveals the entrance number but does not open the transition', () => {
const dense = {...options, height: 5, growth: 25, season: 5};
for (const seed of [0, 2, 5]) {
const {field, game} = fixture(dense, seed);
const view = game.view();
const start = view.cells[view.start];
assert.equal(start.value, field.values[START]);
assert.equal(start.opened, false);
assert.equal(start.available, false);
assert.equal(view.opened, 0);
assert(view.cells.every(cell => cell.entrance || cell.value === null));
assert.equal(game.jump(START).code, 'transition');
assert.equal(game.mark(START).code, 'transition');
assert.equal(game.rate(START, 1).code, 'transition');
assert.equal(game.jump(1).code, 'opened');
assert.equal(game.view().cells[START].value, start.value);
assert.equal(game.view().opened, 1);
}
});
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('manual levels are independent of facts, hazard flags and movement', () => {
const {game} = fixture();
assert.equal(game.rate(1, 7).code, 'rated');
assert.equal(game.rate(1, 1).code, 'rated');
assert.equal(game.rate(1, 1).code, 'unchanged');
assert.equal(game.rate(1, 0).code, 'rated');
for (const level of [8, -1, 1.5, '1', undefined]) assert.equal(game.rate(1, level).code, 'invalid-level');
const view = game.view();
assert.equal(view.marks, 0);
assert.equal(view.position, START);
assert.equal(view.cells[1].rating, 0);
assert.equal(view.cells[1].value, null);
assert.equal(game.mark(1).code, 'marked');
assert.equal(game.rate(1, null).code, 'unrated');
assert.equal(game.rate(1, null).code, 'unchanged');
assert.equal(game.view().cells[1].rating, null);
assert.equal(game.jump(1).code, 'marked');
assert.equal(game.mark(1).code, 'unmarked');
assert.equal(game.jump(1).code, 'opened');
assert.equal(game.rate(1, 3).code, 'rated');
assert.equal(game.rate(START, 3).code, 'transition');
assert.equal(game.rate(view.exit, 3).code, 'transition');
assert.equal(game.clearMarks().code, 'cleared');
assert.equal(game.view().cells[1].rating, null);
assert(game.view().cells[1].opened);
});
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.rate(id, 7);
assert.notEqual(game.view().status, 'won');
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.rate(1, 3).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);
});