repositories / pi-ext

pi-ext

bugabingas pi extensions

owned by bugabinga

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

Raw
File outline
import assert from "node:assert/strict";
import { spawnSync } from "node:child_process";
import {
	lstatSync,
	mkdirSync,
	mkdtempSync,
	readFileSync,
	rmSync,
	symlinkSync,
	writeFileSync,
} from "node:fs";
import { tmpdir } from "node:os";
import path from "node:path";
import { afterEach, describe, it } from "vitest";
import { runDoctor } from "../doctor.ts";
import { SystemDocumentError } from "../documents.ts";
import {
	applyInitialization,
	inspectInitialization,
	type MachineState,
} from "../init.ts";

const roots: string[] = [];
const root = () => {
	const value = mkdtempSync(path.join(tmpdir(), "the-system-init-"));
	roots.push(value);
	mkdirSync(path.join(value, ".git"));
	return value;
};
const write = (project: string, relative: string, content: string) => {
	const target = path.join(project, relative);
	mkdirSync(path.dirname(target), { recursive: true });
	writeFileSync(target, content, "utf8");
};
const cores = (project: string) => {
	for (const name of ["SYSTEM.md", "MISSION.md", "RULES.md"])
		write(project, `.system/${name}`, `# ${name}\n\nApproved.\n`);
};
const legacy = '{ "prefix": "PX", "schemaVersion": 1, "styleVersion": 1 }\n';
const future: MachineState = { schemaVersion: 3 };
afterEach(() => {
	for (const value of roots.splice(0))
		rmSync(value, { recursive: true, force: true });
});

describe("guided initialization machine state", () => {
	it("creates only schema-v2 config and preserves cores on rerun", () => {
		const project = root();
		const cwd = path.join(project, "packages", "app");
		mkdirSync(cwd, { recursive: true });
		const script = path.resolve(import.meta.dirname, "../scripts/init.mjs");
		const child = spawnSync(
			process.execPath,
			["--experimental-strip-types", script, "apply", cwd, "PX"],
			{ encoding: "utf8", env: { ...process.env, NODE_NO_WARNINGS: "1" } },
		);
		assert.equal(child.status, 0);
		assert.match(child.stdout, /WROTE \.system\/config\.json/);
		assert.doesNotMatch(child.stdout, /system\.css/);
		assert.equal(
			readFileSync(path.join(project, ".system/config.json"), "utf8"),
			'{\n\t"prefix": "PX",\n\t"schemaVersion": 2\n}\n',
		);
		assert.equal(
			lstatSync(path.join(project, ".system/system.css"), {
				throwIfNoEntry: false,
			}),
			undefined,
		);
		cores(project);
		const before = ["SYSTEM.md", "MISSION.md", "RULES.md"].map((name) => {
			const target = path.join(project, ".system", name);
			return [name, readFileSync(target, "utf8"), lstatSync(target).mtimeMs];
		});
		assert.deepEqual(applyInitialization(project, "PX"), {
			root: project,
			prefix: "PX",
			changed: [],
			removed: [],
			pending: [],
		});
		for (const [name, source, mtime] of before) {
			const target = path.join(project, ".system", name);
			assert.deepEqual(
				[readFileSync(target, "utf8"), lstatSync(target).mtimeMs],
				[source, mtime],
			);
		}
		assert.equal(runDoctor(project).exitCode, 0);
	});

	it("holds legacy config and CSS pending, then upgrades only when approved", () => {
		const project = root();
		write(project, ".system/config.json", legacy);
		write(project, ".system/system.css", "legacy css\n");
		cores(project);
		const upgrades = [
			".system/config.json schemaVersion 1 -> 2; remove styleVersion",
			".system/system.css remove obsolete machine-owned asset",
		];
		assert.deepEqual(inspectInitialization(project).upgrades, upgrades);
		assert.deepEqual(applyInitialization(project).pending, upgrades);
		assert.equal(
			readFileSync(path.join(project, ".system/config.json"), "utf8"),
			legacy,
		);
		assert.equal(
			readFileSync(path.join(project, ".system/system.css"), "utf8"),
			"legacy css\n",
		);
		const result = applyInitialization(project, undefined, true);
		assert.deepEqual(result.changed, [".system/config.json"]);
		assert.deepEqual(result.removed, [".system/system.css"]);
		assert.equal(
			lstatSync(path.join(project, ".system/system.css"), {
				throwIfNoEntry: false,
			}),
			undefined,
		);
	});

	it("rejects invalid prefixes, newer configs, and unsafe obsolete CSS paths", () => {
		const project = root();
		assert.throws(() => applyInitialization(project), /prefix is required/i);
		assert.throws(() => applyInitialization(project, "x"), SystemDocumentError);
		applyInitialization(project, "PX");
		assert.throws(
			() => inspectInitialization(project, "AB"),
			/prefix.*does not match/i,
		);
		write(project, ".system/config.json", '{"prefix":"PX","schemaVersion":4}');
		assert.throws(
			() => inspectInitialization(project, undefined, future),
			/newer/i,
		);

		const linked = root();
		write(linked, ".system/config.json", legacy);
		symlinkSync("elsewhere.css", path.join(linked, ".system/system.css"));
		assert.throws(
			() => inspectInitialization(linked),
			(error: unknown) =>
				error instanceof SystemDocumentError &&
				error.code === "init.unsafe-path",
		);
	});
});