Split The code is public; the app answers only to the crew.
The ids `main.ts` reaches for are a contract nothing checks #127
Check · Review · Land · Live done — Landed and live in production.
scout/cs:scout/99-the-ids-main-ts-reaches-for-are-a-contra → main 1 commit fork
1 file
changed +94
−0
src/client/main.test.ts added +94
| @@ -0,0 +1,94 @@ | ||
| 1 | /** | |
| 2 | * main.ts reaches into the page by id forty-odd times, and by a handful of | |
| 3 | * querySelectors, and casts whatever it finds — nothing ties either list to | |
| 4 | * the markup gamePage() actually renders, so an id renamed on either side is | |
| 5 | * a TypeError on the first frame, not a test failure. This reads main.ts's | |
| 6 | * own source text for every `$("…")` lookup and every querySelector / | |
| 7 | * querySelectorAll / closest call with a literal selector, and checks each | |
| 8 | * one against gamePage()'s output — except the handful main.ts writes into | |
| 9 | * the page itself (the buy menu, the radio panel, the controls panel), which | |
| 10 | * are named here explicitly rather than inferred, since that is the one | |
| 11 | * place this test cannot check itself. | |
| 12 | */ | |
| 13 | ||
| 14 | import { expect, test } from "bun:test"; | |
| 15 | import { mkdtempSync, readFileSync } from "node:fs"; | |
| 16 | import { tmpdir } from "node:os"; | |
| 17 | import { join } from "node:path"; | |
| 18 | import { openDatabase } from "../db.ts"; | |
| 19 | import { credit } from "../player.ts"; | |
| 20 | import { gamePage } from "../pages/game.ts"; | |
| 21 | ||
| 22 | const mainSource = readFileSync(join(import.meta.dir, "main.ts"), "utf8"); | |
| 23 | ||
| 24 | const db = openDatabase(mkdtempSync(join(tmpdir(), "main-dom-"))); | |
| 25 | const player = credit({ role: "anon" }, "1".repeat(32)); | |
| 26 | const html = gamePage(db, { | |
| 27 | env: "development", pull: undefined, commit: "", script: "/static/app.js", ssh: undefined, player, | |
| 28 | }); | |
| 29 | ||
| 30 | // --- ids, from `$("id")` and `$<Type>("id")` — the only two shapes the file | |
| 31 | // uses `$` in — plus the one dynamic shape: the crosshair panel, built once | |
| 32 | // per prefix ("menu", "pause") and looked up with a template literal. | |
| 33 | const ID_LOOKUP = /\$(?:<[^>]+>)?\("([a-z0-9-]+)"\)/g; | |
| 34 | const PREFIXED_ID_LOOKUP = /\$(?:<[^>]+>)?\(`\$\{prefix\}([a-z0-9-]*)`\)/g; | |
| 35 | ||
| 36 | const plainIds = [...mainSource.matchAll(ID_LOOKUP)].map((m) => m[1] as string); | |
| 37 | const prefixedIds = [...mainSource.matchAll(PREFIXED_ID_LOOKUP)] | |
| 38 | .flatMap((m) => (["menu", "pause"] as const).map((prefix) => `${prefix}${m[1]}`)); | |
| 39 | const ids = [...plainIds, ...prefixedIds]; | |
| 40 | ||
| 41 | // Markup main.ts writes into the page itself — the buy panel and the radio | |
| 42 | // panel are empty in gamePage() and filled in once main.ts runs — so these | |
| 43 | // three ids never appear in gamePage()'s own output. Named here rather than | |
| 44 | // inferred, since nothing can check this line against itself. | |
| 45 | const WRITTEN_BY_MAIN_TS = ["buy-close", "buy-money", "buy-clock", "radio-close"]; | |
| 46 | ||
| 47 | test("main.ts still finds ids the way it always has", () => { | |
| 48 | // A regex that stopped matching `$("…")` at all would make the next test | |
| 49 | // vacuously pass on an empty list; this is the guard that catches that. | |
| 50 | expect(ids.length).toBeGreaterThan(30); | |
| 51 | }); | |
| 52 | ||
| 53 | test("every id main.ts looks up is on the page, or is markup main.ts wrote itself", () => { | |
| 54 | for (const id of ids) { | |
| 55 | if (WRITTEN_BY_MAIN_TS.includes(id)) continue; | |
| 56 | expect(html).toContain(`id="${id}"`); | |
| 57 | } | |
| 58 | }); | |
| 59 | ||
| 60 | test("the ids main.ts writes into the page are ones it actually writes, not stray allowances", () => { | |
| 61 | for (const id of WRITTEN_BY_MAIN_TS) { | |
| 62 | expect(mainSource).toContain(`id="${id}"`); | |
| 63 | // Never on the page gamePage() renders — an id in both places would mean | |
| 64 | // the allow-list is hiding a real duplicate rather than excusing one. | |
| 65 | expect(html).not.toContain(`id="${id}"`); | |
| 66 | } | |
| 67 | }); | |
| 68 | ||
| 69 | // --- querySelectors, the same way: every call with a literal selector, | |
| 70 | // against markup main.ts did not write. | |
| 71 | const SELECTOR_CALL = /\.(?:querySelector|querySelectorAll|closest)(?:<[^>]+>)?\("([^"]+)"\)/g; | |
| 72 | const selectors = [...new Set([...mainSource.matchAll(SELECTOR_CALL)].map((m) => m[1] as string))]; | |
| 73 | ||
| 74 | // Selectors main.ts runs against markup it wrote into the page itself: the | |
| 75 | // controls panel's own buttons, the buy grid it builds, the radio panel's | |
| 76 | // call buttons. `.grid` inside `#buy` is its own the same way these are. | |
| 77 | const SELF_WRITTEN_SELECTORS = [ | |
| 78 | "button.bind", ".bind-status", ".reset-bindings", ".grid", "button", ".buy-line", ".buy-why", ".radio-call", | |
| 79 | ]; | |
| 80 | ||
| 81 | test("main.ts still finds selectors the way it always has", () => { | |
| 82 | expect(selectors.length).toBeGreaterThan(5); | |
| 83 | }); | |
| 84 | ||
| 85 | test("every selector main.ts runs against the page it did not write matches something there", () => { | |
| 86 | for (const selector of selectors) { | |
| 87 | if (SELF_WRITTEN_SELECTORS.includes(selector)) continue; | |
| 88 | // The only shape left reaching into the page is a bare tag name — | |
| 89 | // `details` and `summary`, the structure crosshairControls and the | |
| 90 | // Controls panel both use. | |
| 91 | expect(selector).toMatch(/^[a-z]+$/); | |
| 92 | expect(html).toContain(`<${selector}`); | |
| 93 | } | |
| 94 | }); |
new above
offline