| 1 | import test from "node:test"; |
| 2 | import assert from "node:assert/strict"; |
| 3 | import { findPathToNearestGoal } from "../../shared/navigation/bfs.js"; |
| 4 | |
| 5 | test("BFS picks the nearest stand tile toward the target, not the top-left ring tile", () => |
| 6 | { |
| 7 | // Open field. Player south of target — closest adjacent tile is directly north of player (south of target). |
| 8 | const playerX = 20; |
| 9 | const playerY = 30; |
| 10 | const targetX = 20; |
| 11 | const targetY = 20; |
| 12 | const path = findPathToNearestGoal( |
| 13 | playerX, |
| 14 | playerY, |
| 15 | (x, y) => Math.max(Math.abs(x - targetX), Math.abs(y - targetY)) === 1, |
| 16 | () => true, |
| 17 | 0, |
| 18 | 0, |
| 19 | 40, |
| 20 | 40 |
| 21 | ); |
| 22 | assert.ok(path.length); |
| 23 | const goal = path[path.length - 1]; |
| 24 | assert.deepEqual(goal, { x: 20, y: 21 }); |
| 25 | }); |
| 26 | |
| 27 | test("BFS returns empty when already at a goal", () => |
| 28 | { |
| 29 | const path = findPathToNearestGoal( |
| 30 | 5, |
| 31 | 5, |
| 32 | (x, y) => x === 5 && y === 5, |
| 33 | () => true, |
| 34 | 0, |
| 35 | 0, |
| 36 | 10, |
| 37 | 10 |
| 38 | ); |
| 39 | assert.deepEqual(path, []); |
| 40 | }); |