| 1 |
import { expect, test } from "bun:test"; |
| 2 |
import { makeGrid } from "./grid.ts"; |
| 3 |
|
| 4 |
const LINES = ["#########", "#.......#", "#.##.##.#", "#.......#", "#########"]; |
| 5 |
const cells = (g: ReturnType<typeof makeGrid>) => g.cellsOf("."); |
| 6 |
|
| 7 |
test("many distances to one fixed target build one field", () => { |
| 8 |
const g = makeGrid(LINES, 10); |
| 9 |
const target = cells(g)[0]!; |
| 10 |
for (const from of cells(g)) g.walkDistance(from, target); |
| 11 |
expect(g.fieldsBuilt()).toBe(1); |
| 12 |
}); |
| 13 |
|
| 14 |
test("varying the target builds a field per target", () => { |
| 15 |
const g = makeGrid(LINES, 10); |
| 16 |
const from = cells(g)[0]!; |
| 17 |
for (const to of cells(g)) g.walkDistance(from, to); |
| 18 |
expect(g.fieldsBuilt()).toBe(cells(g).length); |
| 19 |
}); |
| 20 |
|
| 21 |
test("pathStep shares the field walkDistance built for the goal", () => { |
| 22 |
const g = makeGrid(LINES, 10); |
| 23 |
const goal = cells(g).at(-1)!; |
| 24 |
for (const from of cells(g)) { |
| 25 |
g.walkDistance(from, goal); |
| 26 |
g.pathStep(from, goal); |
| 27 |
} |
| 28 |
expect(g.fieldsBuilt()).toBe(1); |
| 29 |
}); |
| 30 |
|
| 31 |
test("walk distance is symmetric, so swapping the arguments is safe", () => { |
| 32 |
const g = makeGrid(LINES, 10); |
| 33 |
for (const a of cells(g)) for (const b of cells(g)) expect(g.walkDistance(a, b)).toBe(g.walkDistance(b, a)); |
| 34 |
}); |
| 35 |
|
| 36 |
test("clearPaths forgets the fields, so the next ask builds again", () => { |
| 37 |
const g = makeGrid(LINES, 10); |
| 38 |
const [a, b] = cells(g); |
| 39 |
g.walkDistance(a!, b!); |
| 40 |
g.clearPaths(); |
| 41 |
g.walkDistance(a!, b!); |
| 42 |
expect(g.fieldsBuilt()).toBe(2); |
| 43 |
}); |