mirror of
https://github.com/paperclipai/paperclip.git
synced 2026-12-05 23:06:17 +01:00
## Thinking Path > - Paperclip is the open source app people use to manage AI agents for work > - The managed CLI lets operators follow a release channel during updates > - Paperclip publishes stable, beta, nightly, and canary channels > - The update command only let users select stable or canary > - A default update also changed recorded beta and nightly installs to stable > - This pull request adds all four channels to the update command and update notice > - The benefit is that each managed install stays on the channel that the operator selected ## Linked Issues or Issue Description **What happened?** `paperclipai update` only accepted `--latest` and `--canary`. It did not accept `--beta` or `--nightly`. A default update mapped every recorded channel except canary to `latest`. The update notice used the same two-channel rule. **Expected behavior** The command must accept all four published channel selectors. A default update must use the moving channel in the install manifest. The update notice must check the same channel. **Steps to reproduce** 1. Record `beta` or `nightly` as the managed install channel. 2. Run `paperclipai update` without a channel flag. 3. Observe that the command resolves `paperclipai@latest` instead of the recorded channel. **Paperclip version or commit** Commit `e1ce7a553c280ef3d2b9ed99e0149afc23b14275`. **Deployment mode** This bug is not specific to a deployment mode. **Installation method** Managed CLI install. **Additional context** The managed update workflow was added in #10045. I found no duplicate issue or pull request for this channel gap. ## What Changed - Added `--beta` and `--nightly` to `paperclipai update` and `paperclipai upgrade`. - Expanded the install manifest channel type and kept the recorded moving channel during a default update. - Made update notices use the recorded channel and made the notice cache channel-aware. - Added regression tests and updated the channel, CLI, and installation documents. ## Verification - `pnpm exec vitest run cli/src/__tests__/update-command.test.ts cli/src/__tests__/update-notice.test.ts` — 2 files and 18 tests passed. - `pnpm --filter paperclipai typecheck` passed. - `pnpm --filter paperclipai build` passed. - `pnpm paperclipai update --help` showed `--latest`, `--beta`, `--nightly`, and `--canary`. ## Risks - Low risk. The change adds accepted manifest values and command flags. Existing latest, canary, and pinned installs keep their current behavior. - Old notice cache files have no channel field. They remain valid for the latest channel and refresh for other channels. > For core feature work, check [`ROADMAP.md`](ROADMAP.md) first and discuss it in `#dev` before opening the PR. Feature PRs that overlap with planned core work may need to be redirected — check the roadmap first. See `CONTRIBUTING.md`. ## Model Used - OpenAI Codex, GPT-5 family. The runtime does not expose the exact deployment ID or context window. The agent used reasoning, repository tools, shell execution, and test execution. ## Checklist - [x] I have included a thinking path that traces from project context to this change - [x] I have specified the model used (with version and capability details) - [x] I have checked ROADMAP.md and confirmed this PR does not duplicate planned core work - [x] I have searched GitHub for duplicate or related PRs and linked them above - [x] I have either (a) linked existing issues with `Fixes: #` / `Closes #` / `Refs #` OR (b) described the issue in-PR following the relevant issue template - [x] I have not referenced internal/instance-local Paperclip issues or links (only public GitHub `#NNN` / `github.com/paperclipai/paperclip` URLs) - [x] My branch name describes the change (e.g. `docs/...`, `fix/...`) and contains no internal Paperclip ticket id or instance-derived details - [x] I have run tests locally and they pass - [x] I have added or updated tests where applicable - [x] I have updated relevant documentation to reflect my changes - [x] I have considered and documented any risks above - [ ] All Paperclip CI gates are green - [ ] Greptile is 5/5 with no open P2s, recommendations, or follow-ups - [x] I will address all Greptile and reviewer comments before requesting merge Co-authored-by: Dev Agent <dev@paperclip.ing> Co-authored-by: Paperclip <noreply@paperclip.ing> Co-authored-by: commitperclip[bot] <284812584+commitperclip[bot]@users.noreply.github.com>
304 lines
20 KiB
TypeScript
304 lines
20 KiB
TypeScript
import fs from "node:fs";
|
|
import os from "node:os";
|
|
import path from "node:path";
|
|
import { afterEach, beforeEach, describe, expect, it, vi } from "vitest";
|
|
import { writeManagedShim, flipCurrentAtomic, initializeInstallStore, payloadPathFor, readInstallManifest, resolveInstallStorePaths, writeInstallManifestAtomic, type InstallManifest, type InstallRecord } from "../install-store.js";
|
|
import type { CommandRunner } from "../commands/install.js";
|
|
import { compareVersions, detectInstallMode, resolveUpdateRequest, rollbackManagedInstall, updateCommand } from "../commands/update.js";
|
|
|
|
let root: string;
|
|
let previousHome: string | undefined;
|
|
let previousPaperclipHome: string | undefined;
|
|
|
|
function record(payloadPath: string, version: string, channel: "latest" | "beta" | "nightly" | "canary" | "pinned" = "latest"): InstallRecord {
|
|
return { source: "npm", version, channel, payloadPath, installedAt: `2026-07-22T00:00:0${version}.000Z` };
|
|
}
|
|
function createPayload(payloadPath: string, version: string): string {
|
|
const entrypoint = path.join(payloadPath, "node_modules", "paperclipai", "dist", "index.js");
|
|
fs.mkdirSync(path.dirname(entrypoint), { recursive: true });
|
|
fs.writeFileSync(entrypoint, version);
|
|
return entrypoint;
|
|
}
|
|
beforeEach(() => {
|
|
root = fs.mkdtempSync(path.join(os.tmpdir(), "paperclip-update-"));
|
|
previousHome = process.env.HOME;
|
|
previousPaperclipHome = process.env.PAPERCLIP_HOME;
|
|
process.env.HOME = path.join(root, "home");
|
|
process.env.PAPERCLIP_HOME = path.join(root, "paperclip");
|
|
});
|
|
afterEach(() => {
|
|
vi.restoreAllMocks();
|
|
vi.unstubAllEnvs();
|
|
if (previousHome === undefined) delete process.env.HOME; else process.env.HOME = previousHome;
|
|
if (previousPaperclipHome === undefined) delete process.env.PAPERCLIP_HOME; else process.env.PAPERCLIP_HOME = previousPaperclipHome;
|
|
fs.rmSync(root, { recursive: true, force: true });
|
|
process.exitCode = undefined;
|
|
});
|
|
|
|
describe("update command", () => {
|
|
it.each(["npm", "git", "global-npm"] as const)("rejects %s updates on unsupported Node before any update work", async (source) => {
|
|
const paths = resolveInstallStorePaths(); initializeInstallStore(paths);
|
|
const payload = payloadPathFor(paths, "npm", "1.0.0");
|
|
const entrypoint = createPayload(payload, "1.0.0");
|
|
flipCurrentAtomic(payload, paths);
|
|
const manifest: InstallManifest = { schemaVersion: 1, ...record(payload, "1.0.0"), source: source === "git" ? "git" : "npm", previous: [] };
|
|
writeInstallManifestAtomic(manifest, paths);
|
|
const runCommand = vi.fn<CommandRunner>();
|
|
const backup = vi.fn();
|
|
const restartActiveService = vi.fn();
|
|
const nodeVersion = Object.getOwnPropertyDescriptor(process.versions, "node")!;
|
|
Object.defineProperty(process.versions, "node", { ...nodeVersion, value: "22.22.2" });
|
|
try {
|
|
await expect(updateCommand({ yes: true }, {
|
|
paths,
|
|
executablePath: source === "global-npm" ? path.join(root, "lib", "node_modules", "paperclipai", "dist", "index.js") : entrypoint,
|
|
runCommand, backup, restartActiveService,
|
|
})).rejects.toThrow("npx paperclipai@latest install --yes");
|
|
expect(runCommand).not.toHaveBeenCalled();
|
|
expect(backup).not.toHaveBeenCalled();
|
|
expect(restartActiveService).not.toHaveBeenCalled();
|
|
expect(readInstallManifest(paths)).toEqual(manifest);
|
|
expect(fs.realpathSync(paths.currentPath)).toBe(fs.realpathSync(payload));
|
|
} finally {
|
|
Object.defineProperty(process.versions, "node", nodeVersion);
|
|
}
|
|
});
|
|
|
|
it("keeps update checks, dry runs, and rollback available on unsupported Node", async () => {
|
|
const paths = resolveInstallStorePaths(); initializeInstallStore(paths);
|
|
const oldPayload = payloadPathFor(paths, "npm", "1.0.0"); createPayload(oldPayload, "1.0.0");
|
|
const payload = payloadPathFor(paths, "npm", "2.0.0"); const executablePath = createPayload(payload, "2.0.0");
|
|
flipCurrentAtomic(payload, paths);
|
|
writeInstallManifestAtomic({ schemaVersion: 1, ...record(payload, "2.0.0"), previous: [record(oldPayload, "1.0.0")] }, paths);
|
|
const runCommand = vi.fn(async () => ({ stdout: '\"3.0.0\"\n', stderr: "" }));
|
|
const restartActiveService = vi.fn(async () => false);
|
|
const nodeVersion = Object.getOwnPropertyDescriptor(process.versions, "node")!;
|
|
Object.defineProperty(process.versions, "node", { ...nodeVersion, value: "22.22.2" });
|
|
try {
|
|
await updateCommand({ check: true }, { paths, executablePath, runCommand });
|
|
await updateCommand({ dryRun: true }, { paths, executablePath, runCommand });
|
|
expect(readInstallManifest(paths)?.version).toBe("2.0.0");
|
|
await updateCommand({ rollback: true }, { paths, executablePath, restartActiveService });
|
|
expect(readInstallManifest(paths)?.version).toBe("1.0.0");
|
|
expect(restartActiveService).toHaveBeenCalledWith("1.0.0");
|
|
} finally {
|
|
Object.defineProperty(process.versions, "node", nodeVersion);
|
|
}
|
|
});
|
|
|
|
it("orders SemVer prerelease identifiers numerically", () => {
|
|
expect(compareVersions("1.0.0-canary.10", "1.0.0-canary.2")).toBeGreaterThan(0);
|
|
expect(compareVersions("1.0.0-1", "1.0.0-alpha")).toBeLessThan(0);
|
|
expect(compareVersions("1.0.0-alpha", "1.0.0-alpha.1")).toBeLessThan(0);
|
|
});
|
|
|
|
it("detects managed, global npm, npx, and source modes", () => {
|
|
const paths = resolveInstallStorePaths(); initializeInstallStore(paths);
|
|
const payload = payloadPathFor(paths, "npm", "1.0.0"); const entrypoint = createPayload(payload, "1.0.0");
|
|
flipCurrentAtomic(payload, paths);
|
|
writeInstallManifestAtomic({ schemaVersion: 1, ...record(payload, "1.0.0"), previous: [] }, paths);
|
|
expect(detectInstallMode(entrypoint, paths)).toBe("managed");
|
|
expect(detectInstallMode(path.join(root, "lib", "node_modules", "paperclipai", "dist", "index.js"), paths)).toBe("global-npm");
|
|
expect(detectInstallMode(path.join(root, ".npm", "_npx", "abc", "node_modules", "paperclipai", "dist", "index.js"), paths)).toBe("npx");
|
|
const source = path.join(root, "source"); fs.mkdirSync(path.join(source, ".git"), { recursive: true });
|
|
expect(detectInstallMode(path.join(source, "cli", "src", "index.ts"), paths)).toBe("source");
|
|
});
|
|
|
|
it("resolves every channel and preserves the recorded channel by default", () => {
|
|
const manifest = { channel: "pinned", version: "1.2.3" } as InstallManifest;
|
|
expect(resolveUpdateRequest(manifest, {})).toEqual({ spec: "1.2.3", channel: "pinned", explicit: false });
|
|
expect(resolveUpdateRequest(manifest, { latest: true })).toEqual({ spec: "latest", channel: "latest", explicit: true });
|
|
expect(resolveUpdateRequest(manifest, { beta: true })).toEqual({ spec: "beta", channel: "beta", explicit: true });
|
|
expect(resolveUpdateRequest(manifest, { nightly: true })).toEqual({ spec: "nightly", channel: "nightly", explicit: true });
|
|
expect(resolveUpdateRequest(manifest, { canary: true })).toEqual({ spec: "canary", channel: "canary", explicit: true });
|
|
expect(resolveUpdateRequest({ ...manifest, channel: "beta" }, {})).toEqual({ spec: "beta", channel: "beta", explicit: false });
|
|
expect(resolveUpdateRequest({ ...manifest, channel: "nightly" }, {})).toEqual({ spec: "nightly", channel: "nightly", explicit: false });
|
|
expect(() => resolveUpdateRequest(manifest, { latest: true, beta: true })).toThrow("only one");
|
|
});
|
|
|
|
it("re-resolves a moving git branch and activates the new SHA payload", async () => {
|
|
const paths = resolveInstallStorePaths(); initializeInstallStore(paths);
|
|
const oldSha = "1".repeat(40); const newSha = "2".repeat(40);
|
|
const oldPayload = payloadPathFor(paths, "git", oldSha.slice(0, 12));
|
|
const executable = createPayload(oldPayload, "0.3.1");
|
|
fs.writeFileSync(path.join(oldPayload, "node_modules", "paperclipai", "package.json"), JSON.stringify({ version: "0.3.1" }));
|
|
const newPayload = payloadPathFor(paths, "git", newSha.slice(0, 12));
|
|
createPayload(newPayload, "0.3.1");
|
|
fs.writeFileSync(path.join(newPayload, "node_modules", "paperclipai", "package.json"), JSON.stringify({ version: "0.3.1" }));
|
|
flipCurrentAtomic(oldPayload, paths);
|
|
writeInstallManifestAtomic({ schemaVersion: 1, source: "git", version: "0.3.1", channel: "pinned", repo: "paperclipai/paperclip", ref: "master", sha: oldSha, payloadPath: oldPayload, installedAt: "2026-07-22T00:00:00.000Z", previous: [] }, paths);
|
|
writeManagedShim(paths);
|
|
// Simulate a launcher generated before child-runtime PATH pinning existed.
|
|
fs.writeFileSync(paths.shimPath, fs.readFileSync(paths.shimPath, "utf8").replace(/^export PATH=.*\n/m, ""));
|
|
const backup = vi.fn(async () => undefined);
|
|
const confirm = vi.fn(async () => true);
|
|
const restartActiveService = vi.fn(async () => true);
|
|
const runCommand = vi.fn(async (file: string) => file === "curl" ? { stdout: JSON.stringify({ sha: newSha }), stderr: "" } : { stdout: "0.3.1\n", stderr: "" });
|
|
await updateCommand({}, { paths, executablePath: executable, runCommand, backup, confirm, restartActiveService, hasInstanceData: () => true, now: () => new Date("2026-07-22T12:00:00Z") });
|
|
expect(confirm).toHaveBeenCalledWith(expect.stringContaining(`commit ${newSha.slice(0, 12)}`));
|
|
expect(fs.readFileSync(paths.shimPath, "utf8")).toContain(`export PATH='${path.dirname(process.execPath)}'`);
|
|
expect(backup).toHaveBeenCalledOnce();
|
|
expect(restartActiveService).toHaveBeenCalledWith("0.3.1");
|
|
expect(readInstallManifest(paths)?.sha).toBe(newSha);
|
|
expect(fs.realpathSync(paths.currentPath)).toBe(fs.realpathSync(newPayload));
|
|
});
|
|
|
|
it("reports SHA git installs as pinned without resolving again", async () => {
|
|
const paths = resolveInstallStorePaths(); initializeInstallStore(paths);
|
|
const sha = "3".repeat(40); const payload = payloadPathFor(paths, "git", sha.slice(0, 12)); const executable = createPayload(payload, "0.3.1");
|
|
flipCurrentAtomic(payload, paths);
|
|
writeInstallManifestAtomic({ schemaVersion: 1, source: "git", version: "0.3.1", channel: "pinned", repo: "paperclipai/paperclip", ref: sha.slice(0, 12), sha, payloadPath: payload, installedAt: "2026-07-22T00:00:00.000Z", previous: [] }, paths);
|
|
const runCommand = vi.fn(async () => ({ stdout: "", stderr: "" }));
|
|
await updateCommand({}, { paths, executablePath: executable, runCommand });
|
|
expect(runCommand).not.toHaveBeenCalled();
|
|
});
|
|
|
|
it("requires explicit confirmation before downgrading", async () => {
|
|
const paths = resolveInstallStorePaths(); initializeInstallStore(paths);
|
|
const payload = payloadPathFor(paths, "npm", "2.0.0"); const entrypoint = createPayload(payload, "2.0.0"); flipCurrentAtomic(payload, paths);
|
|
writeInstallManifestAtomic({ schemaVersion: 1, ...record(payload, "2.0.0"), previous: [] }, paths);
|
|
const runCommand = vi.fn(async () => ({ stdout: '"1.0.0"\n', stderr: "" }));
|
|
await expect(updateCommand({ version: "1.0.0", dryRun: true }, { paths, executablePath: entrypoint, runCommand, confirm: async () => false })).rejects.toThrow("Downgrade cancelled");
|
|
});
|
|
|
|
it("requires explicit confirmation before a global npm downgrade", async () => {
|
|
const paths = resolveInstallStorePaths();
|
|
const executable = path.join(root, "lib", "node_modules", "paperclipai", "dist", "index.js");
|
|
const runCommand = vi.fn(async () => ({ stdout: '"0.2.0"\n', stderr: "" }));
|
|
await expect(updateCommand({ version: "0.2.0" }, { paths, executablePath: executable, runCommand, confirm: async () => false })).rejects.toThrow("Downgrade cancelled");
|
|
expect(runCommand).toHaveBeenCalledTimes(1);
|
|
});
|
|
|
|
it("isolates global npm updates from hostile registry configuration", async () => {
|
|
const paths = resolveInstallStorePaths();
|
|
const executable = path.join(root, "lib", "node_modules", "paperclipai", "dist", "index.js");
|
|
vi.stubEnv("NPM_CONFIG_REGISTRY", "http://attacker-registry.invalid");
|
|
fs.mkdirSync(process.env.HOME!, { recursive: true });
|
|
fs.writeFileSync(path.join(process.env.HOME!, ".npmrc"), "registry=http://attacker-registry.invalid\n");
|
|
const runCommand = vi.fn<CommandRunner>(async (_file, args, commandOptions) => {
|
|
if (args[0] === "view") return { stdout: '"2.0.0"\n', stderr: "" };
|
|
expect(args).toContain("--registry=https://registry.npmjs.org");
|
|
expect(args).toContain("--@paperclipai:registry=https://registry.npmjs.org");
|
|
expect(commandOptions?.env?.NPM_CONFIG_REGISTRY).toBe("https://registry.npmjs.org");
|
|
expect(commandOptions?.env?.npm_config_registry).toBe("https://registry.npmjs.org");
|
|
expect(commandOptions?.env?.NPM_CONFIG_USERCONFIG).toBe(commandOptions?.env?.npm_config_userconfig);
|
|
expect(fs.readFileSync(commandOptions!.env!.NPM_CONFIG_USERCONFIG!, "utf8")).toContain("registry=https://registry.npmjs.org");
|
|
return { stdout: "", stderr: "" };
|
|
});
|
|
await updateCommand({}, { paths, executablePath: executable, runCommand });
|
|
expect(runCommand).toHaveBeenCalledTimes(2);
|
|
});
|
|
|
|
it("backs up, installs side-by-side, flips, and rolls back instantly", async () => {
|
|
const paths = resolveInstallStorePaths(); initializeInstallStore(paths);
|
|
const oldPayload = payloadPathFor(paths, "npm", "1.0.0"); const executable = createPayload(oldPayload, "1.0.0"); flipCurrentAtomic(oldPayload, paths);
|
|
writeInstallManifestAtomic({ schemaVersion: 1, ...record(oldPayload, "1.0.0"), previous: [] }, paths);
|
|
writeManagedShim(paths);
|
|
// Simulate a launcher generated before child-runtime PATH pinning existed.
|
|
fs.writeFileSync(paths.shimPath, fs.readFileSync(paths.shimPath, "utf8").replace(/^export PATH=.*\n/m, ""));
|
|
const backup = vi.fn(async () => undefined);
|
|
const restartActiveService = vi.fn(async () => true);
|
|
const runCommand = vi.fn(async (file: string, args: string[]) => {
|
|
if (args[0] === "view") return { stdout: '"2.0.0"\n', stderr: "" };
|
|
if (file === "npm" && args[0] === "install") { const prefix = args[args.indexOf("--prefix") + 1]; createPayload(prefix, "2.0.0"); return { stdout: "", stderr: "" }; }
|
|
return { stdout: "2.0.0\n", stderr: "" };
|
|
});
|
|
await updateCommand({}, { paths, executablePath: executable, runCommand, backup, restartActiveService, hasInstanceData: () => true, now: () => new Date("2026-07-22T12:00:00Z") });
|
|
expect(fs.readFileSync(paths.shimPath, "utf8")).toContain(`export PATH='${path.dirname(process.execPath)}'`);
|
|
expect(backup).toHaveBeenCalledOnce();
|
|
expect(restartActiveService).toHaveBeenCalledWith("2.0.0");
|
|
expect(readInstallManifest(paths)?.version).toBe("2.0.0");
|
|
expect(fs.realpathSync(paths.currentPath)).toBe(fs.realpathSync(payloadPathFor(paths, "npm", "2.0.0")));
|
|
const rolledBack = rollbackManagedInstall(paths);
|
|
expect(rolledBack.version).toBe("1.0.0");
|
|
expect(fs.realpathSync(paths.currentPath)).toBe(fs.realpathSync(oldPayload));
|
|
});
|
|
|
|
it("explains how to recover when the pre-update database is unreachable", async () => {
|
|
const paths = resolveInstallStorePaths(); initializeInstallStore(paths);
|
|
const oldPayload = payloadPathFor(paths, "npm", "1.0.0"); const executable = createPayload(oldPayload, "1.0.0"); flipCurrentAtomic(oldPayload, paths);
|
|
writeInstallManifestAtomic({ schemaVersion: 1, ...record(oldPayload, "1.0.0"), previous: [] }, paths);
|
|
const backupError = Object.assign(new Error("connect ECONNREFUSED 127.0.0.1:54329"), { code: "ECONNREFUSED" });
|
|
const backup = vi.fn(async () => { throw backupError; });
|
|
const runCommand = vi.fn(async () => ({ stdout: '"2.0.0"\n', stderr: "" }));
|
|
|
|
await expect(updateCommand({}, { paths, executablePath: executable, runCommand, backup, hasInstanceData: () => true })).rejects.toThrow(
|
|
"Start the service with `paperclipai service start` and retry, or skip the backup with `paperclipai update --no-backup`.",
|
|
);
|
|
expect(backup).toHaveBeenCalledOnce();
|
|
expect(readInstallManifest(paths)?.version).toBe("1.0.0");
|
|
});
|
|
|
|
it("skips the pre-update backup when there is no onboarded instance data", async () => {
|
|
const paths = resolveInstallStorePaths(); initializeInstallStore(paths);
|
|
const oldPayload = payloadPathFor(paths, "npm", "1.0.0"); const executable = createPayload(oldPayload, "1.0.0"); flipCurrentAtomic(oldPayload, paths);
|
|
writeInstallManifestAtomic({ schemaVersion: 1, ...record(oldPayload, "1.0.0"), previous: [] }, paths);
|
|
const backup = vi.fn(async () => undefined);
|
|
const log = vi.spyOn(console, "log").mockImplementation(() => undefined);
|
|
const runCommand = vi.fn(async (file: string, args: string[]) => {
|
|
if (args[0] === "view") return { stdout: '"2.0.0"\n', stderr: "" };
|
|
if (file === "npm" && args[0] === "install") { createPayload(args[args.indexOf("--prefix") + 1], "2.0.0"); return { stdout: "", stderr: "" }; }
|
|
return { stdout: "2.0.0\n", stderr: "" };
|
|
});
|
|
|
|
await updateCommand({}, { paths, executablePath: executable, runCommand, backup, restartActiveService: async () => false, hasInstanceData: () => false });
|
|
|
|
expect(backup).not.toHaveBeenCalled();
|
|
expect(log).toHaveBeenCalledWith(expect.stringContaining("has not been onboarded and has no data to back up"));
|
|
expect(readInstallManifest(paths)?.version).toBe("2.0.0");
|
|
});
|
|
|
|
it("does not inherit a managed pin for global npm updates", async () => {
|
|
const paths = resolveInstallStorePaths(); initializeInstallStore(paths);
|
|
const managedPayload = payloadPathFor(paths, "npm", "1.2.3"); createPayload(managedPayload, "1.2.3");
|
|
writeInstallManifestAtomic({ schemaVersion: 1, ...record(managedPayload, "1.2.3"), channel: "pinned", previous: [] }, paths);
|
|
const executable = path.join(root, "lib", "node_modules", "paperclipai", "dist", "index.js");
|
|
const runCommand = vi.fn(async (_file: string, args: string[]) => args[0] === "view" ? { stdout: '"2.0.0"\n', stderr: "" } : { stdout: "", stderr: "" });
|
|
await updateCommand({ dryRun: true }, { paths, executablePath: executable, runCommand });
|
|
expect(runCommand).toHaveBeenCalledWith("npm", expect.arrayContaining(["view", "paperclipai@latest"]), expect.anything());
|
|
});
|
|
|
|
it("rolls back the active payload when restart validation fails", async () => {
|
|
const paths = resolveInstallStorePaths(); initializeInstallStore(paths);
|
|
const oldPayload = payloadPathFor(paths, "npm", "1.0.0"); const executable = createPayload(oldPayload, "1.0.0"); flipCurrentAtomic(oldPayload, paths);
|
|
writeInstallManifestAtomic({ schemaVersion: 1, ...record(oldPayload, "1.0.0"), previous: [] }, paths);
|
|
const runCommand = vi.fn(async (file: string, args: string[]) => {
|
|
if (args[0] === "view") return { stdout: '"2.0.0"\n', stderr: "" };
|
|
if (file === "npm" && args[0] === "install") { createPayload(args[args.indexOf("--prefix") + 1], "2.0.0"); return { stdout: "", stderr: "" }; }
|
|
return { stdout: "2.0.0\n", stderr: "" };
|
|
});
|
|
const restartActiveService = vi.fn(async (version: string) => { if (version === "2.0.0") throw new Error("health timeout"); return true; });
|
|
await expect(updateCommand({}, { paths, executablePath: executable, runCommand, backup: async () => undefined, restartActiveService })).rejects.toThrow("rolled back to 1.0.0");
|
|
expect(readInstallManifest(paths)?.version).toBe("1.0.0");
|
|
expect(fs.realpathSync(paths.currentPath)).toBe(fs.realpathSync(oldPayload));
|
|
expect(restartActiveService).toHaveBeenLastCalledWith("1.0.0");
|
|
});
|
|
|
|
it("surfaces a failure to restart the rolled-back payload", async () => {
|
|
const paths = resolveInstallStorePaths(); initializeInstallStore(paths);
|
|
const oldPayload = payloadPathFor(paths, "npm", "1.0.0"); const executable = createPayload(oldPayload, "1.0.0"); flipCurrentAtomic(oldPayload, paths);
|
|
writeInstallManifestAtomic({ schemaVersion: 1, ...record(oldPayload, "1.0.0"), previous: [] }, paths);
|
|
const runCommand = vi.fn(async (file: string, args: string[]) => {
|
|
if (args[0] === "view") return { stdout: '"2.0.0"\n', stderr: "" };
|
|
if (file === "npm" && args[0] === "install") { createPayload(args[args.indexOf("--prefix") + 1], "2.0.0"); return { stdout: "", stderr: "" }; }
|
|
return { stdout: "2.0.0\n", stderr: "" };
|
|
});
|
|
const restartActiveService = vi.fn(async (version: string) => {
|
|
throw new Error(version === "2.0.0" ? "health timeout" : "rollback restart failed");
|
|
});
|
|
|
|
await expect(updateCommand({}, {
|
|
paths,
|
|
executablePath: executable,
|
|
runCommand,
|
|
backup: async () => undefined,
|
|
restartActiveService,
|
|
})).rejects.toThrow("rolled-back service also failed to restart");
|
|
expect(readInstallManifest(paths)?.version).toBe("1.0.0");
|
|
expect(fs.realpathSync(paths.currentPath)).toBe(fs.realpathSync(oldPayload));
|
|
expect(restartActiveService).toHaveBeenNthCalledWith(1, "2.0.0");
|
|
expect(restartActiveService).toHaveBeenNthCalledWith(2, "1.0.0");
|
|
});
|
|
|
|
});
|