repositories / pi-ext
pi-ext
bugabingas pi extensions
owned by bugabinga
extensions/the-system/__tests__/documents.test.ts
RawFile outline
- roots line 26
- tree line 27
- dir line 32
- write line 37
- source line 42
- spec line 51
- afterEach line 54
- project root and schema-v3 config line 59
- contains paths and resolves nearest marker line 60
- accepts minimal config and inspects narrow v1 config line 71
- schema-v3 identity and metadata line 89
- parses uppercase token IDs and rejects numeric legacy IDs line 90
- projects known graph fields while preserving unknown frontmatter line 107
- validates minimal status fields line 136
- accepts only canonical bundle paths line 160
- bundle discovery and graph line 186
- returns authored indexes and structural diagnostics separately line 187
- reports symlinks without following them line 213
- validates references, duplicates, and phase/spec consistency line 237
- validates dependency references and cycles line 283
- candidate validation reports only new candidate findings line 338
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 {
discoverDocumentBundles,
discoverProjectRoot,
indexBundleDocuments,
inspectSystemConfig,
parseAuthoredDocument,
parseDocumentId,
parseSystemConfig,
resolveGovernedPath,
SystemDocumentError,
validateBundleCandidate,
validateDocumentPath,
} from "../documents.ts";
const roots: string[] = [];
const tree = () => {
const root = mkdtempSync(path.join(tmpdir(), "the-system-documents-"));
roots.push(root);
return root;
};
const dir = (root: string, ...parts: string[]) => {
const target = path.join(root, ...parts);
mkdirSync(target, { recursive: true });
return target;
};
const write = (root: string, relative: string, content: string) => {
const target = path.join(root, relative);
dir(path.dirname(target));
writeFileSync(target, content);
};
const source = (
directory: string,
id: string,
slug: string,
frontmatter: string,
) => ({
path: `.system/${directory}/${id}-${slug}/index.md`,
markdown: `---\n${frontmatter}\n---\n\n# ${id}\n`,
});
const spec = (id = "PX-SPEC-ABCDEFGH") =>
`---\nid: ${id}\ntype: spec\ntitle: Contract\n---\n\n# ${id}\n`;
afterEach(() => {
for (const root of roots.splice(0))
rmSync(root, { recursive: true, force: true });
});
describe("project root and schema-v3 config", () => {
it("contains paths and resolves nearest marker", () => {
const root = tree();
mkdirSync(path.join(root, ".git"));
assert.equal(discoverProjectRoot(path.join(root, "nested")), root);
assert.equal(resolveGovernedPath(root, "x/../x"), path.join(root, "x"));
assert.throws(
() => resolveGovernedPath(root, "../outside"),
SystemDocumentError,
);
});
it("accepts minimal config and inspects narrow v1 config", () => {
assert.deepEqual(
parseSystemConfig(
'{"prefix":"PX","schemaVersion":3,"title":"Project X"}',
),
{ prefix: "PX", schemaVersion: 3, title: "Project X" },
);
assert.deepEqual(parseSystemConfig('{"prefix":"PX","schemaVersion":3}'), {
prefix: "PX",
schemaVersion: 3,
});
assert.deepEqual(
inspectSystemConfig('{"prefix":"PX","schemaVersion":1,"styleVersion":1}'),
{ config: { prefix: "PX", schemaVersion: 1 }, legacyStyleVersion: 1 },
);
});
});
describe("schema-v3 identity and metadata", () => {
it("parses uppercase token IDs and rejects numeric legacy IDs", () => {
assert.deepEqual(parseDocumentId("PX-SPEC-AZ3_P9QK", "PX"), {
id: "PX-SPEC-AZ3_P9QK",
prefix: "PX",
type: "spec",
token: "AZ3_P9QK",
});
assert.throws(
() => parseDocumentId("PX-SPEC-001", "PX"),
SystemDocumentError,
);
assert.throws(
() => parseDocumentId("PX-SPEC-az3_p9qk", "PX"),
SystemDocumentError,
);
});
it("projects known graph fields while preserving unknown frontmatter", () => {
const parsed = parseAuthoredDocument(
`---\nid: PX-PLAN-PL4N0001\ntype: plan\ntitle: Delivery\nspec: PX-SPEC-SPEC0001\nphase: PX-PHASE-PH4S0001\nstatus: draft\ndepends_on: [PX-PLAN-PL4N0000]\nreview:\n owner: human\n---\n`,
"PX",
);
assert.deepEqual(parsed.metadata, {
id: "PX-PLAN-PL4N0001",
type: "plan",
title: "Delivery",
spec: "PX-SPEC-SPEC0001",
phase: "PX-PHASE-PH4S0001",
status: "draft",
depends_on: ["PX-PLAN-PL4N0000"],
});
assert.deepEqual(
[...parsed.frontmatter.keys()],
[
"id",
"type",
"title",
"spec",
"phase",
"status",
"depends_on",
"review",
],
);
});
it("validates minimal status fields", () => {
assert.deepEqual(
parseAuthoredDocument(
"---\nid: PX-ISSUE-ISSUE001\ntype: issue\ntitle: Bug\nstatus: done\nspecs: []\n---\n",
"PX",
).metadata,
{
id: "PX-ISSUE-ISSUE001",
type: "issue",
title: "Bug",
status: "done",
specs: [],
},
);
assert.throws(
() =>
parseAuthoredDocument(
"---\nid: PX-ISSUE-ISSUE001\ntype: issue\ntitle: Bug\nstatus: fixed\nspecs: []\n---\n",
"PX",
),
SystemDocumentError,
);
});
it("accepts only canonical bundle paths", () => {
const metadata = {
id: "PX-SPEC-AZ3_P9QK",
type: "spec",
title: "Login",
} as const;
assert.doesNotThrow(() =>
validateDocumentPath(
".system/specs/PX-SPEC-AZ3_P9QK-login-flow/index.md",
metadata,
"PX",
),
);
for (const item of [
".system/specs/PX-SPEC-AZ3_P9QK-login-flow.md",
".system/phases/PX-SPEC-AZ3_P9QK-login-flow/index.md",
".system/specs/PX-SPEC-AZ3_P9QK-Login/index.md",
".system/specs/PX-SPEC-AZ3_P9QK-login-flow/other/index.md",
])
assert.throws(
() => validateDocumentPath(item, metadata, "PX"),
SystemDocumentError,
);
});
});
describe("bundle discovery and graph", () => {
it("returns authored indexes and structural diagnostics separately", () => {
const root = tree();
write(
root,
".system/specs/PX-SPEC-SPEC0001-contract/index.md",
spec("PX-SPEC-SPEC0001"),
);
write(
root,
".system/specs/PX-SPEC-SPEC0001-contract/assets/data.csv",
"a,b",
);
write(root, ".system/plans/loose.md", "bad");
dir(root, ".system", "specs", "PX-SPEC-SPEC0002-empty");
assert.equal(discoverDocumentBundles(root).sources.length, 1);
assert.deepEqual(
discoverDocumentBundles(root).diagnostics.map(
(item) => `${item.path} ${item.code}`,
),
[
".system/plans/loose.md bundle.expected-directory",
".system/specs/PX-SPEC-SPEC0002-empty bundle.missing-index",
],
);
});
it("reports symlinks without following them", () => {
const root = tree();
const outside = tree();
write(
root,
".system/specs/PX-SPEC-SPEC0001-contract/index.md",
spec("PX-SPEC-SPEC0001"),
);
symlinkSync(
outside,
path.join(root, ".system/specs/PX-SPEC-SPEC0002-link"),
"dir",
);
symlinkSync(
outside,
path.join(root, ".system/specs/PX-SPEC-SPEC0001-contract/assets-link"),
"dir",
);
assert.deepEqual(
discoverDocumentBundles(root).diagnostics.map((item) => item.code),
["bundle.symbolic-link", "bundle.symbolic-link"],
);
});
it("validates references, duplicates, and phase/spec consistency", () => {
const indexed = indexBundleDocuments(
[
source(
"research",
"PX-RESEARCH-RESEARCH",
"notes",
"id: PX-RESEARCH-RESEARCH\ntype: research\ntitle: Notes",
),
source(
"specs",
"PX-SPEC-SPEC0001",
"one",
"id: PX-SPEC-SPEC0001\ntype: spec\ntitle: One\nresearch: [PX-RESEARCH-RESEARCH]",
),
source(
"specs",
"PX-SPEC-SPEC0002",
"two",
"id: PX-SPEC-SPEC0002\ntype: spec\ntitle: Two",
),
source(
"phases",
"PX-PHASE-PHASE001",
"delivery",
"id: PX-PHASE-PHASE001\ntype: phase\ntitle: Delivery\nspec: PX-SPEC-SPEC0001\nstatus: done",
),
source(
"plans",
"PX-PLAN-PLAN0001",
"build",
"id: PX-PLAN-PLAN0001\ntype: plan\ntitle: Build\nspec: PX-SPEC-SPEC0002\nphase: PX-PHASE-PHASE001\nstatus: draft",
),
],
"PX",
);
assert.deepEqual(
indexed.diagnostics.map(
(item) => `${item.path} ${item.code}: ${item.message}`,
),
[
".system/plans/PX-PLAN-PLAN0001-build/index.md graph.forbidden-edge: PX-PHASE-PHASE001 belongs to PX-SPEC-SPEC0001, not PX-SPEC-SPEC0002",
],
);
});
it("validates dependency references and cycles", () => {
const indexed = indexBundleDocuments(
[
source(
"specs",
"PX-SPEC-SPEC0001",
"one",
"id: PX-SPEC-SPEC0001\ntype: spec\ntitle: One",
),
source(
"specs",
"PX-SPEC-SPEC0002",
"two",
"id: PX-SPEC-SPEC0002\ntype: spec\ntitle: Two",
),
source(
"phases",
"PX-PHASE-PHASE001",
"one",
"id: PX-PHASE-PHASE001\ntype: phase\ntitle: One\nspec: PX-SPEC-SPEC0001\nstatus: draft\ndepends_on: [PX-PHASE-PHASE002]",
),
source(
"phases",
"PX-PHASE-PHASE002",
"two",
"id: PX-PHASE-PHASE002\ntype: phase\ntitle: Two\nspec: PX-SPEC-SPEC0001\nstatus: draft\ndepends_on: [PX-PHASE-PHASE001]",
),
source(
"plans",
"PX-PLAN-PLAN0001",
"one",
"id: PX-PLAN-PLAN0001\ntype: plan\ntitle: One\nspec: PX-SPEC-SPEC0001\nstatus: draft\ndepends_on: [PX-PLAN-MISSING1]",
),
source(
"plans",
"PX-PLAN-PLAN0002",
"two",
"id: PX-PLAN-PLAN0002\ntype: plan\ntitle: Two\nspec: PX-SPEC-SPEC0001\nstatus: draft\ndepends_on: [PX-PLAN-PLAN0003]",
),
source(
"plans",
"PX-PLAN-PLAN0003",
"three",
"id: PX-PLAN-PLAN0003\ntype: plan\ntitle: Three\nspec: PX-SPEC-SPEC0002\nstatus: draft",
),
],
"PX",
);
assert.deepEqual(indexed.diagnostics.map((item) => item.code).sort(), [
"graph.dangling-reference",
"graph.dependency-cycle",
"graph.forbidden-edge",
]);
});
it("candidate validation reports only new candidate findings", () => {
const findings = validateBundleCandidate(
[
source(
"specs",
"PX-SPEC-SPEC0001",
"one",
"id: PX-SPEC-SPEC0001\ntype: spec\ntitle: One",
),
],
source(
"phases",
"PX-PHASE-PHASE001",
"delivery",
"id: PX-PHASE-PHASE001\ntype: phase\ntitle: Delivery\nspec: PX-SPEC-MISSING1\nstatus: done",
),
"PX",
);
assert.deepEqual(
findings.map((item) => item.code),
["graph.dangling-reference"],
);
});
});