fire-truck-manual.test.js (1614B)
1 'use strict'; 2 3 const test = require('node:test'); 4 const assert = require('node:assert'); 5 const FT = require('../../games/fire-truck/lib.js'); 6 7 function makeIsland(seed) { 8 return FT.buildIsland({ width: 50, height: 50, routeCount: 50, seed: seed ?? 1 }); 9 } 10 11 function roads(island) { 12 const out = []; 13 for (const row of island.grid) for (const cell of row) { 14 if (cell.type === FT.CELL_TYPES.ROAD) out.push(cell); 15 } 16 return out; 17 } 18 19 test('bestRoadDirection always steps toward the BFS origin', () => { 20 const island = makeIsland(7); 21 const roadCells = roads(island); 22 const target = roadCells[Math.floor(roadCells.length / 2)]; 23 const dist = FT.bfsRoadDistances(island.grid, target); 24 25 for (let i = 0; i < roadCells.length; i += 17) { 26 const cell = roadCells[i]; 27 const card = FT.bestRoadDirection(island.grid, cell, dist); 28 if (cell === target) continue; 29 assert.ok(card, `direction exists from (${cell.x},${cell.y})`); 30 const next = island.grid[cell.y + FT.DIRS[card].dy][cell.x + FT.DIRS[card].dx]; 31 assert.strictEqual(dist.get(next), dist.get(cell) - 1, 'moves one step closer'); 32 } 33 }); 34 35 test('bestRoadDirection is null at the origin itself', () => { 36 const island = makeIsland(3); 37 const target = roads(island)[0]; 38 const dist = FT.bfsRoadDistances(island.grid, target); 39 assert.strictEqual(FT.bestRoadDirection(island.grid, target, dist), null); 40 }); 41 42 test('bestRoadDirection is null when the distance map is empty', () => { 43 const island = makeIsland(3); 44 const cell = roads(island)[0]; 45 assert.strictEqual(FT.bestRoadDirection(island.grid, cell, new Map()), null); 46 });