repositories / pi-ext
pi-ext
bugabingas pi extensions
owned by bugabinga
extensions/the-system/__tests__/system.test.ts
RawFile outline
- roots line 20
- root line 21
- write line 30
- documentSource line 35
- specMetadata line 42
- writeSpec line 47
- approval line 52
- afterEach line 57
- runtime notice line 62
- stays quiet until the project opts into .system line 63
- runtime core injection line 73
- injects cores and reports malformed core paths line 74
- runtime bundle governance line 84
- does not govern mutations outside the detected project root line 85
- allows valid new index candidates and blocks invalid layouts and metadata before approval line 102
- requires a valid index before assets and approves every existing spec file line 152
- validates editable non-spec indexes and blocks done phase and plan bundles line 218
- validates computed edits before approval and rejects symlinks line 323
import assert from "node:assert/strict";
import {
mkdirSync,
mkdtempSync,
rmSync,
symlinkSync,
writeFileSync,
} from "node:fs";
import { tmpdir } from "node:os";
import path from "node:path";
import { afterEach, describe, it } from "vitest";
import { applyInitialization } from "../init.ts";
import {
CORE_BYTE_LIMIT,
composeSystemPrompt,
preflightFileMutation,
runtimeNotice,
} from "../system.ts";
const roots: string[] = [];
const root = () => {
const value = mkdtempSync(path.join(tmpdir(), "the-system-runtime-"));
roots.push(value);
mkdirSync(path.join(value, ".git"));
applyInitialization(value, "PX");
for (const name of ["SYSTEM.md", "MISSION.md", "RULES.md"])
write(value, `.system/${name}`, `# ${name}\n\nApproved.\n`);
return value;
};
const write = (root: string, relative: string, content: string) => {
const target = path.join(root, relative);
mkdirSync(path.dirname(target), { recursive: true });
writeFileSync(target, content, "utf8");
};
const documentSource = (metadata: Record<string, string | readonly string[]>) =>
`---\n${Object.entries(metadata)
.map(
([key, value]) =>
`${key}: ${Array.isArray(value) ? `[${value.join(", ")}]` : value}`,
)
.join("\n")}\n---\n\n# Contract\n\nApproved.\n`;
const specMetadata = {
id: "PX-SPEC-SPEC0001",
type: "spec",
title: "Contract",
};
const writeSpec = (project: string) => {
const relative = ".system/specs/PX-SPEC-SPEC0001-contract/index.md";
write(project, relative, documentSource(specMetadata));
return relative;
};
const approval = (hasUI = true) => ({
hasUI,
select: async (_title: string, items: string[]) => items[0],
});
afterEach(() => {
for (const value of roots.splice(0))
rmSync(value, { recursive: true, force: true });
});
describe("runtime notice", () => {
it("stays quiet until the project opts into .system", () => {
const project = mkdtempSync(path.join(tmpdir(), "the-system-runtime-"));
roots.push(project);
mkdirSync(path.join(project, ".git"));
assert.equal(runtimeNotice(project), undefined);
mkdirSync(path.join(project, ".system"));
assert.match(runtimeNotice(project) ?? "", /incomplete or invalid/);
});
});
describe("runtime core injection", () => {
it("injects cores and reports malformed core paths", () => {
const project = root();
write(project, ".system/MISSION.md", "x".repeat(CORE_BYTE_LIMIT + 1));
assert.match(composeSystemPrompt(project, "BASE"), /MISSION\.md exceeds/);
rmSync(path.join(project, ".system/SYSTEM.md"));
mkdirSync(path.join(project, ".system/SYSTEM.md"));
assert.match(composeSystemPrompt(project, "BASE"), /governance error/);
});
});
describe("runtime bundle governance", () => {
it("does not govern mutations outside the detected project root", async () => {
const project = root();
const outside = path.join(path.dirname(project), "outside.md");
assert.equal(
(
await preflightFileMutation(
project,
"write",
{ path: outside, content: "outside" },
approval(),
)
)?.block,
undefined,
);
});
it("allows valid new index candidates and blocks invalid layouts and metadata before approval", async () => {
const project = root();
assert.equal(
(
await preflightFileMutation(
project,
"write",
{
path: ".system/specs/PX-SPEC-SPEC0001-contract/index.md",
content: documentSource(specMetadata),
},
approval(),
)
)?.block,
undefined,
);
for (const input of [
{
path: ".system/spec/PX-SPEC-SPEC0002-broken/index.md",
content: documentSource({
id: "PX-SPEC-SPEC0002",
type: "spec",
title: "Wrong directory",
}),
},
{
path: ".system/specs/PX-SPEC-SPEC0002-broken/index.md",
content: "# no frontmatter",
},
{
path: ".system/specs/PX-SPEC-SPEC0002-broken.md",
content: "# loose document",
},
{ path: ".system/specs/loose.md", content: "x" },
{
path: ".system/specs/PX-SPEC-SPEC0002-broken/other/index.md",
content: "x",
},
]) {
const result = await preflightFileMutation(
project,
"write",
input,
approval(),
);
assert.equal(result?.block, true, input.path);
assert.match(result?.reason ?? "", /bundle|candidate validation/i);
}
});
it("requires a valid index before assets and approves every existing spec file", async () => {
const project = root();
const asset = ".system/specs/PX-SPEC-SPEC0001-contract/evidence/data.csv";
assert.equal(
(
await preflightFileMutation(
project,
"write",
{ path: asset, content: "a,b" },
approval(),
)
)?.block,
true,
);
const index = writeSpec(project);
assert.equal(
(
await preflightFileMutation(
project,
"write",
{ path: asset, content: "a,b" },
approval(),
)
)?.block,
undefined,
);
for (const relative of [
index,
".system/specs/PX-SPEC-SPEC0001-contract/mockup-mobile.html",
asset,
]) {
write(
project,
relative,
relative === index ? documentSource(specMetadata) : "existing",
);
let approvals = 0;
const result = await preflightFileMutation(
project,
"write",
{
path: relative,
content:
relative === index ? documentSource(specMetadata) : "replacement",
},
{
hasUI: true,
select: async (_title, items) => {
approvals += 1;
return items[0];
},
},
);
assert.equal(result?.block, undefined, relative);
assert.equal(approvals, 1, relative);
}
const closed = await preflightFileMutation(
project,
"write",
{ path: asset, content: "replacement" },
approval(false),
);
assert.equal(closed?.block, true);
assert.match(closed?.reason ?? "", /interactive UI/);
});
it("validates editable non-spec indexes and blocks done phase and plan bundles", async () => {
const project = root();
writeSpec(project);
const phase = ".system/phases/PX-PHASE-PHASE001-delivery/index.md";
const plan = ".system/plans/PX-PLAN-PLAN0001-build/index.md";
const issue = ".system/issues/PX-ISSUE-ISSUE001-bug/index.md";
write(
project,
phase,
documentSource({
id: "PX-PHASE-PHASE001",
type: "phase",
title: "Delivery",
spec: "PX-SPEC-SPEC0001",
status: "done",
}),
);
write(
project,
plan,
documentSource({
id: "PX-PLAN-PLAN0001",
type: "plan",
title: "Build",
spec: "PX-SPEC-SPEC0001",
phase: "PX-PHASE-PHASE001",
status: "done",
}),
);
write(
project,
issue,
documentSource({
id: "PX-ISSUE-ISSUE001",
type: "issue",
title: "Bug",
status: "open",
specs: ["PX-SPEC-SPEC0001"],
}),
);
for (const relative of [phase, plan]) {
const result = await preflightFileMutation(
project,
"edit",
{
path: relative,
edits: [{ oldText: "Approved.", newText: "Reviewed." }],
},
approval(),
);
assert.equal(result?.block, true, relative);
assert.match(result?.reason ?? "", /done/);
const asset = await preflightFileMutation(
project,
"write",
{ path: relative.replace("index.md", "evidence.txt"), content: "new" },
approval(),
);
assert.equal(asset?.block, true, relative);
assert.match(asset?.reason ?? "", /done/);
}
const draftPlan = ".system/plans/PX-PLAN-PLAN0002-draft/index.md";
write(
project,
draftPlan,
documentSource({
id: "PX-PLAN-PLAN0002",
type: "plan",
title: "Draft plan",
spec: "PX-SPEC-SPEC0001",
status: "draft",
}),
);
let doneApprovals = 0;
const doneResult = await preflightFileMutation(
project,
"edit",
{
path: draftPlan,
edits: [{ oldText: "status: draft", newText: "status: done" }],
},
{
hasUI: true,
select: async (_title, items) => {
doneApprovals += 1;
return items[0];
},
},
);
assert.equal(doneResult?.block, undefined);
assert.equal(doneApprovals, 1);
const issueResult = await preflightFileMutation(
project,
"edit",
{ path: issue, edits: [{ oldText: "Approved.", newText: "Reviewed." }] },
{
hasUI: true,
select: async () => {
throw new Error("unexpected approval");
},
},
);
assert.equal(issueResult?.block, undefined);
});
it("validates computed edits before approval and rejects symlinks", async () => {
const project = root();
const relative = writeSpec(project);
const result = await preflightFileMutation(
project,
"edit",
{
path: relative,
edits: [
{ oldText: "id: PX-SPEC-SPEC0001", newText: "id: PX-SPEC-SPEC0002" },
],
},
approval(),
);
assert.equal(result?.block, true);
assert.match(result?.reason ?? "", /candidate validation/i);
symlinkSync(
"missing.md",
path.join(project, ".system/specs/PX-SPEC-SPEC0002-link"),
);
const linked = await preflightFileMutation(
project,
"write",
{
path: ".system/specs/PX-SPEC-SPEC0002-link/index.md",
content: documentSource({
id: "PX-SPEC-SPEC0002",
type: "spec",
title: "Link",
}),
},
approval(),
);
assert.equal(linked?.block, true);
assert.match(linked?.reason ?? "", /symbolic link/i);
});
});