repositories / pi-ext
pi-ext
bugabingas pi extensions
owned by admin
extensions/the-system/__tests__/board-runtime.test.ts
Rawimport assert from "node:assert/strict";
import {
mkdirSync,
mkdtempSync,
readFileSync,
rmSync,
writeFileSync,
} from "node:fs";
import { tmpdir } from "node:os";
import path from "node:path";
import { initTheme } from "@earendil-works/pi-coding-agent";
import { afterEach, beforeAll, describe, it } from "vitest";
import { createBoardRuntime } from "../board-runtime.ts";
const theme = {
fg: (_style: string, text: string) => text,
bold: (text: string) => text,
} as never;
const render = (component: { render(width: number): string[] }) =>
component.render(120).join("\n");
const renderContext = (
expanded: boolean,
lastComponent?: unknown,
keyHint?: () => string,
) => ({ expanded, lastComponent, isError: false, keyHint }) as never;
const items = [
{
id: "PX-SPEC-SPEC0001",
state: "todo" as const,
rationale: "Acceptance remains incomplete.",
},
{
id: "PX-ISSUE-ISSUE001",
state: "done" as const,
rationale: "Regression coverage proves closure.",
},
];
const roots: string[] = [];
const project = () => {
const root = mkdtempSync(path.join(tmpdir(), "system-board-runtime-"));
roots.push(root);
const write = (relative: string, content: string) => {
const target = path.join(root, relative);
mkdirSync(path.dirname(target), { recursive: true });
writeFileSync(target, content);
};
write(".system/SYSTEM.md", "# System\n");
write(".system/config.json", '{"prefix":"PX","schemaVersion":0}');
write(".system/board.json", '{"schemaVersion":0,"items":{}}');
write(
".system/specs/PX-SPEC-SPEC0001-contract/index.md",
"---\nid: PX-SPEC-SPEC0001\ntype: spec\ntitle: Contract\n---\n\n# Contract\n",
);
return root;
};
afterEach(() => {
for (const root of roots.splice(0))
rmSync(root, { recursive: true, force: true });
});
beforeAll(() => initTheme("default", false));
describe("shared book lifecycle", () => {
it("reuses one authenticated server across simultaneous book, view, and board opens", async () => {
const runtime = createBoardRuntime();
const root = project();
const notices = await Promise.all(
["/", "/#PX-SPEC-SPEC0001", "/board.html"].map((page) =>
runtime.open(root, undefined, page),
),
);
const urls = notices.map(
(notice) => new URL(notice.match(/http:\/\/\S+/)?.[0] ?? ""),
);
try {
assert.equal(new Set(urls.map((url) => url.origin)).size, 1);
assert.equal(
new Set(urls.map((url) => url.searchParams.get("k"))).size,
1,
);
assert.equal(urls[1]?.hash, "#PX-SPEC-SPEC0001");
assert.equal(urls[2]?.pathname, "/board.html");
for (const url of urls)
assert.equal((await fetch(url, { redirect: "manual" })).status, 303);
} finally {
runtime.shutdown();
}
await new Promise((resolve) => setTimeout(resolve, 0));
await assert.rejects(fetch(urls[0] as URL));
});
it("cancels an opening when the session shuts down", async () => {
const runtime = createBoardRuntime();
const pending = runtime.open(project());
runtime.shutdown();
await assert.rejects(pending, /cancelled/);
});
});
describe("system_board_propose", () => {
it("exposes string enum states in both provider-facing schemas", () => {
const runtime = createBoardRuntime();
for (const tool of [runtime.tool, runtime.updateTool]) {
const schema = JSON.parse(JSON.stringify(tool.parameters));
const state = schema.properties.items.items.properties.state;
assert.equal(state.type, "string");
assert.deepEqual(state.enum, ["todo", "done", "open", "dropped"]);
}
});
it("accepts dropped proposals and updates", async () => {
const root = project();
const runtime = createBoardRuntime();
const proposal = runtime.tool.execute;
const update = runtime.updateTool.execute;
assert.ok(proposal);
assert.ok(update);
await proposal(
"test",
{
items: [
{
id: "PX-SPEC-SPEC0001",
state: "dropped",
rationale: "Intentionally excluded.",
},
],
} as never,
new AbortController().signal,
() => {},
{ cwd: root } as never,
);
await update(
"test",
{
items: [
{
id: "PX-SPEC-SPEC0001",
state: "dropped",
note: "Intentionally excluded.",
},
],
} as never,
new AbortController().signal,
() => {},
{ cwd: root } as never,
);
assert.match(
readFileSync(path.join(root, ".system/board.json"), "utf8"),
/"state": "dropped"/,
);
});
it("renders update calls and collapsed summaries", () => {
const tool = createBoardRuntime().updateTool;
assert.equal(tool.label, "system_board_update");
assert.ok(tool.renderCall);
const call = render(
tool.renderCall(
{
items: [
{
id: "PX-PLAN-PLAN0001",
state: "done",
note: "Slice verified by test run.",
},
],
},
theme,
renderContext(true),
),
);
assert.match(call, /^system_board_update 1 item/);
assert.match(call, /DONE PX-PLAN-PLAN0001/);
assert.match(call, /Slice verified by test run\./);
assert.ok(tool.renderResult);
assert.equal(
render(
tool.renderResult(
{ content: [], details: { items: [] } },
{ expanded: false, isPartial: false },
theme,
renderContext(false),
),
).trimEnd(),
"✓ no board changes",
);
assert.equal(
render(
tool.renderResult(
{ content: [], details: { items } },
{ expanded: false, isPartial: false },
theme,
renderContext(false),
),
).trimEnd(),
"✓ 2 board changes applied",
);
});
it("keeps proposal details behind expansion and renders errors", () => {
const tool = createBoardRuntime().tool;
assert.ok(tool.renderCall);
const collapsedCallComponent = tool.renderCall(
{ items },
theme,
renderContext(false, undefined, () => "HOST-EXPAND"),
);
const collapsedCall = render(collapsedCallComponent);
const expandedCallComponent = tool.renderCall(
{ items },
theme,
renderContext(true, collapsedCallComponent),
);
const expandedCall = render(expandedCallComponent);
assert.equal(expandedCallComponent, collapsedCallComponent);
assert.match(collapsedCall, /^system_board_propose 2 items/);
assert.match(collapsedCall, /HOST-EXPAND/);
assert.match(
render(tool.renderCall({ items }, theme, renderContext(false))),
/to expand/,
);
assert.doesNotMatch(collapsedCall, /Acceptance remains incomplete/);
assert.match(expandedCall, /TODO PX-SPEC-SPEC0001/);
assert.match(expandedCall, /Acceptance remains incomplete/);
assert.ok(tool.renderResult);
const result = { content: [], details: { items } };
const collapsedResultComponent = tool.renderResult(
result,
{ expanded: false, isPartial: false },
theme,
renderContext(false),
);
const collapsedResult = render(collapsedResultComponent);
const expandedResultComponent = tool.renderResult(
result,
{ expanded: true, isPartial: false },
theme,
renderContext(true, collapsedResultComponent),
);
const expandedResult = render(expandedResultComponent);
assert.equal(expandedResultComponent, collapsedResultComponent);
assert.equal(collapsedResult.trimEnd(), "✓ 2 changes proposed");
assert.equal(expandedResult.trimEnd(), "✓ 2 changes proposed");
const empty = render(
tool.renderResult(
{ content: [], details: { items: [] } },
{ expanded: false, isPartial: false },
theme,
renderContext(false, undefined, () => "HOST-EXPAND"),
),
);
assert.equal(empty.trimEnd(), "✓ no changes proposed");
const errorResult = {
content: [
{ type: "text", text: "proposal failed\nEND-PROPOSAL-DETAILS" },
],
};
const error = render(
tool.renderResult(
errorResult as Parameters<NonNullable<typeof tool.renderResult>>[0],
{ expanded: false, isPartial: false },
theme,
{ expanded: false, lastComponent: undefined, isError: true } as never,
),
);
const expandedError = render(
tool.renderResult(
errorResult as Parameters<NonNullable<typeof tool.renderResult>>[0],
{ expanded: true, isPartial: false },
theme,
{ expanded: true, lastComponent: undefined, isError: true } as never,
),
);
assert.match(error, /^proposal failed/);
assert.match(error, /to expand/);
assert.doesNotMatch(error, /END-PROPOSAL-DETAILS/);
assert.match(expandedError, /END-PROPOSAL-DETAILS/);
});
});