mirror of
https://github.com/jparkerweb/plan2code.git
synced 2026-11-16 20:45:33 -08:00
Add the Changes tab: changes.mjs and a diff block for the sign-off page
AI Assisted
This commit is contained in:
1 parent
cb64a8d15e
commit
7021ff9a48
10 files changed
+741
-14
No files matched your search
@@ -14,6 +14,7 @@
|
||||
"test-install-update.mjs",
|
||||
"test-line-endings.mjs",
|
||||
"test-script-changelog.mjs",
|
||||
"test-script-changes.mjs",
|
||||
"test-script-coverage.mjs",
|
||||
"test-script-dead-code.mjs",
|
||||
"test-script-deps.mjs",
|
||||
|
||||
@@ -0,0 +1,215 @@
|
||||
// Tests for the skills' scripts/changes.mjs: `start` snapshots the working tree
|
||||
// through a temporary index and `diff` writes what changed since.
|
||||
//
|
||||
// See skill-script-helpers.mjs for how these run.
|
||||
|
||||
import { test } from "node:test";
|
||||
import assert from "node:assert/strict";
|
||||
import fs from "node:fs";
|
||||
import path from "node:path";
|
||||
import { pathToFileURL } from "node:url";
|
||||
import { SKILLS, runScript, tmp, write, read, gitInit } from "./skill-script-helpers.mjs";
|
||||
|
||||
const SKILL = "plan2code-3-implement";
|
||||
const run = (root, session, ...a) => runScript(SKILL, "changes.mjs", [...a, "--session", session, "--root", root]);
|
||||
|
||||
// A repository with a few committed files and a session folder beside it.
|
||||
function fixture(name) {
|
||||
const root = tmp(name);
|
||||
const session = tmp(`${name}-session`);
|
||||
const g = gitInit(root);
|
||||
write(root, "a.txt", "one\ntwo\nthree\n");
|
||||
write(root, "gone.txt", "bye\nbye\n");
|
||||
write(root, "old-name.txt", "keep this\nand this\nand more of it\nand a fourth line\n");
|
||||
write(root, "src/b.js", "export const b = 1;\n");
|
||||
write(root, ".gitignore", "ignored/\n");
|
||||
g("add", "-A");
|
||||
g("commit", "-q", "-m", "init");
|
||||
return { root, session, g };
|
||||
}
|
||||
|
||||
const state = (g) => ({ status: g("status", "--porcelain"), head: g("rev-parse", "HEAD"), branch: g("branch", "--show-current"), staged: g("diff", "--cached", "--name-only") });
|
||||
const names = (diff) => [...diff.matchAll(/^diff --git a\/(.+) b\/(.+)$/gm)].map((m) => m[2]);
|
||||
|
||||
test("changes: start then diff lists exactly what changed since, and leaves the repository alone", () => {
|
||||
const { root, session, g } = fixture("changes-basic");
|
||||
// Work done before start must never show up.
|
||||
write(root, "before.txt", "earlier work\n");
|
||||
fs.appendFileSync(path.join(root, "a.txt"), "before\n");
|
||||
const before = state(g);
|
||||
const s = run(root, session, "start");
|
||||
assert.equal(s.code, 0, s.stderr);
|
||||
assert.match(s.json.tree, /^[0-9a-f]{40,64}$/);
|
||||
const startFile = JSON.parse(read(session, "changes-start.json"));
|
||||
assert.equal(startFile.tree, s.json.tree);
|
||||
assert.ok(!Number.isNaN(Date.parse(startFile.at)));
|
||||
assert.deepEqual(state(g), before, "start leaves status, HEAD, branch and the real index as they were");
|
||||
|
||||
// The run's own work: modify, add, delete, rename, ignored file.
|
||||
fs.appendFileSync(path.join(root, "a.txt"), "four\n");
|
||||
write(root, "new.txt", "fresh\nfile\n");
|
||||
fs.rmSync(path.join(root, "gone.txt"));
|
||||
fs.renameSync(path.join(root, "old-name.txt"), path.join(root, "new-name.txt"));
|
||||
write(root, "ignored/x.txt", "never shown\n");
|
||||
const mid = state(g);
|
||||
const d = run(root, session, "diff");
|
||||
assert.equal(d.code, 0, d.stderr);
|
||||
assert.deepEqual(state(g), mid, "diff leaves the repository alone too");
|
||||
assert.equal(d.json.file, "changes.diff");
|
||||
assert.equal(d.json.files, 4);
|
||||
assert.equal(d.json.added, 3, "four, two new lines");
|
||||
assert.equal(d.json.removed, 2, "the two lines of gone.txt");
|
||||
assert.equal(d.json.truncated, false);
|
||||
assert.deepEqual(d.json.hidden, []);
|
||||
const diff = read(session, "changes.diff");
|
||||
assert.deepEqual(names(diff).sort(), ["a.txt", "gone.txt", "new-name.txt", "new.txt"]);
|
||||
assert.ok(!diff.includes("before.txt") && !/^[+-]before$/m.test(diff), "work done before start never appears as a change");
|
||||
assert.ok(!diff.includes("ignored"), ".gitignore'd files never appear");
|
||||
assert.match(diff, /^rename from old-name\.txt$/m);
|
||||
assert.match(diff, /^rename to new-name\.txt$/m);
|
||||
assert.match(diff, /^similarity index 100%$/m);
|
||||
});
|
||||
|
||||
test("changes: a pure rename counts as one file, a binary file is listed with no lines", () => {
|
||||
const { root, session } = fixture("changes-binary");
|
||||
run(root, session, "start");
|
||||
fs.renameSync(path.join(root, "src", "b.js"), path.join(root, "src", "c.js"));
|
||||
fs.writeFileSync(path.join(root, "pic.bin"), Buffer.from([0, 1, 2, 3, 0, 255, 254, 0]));
|
||||
const d = run(root, session, "diff");
|
||||
assert.equal(d.code, 0, d.stderr);
|
||||
assert.equal(d.json.files, 2);
|
||||
assert.equal(d.json.added, 0, "binary adds nothing and a pure rename adds nothing");
|
||||
assert.equal(d.json.removed, 0);
|
||||
assert.ok(Number.isFinite(d.json.added) && Number.isFinite(d.json.removed), "never NaN");
|
||||
const diff = read(session, "changes.diff");
|
||||
assert.match(diff, /^Binary files .*pic\.bin differ$/m);
|
||||
assert.ok(!/^@@/m.test(diff), "no hunks at all");
|
||||
assert.match(diff, /^rename from src\/b\.js$/m);
|
||||
});
|
||||
|
||||
test("changes: no changes is an empty file, not an error", () => {
|
||||
const { root, session } = fixture("changes-empty");
|
||||
run(root, session, "start");
|
||||
const d = run(root, session, "diff");
|
||||
assert.equal(d.code, 0, d.stderr);
|
||||
assert.deepEqual({ files: d.json.files, added: d.json.added, removed: d.json.removed, truncated: d.json.truncated }, { files: 0, added: 0, removed: 0, truncated: false });
|
||||
assert.equal(read(session, "changes.diff"), "");
|
||||
});
|
||||
|
||||
test("changes: a patch over the cap is cut at a file boundary, every file still named", () => {
|
||||
const { root, session } = fixture("changes-cap");
|
||||
run(root, session, "start");
|
||||
for (const n of ["f1", "f2", "f3"]) write(root, `${n}.txt`, `${n} line\n`.repeat(200));
|
||||
const d = run(root, session, "diff", "--cap", "1000");
|
||||
assert.equal(d.code, 0, d.stderr);
|
||||
assert.equal(d.json.truncated, true);
|
||||
assert.equal(d.json.files, 3);
|
||||
assert.equal(d.json.added, 600, "counts cover the whole change, not the cut patch");
|
||||
const diff = read(session, "changes.diff");
|
||||
assert.deepEqual(names(diff).sort(), ["f1.txt", "f2.txt", "f3.txt"]);
|
||||
assert.match(diff, /^\\ No more lines shown: this change is too large to show in full\.$/m);
|
||||
assert.match(diff, /^# truncated: true$/m);
|
||||
assert.ok(diff.length < 1500, "the big first file shows no lines of itself");
|
||||
assert.ok(!diff.includes("f1 line"));
|
||||
const bad = run(root, session, "diff", "--cap", "0");
|
||||
assert.equal(bad.code, 2);
|
||||
});
|
||||
|
||||
test("changes: capPatch keeps whole files that fit and cuts the rest to their headers", async () => {
|
||||
const { capPatch } = await import(pathToFileURL(path.join(SKILLS, SKILL, "scripts", "changes.mjs")).href);
|
||||
const small = "diff --git a/s b/s\n--- a/s\n+++ b/s\n@@ -1 +1 @@\n-a\n+b\n";
|
||||
const big = "diff --git a/big b/big\nrename from x\nrename to big\n--- a/x\n+++ b/big\n@@ -1 +1 @@\n" + "+line\n".repeat(500);
|
||||
const patch = small + big;
|
||||
const fit = capPatch(patch, patch.length);
|
||||
assert.deepEqual(fit, { patch, truncated: false });
|
||||
const cut = capPatch(patch, 600);
|
||||
assert.equal(cut.truncated, true);
|
||||
assert.ok(cut.patch.startsWith(small), "the first file stays whole");
|
||||
assert.ok(cut.patch.endsWith("diff --git a/big b/big\nrename from x\nrename to big\n"), "the cut file keeps its name and rename lines");
|
||||
assert.ok(!cut.patch.includes("+line"));
|
||||
assert.ok(cut.patch.includes("No more lines shown"));
|
||||
const first = capPatch(patch, 100);
|
||||
assert.ok(!first.patch.includes("@@"), "a first file over the cap shows no lines");
|
||||
assert.ok(first.patch.includes("diff --git a/s b/s") && first.patch.includes("diff --git a/big b/big"));
|
||||
});
|
||||
|
||||
test("changes: a sensitive file shows its name and counts but no lines", () => {
|
||||
const { root, session } = fixture("changes-secret");
|
||||
run(root, session, "start");
|
||||
write(root, ".env", "API_KEY=hunter2\nOTHER=1\n");
|
||||
write(root, "ok.txt", "fine\n");
|
||||
const d = run(root, session, "diff");
|
||||
assert.equal(d.code, 0, d.stderr);
|
||||
assert.equal(d.json.files, 2);
|
||||
assert.equal(d.json.added, 3, "the hidden file's lines still count");
|
||||
assert.deepEqual(d.json.hidden, [".env"]);
|
||||
const diff = read(session, "changes.diff");
|
||||
assert.ok(!diff.includes("hunter2") && !diff.includes("OTHER=1"));
|
||||
assert.match(diff, /^diff --git a\/\.env b\/\.env$/m);
|
||||
assert.match(diff, /^Lines hidden: this file may hold a secret\.$/m);
|
||||
assert.match(diff, /^# hidden: \.env$/m);
|
||||
assert.match(diff, /^\+fine$/m, "other files keep their lines");
|
||||
});
|
||||
|
||||
test("changes: failures are plain exit 3s, bad arguments exit 2", () => {
|
||||
const { root, session } = fixture("changes-fail");
|
||||
const noStart = run(root, session, "diff");
|
||||
assert.equal(noStart.code, 3);
|
||||
assert.equal(noStart.json.error, "no-start");
|
||||
assert.ok(noStart.json.message && noStart.json.next);
|
||||
|
||||
const notRepo = tmp("changes-norepo");
|
||||
const sess2 = tmp("changes-norepo-session");
|
||||
for (const cmd of ["start", "diff"]) {
|
||||
if (cmd === "diff") write(sess2, "changes-start.json", JSON.stringify({ tree: "a".repeat(40), at: "x" }));
|
||||
const r = run(notRepo, sess2, cmd);
|
||||
assert.equal(r.code, 3, `${cmd}: ${r.stderr}`);
|
||||
assert.equal(r.json.error, "not-a-repo");
|
||||
assert.ok(r.json.next);
|
||||
}
|
||||
|
||||
write(session, "changes-start.json", JSON.stringify({ tree: "b".repeat(40), at: "x" }));
|
||||
const gone = run(root, session, "diff");
|
||||
assert.equal(gone.code, 3);
|
||||
assert.equal(gone.json.error, "start-missing");
|
||||
|
||||
const usage = runScript(SKILL, "changes.mjs", ["start", "--root", root]);
|
||||
assert.equal(usage.code, 2, "--session is required");
|
||||
assert.equal(runScript(SKILL, "changes.mjs", ["nope", "--session", session]).code, 2);
|
||||
});
|
||||
|
||||
test("changes: start twice overwrites the snapshot", () => {
|
||||
const { root, session } = fixture("changes-twice");
|
||||
const a = run(root, session, "start");
|
||||
fs.appendFileSync(path.join(root, "a.txt"), "first run work\n");
|
||||
const b = run(root, session, "start");
|
||||
assert.notEqual(a.json.tree, b.json.tree);
|
||||
assert.equal(JSON.parse(read(session, "changes-start.json")).tree, b.json.tree);
|
||||
fs.appendFileSync(path.join(root, "a.txt"), "second run work\n");
|
||||
const d = run(root, session, "diff");
|
||||
const diff = read(session, "changes.diff");
|
||||
assert.equal(d.json.files, 1);
|
||||
assert.match(diff, /^[+]second run work$/m);
|
||||
assert.ok(!/^[+-]first run work$/m.test(diff), "work before the second start is context at most, never a change");
|
||||
});
|
||||
|
||||
test("changes: hiding holds for a spaced name, a sensitive rename, diff.noprefix and a cap", () => {
|
||||
const { root, session, g } = fixture("changes-secret-hard");
|
||||
write(root, "old.pem", "OLDSECRET1\nOLDSECRET2\nOLDSECRET3\nOLDSECRET4\n");
|
||||
g("add", "-A");
|
||||
g("commit", "-q", "-m", "pem");
|
||||
g("config", "diff.noprefix", "true");
|
||||
run(root, session, "start");
|
||||
write(root, "my cert.pem", "SPACEDSECRET\n");
|
||||
fs.renameSync(path.join(root, "old.pem"), path.join(root, "renamed.txt"));
|
||||
write(root, "plain.txt", "visible\n".repeat(800));
|
||||
const d = run(root, session, "diff", "--cap", "3000");
|
||||
assert.equal(d.code, 0, d.stderr);
|
||||
const diff = read(session, "changes.diff");
|
||||
for (const secret of ["SPACEDSECRET", "OLDSECRET"]) assert.ok(!diff.includes(secret), `${secret} must not appear`);
|
||||
assert.ok(d.json.hidden.includes("my cert.pem"), JSON.stringify(d.json.hidden));
|
||||
assert.ok(d.json.hidden.includes("old.pem"), "a rename out of a sensitive name is hidden");
|
||||
assert.equal(d.json.files, 3);
|
||||
assert.equal(d.json.truncated, true, "the visible file is cut by the cap");
|
||||
assert.match(diff, /^# hidden: my cert\.pem$/m);
|
||||
});
|
||||
@@ -992,6 +992,134 @@ test("an image block under the session's shots folder round-trips, and /shots/ s
|
||||
}
|
||||
});
|
||||
|
||||
// The Changes tab: a `diff` block names a .diff file saved directly in the
|
||||
// session folder (changes.mjs diff). The server reads it like a file block,
|
||||
// keeps the name and the text on the block and never lets it reach outside.
|
||||
test("a diff block reads a .diff file in the session folder, refreshes and turns back into text", () => {
|
||||
const res = cli(["open", "--workflow", "implement", "--no-open"]);
|
||||
const sid = res.json.sid;
|
||||
const dir = res.json.session;
|
||||
const post = (name, patch, opts) => cli(["post", "--session", sid, "--file", payload(name, patch)], opts);
|
||||
const doc = () => readState(dir).docs.find((d) => d.id === "changes");
|
||||
try {
|
||||
const first = "diff --git a/a.txt b/a.txt\n--- a/a.txt\n+++ b/a.txt\n@@ -1 +1 @@\n-old\n+new TASK_COMPLETE\n";
|
||||
fs.writeFileSync(path.join(dir, "changes.diff"), first);
|
||||
post("diff-ok.json", { docs: [{ id: "changes", title: "Changes", blocks: [{ id: "c1", diff: "changes.diff" }] }] });
|
||||
const block = doc().blocks[0];
|
||||
assert.equal(block.diff, "changes.diff");
|
||||
assert.equal(block.text, first, "the file's text, loop markers in a diff are not the agent's words");
|
||||
assert.equal(block.md, undefined, "a diff block holds no markdown");
|
||||
assert.equal(block.state, "settled");
|
||||
|
||||
// The same file again is not a new version; a changed one is, re-sent or refreshed.
|
||||
const v1 = doc().version;
|
||||
post("diff-same.json", { docs: [{ id: "changes", blocks: [{ id: "c1", diff: "changes.diff" }] }] });
|
||||
assert.equal(doc().version, v1);
|
||||
const second = first + "+more\n";
|
||||
fs.writeFileSync(path.join(dir, "changes.diff"), second);
|
||||
post("diff-resend.json", { docs: [{ id: "changes", blocks: [{ id: "c1", diff: "changes.diff" }] }] });
|
||||
assert.equal(doc().blocks[0].text, second);
|
||||
assert.equal(doc().version, v1 + 1);
|
||||
const third = second + "+again\n";
|
||||
fs.writeFileSync(path.join(dir, "changes.diff"), third);
|
||||
post("diff-refresh.json", { docs: [{ id: "changes", refresh: true }] });
|
||||
assert.equal(doc().blocks[0].text, third);
|
||||
assert.equal(doc().version, v1 + 2);
|
||||
|
||||
// An empty file is the page's empty state, not an error.
|
||||
fs.writeFileSync(path.join(dir, "changes.diff"), "");
|
||||
post("diff-empty.json", { docs: [{ id: "changes", refresh: true }] });
|
||||
assert.equal(doc().blocks[0].text, "");
|
||||
|
||||
// A diff block cannot carry other content.
|
||||
const both = post("diff-md.json", { docs: [{ id: "changes", blocks: [{ id: "c2", diff: "changes.diff", md: "text" }] }] }, { expectFail: true });
|
||||
assert.equal(both.status, 3);
|
||||
assert.match(both.stderr, /cannot also carry "md", "file" or "image"/);
|
||||
|
||||
// Text over it: the block stops being a diff and a refresh no longer reads it.
|
||||
post("diff-to-md.json", { docs: [{ id: "changes", blocks: [{ id: "c1", md: "Plain words." }] }] });
|
||||
assert.equal(doc().blocks[0].diff, undefined);
|
||||
assert.equal(doc().blocks[0].text, undefined);
|
||||
assert.equal(doc().blocks[0].md, "Plain words.");
|
||||
const none = post("diff-none.json", { docs: [{ id: "changes", refresh: true }] }, { expectFail: true });
|
||||
assert.match(none.stderr, /found no file or diff blocks to re-read/);
|
||||
} finally {
|
||||
cli(["stop", "--session", sid, "--force"]);
|
||||
}
|
||||
});
|
||||
|
||||
test("a diff block refuses a path, a wrong extension, a missing file and a link out of the session folder", () => {
|
||||
const res = cli(["open", "--workflow", "implement", "--no-open"]);
|
||||
const sid = res.json.sid;
|
||||
const dir = res.json.session;
|
||||
const outside = fs.mkdtempSync(path.join(os.tmpdir(), "p2c-diff-outside-"));
|
||||
const post = (name, diff) => cli(["post", "--session", sid, "--file", payload(name, { docs: [{ id: "changes", blocks: [{ id: "c1", diff }] }] })], { expectFail: true });
|
||||
try {
|
||||
fs.mkdirSync(path.join(dir, "sub"), { recursive: true });
|
||||
fs.writeFileSync(path.join(dir, "sub", "changes.diff"), "x\n");
|
||||
fs.writeFileSync(path.join(dir, "notes.txt"), "x\n");
|
||||
fs.writeFileSync(path.join(outside, "secret.diff"), "x\n");
|
||||
const cases = [
|
||||
["sub/changes.diff", /needs a plain name/],
|
||||
["sub\\changes.diff", /needs a plain name/],
|
||||
["../changes.diff", /needs a plain name/],
|
||||
[path.join(outside, "secret.diff"), /needs a plain name/],
|
||||
["notes.txt", /needs a plain name/],
|
||||
["missing.diff", /does not exist in the session folder. Run changes.mjs diff first/],
|
||||
];
|
||||
for (const [name, message] of cases) {
|
||||
const r = post("diff-bad.json", name);
|
||||
assert.equal(r.status, 3, name);
|
||||
assert.match(r.stderr, message, name);
|
||||
}
|
||||
try {
|
||||
fs.symlinkSync(path.join(outside, "secret.diff"), path.join(dir, "link.diff"), "file");
|
||||
} catch {
|
||||
console.log("skipped the link case: this platform cannot create a symbolic link here");
|
||||
return;
|
||||
}
|
||||
const linked = post("diff-link.json", "link.diff");
|
||||
assert.equal(linked.status, 3);
|
||||
assert.match(linked.stderr, /is outside the session folder/);
|
||||
} finally {
|
||||
cli(["stop", "--session", sid, "--force"]);
|
||||
fs.rmSync(outside, { recursive: true, force: true });
|
||||
}
|
||||
});
|
||||
|
||||
test("a diff file over 512 KB is cut at a whole line with the too-large line, never an error", () => {
|
||||
const res = cli(["open", "--workflow", "implement", "--no-open"]);
|
||||
const sid = res.json.sid;
|
||||
const dir = res.json.session;
|
||||
try {
|
||||
const line = "+" + "x".repeat(99) + "\n";
|
||||
fs.writeFileSync(path.join(dir, "big.diff"), line.repeat(Math.ceil((600 * 1024) / line.length)));
|
||||
cli(["post", "--session", sid, "--file", payload("diff-big.json", { docs: [{ id: "changes", blocks: [{ id: "c1", diff: "big.diff" }] }] })]);
|
||||
const text = readState(dir).docs.find((d) => d.id === "changes").blocks[0].text;
|
||||
const notice = "\\ No more lines shown: this change is too large to show in full.\n";
|
||||
assert.ok(text.endsWith(notice), "ends with the plain line");
|
||||
const kept = text.slice(0, -notice.length);
|
||||
assert.ok(Buffer.byteLength(kept) <= 512 * 1024, "within the cap");
|
||||
assert.ok(kept.endsWith("\n") && kept.split("\n").every((l) => l === "" || l === line.trimEnd()), "only whole lines");
|
||||
// A file changes.mjs already cut, and that carries the line, does not get a second one.
|
||||
fs.writeFileSync(path.join(dir, "cut.diff"), "diff --git a/a b/a\n" + notice);
|
||||
cli(["post", "--session", sid, "--file", payload("diff-cut.json", { docs: [{ id: "changes", blocks: [{ id: "c1", diff: "cut.diff" }] }] })]);
|
||||
assert.equal(readState(dir).docs.find((d) => d.id === "changes").blocks[0].text.split(notice).length, 2);
|
||||
} finally {
|
||||
cli(["stop", "--session", sid, "--force"]);
|
||||
}
|
||||
});
|
||||
|
||||
// The server and changes.mjs each spell the too-large line: they must match.
|
||||
test("the too-large line the server adds is the one changes.mjs adds", async () => {
|
||||
const { capPatch } = await import("../src/skill-scripts/changes.mjs");
|
||||
const { DIFF_TOO_LARGE } = await import("../src/web-console/patches.mjs");
|
||||
const patch = "diff --git a/a b/a\n" + "+x\n".repeat(50) + "diff --git a/b b/b\n" + "+y\n".repeat(50);
|
||||
const cut = capPatch(patch, 200);
|
||||
assert.equal(cut.truncated, true);
|
||||
assert.ok(cut.patch.includes(DIFF_TOO_LARGE + "\n"));
|
||||
});
|
||||
|
||||
// /app.css is the files under public/styles/ joined in CSS_ORDER (one newline
|
||||
// after each), then the accent rules. The files themselves are never served.
|
||||
test("/app.css is the style files joined in CSS_ORDER with the accent rules last", async () => {
|
||||
|
||||
Reference in new issue
Block a user