Files
pi-subagent/test/check-state-log.test.ts
T
alex 01cea75342 Add an append-only state-log check as a verify gate
The 2026-11-26 pipeline session mangled committed wiki state-log bullets three times with prefix-anchored edits, each silent at edit time. scripts/check-state-log.ts fails when a state-log line present in HEAD is missing or modified in the working tree; additions pass. Wired as npm run wikicheck, on_settle in .pi/verify.json.
2026-11-26 20:05:39 +00:00

91 lines
3.7 KiB
TypeScript

import assert from "node:assert/strict";
import { execFileSync } from "node:child_process";
import { mkdirSync, mkdtempSync, rmSync, writeFileSync } from "node:fs";
import test from "node:test";
import { join } from "node:path";
import { tmpdir } from "node:os";
import { fileURLToPath } from "node:url";
import { extractStateLogLines, removedCommittedLines } from "../scripts/check-state-log.ts";
const scriptPath = fileURLToPath(new URL("../scripts/check-state-log.ts", import.meta.url));
test("extractStateLogLines returns the bullets between the State log heading and the next heading", () => {
const page = [
"# Page",
"",
"## State log",
"",
"- 2026-01-01: First entry.",
"- 2026-01-02: Second entry.",
"",
"## Conventions",
"",
"- Convention line.",
].join("\n");
assert.deepEqual(extractStateLogLines(page), ["- 2026-01-01: First entry.", "- 2026-01-02: Second entry."]);
});
test("extractStateLogLines returns nothing without a State log section", () => {
assert.deepEqual(extractStateLogLines("# Page\n\n- Not a state log.\n"), []);
});
test("removedCommittedLines flags modifications and deletions, passes additions and reordering", () => {
const committed = ["- 2026-01-01: First entry.", "- 2026-01-02: Second entry.", "- 2026-01-03: Third entry."];
// Append-only: no damage.
assert.deepEqual(removedCommittedLines(committed, [...committed, "- 2026-01-04: Fourth entry."]), []);
// Reordering within the section leaves every committed line intact.
assert.deepEqual(removedCommittedLines(committed, [...committed].reverse()), []);
// A modified line is missing from the current set.
const modified = [...committed];
modified[1] = "- 2026-01-02: Second entry, edited.";
assert.deepEqual(removedCommittedLines(committed, modified), ["- 2026-01-02: Second entry."]);
// A deleted line is missing.
assert.deepEqual(removedCommittedLines(committed, [committed[0], committed[2]]), ["- 2026-01-02: Second entry."]);
});
test("the CLI passes an append-only edit and fails a committed-line modification", { skip: process.platform === "win32" }, (t) => {
const tmp = mkdtempSync(join(tmpdir(), "state-log-check-"));
t.after(() => rmSync(tmp, { recursive: true, force: true }));
const wiki = join(tmp, "docs", "wiki");
mkdirSync(wiki, { recursive: true });
const page = join(wiki, "goal.md");
const committedPage = "## State log\n\n- 2026-01-01: First entry.\n";
const run = (expectFail: boolean) => {
let result: { status: number; stderr: string } | undefined;
try {
const stdout = execFileSync(process.execPath, [scriptPath], {
cwd: tmp,
encoding: "utf8",
});
result = { status: 0, stderr: stdout };
} catch (error) {
assert.ok(error instanceof Error && "status" in error, "expected an exec failure object");
const e = error as { status?: number; stderr?: string };
result = { status: e.status ?? -1, stderr: e.stderr ?? "" };
}
if (expectFail) {
assert.notEqual(result.status, 0, "expected the check to fail");
assert.match(result.stderr, /2026-01-01: First entry\./);
} else {
assert.equal(result.status, 0);
}
};
const git = (...args: string[]) => execFileSync("git", args, { cwd: tmp, encoding: "utf8" });
writeFileSync(page, committedPage);
git("init", "-q");
git("config", "user.email", "test@example.com");
git("config", "user.name", "Test");
git("add", "-A");
git("commit", "-qm", "wiki");
// Append-only edit passes; a page new to HEAD passes untouched.
writeFileSync(page, committedPage + "- 2026-01-02: Second entry.\n");
writeFileSync(join(wiki, "new-page.md"), "## State log\n\n- 2026-01-01: Fresh page.\n");
run(false);
// Modifying a committed line fails and names the line.
writeFileSync(page, committedPage.replace("First", "Rewritten") + "- 2026-01-02: Second entry.\n");
run(true);
});