repositories / pi-ext

pi-ext

bugabingas pi extensions

owned by bugabinga

feat(the-system): expose doctor CLI

0d7d929cf27a2c60a1331c1121761aba8973f560

Oliver Jan Krylow <oliver@bugabinga.net> ·

unsigned

Browse filesCompare with HEAD

Changed files (3)

extensions/the-system/__tests__/doctor.test.ts

Modified
1 1 import assert from "node:assert/strict";
2 +import { spawnSync } from "node:child_process";
2 3 import {
3 4 mkdirSync,
4 5 mkdtempSync,
51 52 write(root, ".system/system.css", "/* The System styleVersion: 1 */\n");
55 +const snapshot = (root: string) => {
56 + const entries: { path: string; content?: string; mode: number; mtimeMs: number }[] = [];
57 + const visit = (directory: string) => {
58 + for (const entry of readdirSync(directory, { withFileTypes: true }).sort(
59 + (left, right) => left.name.localeCompare(right.name),
60 + )) {
61 + const target = path.join(directory, entry.name);
62 + const stats = statSync(target);
63 + const record = {
64 + path: path.relative(root, target).split(path.sep).join("/"),
65 + mode: stats.mode,
66 + mtimeMs: stats.mtimeMs,
67 + };
68 + entries.push(
69 + entry.isFile()
70 + ? { ...record, content: readFileSync(target, "utf8") }
71 + : record,
72 + );
73 + if (entry.isDirectory()) visit(target);
74 + }
75 + };
76 + visit(root);
77 + return entries;
78 +};
54 80 afterEach(() => {
55 81 for (const root of roots.splice(0))
56 82 rmSync(root, { recursive: true, force: true });
148 174 assert.deepEqual(result.references, []);
149 175 assert.equal(result.exitCode, 0);
178 + it("renders stable CLI output, returns status, and does not mutate the tree", () => {
179 + const root = temporaryRoot();
180 + validSystem(root);
181 + write(
182 + root,
183 + ".system/specs/PX-SPEC-001-contract.html",
184 + html({ id: "PX-SPEC-001", type: "spec" }),
185 + );
186 + write(root, "tests/contract.test.ts", "// SYSTEM-SPEC: PX-SPEC-001\n");
187 + const before = snapshot(root);
188 + const script = path.resolve(import.meta.dirname, "../scripts/doctor.mjs");
190 + const green = spawnSync(
191 + process.execPath,
192 + ["--experimental-strip-types", script, root],
193 + { encoding: "utf8", env: { ...process.env, NODE_NO_WARNINGS: "1" } },
194 + );
196 + assert.equal(green.status, 0);
197 + assert.equal(
198 + green.stdout,
199 + `ROOT ${root}\nREF tests/contract.test.ts PX-SPEC-001\nOK The System is valid\n`,
200 + );
201 + assert.equal(green.stderr, "");
202 + assert.deepEqual(snapshot(root), before);
204 + write(root, ".system/system.css", "unversioned\n");
205 + const brokenBefore = snapshot(root);
206 + const red = spawnSync(
207 + process.execPath,
208 + ["--experimental-strip-types", script, root],
209 + { encoding: "utf8", env: { ...process.env, NODE_NO_WARNINGS: "1" } },
210 + );
212 + assert.equal(red.status, 1);
213 + assert.equal(
214 + red.stdout,
215 + `ROOT ${root}\nREF tests/contract.test.ts PX-SPEC-001\nERROR css.version .system/system.css: Missing The System styleVersion marker\n`,
216 + );
217 + assert.equal(red.stderr, "");
218 + assert.deepEqual(snapshot(root), brokenBefore);
219 + });

extensions/the-system/documents.ts

Modified
5 5 export const STYLE_VERSION = 1;
7 7 export class SystemDocumentError extends Error {
8 - constructor(
9 - readonly code: string,
10 - message: string,
11 - ) {
8 + readonly code: string;
10 + constructor(code: string, message: string) {
12 11 super(message);
12 + this.code = code;
13 13 this.name = "SystemDocumentError";

extensions/the-system/scripts/doctor.mjs

Added
new file mode 100644
1 +#!/usr/bin/env node
2 +import { runDoctor } from "../doctor.ts";
4 +const result = runDoctor(process.argv[2] ?? process.cwd());
5 +console.log(`ROOT ${result.root}`);
6 +for (const reference of result.references)
7 + console.log(`REF ${reference.path} ${reference.spec}`);
8 +for (const finding of result.diagnostics)
9 + console.log(`ERROR ${finding.code} ${finding.path}: ${finding.message}`);
10 +if (result.exitCode === 0) console.log("OK The System is valid");
11 +process.exitCode = result.exitCode;