| 1 | import test from "node:test"; |
| 2 | import assert from "node:assert/strict"; |
| 3 | import { findPathToNearestGoal } from "../../shared/navigation/bfs.js"; |
| 4 | import { withCombatBackstepCost } from "../../shared/navigation/combatPathCost.js"; |
| 5 | |
| 6 | test("BFS picks the nearest stand tile toward the target, not the top-left ring tile", () => |
| 7 | { |
| 8 | // Open field. Player south of target — closest adjacent tile is directly north of player (south of target). |
| 9 | const playerX = 20; |
| 10 | const playerY = 30; |
| 11 | const targetX = 20; |
| 12 | const targetY = 20; |
| 13 | const path = findPathToNearestGoal( |
| 14 | playerX, |
| 15 | playerY, |
| 16 | (x, y) => Math.max(Math.abs(x - targetX), Math.abs(y - targetY)) === 1, |
| 17 | () => true, |
| 18 | 0, |
| 19 | 0, |
| 20 | 40, |
| 21 | 40 |
| 22 | ); |
| 23 | assert.ok(path.length); |
| 24 | const goal = path[path.length - 1]; |
| 25 | assert.deepEqual(goal, { x: 20, y: 21 }); |
| 26 | }); |
| 27 | |
| 28 | test("BFS returns empty when already at a goal", () => |
| 29 | { |
| 30 | const path = findPathToNearestGoal( |
| 31 | 5, |
| 32 | 5, |
| 33 | (x, y) => x === 5 && y === 5, |
| 34 | () => true, |
| 35 | 0, |
| 36 | 0, |
| 37 | 10, |
| 38 | 10 |
| 39 | ); |
| 40 | assert.deepEqual(path, []); |
| 41 | }); |
| 42 | |
| 43 | test("weighted nearest-goal search avoids an expensive backstep tile", () => |
| 44 | { |
| 45 | // Start (0,0); goals are (1,0) and (0,1). Prev is (1,0) so prefer (0,1). |
| 46 | const path = findPathToNearestGoal( |
| 47 | 0, |
| 48 | 0, |
| 49 | (x, y) => (x === 1 && y === 0) || (x === 0 && y === 1), |
| 50 | () => true, |
| 51 | -2, |
| 52 | -2, |
| 53 | 4, |
| 54 | 4, |
| 55 | { stepCost: (x, y) => withCombatBackstepCost(1, x, y, 1, 0) } |
| 56 | ); |
| 57 | assert.deepEqual(path, [{ x: 0, y: 1 }]); |
| 58 | }); |