90 lines
3.9 KiB
TypeScript
90 lines
3.9 KiB
TypeScript
import assert from 'node:assert/strict';
|
||
import { readFileSync } from 'node:fs';
|
||
import { test } from 'node:test';
|
||
import * as map from '../src/map.ts';
|
||
import { GRID, LINES, LINES_AT, LOCATIONS, levelFor, otherEnd, useBoard } from '../src/map.ts';
|
||
import { DEFAULT_RULES, withRule } from '../src/rules.ts';
|
||
|
||
const RULES_MD = readFileSync(new URL('../../rules.md', import.meta.url), 'utf8');
|
||
|
||
test('22 lines, 20 of them with gateways', () => {
|
||
assert.equal(LINES.length, 22);
|
||
assert.equal(LINES.filter((l) => l.gateway).length, 20);
|
||
});
|
||
|
||
test('adjacency matches the table in rules.md', () => {
|
||
const table = RULES_MD.split('### Adjacency')[1].split('###')[0];
|
||
const rows = [...table.matchAll(/^\| (S1|S2|[A-E]\d) \| ([^|]+) \|$/gm)];
|
||
assert.equal(rows.length, LOCATIONS.length);
|
||
for (const [, loc, list] of rows) {
|
||
const expected = list.split(',').map((x) => x.trim()).sort();
|
||
const actual = LINES_AT.get(loc)!.map((l) => otherEnd(l, loc)).sort();
|
||
assert.deepEqual(actual, expected, loc);
|
||
}
|
||
});
|
||
|
||
test('levels are mirrored between the players', () => {
|
||
const mirror = (loc: string) => ({ A: 'E', B: 'D', C: 'C', D: 'B', E: 'A' })[loc[0]]! + loc[1];
|
||
for (const loc of GRID) assert.equal(levelFor(0, loc), levelFor(1, mirror(loc)), loc);
|
||
assert.equal(levelFor(0, 'S1'), 1);
|
||
assert.equal(levelFor(0, 'A2'), 1);
|
||
assert.equal(levelFor(0, 'B1'), 2);
|
||
assert.equal(levelFor(0, 'C2'), 3);
|
||
assert.equal(levelFor(0, 'D1'), 4);
|
||
assert.equal(levelFor(0, 'E3'), 5);
|
||
});
|
||
|
||
test('the 4×4 board: squares joined side to side, levels 1–4, starts join the middle of their row', () => {
|
||
useBoard('grid4');
|
||
try {
|
||
assert.equal(map.GRID.length, 16);
|
||
assert.equal(map.LINES.filter((l) => l.gateway).length, 24);
|
||
const near = (loc: string) => map.LINES_AT.get(loc)!.map((l) => otherEnd(l, loc)).sort();
|
||
assert.deepEqual(near('S1'), ['A2', 'A3']);
|
||
assert.deepEqual(near('S2'), ['D2', 'D3']);
|
||
assert.deepEqual(near('B2'), ['A2', 'B1', 'B3', 'C2']);
|
||
assert.deepEqual(near('A1'), ['A2', 'B1']);
|
||
const rotate = (loc: string) =>
|
||
({ S1: 'S2', S2: 'S1' })[loc] ?? 'DCBA'['ABCD'.indexOf(loc[0])] + (5 - Number(loc[1]));
|
||
for (const loc of map.GRID) {
|
||
assert.equal(levelFor(0, loc), levelFor(1, rotate(loc)), loc);
|
||
assert.deepEqual(near(loc).map(rotate).sort(), near(rotate(loc)), loc);
|
||
}
|
||
assert.equal(levelFor(0, 'A4'), 1);
|
||
assert.equal(levelFor(0, 'D1'), 4);
|
||
assert.equal(levelFor(1, 'A1'), 4);
|
||
} finally {
|
||
useBoard('classic');
|
||
}
|
||
});
|
||
|
||
test('the lanes board: three lanes of five, linked only across the middle row', () => {
|
||
useBoard('lanes');
|
||
try {
|
||
assert.equal(map.GRID.length, 15);
|
||
assert.equal(map.LINES.filter((l) => l.gateway).length, 14);
|
||
const near = (loc: string) => map.LINES_AT.get(loc)!.map((l) => otherEnd(l, loc)).sort();
|
||
assert.deepEqual(near('S1'), ['A1', 'A2', 'A3']);
|
||
assert.deepEqual(near('S2'), ['E1', 'E2', 'E3']);
|
||
assert.deepEqual(near('B1'), ['A1', 'C1']);
|
||
assert.deepEqual(near('C2'), ['B2', 'C1', 'C3', 'D2']);
|
||
const rotate = (loc: string) =>
|
||
({ S1: 'S2', S2: 'S1' })[loc] ?? 'EDCBA'['ABCDE'.indexOf(loc[0])] + (4 - Number(loc[1]));
|
||
for (const loc of map.GRID) {
|
||
assert.equal(levelFor(0, loc), levelFor(1, rotate(loc)), loc);
|
||
assert.deepEqual(near(loc).map(rotate).sort(), near(rotate(loc)), loc);
|
||
}
|
||
assert.equal(levelFor(0, 'C1'), 3);
|
||
useBoard('lanes-open');
|
||
assert.deepEqual(map.LINES.filter((l) => l.gateway).map((l) => l.id), ['C1-C2', 'C2-C3']);
|
||
} finally {
|
||
useBoard('classic');
|
||
}
|
||
});
|
||
|
||
test('board=grid4 wins with 12, unless a later winLocations changes it', () => {
|
||
assert.equal(withRule(DEFAULT_RULES, 'board=grid4').winLocations, 12);
|
||
assert.equal(withRule(withRule(DEFAULT_RULES, 'board=grid4'), 'winLocations=11').winLocations, 11);
|
||
assert.equal(withRule(DEFAULT_RULES, 'board=lanes').winLocations, 11);
|
||
assert.throws(() => withRule(DEFAULT_RULES, 'board=hex'), /Unknown board/);
|
||
});
|