-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathreflect.test.mjs
More file actions
122 lines (97 loc) · 3.54 KB
/
Copy pathreflect.test.mjs
File metadata and controls
122 lines (97 loc) · 3.54 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
import { test } from 'node:test';
import assert from 'node:assert/strict';
import { readFileSync } from 'node:fs';
import { Board, formatDate } from './reflect.js';
const puzzleText = readFileSync('./puzzles/puzzle-2023-03-31.txt', 'utf8');
// formatDate
test('formatDate formats a basic date as YYYY-MM-DD', () => {
assert.equal(formatDate(new Date(2025, 0, 13)), '2025-01-13');
});
test('formatDate zero-pads single-digit day', () => {
assert.equal(formatDate(new Date(2025, 0, 5)), '2025-01-05');
});
test('formatDate zero-pads single-digit month', () => {
assert.equal(formatDate(new Date(2025, 8, 1)), '2025-09-01');
});
test('formatDate handles year-end date', () => {
assert.equal(formatDate(new Date(2024, 11, 31)), '2024-12-31');
});
test('formatDate handles leap day', () => {
assert.equal(formatDate(new Date(2024, 1, 29)), '2024-02-29');
});
// Board construction
test('board.n is 4 for a 4x4 puzzle', () => {
const board = new Board(puzzleText);
assert.equal(board.n, 4);
});
test('board.hiddenBlocks is an n×n array', () => {
const board = new Board(puzzleText);
assert.equal(board.hiddenBlocks.length, 4);
assert.equal(board.hiddenBlocks[0].length, 4);
});
// Board.pieces
test('board.pieces returns sorted non-empty interior pieces', () => {
const board = new Board(puzzleText);
assert.deepEqual(board.pieces, ['/', '/', '\\', '\\', 'o']);
});
// Board.beamNames
test('board.beamNames returns sorted unique beam labels from edges', () => {
const board = new Board(puzzleText);
assert.deepEqual(board.beamNames, ['A', 'B', 'C', 'D', 'G', 'J']);
});
// Board.edgeLocations()
test('edgeLocations yields 4*n locations for an n×n board', () => {
const board = new Board(puzzleText);
assert.equal([...board.edgeLocations()].length, 4 * board.n);
});
// Board.getPath() — mirror physics
// Uses a minimal n=2 board (4×4 fullBoard) with a single piece in the interior.
function makeBoard(interiorChar) {
// n=2: fullBoard rows 0..3, cols 0..3
// interior is rows 1..2, cols 1..2
// Place interiorChar at (1,1) — row 1, col 1
return new Board(
`....\n.${interiorChar}..\n....\n....\n`
);
}
test('getPath: beam travels straight through empty board', () => {
const board = makeBoard('.');
const path = board.getPath(1, 0); // enter from top at col 1
assert.deepEqual(path, [[1, 0], [1, 1], [1, 2], [1, 3]]);
});
test('getPath: / mirror deflects beam left', () => {
const board = makeBoard('/');
const path = board.getPath(1, 0);
assert.deepEqual(path, [[1, 0], [1, 1], [0, 1]]);
});
test('getPath: \\ mirror deflects beam right', () => {
const board = makeBoard('\\');
const path = board.getPath(1, 0);
assert.deepEqual(path, [[1, 0], [1, 1], [2, 1], [3, 1]]);
});
test('getPath: o mirror ball reflects beam back', () => {
const board = makeBoard('o');
const path = board.getPath(1, 0);
assert.deepEqual(path, [[1, 0], [1, 1], [1, 0]]);
});
// Board.beamPaths — integration
test('beamPaths has one path per beam name', () => {
const board = new Board(puzzleText);
assert.equal(board.beamPaths.length, board.beamNames.length);
});
test('each beam path starts and ends at an edge location', () => {
const board = new Board(puzzleText);
const n1 = board.n + 1;
for (const path of board.beamPaths) {
const [sx, sy] = path[0];
const [ex, ey] = path[path.length - 1];
assert.ok(
sx === 0 || sx === n1 || sy === 0 || sy === n1,
`start (${sx},${sy}) not on edge`
);
assert.ok(
ex === 0 || ex === n1 || ey === 0 || ey === n1,
`end (${ex},${ey}) not on edge`
);
}
});