import test from "node:test"; import assert from "node:assert/strict"; import { findPathToNearestGoal } from "../../shared/navigation/bfs.js"; import { withCombatBackstepCost } from "../../shared/navigation/combatPathCost.js"; test("BFS picks the nearest stand tile toward the target, not the top-left ring tile", () => { // Open field. Player south of target — closest adjacent tile is directly north of player (south of target). const playerX = 20; const playerY = 30; const targetX = 20; const targetY = 20; const path = findPathToNearestGoal( playerX, playerY, (x, y) => Math.max(Math.abs(x - targetX), Math.abs(y - targetY)) === 1, () => true, 0, 0, 40, 40 ); assert.ok(path.length); const goal = path[path.length - 1]; assert.deepEqual(goal, { x: 20, y: 21 }); }); test("BFS returns empty when already at a goal", () => { const path = findPathToNearestGoal( 5, 5, (x, y) => x === 5 && y === 5, () => true, 0, 0, 10, 10 ); assert.deepEqual(path, []); }); test("weighted nearest-goal search avoids an expensive backstep tile", () => { // Start (0,0); goals are (1,0) and (0,1). Prev is (1,0) so prefer (0,1). const path = findPathToNearestGoal( 0, 0, (x, y) => (x === 1 && y === 0) || (x === 0 && y === 1), () => true, -2, -2, 4, 4, { stepCost: (x, y) => withCombatBackstepCost(1, x, y, 1, 0) } ); assert.deepEqual(path, [{ x: 0, y: 1 }]); });