Files
plan2code/scripts/test-install-e2e.mjs
T

277 lines
14 KiB
JavaScript
Raw Permalink Normal View History

// End-to-end installer validation against real local git refs. The fetches are
// real `git clone`s from a bare fixture, the installer is the repository's real
// install.js, and every write lands in an isolated HOME/npm/console/app-data
// tree. The only stand-ins are a cached no-op skills CLI (the seam production
// uses for an already-warm npx cache); release app assets are deliberately
// unreachable here, while their successful checksum path stays in
// test-install-update.mjs. Nothing here contacts GitHub or the npm registry.
import "./skill-script-helpers.mjs";
import test, { after } from "node:test";
import assert from "node:assert/strict";
import fs from "node:fs";
import path from "node:path";
import { spawnSync } from "node:child_process";
import { installRefForRelease, chooseReleaseRef, parseLsRemoteSnapshot } from "../src/web-console/update-check.mjs";
import {
createInstallE2EFixture,
installE2EExists,
installE2EJson,
installLocalNpxShim,
launcherRunEnvironment,
makeInstallE2ERunRoot,
readSkillsCliLog,
runFixtureInstaller,
writeFixtureSnapshot,
} from "./install-e2e-fixture.mjs";
let fixtureInstance = null;
function fixture() {
if (!fixtureInstance) fixtureInstance = createInstallE2EFixture();
return fixtureInstance;
}
after(() => fixtureInstance?.cleanup());
function readJson(file) {
return JSON.parse(fs.readFileSync(file, "utf8"));
}
function timingEvents(run) {
try {
return fs
.readFileSync(run.isolated.paths.benchJson, "utf8")
.split(/\r?\n/)
.filter(Boolean)
.map((line) => JSON.parse(line));
} catch {
return [];
}
}
function assertInstallerSucceeded(run) {
assert.equal(run.result.error, undefined, run.result.error?.message || "");
assert.equal(run.result.status, 0, `stdout:\n${run.stdout}\nstderr:\n${run.stderr}`);
}
function runInstalledLauncher(run, env, args) {
const launcher = path.join(run.isolated.paths.home, ".plan2code", "bin", "plan2code.js");
const result = spawnSync(process.execPath, [launcher, ...args], { env, encoding: "utf8", input: "", timeout: 240000, windowsHide: true });
return { result, stdout: result.stdout || "", stderr: result.stderr || "" };
}
function assertLauncherSucceeded(run) {
assert.equal(run.result.error, undefined, run.result.error?.message || "");
assert.equal(run.result.status, 0, `stdout:\n${run.stdout}\nstderr:\n${run.stderr}`);
}
function readJsonLines(file) {
try {
return fs
.readFileSync(file, "utf8")
.split(/\r?\n/)
.filter(Boolean)
.map((line) => JSON.parse(line));
} catch {
return [];
}
}
function canonicalManifest(run) {
return installE2EJson(run, ".agents", "skills", ".plan2code-canonical.json");
}
function consoleMarker(run, version) {
return readJson(path.join(run.isolated.paths.consoleHome, version, ".complete"));
}
test("fixture: local refs carry installable full-source releases and slim synthetic payloads", (t) => {
const fx = fixture();
const full = path.join(fx.root, "check-full");
const slim = path.join(fx.root, "check-slim");
fx.checkout("v1.0.0", full);
fx.checkout("install-v3.1.2", slim);
t.after(() => {
fs.rmSync(full, { recursive: true, force: true, maxRetries: 5, retryDelay: 100 });
fs.rmSync(slim, { recursive: true, force: true, maxRetries: 5, retryDelay: 100 });
});
for (const required of [
"install.js",
"package.json",
"version.json",
"src/launcher/plan2code.js",
"src/web-console/console.mjs",
"src/web-console/update-check.mjs",
"src/installer/timing.js",
"src/installer/skills-cli.js",
"src/installer/canonical-skills.js",
"src/installer/console-stage.js",
]) {
assert.ok(fs.existsSync(path.join(slim, ...required.split("/"))), required);
}
assert.ok(fs.existsSync(path.join(full, "docs", "promo.md")), "the plain heavy tag carries documentation");
assert.ok(!fs.existsSync(path.join(slim, "docs", "promo.md")), "the synthetic payload drops documentation");
assert.ok(fs.existsSync(path.join(slim, ".plan2code-install-ref.json")), "the synthetic payload is bound by its manifest");
assert.ok(!fs.existsSync(path.join(full, ".plan2code-install-ref.json")), "the plain source tag is not mistaken for a synthetic payload");
assert.ok(fs.existsSync(path.join(slim, "src", "launcher", "plan2code.ico")), "binary runtime files survive the fixture byte-for-byte path");
});
// Launcher-style fetch: the launcher sets PLAN2CODE_INSTALL_REF and PLAN2CODE_RELEASE_REF, a bare
// README `npx ...#release` run sets neither. The old-launcher redirect is covered in
// scripts/test-install-update.mjs ("release redirect: ..."), against a stand-in npx.
test("e2e: a launcher-style first install through #release installs the newest slim payload completely", async (t) => {
const fx = fixture();
const root = makeInstallE2ERunRoot();
const run = runFixtureInstaller(fx, { ref: "release", releaseRef: "v3.1.2", root });
t.after(run.cleanup);
assertInstallerSucceeded(run);
assert.match(run.stdout, /Updating to 3\.1\.2/);
assert.match(run.stdout, /Installing desktop app…/, "compact unattended progress names the long app phase before it runs");
assert.match(run.stdout, /Plan2Code is up to date \(v3\.1\.2\)/);
assert.ok(!fs.existsSync(path.join(run.checkout, "docs")), "the fetched release payload is slim");
assert.ok(fs.existsSync(path.join(run.checkout, ".plan2code-install-ref.json")), "the fetched payload is synthetic");
const record = installE2EJson(run, ".plan2code", "install.json");
assert.equal(record.version, "3.1.2");
assert.equal(record.scope, "global");
assert.deepEqual(record.pieces, ["launcher", "skills"]);
assert.equal(installE2EJson(run, ".plan2code", "bin", "version.json").version, "3.1.2");
assert.ok(!installE2EExists(run, ".plan2code", "app", process.platform === "win32" ? "plan2code-app.exe" : "plan2code-app"), "the deliberately unreachable app asset did not stop the required pieces");
const canonical = canonicalManifest(run);
assert.equal(canonical.version, "3.1.2");
assert.equal(canonical.sourceRef, "v3.1.2");
assert.equal(canonical.complete, true);
assert.equal(canonical.journal, null);
assert.ok(Object.keys(canonical.skills).length >= 20, "every workflow skill is in the canonical manifest");
assert.equal(consoleMarker(run, "3.1.2").version, "3.1.2");
assert.ok(fs.existsSync(path.join(run.isolated.paths.consoleHome, "3.1.2", ".plan2code-console-manifest.json")), "the console manifest is present");
assert.equal(fs.readFileSync(path.join(run.isolated.paths.consoleHome, "console-dir"), "utf8").trim(), path.join(run.isolated.paths.consoleHome, "3.1.2"), "console-dir names the complete version folder");
});
test("e2e: exact updates roll back through install tags and fall back to a plain heavy tag", async (t) => {
const fx = fixture();
const root = makeInstallE2ERunRoot();
t.after(() => fs.rmSync(root, { recursive: true, force: true, maxRetries: 5, retryDelay: 100 }));
const snapshot = parseLsRemoteSnapshot(fx.lsRemote());
assert.deepEqual(chooseReleaseRef(snapshot), { releaseRef: "v3.1.2", fetchRef: "install-v3.1.2", synthetic: true, version: "3.1.2" });
assert.deepEqual(installRefForRelease("v2.0.0", snapshot), { releaseRef: "v2.0.0", fetchRef: "install-v2.0.0", synthetic: true });
assert.deepEqual(installRefForRelease("v1.0.0", snapshot), { releaseRef: "v1.0.0", fetchRef: "v1.0.0", synthetic: false });
const newest = runFixtureInstaller(fx, { ref: "install-v3.1.2", releaseRef: "v3.1.2", root });
assertInstallerSucceeded(newest);
assert.equal(installE2EJson(newest, ".plan2code", "install.json").version, "3.1.2");
assert.equal(canonicalManifest(newest).version, "3.1.2");
assert.ok(fs.existsSync(path.join(newest.checkout, ".plan2code-install-ref.json")));
const shim = installLocalNpxShim(root, fx);
const lsRemoteFile = writeFixtureSnapshot(fx, root);
const launcherEnv = launcherRunEnvironment(newest, { bin: shim.bin, lsRemoteFile });
const npxLogFile = path.join(root, "local-npx.jsonl");
launcherEnv.PLAN2CODE_LOCAL_NPX_LOG = npxLogFile;
const rollback = runInstalledLauncher(newest, launcherEnv, ["update", "--tag", "v2.0.0"]);
assertLauncherSucceeded(rollback);
assert.match(rollback.stdout, /Updating 3\.1\.2 -> 2\.0\.0/);
assert.equal(installE2EJson(newest, ".plan2code", "install.json").version, "2.0.0");
assert.equal(canonicalManifest(newest).version, "2.0.0");
assert.equal(consoleMarker(newest, "2.0.0").version, "2.0.0");
let npxLog = readJsonLines(npxLogFile);
assert.equal(npxLog.length, 1);
assert.equal(npxLog[0].ref, "install-v2.0.0", "the launcher mapped the logical rollback to its slim immutable tag");
assert.equal(npxLog[0].synthetic, true);
assert.equal(npxLog[0].docs, false);
assert.deepEqual(npxLog[0].rest, ["--update"]);
const plain = runInstalledLauncher(newest, launcherEnv, ["update", "--tag", "v1.0.0"]);
assertLauncherSucceeded(plain);
assert.match(plain.stdout, /Updating 2\.0\.0 -> 1\.0\.0/);
assert.equal(installE2EJson(newest, ".plan2code", "install.json").version, "1.0.0");
assert.equal(canonicalManifest(newest).version, "1.0.0");
npxLog = readJsonLines(npxLogFile);
assert.equal(npxLog.length, 2);
assert.equal(npxLog[1].ref, "v1.0.0", "the no-synthetic rollback stayed on the plain tag");
assert.equal(npxLog[1].synthetic, false);
assert.equal(npxLog[1].docs, true, "the fallback fetched the heavy full-source tree");
fs.mkdirSync(path.join(newest.isolated.paths.home, ".plan2code", "notepads"), { recursive: true });
fs.writeFileSync(path.join(newest.isolated.paths.home, ".plan2code", "notepads", "keep.txt"), "personal data stays\n");
const uninstall = runInstalledLauncher(newest, launcherEnv, ["uninstall", "--yes"]);
assertLauncherSucceeded(uninstall);
assert.match(uninstall.stdout, /Plan2Code is uninstalled/);
npxLog = readJsonLines(npxLogFile);
assert.equal(npxLog.length, 3);
assert.equal(npxLog[2].ref, "install-v3.1.2", "uninstall used the newest release's slim installer, not the installed rollback");
assert.deepEqual(npxLog[2].rest, ["--uninstall", "--yes"]);
assert.equal(installE2EJson(newest, ".plan2code", "install.json"), null, "the install record is removed");
assert.ok(!installE2EExists(newest, ".plan2code", "bin", "plan2code.js"), "the launcher copy is removed");
assert.ok(!installE2EExists(newest, ".plan2code", "app", process.platform === "win32" ? "plan2code-app.exe" : "plan2code-app"), "the desktop app file is removed");
assert.ok(!installE2EExists(newest, ".agents", "skills", "plan2code"), "the canonical skills are removed");
assert.equal(fs.readFileSync(path.join(newest.isolated.paths.home, ".plan2code", "notepads", "keep.txt"), "utf8"), "personal data stays\n");
});
test("e2e: --main and a branch --tag stay on the intentional refs the launcher was given", async (t) => {
const fx = fixture();
const root = makeInstallE2ERunRoot();
t.after(() => fs.rmSync(root, { recursive: true, force: true, maxRetries: 5, retryDelay: 100 }));
const initial = runFixtureInstaller(fx, { ref: "release", releaseRef: "v3.1.2", root });
assertInstallerSucceeded(initial);
const shim = installLocalNpxShim(root, fx);
const launcherEnv = launcherRunEnvironment(initial, { bin: shim.bin, lsRemoteFile: writeFixtureSnapshot(fx, root) });
const npxLogFile = path.join(root, "local-npx.jsonl");
launcherEnv.PLAN2CODE_LOCAL_NPX_LOG = npxLogFile;
const main = runInstalledLauncher(initial, launcherEnv, ["update", "--main", "--force"]);
assertLauncherSucceeded(main);
assert.equal(canonicalManifest(initial).sourceRef, "main", "main is stamped as main, never redirected onto a release");
let npxLog = readJsonLines(npxLogFile);
assert.equal(npxLog.length, 1);
assert.equal(npxLog[0].ref, "main");
assert.equal(npxLog[0].synthetic, false);
assert.equal(npxLog[0].docs, true, "main intentionally fetches the full-source tree");
assert.deepEqual(npxLog[0].rest, ["--update", "--force"]);
const branch = runInstalledLauncher(initial, launcherEnv, ["update", "--tag", "feature/demo", "--force"]);
assertLauncherSucceeded(branch);
assert.equal(canonicalManifest(initial).sourceRef, "feature/demo", "a branch tag is stamped as that branch");
npxLog = readJsonLines(npxLogFile);
assert.equal(npxLog.length, 2);
assert.equal(npxLog[1].ref, "feature/demo");
assert.equal(npxLog[1].synthetic, false);
assert.equal(npxLog[1].docs, true);
assert.deepEqual(npxLog[1].rest, ["--update", "--force"]);
});
test("e2e: canonical link refresh and benchmark events carry the selected ref without secrets", async (t) => {
const fx = fixture();
const root = makeInstallE2ERunRoot();
// A sentinel the installer can read (install.js takes GH_TOKEN for release assets) must never
// reach stdout, stderr or the timing JSONL.
const sentinel = "p2c-e2e-sentinel-6f1d0c9a";
const run = runFixtureInstaller(fx, { ref: "install-v2.0.0", releaseRef: "v2.0.0", root, planted: { GH_TOKEN: sentinel } });
assert.equal(run.env.GH_TOKEN, sentinel, "the sentinel reached the installer's environment");
t.after(run.cleanup);
assertInstallerSucceeded(run);
const cli = readSkillsCliLog(run.cliLog);
assert.equal(cli.length, 1, "one canonical-source link refresh, not one CLI add per skill");
assert.equal(cli[0].argv[0], "add");
assert.equal(cli[0].argv[1], path.join(run.isolated.paths.home, ".agents", "skills"));
assert.ok(cli[0].argv.includes("-g"));
assert.ok(cli[0].argv.includes("plan2code-3-implement-review"));
const events = timingEvents(run);
const names = events.map((event) => event.event);
for (const required of ["installer-start", "skills-built", "console-committed", "canonical-committed", "links-refreshed", "installer-end"]) {
assert.ok(names.includes(required), `missing ${required}: ${names.join(",")}`);
}
assert.equal(events.find((event) => event.event === "installer-start").ref, "install-v2.0.0");
assert.equal(canonicalManifest(run).sourceRef, "v2.0.0", "the logical release ref is stamped separately from the fetch ref");
const artifact = fs.readFileSync(run.isolated.paths.benchJson, "utf8") + run.stdout + run.stderr;
assert.ok(!artifact.includes(sentinel), "a token the installer can read is never copied into its output or timing events");
});