|
| 1 | +import assert from "node:assert/strict"; |
| 2 | +import { describe, it } from "node:test"; |
| 3 | +import { findPath, type Point } from "../src/grid.js"; |
| 4 | + |
| 5 | +// Helper: verify path is valid (each step is adjacent and on walkable terrain) |
| 6 | +function isValidPath(grid: number[][], path: Point[]): boolean { |
| 7 | + for (let i = 0; i < path.length; i++) { |
| 8 | + const [r, c] = path[i]!; |
| 9 | + if (r < 0 || r >= grid.length || c < 0 || c >= grid[0]!.length) return false; |
| 10 | + if (grid[r]![c] !== 0) return false; |
| 11 | + if (i > 0) { |
| 12 | + const [pr, pc] = path[i - 1]!; |
| 13 | + const dr = Math.abs(r - pr); |
| 14 | + const dc = Math.abs(c - pc); |
| 15 | + if (dr + dc !== 1) return false; // must be 4-directional adjacent |
| 16 | + } |
| 17 | + } |
| 18 | + return true; |
| 19 | +} |
| 20 | + |
| 21 | +describe("A* Pathfinding", () => { |
| 22 | + it("finds a straight-line path", () => { |
| 23 | + const grid = [ |
| 24 | + [0, 0, 0, 0, 0], |
| 25 | + [0, 0, 0, 0, 0], |
| 26 | + [0, 0, 0, 0, 0], |
| 27 | + ]; |
| 28 | + const result = findPath(grid, [0, 0], [0, 4]); |
| 29 | + assert.ok(result, "should find a path"); |
| 30 | + assert.deepEqual(result.path[0], [0, 0], "starts at start"); |
| 31 | + assert.deepEqual(result.path[result.path.length - 1], [0, 4], "ends at end"); |
| 32 | + assert.equal(result.path.length, 5, "shortest path is 5 cells"); |
| 33 | + assert.ok(isValidPath(grid, result.path), "path must be valid"); |
| 34 | + }); |
| 35 | + |
| 36 | + it("navigates around obstacles", () => { |
| 37 | + const grid = [ |
| 38 | + [0, 0, 0, 0, 0], |
| 39 | + [0, 1, 1, 1, 0], |
| 40 | + [0, 0, 0, 0, 0], |
| 41 | + ]; |
| 42 | + const result = findPath(grid, [0, 0], [2, 4]); |
| 43 | + assert.ok(result, "should find a path"); |
| 44 | + assert.deepEqual(result.path[0], [0, 0]); |
| 45 | + assert.deepEqual(result.path[result.path.length - 1], [2, 4]); |
| 46 | + assert.ok(isValidPath(grid, result.path), "path must be valid"); |
| 47 | + assert.equal(result.path.length, 7, "shortest path around obstacle is 7"); |
| 48 | + }); |
| 49 | + |
| 50 | + it("returns null for unreachable target", () => { |
| 51 | + const grid = [ |
| 52 | + [0, 1, 0], |
| 53 | + [0, 1, 0], |
| 54 | + [0, 1, 0], |
| 55 | + ]; |
| 56 | + const result = findPath(grid, [0, 0], [0, 2]); |
| 57 | + assert.equal(result, null, "should return null when target is unreachable"); |
| 58 | + }); |
| 59 | + |
| 60 | + it("handles start equals end", () => { |
| 61 | + const grid = [[0, 0], [0, 0]]; |
| 62 | + const result = findPath(grid, [1, 1], [1, 1]); |
| 63 | + assert.ok(result, "should find a path"); |
| 64 | + assert.equal(result.path.length, 1, "path is just the start/end point"); |
| 65 | + assert.deepEqual(result.path[0], [1, 1]); |
| 66 | + }); |
| 67 | + |
| 68 | + it("solves a maze", () => { |
| 69 | + const grid = [ |
| 70 | + [0, 1, 0, 0, 0], |
| 71 | + [0, 1, 0, 1, 0], |
| 72 | + [0, 0, 0, 1, 0], |
| 73 | + [1, 1, 0, 0, 0], |
| 74 | + [0, 0, 0, 1, 0], |
| 75 | + ]; |
| 76 | + const result = findPath(grid, [0, 0], [4, 4]); |
| 77 | + assert.ok(result, "should find a path through the maze"); |
| 78 | + assert.deepEqual(result.path[0], [0, 0]); |
| 79 | + assert.deepEqual(result.path[result.path.length - 1], [4, 4]); |
| 80 | + assert.ok(isValidPath(grid, result.path), "path must be valid"); |
| 81 | + assert.equal(result.path.length, 13, "shortest maze path is 13"); |
| 82 | + }); |
| 83 | + |
| 84 | + it("handles large grid efficiently", () => { |
| 85 | + // 50x50 grid with a clear path |
| 86 | + const size = 50; |
| 87 | + const grid: number[][] = Array.from({ length: size }, () => |
| 88 | + Array.from({ length: size }, () => 0) |
| 89 | + ); |
| 90 | + // Add some obstacles but leave a clear path |
| 91 | + for (let i = 1; i < size - 1; i++) { |
| 92 | + grid[i]![Math.floor(size / 2)] = 1; // vertical wall with gap at top and bottom |
| 93 | + } |
| 94 | + |
| 95 | + const start = performance.now(); |
| 96 | + const result = findPath(grid, [0, 0], [size - 1, size - 1]); |
| 97 | + const elapsed = performance.now() - start; |
| 98 | + |
| 99 | + assert.ok(result, "should find a path on large grid"); |
| 100 | + assert.ok(isValidPath(grid, result.path), "path must be valid"); |
| 101 | + assert.ok(elapsed < 1000, `should complete in < 1 second (took ${elapsed.toFixed(0)}ms)`); |
| 102 | + assert.ok(result.nodesExplored < size * size, "A* should not explore every cell"); |
| 103 | + }); |
| 104 | + |
| 105 | + it("tracks nodes explored accurately", () => { |
| 106 | + const grid = [ |
| 107 | + [0, 0, 0], |
| 108 | + [0, 0, 0], |
| 109 | + [0, 0, 0], |
| 110 | + ]; |
| 111 | + const result = findPath(grid, [0, 0], [2, 2]); |
| 112 | + assert.ok(result, "should find a path"); |
| 113 | + assert.ok(result.nodesExplored > 0, "should explore at least 1 node"); |
| 114 | + assert.ok(result.nodesExplored <= 9, "should not explore more than grid size"); |
| 115 | + }); |
| 116 | +}); |
0 commit comments