Skip to content

Commit fee59c1

Browse files
TMHSDigitalclaude
andcommitted
site: move shared helpers to site/lib and add a lib test harness
First task of the entry-pages plan. esc, commercialLabel, verifiedAge and PERSPECTIVE_LABELS move from build.mjs to site/lib/shared.mjs, with STATUS_NOTES and entryPageUrl added for the page modules. The built index.html is byte-identical apart from timestamps. site/lib/lib.test.mjs (8 assertions) runs in both workflows. Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01Wc67NX9MM964uaVGn4bpZJ
1 parent 6b26b74 commit fee59c1

5 files changed

Lines changed: 111 additions & 42 deletions

File tree

‎.github/workflows/ci.yml‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,8 @@ jobs:
2525

2626
- name: Test catalog checks
2727
run: node site/checks.test.mjs
28+
- name: Test site build modules
29+
run: node site/lib/lib.test.mjs
2830

2931
- name: Validate catalog
3032
run: node site/validate.mjs

‎.github/workflows/pages.yml‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,8 @@ jobs:
3333

3434
- name: Test catalog checks
3535
run: node site/checks.test.mjs
36+
- name: Test site build modules
37+
run: node site/lib/lib.test.mjs
3638

3739
- name: Validate catalog
3840
run: node site/validate.mjs

‎site/build.mjs‎

Lines changed: 1 addition & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
import fs from "node:fs";
77
import path from "node:path";
88
import { fileURLToPath } from "node:url";
9+
import { commercialLabel, esc, PERSPECTIVE_LABELS, verifiedAge } from "./lib/shared.mjs";
910

1011
const __dirname = path.dirname(fileURLToPath(import.meta.url));
1112
const ROOT = path.resolve(__dirname, "..");
@@ -23,32 +24,6 @@ const OG_CARD_SRC = path.join(ROOT, "docs", "images", "readme", OG_CARD_NAME);
2324
/** Sort rank for "license permissiveness": least owed first. */
2425
const ATTRIBUTION_RANK = { none: 0, notice: 1, required: 2, any: 3 };
2526

