Files
tiny-worlds/test/check-repo.mjs
T

164 lines
7.1 KiB
JavaScript

// test/check-repo.mjs -- the repo's rules, enforced. Static scan of
// git-tracked files; no server needed:
// node test/check-repo.mjs
//
// 1. ASCII bytes everywhere (GM, 2026-11-15). No unicode in any
// tracked file; gitignored state (runs/, state/, server.json) is
// per-deployment and exempt. The ENCODING is the problem, never
// the visual: rendered glyphs travel ASCII-safe (\u escapes in
// JS, entities in HTML), never verbatim bytes.
// 2. No text-mutating CSS (GM, 2026-11-15). Rendered text must
// match authored text: no text-transform (any value, inline
// styles included), no generated content (content:, ::before and
// ::after included), no caps font variants. Authored content is
// fine -- want caps on screen, write caps in the source.
// 3. No HTML comments in prose or prompt files (GM, 2026-11-14):
// purpose descriptions live in README, AGENTS, or code comments.
// gm.md files are exempt -- GM-private, flagged to the GM, never
// agent-edited.
// 4. Retired terms stay retired (DICTIONARY rules the terms). The
// living prose -- README, the root prompts, the packages --
// never says: the wrap, prelude, paper (the old status), window
// (the old session; a line naming contwin may gloss it as the
// context window), turn-0, seed (memory.json is the name).
// Historical records keep the vocabulary of
// their day; gm.md is GM-private, flagged never fixed.
// 5. Zero-dependency runtime (GM, 2026-11-13, amended 2026-11-15):
// every import is node: builtin or relative -- no runtime deps,
// no package-manager requirement to run. Dev tooling is exempt
// (typescript rides devDependencies; nothing imports it).
// 6. UTC discipline (GM, 2026-11-14): no locale- or timezone-
// dependent date formatting in code. No time zones, ever.
// 7. web/ static files never start with "api" -- the server's route
// guard matches the /api prefix, so web/api.mjs 404s as a bogus
// API route (bit once, 2026-11-14).
// 8. The bundle boundary (GM, 2026-11-14): server/bundle.mjs names
// neither gm.md (never model-facing) nor housekeeper.md (agent
// prompts never enter the bundle).
// 9. Seeds carry no date keys (GM, 2026-11-14): no model-facing
// surface renders a date; story time lives in prose.
import { execFileSync } from 'node:child_process';
import { readFileSync } from 'node:fs';
let failures = 0;
const fail = (msg) => { console.log(`FAIL ${msg}`); failures++; };
const files = execFileSync('git', ['ls-files'], { encoding: 'utf8' }).split('\n').filter(Boolean);
const ROOT_PROMPTS = new Set(['system.md', 'invariants.md', 'handbook.md', 'housekeeper.md', 'reminder.md']);
const livingProse = (f) =>
f === 'README.md' || ROOT_PROMPTS.has(f)
|| (f.startsWith('packages/') && !f.endsWith('gm.md'));
// the checker's own source names what it bans (the rule patterns and
// their comments); it is exempt from the rules it defines
const isSelf = (f) => f === 'test/check-repo.mjs';
for (const f of files) {
const buf = readFileSync(f);
if (buf.includes(0)) continue; // binary -- no text rules to enforce
const text = buf.toString('utf8');
// rule 1: ascii bytes
text.split('\n').forEach((line, i) => {
for (const ch of line) {
if (ch.charCodeAt(0) > 0x7f) {
fail(`${f}:${i + 1} non-ascii U+${ch.charCodeAt(0).toString(16).padStart(4, '0')} "${ch}"`);
break; // one report per line is plenty
}
}
});
// rule 2: no text-mutating css. text-transform is linted in every
// code file (it only ever appears as a css property -- inline
// style attributes included); content: and font-variant are css
// file checks (content is a common js object key, and the docs
// discuss the rule by name)
if (/\.(css|html|mjs|js)$/.test(f) && !isSelf(f)) {
text.split('\n').forEach((line, i) => {
if (/text-transform/i.test(line)) {
fail(`${f}:${i + 1} text-mutating css: text-transform`);
}
});
}
if (f.endsWith('.css')) {
const stripped = text.replace(/\/\*[\s\S]*?\*\//g, ''); // comments are prose, not style
stripped.split('\n').forEach((line, i) => {
if (/(?<![-\w])content:\s/.test(line)) {
fail(`${f}:${i + 1} text-mutating css: generated content (${line.trim().slice(0, 60)})`);
}
if (/font-variant[^;]*caps/.test(line)) {
fail(`${f}:${i + 1} text-mutating css: caps font variant`);
}
});
}
// rule 3: no html comments in prose/prompt files
if (f.endsWith('.md') && !f.endsWith('gm.md')) {
text.split('\n').forEach((line, i) => {
if (line.includes('<!--')) fail(`${f}:${i + 1} html comment in a prose file`);
});
}
// rule 4: retired terms in living prose
if (livingProse(f)) {
text.split('\n').forEach((line, i) => {
if (/\bthe wrap\b/i.test(line)) fail(`${f}:${i + 1} retired term: "the wrap" (housekeeping)`);
if (/prelude/i.test(line)) fail(`${f}:${i + 1} retired term: "prelude" (turn-0)`);
if (/\bpaper\b/i.test(line)) fail(`${f}:${i + 1} retired term: "paper" (status)`);
// lowercase "window(s)" is the retired session; "Windows" the OS
// is consumed as-is; a line naming contwin may gloss its meaning
if (!/contwin/i.test(line) && /\bwindows?\b/.test(line)) {
fail(`${f}:${i + 1} retired term: "window" (session / contwin)`);
}
if (/turn-0/i.test(line)) fail(`${f}:${i + 1} retired term: "turn-0" (the carryover)`);
if (/\bseed(ed|s|ing)?\b/i.test(line)) fail(`${f}:${i + 1} retired term: "seed" (memory.json -- her starting memories)`);
});
}
// rule 5: zero-dependency imports
if (f.endsWith('.mjs')) {
for (const m of text.matchAll(/from\s+['"]([^'"]+)['"]/g)) {
const spec = m[1];
if (!spec.startsWith('node:') && !spec.startsWith('./') && !spec.startsWith('../')) {
fail(`${f}: non-builtin import "${spec}" -- the stack is zero-dependency`);
}
}
}
// rule 6: utc discipline
if (/\.(mjs|js)$/.test(f) && !isSelf(f)) {
text.split('\n').forEach((line, i) => {
if (/toLocale|getTimezoneOffset/.test(line)) {
fail(`${f}:${i + 1} locale/timezone-dependent formatting (UTC everywhere)`);
}
});
}
// rule 7: web static naming
if (f.startsWith('web/') && f.split('/').pop().startsWith('api')) {
fail(`${f}: a web/ static name starting with "api" 404s as a bogus API route`);
}
// rule 8: the bundle boundary
if (f === 'server/bundle.mjs') {
text.split('\n').forEach((line, i) => {
if (/gm\.md|housekeeper\.md/.test(line)) {
fail(`${f}:${i + 1} the bundle must not name gm.md or housekeeper.md`);
}
});
}
// rule 9: dateless memory -- no model-facing surface renders a date;
// story time lives in prose
if (f === 'memory.json' || (f.startsWith('packages/') && f.endsWith('memory.json'))) {
text.split('\n').forEach((line, i) => {
if (/"[^"]*date[^"]*"\s*:/i.test(line)) {
fail(`${f}:${i + 1} a memory manifest carries a date key -- story time lives in prose`);
}
});
}
}
console.log(failures ? `\n${failures} VIOLATIONS` : 'ALL CLEAN');
process.exit(failures ? 1 : 0);