mirror of
https://github.com/jparkerweb/plan2code.git
synced 2026-12-04 00:15:41 -08:00
584 lines
30 KiB
JavaScript
584 lines
30 KiB
JavaScript
// Tests for scripts/publish-install-refs.mjs, the release workflow's slim
|
|||
|
|
// install ref publisher, against local fixtures: a bare `origin.git` and a
|
||
|
|
// full source checkout beside it in the OS temp dir. Every push goes to that
|
||
|
|
// bare repository, never anywhere else. Also checks the shape of the
|
||
|
|
// release-app workflow's publish job. Run with:
|
||
|
|
// node --test scripts/test-publish-install-refs.mjs
|
||
|
|
//
|
||
|
|
// skill-script-helpers.mjs is imported first on purpose: it scrubs the whole
|
||
|
|
// test process's git config, so the fixtures and the spawned publisher agree
|
||
|
|
// (the AGENTS.md failure-log rule). The scrubbed config names no user, which
|
||
|
|
// also exercises the publisher's stand-in commit identity.
|
||
|
|
|
||
|
|
import test from "node:test";
|
||
|
|
import assert from "node:assert/strict";
|
||
|
|
import fs from "node:fs";
|
||
|
|
import os from "node:os";
|
||
|
|
import path from "node:path";
|
||
|
|
import crypto from "node:crypto";
|
||
|
|
import { execFileSync, spawnSync } from "node:child_process";
|
||
|
|
|
||
|
|
import { ROOT, scrubbedEnv } from "./skill-script-helpers.mjs";
|
||
|
|
import {
|
||
|
|
INSTALL_REF_MANIFEST,
|
||
|
|
RUNTIME_PAYLOAD,
|
||
|
|
runtimePayloadAllowlist,
|
||
|
|
isAllowedPayloadPath,
|
||
|
|
workflowSkillSources,
|
||
|
|
assemblePayload,
|
||
|
|
validatePayload,
|
||
|
|
parseArgs,
|
||
|
|
} from "./publish-install-refs.mjs";
|
||
|
|
|
||
|
|
const SCRIPT = path.join(ROOT, "scripts", "publish-install-refs.mjs");
|
||
|
|
const WORKFLOW = path.join(ROOT, ".github", "workflows", "release-app.yml");
|
||
|
|
const env = scrubbedEnv();
|
||
|
|
|
||
|
|
const git = (dir, args, options = {}) => execFileSync("git", ["-C", dir, ...args], { encoding: "utf8", stdio: ["pipe", "pipe", "pipe"], env, ...options }).trim();
|
||
|
|
const gitOk = (dir, args) => spawnSync("git", ["-C", dir, ...args], { env, encoding: "utf8" }).status === 0;
|
||
|
|
const json = (value) => `${JSON.stringify(value, null, 2)}\n`;
|
||
|
|
|
||
|
|
const INSTALL_JS = `// fixture installer
|
||
|
|
const SHARED_SCRIPTS_ALWAYS = ['common.mjs'];
|
||
|
|
const SOURCE_PROMPTS = [
|
||
|
|
{
|
||
|
|
source: 'plan2code.md',
|
||
|
|
scripts: ['lessons.mjs'],
|
||
|
|
additionalReferences: [
|
||
|
|
{ source: 'web-console', target: 'web-console' },
|
||
|
|
{ source: 'shared-references/lessons.md', target: 'lessons.md' }
|
||
|
|
]
|
||
|
|
},
|
||
|
|
{
|
||
|
|
source: 'plan2code-3-implement.md',
|
||
|
|
scripts: ['specs.mjs', 'lessons.mjs'],
|
||
|
|
additionalReferences: [{ source: 'shared-references/test-runs.md', target: 'test-runs.md' }]
|
||
|
|
}
|
||
|
|
];
|
||
|
|
module.exports = { SOURCE_PROMPTS };
|
||
|
|
`;
|
||
|
|
|
||
|
|
// Bytes git must carry through unchanged: CRLF, a NUL and high bytes.
|
||
|
|
const BINARY = Buffer.from([0x89, 0x50, 0x4e, 0x47, 0x0d, 0x0a, 0x1a, 0x0a, 0x00, 0xff, 0xfe, 0x0d, 0x0a]);
|
||
|
|
|
||
|
|
/** The runtime files of a release commit, `{ path: { content, mode? } }`. */
|
||
|
|
function runtimeTree(version) {
|
||
|
|
return {
|
||
|
|
"install.js": { content: INSTALL_JS },
|
||
|
|
"package.json": { content: json({ name: "plan2code", version }) },
|
||
|
|
"version.json": { content: json({ name: "Plan2Code", version, releaseDate: "2026-12-01" }) },
|
||
|
|
"src/launcher/plan2code.js": { content: `#!/usr/bin/env node\n// launcher ${version}\n`, mode: "100755" },
|
||
|
|
"src/web-console/update-check.mjs": { content: `// update-check ${version}\n` },
|
||
|
|
"src/web-console/console.mjs": { content: `// console ${version}\n` },
|
||
|
|
"src/web-console/public/logo.png": { content: BINARY },
|
||
|
|
"src/installer/timing.js": { content: "// timing\n" },
|
||
|
|
"src/installer/skills-cli.js": { content: "// skills-cli\n" },
|
||
|
|
"src/installer/canonical-skills.js": { content: "// canonical-skills\n" },
|
||
|
|
"src/installer/console-stage.js": { content: "// console-stage\n" },
|
||
|
|
"src/web-console-stub/console.mjs": { content: "// stub\n" },
|
||
|
|
"src/statusline-claude/statusline.js": { content: "// statusline\n" },
|
||
|
|
"src/launcher/models.json": { content: "{}\n" },
|
||
|
|
"src/launcher/agents.json": { content: "{}\n" },
|
||
|
|
"src/web-console/agent-lookup.mjs": { content: "// agent-lookup\n" },
|
||
|
|
"src/launcher/plan2code.ico": { content: BINARY },
|
||
|
|
"src/web-console/public/favicon.png": { content: BINARY },
|
||
|
|
"src/plan2code.md": { content: "# Dashboard\n" },
|
||
|
|
"src/plan2code-3-implement.md": { content: "# Implement\r\nCRLF kept as committed\r\n" },
|
||
|
|
"src/skill-scripts/common.mjs": { content: "// common\n" },
|
||
|
|
"src/skill-scripts/lessons.mjs": { content: "// lessons\n" },
|
||
|
|
"src/skill-scripts/specs.mjs": { content: "// specs\n" },
|
||
|
|
"src/shared-references/lessons.md": { content: "# Lessons\n" },
|
||
|
|
"src/shared-references/test-runs.md": { content: "# Test runs\n" },
|
||
|
|
};
|
||
|
|
}
|
||
|
|
|
||
|
|
/** What a full checkout also carries and the payload must leave out. */
|
||
|
|
const HEAVY = {
|
||
|
|
"README.md": { content: "# readme\n" },
|
||
|
|
"CHANGELOG.md": { content: "# changelog\n" },
|
||
|
|
"docs/promo.md": { content: "# heavy docs\n" },
|
||
|
|
"scripts/publish-install-refs.mjs": { content: "// the publisher itself\n" },
|
||
|
|
"specs/idea/overview.md": { content: "# spec\n" },
|
||
|
|
"skills/plan2code/SKILL.md": { content: "# built skill\n" },
|
||
|
|
"plan2code-app/package.json": { content: "{}\n" },
|
||
|
|
"plan2code-metrics/index.js": { content: "// metrics\n" },
|
||
|
|
"plan2code-bot/index.js": { content: "// bot\n" },
|
||
|
|
".github/workflows/release-app.yml": { content: "name: release-app\n" },
|
||
|
|
"src/node_modules/junk/index.js": { content: "// vendored junk\n" },
|
||
|
|
"src/web-console/fixtures/sample.json": { content: "{}\n" },
|
||
|
|
};
|
||
|
|
|
||
|
|
const fullTree = (version) => ({ ...runtimeTree(version), ...HEAVY });
|
||
|
|
|
||
|
|
const data = (content) => {
|
||
|
|
const buf = Buffer.isBuffer(content) ? content : Buffer.from(content);
|
||
|
|
return Buffer.concat([Buffer.from(`data ${buf.length}\n`), buf, Buffer.from("\n")]);
|
||
|
|
};
|
||
|
|
|
||
|
|
/**
|
||
|
|
* A source checkout `work/` whose origin is a bare `origin.git`, both in one
|
||
|
|
* temp folder. `release: "legacy"` gives origin a full-source release branch
|
||
|
|
* (the pre-migration shape); `"none"` gives it none.
|
||
|
|
*/
|
||
|
|
function createSource({ release = "legacy" } = {}) {
|
||
|
|
const root = fs.mkdtempSync(path.join(fs.realpathSync.native(os.tmpdir()), "p2c-publish-refs-"));
|
||
|
|
const work = path.join(root, "work");
|
||
|
|
const origin = path.join(root, "origin.git");
|
||
|
|
execFileSync("git", ["init", "-q", "--bare", "-b", "main", origin], { env, stdio: "pipe" });
|
||
|
|
execFileSync("git", ["init", "-q", "-b", "main", work], { env, stdio: "pipe" });
|
||
|
|
git(work, ["remote", "add", "origin", origin]);
|
||
|
|
let ts = 1700000000;
|
||
|
|
|
||
|
|
/** Commits `files` as the whole tree of main (optionally tagging it), pushes, and checks it out. */
|
||
|
|
function commit(files, { tag = null, annotated = false, message = "commit" } = {}) {
|
||
|
|
ts += 60;
|
||
|
|
const parent = gitOk(work, ["rev-parse", "--verify", "--quiet", "refs/heads/main"]) ? git(work, ["rev-parse", "refs/heads/main"]) : null;
|
||
|
|
const who = `Fixture <fixture@example.com> ${ts} +0000`;
|
||
|
|
const parts = [Buffer.from(`commit refs/heads/main\nauthor ${who}\ncommitter ${who}\n`), data(message)];
|
||
|
|
if (parent) parts.push(Buffer.from(`from ${parent}\n`));
|
||
|
|
parts.push(Buffer.from("deleteall\n"));
|
||
|
|
for (const [name, { content, mode = "100644" }] of Object.entries(files)) parts.push(Buffer.from(`M ${mode} inline ${name}\n`), data(content));
|
||
|
|
if (tag && annotated) parts.push(Buffer.from(`tag ${tag}\nfrom refs/heads/main\ntagger ${who}\n`), data(`release ${tag}\n`));
|
||
|
|
git(work, ["fast-import", "--quiet"], { input: Buffer.concat(parts) });
|
||
|
|
if (tag && !annotated) git(work, ["update-ref", `refs/tags/${tag}`, "refs/heads/main"]);
|
||
|
|
git(work, ["checkout", "-q", "-f", "main"]);
|
||
|
|
git(work, ["push", "-q", "origin", "main", ...(tag ? [`refs/tags/${tag}`] : [])]);
|
||
|
|
return git(work, ["rev-parse", "HEAD"]);
|
||
|
|
}
|
||
|
|
|
||
|
|
let legacyTip = null;
|
||
|
|
if (release === "legacy") {
|
||
|
|
legacyTip = commit(fullTree("1.0.0"), { tag: "v1.0.0", message: "legacy release 1.0.0" });
|
||
|
|
git(work, ["push", "-q", "origin", `${legacyTip}:refs/heads/release`]);
|
||
|
|
}
|
||
|
|
|
||
|
|
return {
|
||
|
|
root,
|
||
|
|
work,
|
||
|
|
origin,
|
||
|
|
legacyTip,
|
||
|
|
commit,
|
||
|
|
/** Commits a full tree for `version`, tagged `vX.Y.Z`; `mutate(files)` edits it first. */
|
||
|
|
release(version, { annotated = false, mutate } = {}) {
|
||
|
|
const files = fullTree(version);
|
||
|
|
if (mutate) mutate(files);
|
||
|
|
return commit(files, { tag: `v${version}`, annotated, message: `release ${version}` });
|
||
|
|
},
|
||
|
|
remoteRef: (ref) => {
|
||
|
|
const out = git(origin, ["for-each-ref", "--format=%(objectname)", ref]);
|
||
|
|
return out || null;
|
||
|
|
},
|
||
|
|
cleanup: () => fs.rmSync(root, { recursive: true, force: true }),
|
||
|
|
};
|
||
|
|
}
|
||
|
|
|
||
|
|
/** Runs the publisher CLI from the checkout; `result` is its --json output when it wrote one. */
|
||
|
|
function publish(fx, args) {
|
||
|
|
const jsonPath = path.join(fx.root, `result-${crypto.randomBytes(3).toString("hex")}.json`);
|
||
|
|
const r = spawnSync(process.execPath, [SCRIPT, "--repo", fx.work, "--json", jsonPath, ...args], { cwd: fx.work, env, encoding: "utf8" });
|
||
|
|
const result = fs.existsSync(jsonPath) ? JSON.parse(fs.readFileSync(jsonPath, "utf8")) : null;
|
||
|
|
return { status: r.status, stdout: r.stdout, stderr: r.stderr, result };
|
||
|
|
}
|
||
|
|
|
||
|
|
/** `{ path: mode }` of every file in a commit's tree in origin. */
|
||
|
|
function remoteTree(fx, commit) {
|
||
|
|
const out = git(fx.origin, ["ls-tree", "-r", "--full-tree", commit]);
|
||
|
|
const files = {};
|
||
|
|
for (const line of out.split("\n").filter(Boolean)) {
|
||
|
|
const [meta, name] = line.split("\t");
|
||
|
|
files[name] = meta.split(" ")[0];
|
||
|
|
}
|
||
|
|
return files;
|
||
|
|
}
|
||
|
|
|
||
|
|
const indexHash = (fx) => crypto.createHash("sha256").update(fs.readFileSync(path.join(fx.work, ".git", "index"))).digest("hex");
|
||
|
|
|
||
|
|
test("allowlist: one constant-driven structure of explicit files and prefixes", () => {
|
||
|
|
const allow = runtimePayloadAllowlist();
|
||
|
|
assert.equal(allow, RUNTIME_PAYLOAD);
|
||
|
|
assert.deepEqual([...allow.files], ["install.js", "package.json", "version.json"]);
|
||
|
|
assert.deepEqual([...allow.prefixes], ["src/"]);
|
||
|
|
assert.ok(Object.isFrozen(allow) && Object.isFrozen(allow.files) && Object.isFrozen(allow.required));
|
||
|
|
for (const ok of ["install.js", "src/launcher/plan2code.js", "src/web-console/public/activities/snake.js"]) assert.equal(isAllowedPayloadPath(ok), true, ok);
|
||
|
|
for (const bad of ["README.md", "docs/a.md", "scripts/publish-install-refs.mjs", "specs/x.md", "skills/a/SKILL.md", "plan2code-app/x", "plan2code-metrics/x", "plan2code-bot/x", ".github/x.yml", ".git/HEAD", "src/node_modules/a.js", "src/x/fixtures/a.json", "src/", "src/../install.js", "src\\a.js", ""]) {
|
||
|
|
assert.equal(isAllowedPayloadPath(bad), false, bad);
|
||
|
|
}
|
||
|
|
});
|
||
|
|
|
||
|
|
test("allowlist: every skill build input comes from install.js's SOURCE_PROMPTS", () => {
|
||
|
|
assert.deepEqual(workflowSkillSources(INSTALL_JS), [
|
||
|
|
"src/plan2code.md",
|
||
|
|
"src/plan2code-3-implement.md",
|
||
|
|
"src/skill-scripts/lessons.mjs",
|
||
|
|
"src/skill-scripts/specs.mjs",
|
||
|
|
"src/skill-scripts/common.mjs",
|
||
|
|
"src/web-console",
|
||
|
|
"src/shared-references/lessons.md",
|
||
|
|
"src/shared-references/test-runs.md",
|
||
|
|
]);
|
||
|
|
assert.deepEqual(workflowSkillSources("// no prompts here\n"), []);
|
||
|
|
// The real installer: every input it names exists in this checkout's src/.
|
||
|
|
const real = workflowSkillSources(fs.readFileSync(path.join(ROOT, "install.js"), "utf8"));
|
||
|
|
assert.ok(real.includes("src/plan2code-3-implement-review.md") && real.includes("src/skill-scripts/specs.mjs") && real.includes("src/web-console"));
|
||
|
|
for (const rel of real) assert.ok(fs.existsSync(path.join(ROOT, ...rel.split("/"))), rel);
|
||
|
|
});
|
||
|
|
|
||
|
|
test("arguments: unknown, missing and malformed arguments print the usage and exit 2", () => {
|
||
|
|
assert.equal(parseArgs(["--tag", "v1.2.3", "--dry-run"]).options.dryRun, true);
|
||
|
|
assert.match(parseArgs(["--tag", "1.2.3"]).error, /exact vX\.Y\.Z/);
|
||
|
|
assert.match(parseArgs(["--tag", "v1.2"]).error, /exact vX\.Y\.Z/);
|
||
|
|
assert.match(parseArgs(["--tag", "install-v1.2.3"]).error, /exact vX\.Y\.Z/);
|
||
|
|
assert.match(parseArgs([]).error, /--tag is required/);
|
||
|
|
assert.match(parseArgs(["--tag"]).error, /needs a value/);
|
||
|
|
for (const args of [["--tag", "v1.2.3", "--force"], ["--tag", "v1.2.3-rc.1"], ["--repo", "."]]) {
|
||
|
|
const r = spawnSync(process.execPath, [SCRIPT, ...args], { env, encoding: "utf8" });
|
||
|
|
assert.equal(r.status, 2, args.join(" "));
|
||
|
|
assert.match(r.stderr, /Usage: node scripts\/publish-install-refs\.mjs/);
|
||
|
|
}
|
||
|
|
});
|
||
|
|
|
||
|
|
test("first publication: parents the legacy release tip, pushes one slim commit to both refs", (t) => {
|
||
|
|
const fx = createSource();
|
||
|
|
t.after(fx.cleanup);
|
||
|
|
const tagCommit = fx.release("1.2.0");
|
||
|
|
const statusBefore = git(fx.work, ["status", "--porcelain"]);
|
||
|
|
const indexBefore = indexHash(fx);
|
||
|
|
|
||
|
|
const r = publish(fx, ["--tag", "v1.2.0"]);
|
||
|
|
assert.equal(r.status, 0, r.stderr);
|
||
|
|
const res = r.result;
|
||
|
|
assert.equal(res.ok, true);
|
||
|
|
assert.equal(res.pushed, true);
|
||
|
|
assert.equal(res.parent, fx.legacyTip, "the first migration parents the legacy full-source tip");
|
||
|
|
assert.equal(res.parentKind, "release");
|
||
|
|
assert.equal(res.sourceCommit, tagCommit);
|
||
|
|
assert.deepEqual(res.refs, { tag: "refs/tags/install-v1.2.0", branch: "refs/heads/release" });
|
||
|
|
assert.equal(fx.remoteRef("refs/tags/install-v1.2.0"), res.commit);
|
||
|
|
assert.equal(fx.remoteRef("refs/heads/release"), res.commit);
|
||
|
|
assert.equal(git(fx.origin, ["rev-parse", `${res.commit}^{tree}`]), res.tree);
|
||
|
|
assert.equal(git(fx.origin, ["rev-parse", `${res.commit}^1`]), fx.legacyTip);
|
||
|
|
assert.equal(gitOk(fx.origin, ["rev-parse", "--verify", "--quiet", `${res.commit}^2`]), false, "exactly one parent");
|
||
|
|
assert.match(r.stdout, /Published [0-9a-f]{40}/);
|
||
|
|
|
||
|
|
const files = remoteTree(fx, res.commit);
|
||
|
|
const expected = [...Object.keys(runtimeTree("1.2.0")), INSTALL_REF_MANIFEST].sort();
|
||
|
|
assert.deepEqual(Object.keys(files).sort(), expected, "exactly the runtime allowlist plus the manifest");
|
||
|
|
assert.equal(files["src/launcher/plan2code.js"], "100755", "the executable bit survives");
|
||
|
|
assert.equal(files["install.js"], "100644");
|
||
|
|
assert.equal(res.files, expected.length);
|
||
|
|
assert.ok(res.excluded >= Object.keys(HEAVY).length);
|
||
|
|
|
||
|
|
const blob = (name) => execFileSync("git", ["-C", fx.origin, "cat-file", "blob", `${res.commit}:${name}`], { env });
|
||
|
|
assert.deepEqual(blob("src/web-console/public/logo.png"), BINARY, "binary bytes are carried unchanged");
|
||
|
|
assert.equal(blob("src/plan2code-3-implement.md").toString(), "# Implement\r\nCRLF kept as committed\r\n");
|
||
|
|
assert.equal(res.bytes, expected.reduce((sum, name) => sum + blob(name).length, 0));
|
||
|
|
|
||
|
|
const manifest = JSON.parse(blob(INSTALL_REF_MANIFEST).toString());
|
||
|
|
assert.deepEqual(Object.keys(manifest), ["kind", "version", "sourceTag", "sourceCommit", "files"]);
|
||
|
|
assert.equal(manifest.kind, "install-ref");
|
||
|
|
assert.equal(manifest.version, "1.2.0");
|
||
|
|
assert.equal(manifest.sourceTag, "v1.2.0");
|
||
|
|
assert.equal(manifest.sourceCommit, tagCommit);
|
||
|
|
assert.equal(manifest.files[INSTALL_REF_MANIFEST], undefined, "the manifest never lists itself");
|
||
|
|
assert.deepEqual(Object.keys(manifest.files).sort(), Object.keys(runtimeTree("1.2.0")).sort());
|
||
|
|
for (const [name, hash] of Object.entries(manifest.files)) {
|
||
|
|
assert.equal(hash, crypto.createHash("sha256").update(blob(name)).digest("hex"), name);
|
||
|
|
}
|
||
|
|
|
||
|
|
assert.equal(git(fx.work, ["status", "--porcelain"]), statusBefore, "the working tree is untouched");
|
||
|
|
assert.equal(indexHash(fx), indexBefore, "the normal index is untouched");
|
||
|
|
assert.deepEqual(fs.readdirSync(path.join(fx.work, ".git")).filter((n) => n.startsWith("p2c-install-index")), [], "the temporary index is removed");
|
||
|
|
assert.equal(git(fx.origin, ["log", "-1", "--format=%an <%ae>", res.commit]), "github-actions[bot] <41898099+github-actions[bot]@users.noreply.github.com>");
|
||
|
|
});
|
||
|
|
|
||
|
|
test("subsequent publication: parents the previous synthetic commit and fast-forwards release", (t) => {
|
||
|
|
const fx = createSource();
|
||
|
|
t.after(fx.cleanup);
|
||
|
|
fx.release("1.2.0");
|
||
|
|
const first = publish(fx, ["--tag", "v1.2.0"]);
|
||
|
|
assert.equal(first.status, 0, first.stderr);
|
||
|
|
git(fx.work, ["fetch", "-q", "--no-tags", "origin", "+refs/heads/release:refs/remotes/origin/release"]);
|
||
|
|
|
||
|
|
const tagCommit = fx.release("1.3.0");
|
||
|
|
const second = publish(fx, ["--tag", "v1.3.0"]);
|
||
|
|
assert.equal(second.status, 0, second.stderr);
|
||
|
|
assert.equal(second.result.parent, first.result.commit, "the synthetic chain, never the full-source commit");
|
||
|
|
assert.notEqual(second.result.parent, tagCommit);
|
||
|
|
assert.equal(fx.remoteRef("refs/heads/release"), second.result.commit);
|
||
|
|
assert.equal(fx.remoteRef("refs/tags/install-v1.2.0"), first.result.commit, "the older install tag stays where it was");
|
||
|
|
assert.equal(fx.remoteRef("refs/tags/install-v1.3.0"), second.result.commit);
|
||
|
|
assert.equal(gitOk(fx.origin, ["merge-base", "--is-ancestor", first.result.commit, second.result.commit]), true);
|
||
|
|
});
|
||
|
|
|
||
|
|
test("the release tip must be in the checkout: an unfetched tip fails before anything is pushed", (t) => {
|
||
|
|
const fx = createSource();
|
||
|
|
t.after(fx.cleanup);
|
||
|
|
fx.release("1.2.0");
|
||
|
|
// Another runner moved origin's release branch to a commit this checkout never fetched.
|
||
|
|
const elsewhere = path.join(fx.root, "elsewhere");
|
||
|
|
execFileSync("git", ["clone", "-q", fx.origin, elsewhere], { env, stdio: "pipe" });
|
||
|
|
git(elsewhere, ["-c", "user.name=x", "-c", "user.email=x@example.com", "commit", "-q", "--allow-empty", "-m", "moved"]);
|
||
|
|
git(elsewhere, ["push", "-q", "origin", "HEAD:refs/heads/release"]);
|
||
|
|
const moved = fx.remoteRef("refs/heads/release");
|
||
|
|
|
||
|
|
const r = publish(fx, ["--tag", "v1.2.0"]);
|
||
|
|
assert.equal(r.status, 1);
|
||
|
|
assert.match(r.stderr, /fetch refs\/heads\/release first/);
|
||
|
|
assert.equal(fx.remoteRef("refs/heads/release"), moved);
|
||
|
|
assert.equal(fx.remoteRef("refs/tags/install-v1.2.0"), null);
|
||
|
|
});
|
||
|
|
|
||
|
|
test("tag collision: an existing install tag, remote or local, is never overwritten", (t) => {
|
||
|
|
const fx = createSource();
|
||
|
|
t.after(fx.cleanup);
|
||
|
|
fx.release("1.2.0");
|
||
|
|
const first = publish(fx, ["--tag", "v1.2.0"]);
|
||
|
|
assert.equal(first.status, 0, first.stderr);
|
||
|
|
|
||
|
|
// A second run for the same, fully published release (the workflow has more than one trigger)
|
||
|
|
// succeeds and pushes nothing, rather than failing red after a good release.
|
||
|
|
const again = publish(fx, ["--tag", "v1.2.0"]);
|
||
|
|
assert.equal(again.status, 0, again.stderr);
|
||
|
|
assert.equal(again.result.alreadyPublished, true);
|
||
|
|
assert.equal(again.result.pushed, false);
|
||
|
|
assert.match(again.stdout, /v1\.2\.0 is already published/);
|
||
|
|
assert.equal(fx.remoteRef("refs/tags/install-v1.2.0"), first.result.commit);
|
||
|
|
assert.equal(fx.remoteRef("refs/heads/release"), first.result.commit);
|
||
|
|
|
||
|
|
git(fx.work, ["fetch", "-q", "--no-tags", "origin", "+refs/heads/release:refs/remotes/origin/release"]);
|
||
|
|
fx.release("1.3.0");
|
||
|
|
git(fx.work, ["tag", "install-v1.3.0", "HEAD"]);
|
||
|
|
const local = publish(fx, ["--tag", "v1.3.0"]);
|
||
|
|
assert.equal(local.status, 1);
|
||
|
|
assert.match(local.stderr, /already exists in this checkout/);
|
||
|
|
assert.equal(fx.remoteRef("refs/tags/install-v1.3.0"), null);
|
||
|
|
assert.equal(fx.remoteRef("refs/heads/release"), first.result.commit);
|
||
|
|
});
|
||
|
|
|
||
|
|
test("missing release branch: refused without the allowance, parents the source tag commit with it", (t) => {
|
||
|
|
const fx = createSource({ release: "none" });
|
||
|
|
t.after(fx.cleanup);
|
||
|
|
const tagCommit = fx.release("1.2.0");
|
||
|
|
|
||
|
|
const refused = publish(fx, ["--tag", "v1.2.0"]);
|
||
|
|
assert.equal(refused.status, 1);
|
||
|
|
assert.match(refused.stderr, /refs\/heads\/release does not exist on origin; pass --allow-missing-release-branch/);
|
||
|
|
assert.equal(fx.remoteRef("refs/heads/release"), null);
|
||
|
|
assert.equal(fx.remoteRef("refs/tags/install-v1.2.0"), null);
|
||
|
|
|
||
|
|
const allowed = publish(fx, ["--tag", "v1.2.0", "--allow-missing-release-branch"]);
|
||
|
|
assert.equal(allowed.status, 0, allowed.stderr);
|
||
|
|
assert.equal(allowed.result.parent, tagCommit);
|
||
|
|
assert.equal(allowed.result.parentKind, "source-tag");
|
||
|
|
assert.equal(fx.remoteRef("refs/heads/release"), allowed.result.commit);
|
||
|
|
assert.equal(fx.remoteRef("refs/tags/install-v1.2.0"), allowed.result.commit);
|
||
|
|
});
|
||
|
|
|
||
|
|
test("payload contamination: validation lists every unexpected path and forbidden top-level entry", (t) => {
|
||
|
|
const fx = createSource();
|
||
|
|
t.after(fx.cleanup);
|
||
|
|
const tagCommit = fx.release("1.2.0");
|
||
|
|
const out = path.join(fx.root, "payload");
|
||
|
|
const { modes, excluded } = assemblePayload({ repo: fx.work, commit: tagCommit, output: out });
|
||
|
|
assert.equal(modes["src/launcher/plan2code.js"], "100755");
|
||
|
|
assert.equal(excluded, Object.keys(HEAVY).length);
|
||
|
|
const clean = validatePayload(out);
|
||
|
|
assert.equal(clean.ok, true, clean.report.join("\n"));
|
||
|
|
assert.equal(clean.manifest, false);
|
||
|
|
|
||
|
|
for (const name of ["docs/promo.md", "scripts/publish-install-refs.mjs", "README.md", "src/node_modules/junk/index.js", "plan2code-bot/index.js"]) {
|
||
|
|
fs.mkdirSync(path.dirname(path.join(out, name)), { recursive: true });
|
||
|
|
fs.writeFileSync(path.join(out, name), "contamination\n");
|
||
|
|
}
|
||
|
|
const dirty = validatePayload(out);
|
||
|
|
assert.equal(dirty.ok, false);
|
||
|
|
assert.deepEqual(dirty.unexpected, ["README.md", "docs/promo.md", "plan2code-bot/index.js", "scripts/publish-install-refs.mjs", "src/node_modules/junk/index.js"]);
|
||
|
|
assert.deepEqual(dirty.forbidden, ["docs", "scripts", "plan2code-bot"]);
|
||
|
|
assert.ok(dirty.report.includes("+ docs/promo.md"));
|
||
|
|
assert.ok(dirty.report.includes("+ docs/ (forbidden top-level entry)"));
|
||
|
|
|
||
|
|
// An --output folder that already holds anything is refused before assembly.
|
||
|
|
const r = publish(fx, ["--tag", "v1.2.0", "--output", out, "--dry-run"]);
|
||
|
|
assert.equal(r.status, 1);
|
||
|
|
assert.match(r.stderr, /is not empty/);
|
||
|
|
});
|
||
|
|
|
||
|
|
test("missing required runtime files: the diff-like report names each one and nothing is pushed", (t) => {
|
||
|
|
const fx = createSource();
|
||
|
|
t.after(fx.cleanup);
|
||
|
|
fx.release("1.2.0", {
|
||
|
|
mutate: (files) => {
|
||
|
|
delete files["src/installer/console-stage.js"];
|
||
|
|
delete files["src/plan2code-3-implement.md"];
|
||
|
|
delete files["src/skill-scripts/specs.mjs"];
|
||
|
|
delete files["src/shared-references/test-runs.md"];
|
||
|
|
// The whole src/web-console folder, a reference SOURCE_PROMPTS names.
|
||
|
|
delete files["src/web-console/console.mjs"];
|
||
|
|
delete files["src/web-console/update-check.mjs"];
|
||
|
|
delete files["src/web-console/public/logo.png"];
|
||
|
|
delete files["src/web-console/agent-lookup.mjs"];
|
||
|
|
delete files["src/web-console/public/favicon.png"];
|
||
|
|
},
|
||
|
|
});
|
||
|
|
const r = publish(fx, ["--tag", "v1.2.0"]);
|
||
|
|
assert.equal(r.status, 1);
|
||
|
|
assert.match(r.stderr, /runtime payload of v1\.2\.0 is not valid/);
|
||
|
|
const reported = [
|
||
|
|
"src/installer/console-stage.js",
|
||
|
|
"src/plan2code-3-implement.md",
|
||
|
|
"src/shared-references/test-runs.md",
|
||
|
|
"src/skill-scripts/specs.mjs",
|
||
|
|
"src/web-console",
|
||
|
|
"src/web-console/agent-lookup.mjs",
|
||
|
|
"src/web-console/console.mjs",
|
||
|
|
"src/web-console/public/favicon.png",
|
||
|
|
"src/web-console/update-check.mjs",
|
||
|
|
];
|
||
|
|
for (const name of reported) {
|
||
|
|
assert.match(r.stderr, new RegExp(`^- ${name.replace(/\./g, "\\.")}$`, "m"), name);
|
||
|
|
}
|
||
|
|
assert.deepEqual(
|
||
|
|
r.result.details.filter((l) => l.startsWith("- ")),
|
||
|
|
reported.map((name) => `- ${name}`),
|
||
|
|
);
|
||
|
|
assert.equal(fx.remoteRef("refs/tags/install-v1.2.0"), null);
|
||
|
|
assert.equal(fx.remoteRef("refs/heads/release"), fx.legacyTip);
|
||
|
|
});
|
||
|
|
|
||
|
|
test("version mismatch: version.json, package.json and the checkout's HEAD must all agree with the tag", (t) => {
|
||
|
|
const fx = createSource();
|
||
|
|
t.after(fx.cleanup);
|
||
|
|
fx.release("1.2.0", { mutate: (files) => (files["package.json"] = { content: json({ name: "plan2code", version: "1.2.1" }) }) });
|
||
|
|
const pkg = publish(fx, ["--tag", "v1.2.0"]);
|
||
|
|
assert.equal(pkg.status, 1);
|
||
|
|
assert.match(pkg.stderr, /do not match the tag v1\.2\.0: package\.json says 1\.2\.1/);
|
||
|
|
|
||
|
|
fx.release("1.3.0", { mutate: (files) => (files["version.json"] = { content: json({ version: "1.2.9" }) }) });
|
||
|
|
const ver = publish(fx, ["--tag", "v1.3.0"]);
|
||
|
|
assert.equal(ver.status, 1);
|
||
|
|
assert.match(ver.stderr, /version\.json says 1\.2\.9/);
|
||
|
|
|
||
|
|
fx.release("1.4.0");
|
||
|
|
fx.commit(fullTree("1.4.0"), { message: "work after the tag" });
|
||
|
|
const head = publish(fx, ["--tag", "v1.4.0"]);
|
||
|
|
assert.equal(head.status, 1);
|
||
|
|
assert.match(head.stderr, /the checkout's HEAD [0-9a-f]{40} is not the commit v1\.4\.0 names/);
|
||
|
|
|
||
|
|
const missing = publish(fx, ["--tag", "v9.9.9"]);
|
||
|
|
assert.equal(missing.status, 1);
|
||
|
|
assert.match(missing.stderr, /the tag v9\.9\.9 does not exist/);
|
||
|
|
|
||
|
|
for (const ref of ["refs/tags/install-v1.2.0", "refs/tags/install-v1.3.0", "refs/tags/install-v1.4.0"]) assert.equal(fx.remoteRef(ref), null, ref);
|
||
|
|
assert.equal(fx.remoteRef("refs/heads/release"), fx.legacyTip);
|
||
|
|
});
|
||
|
|
|
||
|
|
test("annotated and lightweight source tags both resolve to their commit", (t) => {
|
||
|
|
const fx = createSource();
|
||
|
|
t.after(fx.cleanup);
|
||
|
|
const light = fx.release("1.2.0");
|
||
|
|
assert.equal(git(fx.work, ["cat-file", "-t", "refs/tags/v1.2.0"]), "commit");
|
||
|
|
const a = publish(fx, ["--tag", "v1.2.0"]);
|
||
|
|
assert.equal(a.status, 0, a.stderr);
|
||
|
|
assert.equal(a.result.sourceCommit, light);
|
||
|
|
|
||
|
|
git(fx.work, ["fetch", "-q", "--no-tags", "origin", "+refs/heads/release:refs/remotes/origin/release"]);
|
||
|
|
const annotatedCommit = fx.release("1.3.0", { annotated: true });
|
||
|
|
assert.equal(git(fx.work, ["cat-file", "-t", "refs/tags/v1.3.0"]), "tag");
|
||
|
|
const b = publish(fx, ["--tag", "v1.3.0"]);
|
||
|
|
assert.equal(b.status, 0, b.stderr);
|
||
|
|
assert.equal(b.result.sourceCommit, annotatedCommit, "the annotated tag peels to its commit");
|
||
|
|
const manifest = JSON.parse(execFileSync("git", ["-C", fx.origin, "cat-file", "blob", `${b.result.commit}:${INSTALL_REF_MANIFEST}`], { env, encoding: "utf8" }));
|
||
|
|
assert.equal(manifest.sourceCommit, annotatedCommit);
|
||
|
|
});
|
||
|
|
|
||
|
|
test("dry run: builds and checks the commit, pushes nothing, keeps --output", (t) => {
|
||
|
|
const fx = createSource();
|
||
|
|
t.after(fx.cleanup);
|
||
|
|
fx.release("1.2.0");
|
||
|
|
const out = path.join(fx.root, "kept-payload");
|
||
|
|
const r = publish(fx, ["--tag", "v1.2.0", "--dry-run", "--output", out]);
|
||
|
|
assert.equal(r.status, 0, r.stderr);
|
||
|
|
assert.equal(r.result.dryRun, true);
|
||
|
|
assert.equal(r.result.pushed, false);
|
||
|
|
assert.match(r.stdout, /^Would publish [0-9a-f]{40}/);
|
||
|
|
assert.equal(r.result.payload, out);
|
||
|
|
assert.equal(fs.existsSync(path.join(out, INSTALL_REF_MANIFEST)), true);
|
||
|
|
assert.equal(git(fx.work, ["cat-file", "-t", r.result.commit]), "commit", "the commit exists locally");
|
||
|
|
assert.equal(fx.remoteRef("refs/tags/install-v1.2.0"), null);
|
||
|
|
assert.equal(fx.remoteRef("refs/heads/release"), fx.legacyTip);
|
||
|
|
});
|
||
|
|
|
||
|
|
/** The publish job's steps from release-app.yml, `[{ name, text }]` in order. */
|
||
|
|
function publishSteps() {
|
||
|
|
const yml = fs.readFileSync(WORKFLOW, "utf8").replace(/\r\n/g, "\n");
|
||
|
|
const job = yml.slice(yml.indexOf("\n publish:\n"));
|
||
|
|
assert.ok(job.length > 1, "release-app.yml has a publish job");
|
||
|
|
const steps = job.slice(job.indexOf("\n steps:\n")).split("\n - ").slice(1);
|
||
|
|
return { yml, steps: steps.map((text) => ({ name: (/^(?:name:\s*(.+)|uses:\s*(.+))/.exec(text) || [])[1] || text.split("\n")[0], text })) };
|
||
|
|
}
|
||
|
|
|
||
|
|
test("release workflow: synthetic-ref publication is the publish job's last step, after the release is public", () => {
|
||
|
|
const { yml, steps } = publishSteps();
|
||
|
|
const names = steps.map((s) => s.name.trim());
|
||
|
|
const attach = names.indexOf("Attach to the release, creating it last");
|
||
|
|
const pub = names.indexOf("Publish the slim install refs");
|
||
|
|
assert.ok(attach !== -1, "the step that creates and publishes the release is there");
|
||
|
|
assert.ok(pub > attach, "publication comes after the release is public");
|
||
|
|
assert.equal(pub, steps.length - 1, "publication is the last step");
|
||
|
|
assert.match(steps[attach].text, /gh release edit "\$TAG" --draft=false/);
|
||
|
|
const run = steps[pub].text;
|
||
|
|
assert.match(steps[0].text, /persist-credentials:\s*false/, "the publish checkout does not persist the workflow token in .git/config");
|
||
|
|
assert.match(run, /GIT_ASKPASS="\$RUNNER_TEMP\/p2c-git-askpass\.sh"/);
|
||
|
|
assert.match(run, /GIT_TERMINAL_PROMPT=0/);
|
||
|
|
assert.match(run, /GCM_INTERACTIVE=never/);
|
||
|
|
assert.match(run, /credential\.username x-access-token/);
|
||
|
|
assert.doesNotMatch(run, /extraheader|credential-helper output/, "no token or credential-helper output is written or echoed");
|
||
|
|
assert.match(run, /node scripts\/publish-install-refs\.mjs --tag "\$TAG" --repo "\$GITHUB_WORKSPACE"/);
|
||
|
|
assert.match(run, /git fetch --no-tags --unshallow origin/);
|
||
|
|
assert.match(run, /refs\/heads\/release:refs\/remotes\/origin\/release/);
|
||
|
|
assert.match(run, /refs\/tags\/\$\{TAG\}:refs\/tags\/\$\{TAG\}/);
|
||
|
|
assert.doesNotMatch(run, /continue-on-error|\|\| true/, "a failed publication fails the workflow");
|
||
|
|
assert.equal(names.includes("Move the release branch to this tag"), false, "the old branch move is gone");
|
||
|
|
const code = yml
|
||
|
|
.split("\n")
|
||
|
|
.filter((line) => !/^\s*#/.test(line))
|
||
|
|
.join("\n");
|
||
|
|
assert.doesNotMatch(code, /--force|git push[^\n]*\s-f\b|git push[^\n]*\s\+/, "no force push anywhere in the workflow");
|
||
|
|
assert.doesNotMatch(code, /git push/, "the publisher does the only push");
|
||
|
|
});
|
||
|
|
|
||
|
|
test("an older or equal release never moves the release branch back, unless --allow-older says so", (t) => {
|
||
|
|
const fx = createSource();
|
||
|
|
t.after(fx.cleanup);
|
||
|
|
fx.release("1.3.0");
|
||
|
|
const newer = publish(fx, ["--tag", "v1.3.0"]);
|
||
|
|
assert.equal(newer.status, 0, newer.stderr);
|
||
|
|
git(fx.work, ["fetch", "-q", "--no-tags", "origin", "+refs/heads/release:refs/remotes/origin/release"]);
|
||
|
|
|
||
|
|
// A hotfix tagged after it, from an older line: refused, nothing pushed.
|
||
|
|
fx.release("1.2.9");
|
||
|
|
const older = publish(fx, ["--tag", "v1.2.9"]);
|
||
|
|
assert.equal(older.status, 1);
|
||
|
|
assert.match(older.stderr, /already installs 1\.3\.0; refusing to move it to 1\.2\.9/);
|
||
|
|
assert.equal(fx.remoteRef("refs/tags/install-v1.2.9"), null);
|
||
|
|
assert.equal(fx.remoteRef("refs/heads/release"), newer.result.commit);
|
||
|
|
|
||
|
|
// The deliberate override still works.
|
||
|
|
const forced = publish(fx, ["--tag", "v1.2.9", "--allow-older"]);
|
||
|
|
assert.equal(forced.status, 0, forced.stderr);
|
||
|
|
assert.equal(fx.remoteRef("refs/heads/release"), forced.result.commit);
|
||
|
|
});
|
||
|
|
|
||
|
|
test("a package.json script that makes npm do a full dev install of a git package refuses the publication", (t) => {
|
||
|
|
const fx = createSource();
|
||
|
|
t.after(fx.cleanup);
|
||
|
|
fx.release("1.2.0", {
|
||
|
|
mutate: (files) => {
|
||
|
|
files["package.json"] = { content: json({ name: "plan2code", version: "1.2.0", scripts: { prepare: "node build.js", test: "node --test" } }) };
|
||
|
|
},
|
||
|
|
});
|
||
|
|
const r = publish(fx, ["--tag", "v1.2.0"]);
|
||
|
|
assert.equal(r.status, 1);
|
||
|
|
assert.match(r.stderr, /package\.json has prepare/);
|
||
|
|
assert.equal(fx.remoteRef("refs/tags/install-v1.2.0"), null);
|
||
|
|
});
|