26-
/** Readable labels for the optional 2D/UI taxonomy. */
27-
const PERSPECTIVE_LABELS = {
28-
top_down: "top-down",
29-
isometric_3_4: "3/4 view",
30-
side_scroller: "side-scroller",
31-
"2d_flat": "flat UI",
32-
};
33-
34-
/** Age buckets for the verified-date cue. Days. */
35-
const VERIFIED_FRESH_DAYS = 180;
36-
const VERIFIED_AGING_DAYS = 365;
37-
38-
function verifiedAge(verified, now) {
39-
if (!verified) return { days: null, bucket: "unknown" };
40-
const t = Date.parse(`${verified}T00:00:00Z`);
41-
if (Number.isNaN(t)) return { days: null, bucket: "unknown" };
42-
const days = Math.max(0, Math.floor((now - t) / 86400000));
43-
const bucket =
44-
days <= VERIFIED_FRESH_DAYS
45-
? "fresh"
46-
: days <= VERIFIED_AGING_DAYS
47-
? "aging"
48-
: "stale";
49-
return { days, bucket };
50-
}
51-
5227
function parseScalar(raw) {
5328
const v = raw.trim();
5429
if (v === "true") return true;
@@ -191,22 +166,6 @@ function loadEntries(vocab) {
191166
return { entries, errors };
192167
}
193168

194-
function esc(str) {
195-
return String(str)
196-
.replaceAll("&", "&amp;")
197-
.replaceAll("<", "&lt;")
198-
.replaceAll(">", "&gt;")
199-
.replaceAll('"', "&quot;")
200-
.replaceAll("'", "&#39;");
201-
}
202-
203-
function commercialLabel(v) {
204-
if (v === true) return "commercial OK";
205-
if (v === false) return "non-commercial";
206-
if (v === "varies") return "per-file review";
207-
return "commercial ?";
208-
}
209-
210169
function edgeVar(entry) {
211170
if (entry.commercial === true) return "var(--ok)";
212171
if (entry.commercial === false) return "var(--danger)";

‎site/lib/lib.test.mjs‎

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
#!/usr/bin/env node
2+
/**
3+
* Tests for the site's build modules. Run: node site/lib/lib.test.mjs
4+
* Each task appends a section; the report block stays last.
5+
*/
6+
import {
7+
commercialLabel,
8+
entryPageUrl,
9+
esc,
10+
STATUS_NOTES,
11+
verifiedAge,
12+
} from "./shared.mjs";
13+
14+
let passed = 0;
15+
const failures = [];
16+
17+
function eq(label, actual, expected) {
18+
if (actual === expected) passed += 1;
19+
else failures.push(`${label}: expected ${JSON.stringify(expected)}, got ${JSON.stringify(actual)}`);
20+
}
21+
function has(label, text, needle) {
22+
if (String(text).includes(needle)) passed += 1;
23+
else failures.push(`${label}: expected to contain ${JSON.stringify(needle)}`);
24+
}
25+
function lacks(label, text, needle) {
26+
if (!String(text).includes(needle)) passed += 1;
27+
else failures.push(`${label}: expected not to contain ${JSON.stringify(needle)}`);
28+
}
29+
function throws(label, fn, needle) {
30+
try {
31+
fn();
32+
failures.push(`${label}: expected an error`);
33+
} catch (err) {
34+
if (!needle || String(err.message).includes(needle)) passed += 1;
35+
else failures.push(`${label}: error did not mention ${JSON.stringify(needle)}; got ${err.message}`);
36+
}
37+
}
38+
39+
/* shared ----------------------------------------------------------------- */
40+
eq("esc escapes all five characters", esc(`<a href="x">'&'</a>`), "&lt;a href=&quot;x&quot;&gt;&#39;&amp;&#39;&lt;/a&gt;");
41+
eq("commercialLabel true", commercialLabel(true), "commercial OK");
42+
eq("commercialLabel varies", commercialLabel("varies"), "per-file review");
43+
eq("commercialLabel unknown", commercialLabel("unknown"), "commercial ?");
44+
eq("verifiedAge fresh", verifiedAge("2026-09-01", Date.parse("2026-09-24T00:00:00Z")).bucket, "fresh");
45+
eq("verifiedAge missing", verifiedAge(null, Date.now()).bucket, "unknown");
46+
eq("entryPageUrl trims the site slash", entryPageUrl({ siteUrl: "https://x.test/site/" }, "a-b"), "https://x.test/site/entry/a-b/");
47+
has("STATUS_NOTES covers needs-review", STATUS_NOTES["needs-review"], "open");
48+
49+
/* report (keep last) ------------------------------------------------------ */
50+
if (failures.length) {
51+
console.error(`lib.test failed (${failures.length}):`);
52+
for (const f of failures) console.error(` - ${f}`);
53+
process.exit(1);
54+
}
55+
console.log(`lib.test ok: ${passed} assertions`);

‎site/lib/shared.mjs‎

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
/** Helpers shared by build.mjs and the page modules. */
2+
3+
/** Readable labels for the optional 2D/UI taxonomy. */
4+
export const PERSPECTIVE_LABELS = {
5+
top_down: "top-down",
6+
isometric_3_4: "3/4 view",
7+
side_scroller: "side-scroller",
8+
"2d_flat": "flat UI",
9+
};
10+
11+
/** One-line meaning of each status, matching the homepage legend. */
12+
export const STATUS_NOTES = {
13+
active: "License spot-checked at the source; the entry carries dated evidence.",
14+
"needs-review": "Useful, but a real question is open. Verify before you ship.",
15+
deprecated: "Kept for history only.",
16+
};
17+
18+
/** Age buckets for the verified-date cue. Days. */
19+
const VERIFIED_FRESH_DAYS = 180;
20+
const VERIFIED_AGING_DAYS = 365;
21+
22+
export function verifiedAge(verified, now) {
23+
if (!verified) return { days: null, bucket: "unknown" };
24+
const t = Date.parse(`${verified}T00:00:00Z`);
25+
if (Number.isNaN(t)) return { days: null, bucket: "unknown" };
26+
const days = Math.max(0, Math.floor((now - t) / 86400000));
27+
const bucket =
28+
days <= VERIFIED_FRESH_DAYS ? "fresh" : days <= VERIFIED_AGING_DAYS ? "aging" : "stale";
29+
return { days, bucket };
30+
}
31+
32+
export function esc(str) {
33+
return String(str)
34+
.replaceAll("&", "&amp;")
35+
.replaceAll("<", "&lt;")
36+
.replaceAll(">", "&gt;")
37+
.replaceAll('"', "&quot;")
38+
.replaceAll("'", "&#39;");
39+
}
40+
41+
export function commercialLabel(v) {
42+
if (v === true) return "commercial OK";
43+
if (v === false) return "non-commercial";
44+
if (v === "varies") return "per-file review";
45+
return "commercial ?";
46+
}
47+
48+
/** Absolute URL of an entry's page. */
49+
export function entryPageUrl(site, id) {
50+
return `${String(site.siteUrl).replace(/\/+$/, "")}/entry/${id}/`;
51+
}

0 commit comments

Comments
 (0)