Public: anyone can read this; the crew can change it.

Every in-page link in the README lands on a heading that exists, and a test keeps it so #219

Parked opened this
Closed without merging

mason/deal-clock:mason/215-every-in-page-link-in-the-readme-lands-o → main 2 commits fork

  • Closed without merging. Nothing was applied to main.

Review

Sign in to review this change.
5 files changed +67 −3
src/db.ts +4
@@ -572,6 +572,10 @@ export const migrations: string[] = [
572572 // An inspection or a final walkthrough is an appointment: an optional time and place, like the closing's.
573573 `ALTER TABLE deadline ADD COLUMN time TEXT CHECK (time IS NULL OR time GLOB '[0-2][0-9]:[0-5][0-9]')`,
574574 `ALTER TABLE deadline ADD COLUMN place TEXT CHECK (place IS NULL OR length(place) BETWEEN 1 AND 200)`,
575 // Repair: a database whose version ran past the closing's time and place without the columns
576 // (migrations applied in another order on a branch) gets them. Tolerated as a duplicate where they exist.
577 `ALTER TABLE deal ADD COLUMN closing_time TEXT CHECK (closing_time IS NULL OR closing_time GLOB '[0-2][0-9]:[0-5][0-9]')`,
578 `ALTER TABLE deal ADD COLUMN closing_place TEXT CHECK (closing_place IS NULL OR length(closing_place) BETWEEN 1 AND 200)`,
575579 ];
576580
577581 export function openDb(dir = dataDir): Database {
src/health.test.ts +1 −1
@@ -29,7 +29,7 @@ test("a database missing profile.line answers 503 naming profile, and nothing el
2929 const db = new Database(":memory:");
3030 db.exec("CREATE TABLE schema_version (version INTEGER PRIMARY KEY, applied_at TEXT NOT NULL DEFAULT (strftime('%Y-%m-%dT%H:%M:%fZ', 'now')))");
3131 migrations
32 .filter((m) => !/profile ADD COLUMN line/.test(m))
32 .filter((m, i, all) => !/profile ADD COLUMN line/.test(m) && all.indexOf(m) === i)
3333 .forEach((sql, i) => {
3434 db.exec(sql);
3535 db.query("INSERT INTO schema_version (version) VALUES (?1)").run(i + 1);
src/migrations.pin.json +3 −1
@@ -57,5 +57,7 @@
5757 "7e011fb3dab2123101aa981ef9050d8f8d2c34fb3ba137ecc832f86d464b98a2",
5858 "429a2adfb36ff61def2d1979556a9db87f4758663fa9c56b97e599b3bc13b773",
5959 "c5df0d263e04d4e07f9df383beb57f033966af540bb228d9bbbd7b57f3620408",
60 "4a52d9de76ede6d34d831d9bc6fc1d3d3a501a5d3652bf63695a9de49f2a7d39"
60 "4a52d9de76ede6d34d831d9bc6fc1d3d3a501a5d3652bf63695a9de49f2a7d39",
61 "7255f8baa8700202884be4fbfbd925383182f00a049f8fe49df190cabbf90632",
62 "cf6972801626419644e27b9bc4e6d338367e1c6fe67bb5ca338dc0c9bb408fc1"
6163 ]
src/migrations.test.ts +10 −1
@@ -13,7 +13,7 @@ test("every migration is pinned by position; a pull may only append", () => {
1313 });
1414
1515 test("a database made before the Profile line gets the column and reads the agent", () => {
16 const before = migrations.filter((m) => !/profile ADD COLUMN line|commission_rate|profile ADD COLUMN zone|ADD COLUMN cash|'anniversary'|deadline ADD COLUMN (time|place)/.test(m));
16 const before = migrations.filter((m) => !/profile ADD COLUMN line|commission_rate|profile ADD COLUMN zone|ADD COLUMN cash|'anniversary'|deadline ADD COLUMN (time|place)/.test(m)).filter((m, i, all) => !/deal ADD COLUMN closing_(time|place)/.test(m) || all.findIndex((x) => x === m) === i);
1717 const db = new Database(":memory:");
1818 db.exec("CREATE TABLE schema_version (version INTEGER PRIMARY KEY, applied_at TEXT NOT NULL DEFAULT (strftime('%Y-%m-%dT%H:%M:%fZ', 'now')))");
1919 before.forEach((sql, i) => {
@@ -52,3 +52,12 @@ test("a database at every earlier version boots with its deals unchanged and cas
5252 expect(rows, `version ${version}`).toEqual(seeded ? [{ address: "1 Old St", cash: 0 }] : []);
5353 }
5454 });
55
56 test("a database whose version is current but lacks the closing's time and place gets both columns", () => {
57 const db = new Database(":memory:");
58 migrate(db);
59 db.exec("ALTER TABLE deal DROP COLUMN closing_time; ALTER TABLE deal DROP COLUMN closing_place");
60 db.exec("DELETE FROM schema_version WHERE version > " + (migrations.length - 2));
61 expect(migrate(db)).toBe(migrations.length);
62 expect(db.query("SELECT closing_time, closing_place FROM deal").all()).toEqual([]);
63 });
src/readme.test.ts added +49
@@ -0,0 +1,49 @@
1 import { expect, test } from "bun:test";
2 import { readFileSync } from "node:fs";
3
4 // GitHub-style heading slug: lowercase, punctuation dropped, spaces to hyphens.
5 function slug(heading: string): string {
6 return heading
7 .trim()
8 .toLowerCase()
9 .replace(/[^\p{L}\p{N}\s_-]/gu, "")
10 .replace(/\s/g, "-");
11 }
12
13 function anchors(markdown: string): Set<string> {
14 const seen = new Map<string, number>();
15 const out = new Set<string>();
16 let fenced = false;
17 for (const line of markdown.split("\n")) {
18 if (/^\s*(```|~~~)/.test(line)) fenced = !fenced;
19 if (fenced) continue;
20 const m = /^#{1,6}\s+(.*?)\s*#*\s*$/.exec(line);
21 if (!m) continue;
22 const base = slug(m[1]!.replace(/[`*_]/g, (c) => (c === "_" ? "_" : "")));
23 const n = seen.get(base) ?? 0;
24 seen.set(base, n + 1);
25 out.add(n === 0 ? base : `${base}-${n}`);
26 }
27 return out;
28 }
29
30 export function brokenAnchors(markdown: string): string[] {
31 const have = anchors(markdown);
32 const broken: string[] = [];
33 for (const m of markdown.matchAll(/\]\(#([^)\s]*)\)/g)) {
34 const a = decodeURIComponent(m[1]!).toLowerCase();
35 if (!have.has(a) && !broken.includes(a)) broken.push(a);
36 }
37 return broken;
38 }
39
40 test("every in-page link in README.md lands on a heading", () => {
41 const readme = readFileSync(new URL("../README.md", import.meta.url), "utf8");
42 expect(brokenAnchors(readme)).toEqual([]);
43 });
44
45 test("a broken anchor is reported, a good one and repeated headings are not", () => {
46 const doc = "# Top\n\n## Make it yours!\n\n## Same\n\n## Same\n\n" +
47 "[a](#make-it-yours) [b](#same-1) [c](#nowhere) [d](#top)\n";
48 expect(brokenAnchors(doc)).toEqual(["nowhere"]);
49 });

↑↓ navigate ↵ open ⌘↵ its app esc close

Keyboard shortcuts

Docs