feat(cli): add pi2 update with a versioned install layout
The standalone (curl installer) channel had no upgrade path of its own: the
documented one was 're-run the installer', which the Neovim plugin cannot do
safely and which re-downloads 35 MB without version arithmetic, provenance or
rollback. ADR 0003 records the design; this is the implementation.
- install.sh now installs one immutable tree per version
(`<prefix>/versions/<v>/{bin,share,LICENSE}`) behind a single atomically
swapped `current` symlink, writes `<prefix>/install.json` as the install
receipt, and migrates a pre-0.1.7 flat layout in place. Keeping `current` plus
one previous tree gives a one-step rollback for free.
- `pi2 update` (native TypeScript, no `curl | sh` subprocess) resolves the
release with an ETag-cached GitHub lookup, picks the asset from the
build-time `PI2_BUILD_TARGET` define instead of re-probing libc, verifies
SHA256SUMS, verifies SLSA provenance with `gh attestation verify`, runs the
new binary once, then swaps `current` and rewrites the receipt. Flags:
--check/--dry-run/--version/--force/--json/--require-attestation/
--skip-attestation. Downgrades need --force; restarts stay manual.
- the receipt is the only channel detection: npm, Homebrew and the AUR are
refused with the command that owns their tree, and a flat install is told to
re-run the installer once. No heuristics on `process.execPath`.
- `pi2 install-service` renders `<prefix>/current/bin/pi2`, so an update no
longer invalidates the unit.
- `scripts/build-binary.sh` injects `PI2_BUILD_TARGET`; `tarball.ts` is a
dependency-free ustar/PAX reader with traversal and symlink rejection.
Verified against the real release: install.sh into a scratch prefix, update
--check/--dry-run, a local 0.1.7 mirror upgrade (attestation warn path), then
`pi2 update --version 0.1.6 --force` (real download, real attestation
verified), plus boot-check and the refusal paths. typecheck + 567 daemon tests
+ web typecheck + 537 web tests + smoke are green.
This commit is contained in:
1 parent
29aee26733
commit
8fb535f7f3
20 files changed
+2557
-43
No files matched your search
@@ -0,0 +1,175 @@
|
||||
import { describe, expect, it } from "vitest";
|
||||
import {
|
||||
assetName,
|
||||
assetUrl,
|
||||
channelUpgradeCommand,
|
||||
checksumsUrl,
|
||||
compareVersions,
|
||||
matchesChecksum,
|
||||
normalizeVersion,
|
||||
parseChecksums,
|
||||
parseRedirectVersion,
|
||||
parseReleaseTag,
|
||||
resolveLatestVersion,
|
||||
sha256Hex,
|
||||
type UpdateCache,
|
||||
} from "../../src/core/release.js";
|
||||
|
||||
function jsonResponse(payload: unknown, init: { status?: number; headers?: Record<string, string> } = {}): Response {
|
||||
return new Response(JSON.stringify(payload), {
|
||||
status: init.status ?? 200,
|
||||
headers: { "content-type": "application/json", ...(init.headers ?? {}) },
|
||||
});
|
||||
}
|
||||
|
||||
describe("versions", () => {
|
||||
it("normalises a v prefix and orders triplets", () => {
|
||||
expect(normalizeVersion("v0.1.7")).toBe("0.1.7");
|
||||
expect(normalizeVersion("0.1.7")).toBe("0.1.7");
|
||||
expect(compareVersions("0.1.7", "0.1.6")).toBe(1);
|
||||
expect(compareVersions("0.1.6", "0.1.7")).toBe(-1);
|
||||
expect(compareVersions("v0.1.6", "0.1.6")).toBe(0);
|
||||
expect(compareVersions("0.10.0", "0.9.9")).toBe(1);
|
||||
expect(compareVersions("nonsense", "0.0.1")).toBe(-1);
|
||||
});
|
||||
});
|
||||
|
||||
describe("asset naming", () => {
|
||||
it("matches scripts/build-binary.sh", () => {
|
||||
expect(assetName("0.1.7", "linux-x64")).toBe("pi2d-0.1.7-linux-x64.tar.gz");
|
||||
expect(assetName("v0.1.7", "linux-x64-musl")).toBe("pi2d-0.1.7-linux-x64-musl.tar.gz");
|
||||
});
|
||||
|
||||
it("prefers PI2_BASE_URL over github.com", () => {
|
||||
expect(assetUrl("0.1.7", "darwin-arm64")).toBe(
|
||||
"https://github.com/zgs225/pi2d/releases/download/v0.1.7/pi2d-0.1.7-darwin-arm64.tar.gz",
|
||||
);
|
||||
expect(assetUrl("0.1.7", "linux-x64", "http://127.0.0.1:9/fixture/")).toBe(
|
||||
"http://127.0.0.1:9/fixture/pi2d-0.1.7-linux-x64.tar.gz",
|
||||
);
|
||||
expect(checksumsUrl("0.1.7", "http://127.0.0.1:9/fixture")).toBe("http://127.0.0.1:9/fixture/SHA256SUMS");
|
||||
});
|
||||
});
|
||||
|
||||
describe("parseChecksums", () => {
|
||||
it("reads sha256sum -c output, binary markers, comments and CRLF", () => {
|
||||
const digest = "a".repeat(64);
|
||||
const text = [
|
||||
`# generated by the release workflow`,
|
||||
`${digest} pi2d-0.1.7-linux-x64.tar.gz`,
|
||||
`${digest.toUpperCase()} *pi2d-0.1.7-darwin-arm64.tar.gz`,
|
||||
`${digest} ./pi2d-0.1.7-linux-arm64.tar.gz`,
|
||||
"",
|
||||
"not a checksum line",
|
||||
].join("\r\n");
|
||||
const checksums = parseChecksums(text);
|
||||
expect(checksums.get("pi2d-0.1.7-linux-x64.tar.gz")).toBe(digest);
|
||||
expect(checksums.get("pi2d-0.1.7-darwin-arm64.tar.gz")).toBe(digest);
|
||||
expect(checksums.get("pi2d-0.1.7-linux-arm64.tar.gz")).toBe(digest);
|
||||
expect(checksums.size).toBe(3);
|
||||
});
|
||||
});
|
||||
|
||||
describe("digests", () => {
|
||||
it("verifies bytes against the published digest", () => {
|
||||
const bytes = Buffer.from("payload");
|
||||
const digest = sha256Hex(bytes);
|
||||
expect(digest).toHaveLength(64);
|
||||
expect(matchesChecksum(bytes, digest)).toBe(true);
|
||||
expect(matchesChecksum(bytes, digest.toUpperCase())).toBe(true);
|
||||
expect(matchesChecksum(Buffer.from("other"), digest)).toBe(false);
|
||||
});
|
||||
});
|
||||
|
||||
describe("release payloads", () => {
|
||||
it("parses tag_name and the releases/latest redirect", () => {
|
||||
expect(parseReleaseTag({ tag_name: "v0.1.7" })).toBe("0.1.7");
|
||||
expect(parseReleaseTag({})).toBeUndefined();
|
||||
expect(parseReleaseTag(null)).toBeUndefined();
|
||||
expect(parseRedirectVersion("https://github.com/zgs225/pi2d/releases/tag/v0.1.7")).toBe("0.1.7");
|
||||
expect(parseRedirectVersion(undefined)).toBeUndefined();
|
||||
});
|
||||
|
||||
it("maps a channel to the command that owns it", () => {
|
||||
expect(channelUpgradeCommand("brew")).toContain("brew upgrade pi2d");
|
||||
expect(channelUpgradeCommand("aur")).toContain("paru");
|
||||
expect(channelUpgradeCommand("npm")).toContain("npm i -g");
|
||||
expect(channelUpgradeCommand("unknown")).toContain("npm i -g");
|
||||
});
|
||||
});
|
||||
|
||||
describe("resolveLatestVersion", () => {
|
||||
it("uses a fresh cache without touching the network", async () => {
|
||||
const cache: UpdateCache = { version: "0.1.7", checkedAt: new Date().toISOString() };
|
||||
let calls = 0;
|
||||
const result = await resolveLatestVersion({
|
||||
cache,
|
||||
fetchImpl: (async () => {
|
||||
calls += 1;
|
||||
return jsonResponse({ tag_name: "v9.9.9" });
|
||||
}) as unknown as typeof fetch,
|
||||
});
|
||||
expect(result.version).toBe("0.1.7");
|
||||
expect(result.source).toBe("cache");
|
||||
expect(calls).toBe(0);
|
||||
});
|
||||
|
||||
it("queries the API, caching the tag and the ETag", async () => {
|
||||
const result = await resolveLatestVersion({
|
||||
fetchImpl: (async () =>
|
||||
jsonResponse({ tag_name: "v0.1.7" }, { headers: { etag: 'W/"abc"' } })) as unknown as typeof fetch,
|
||||
now: new Date("2026-10-29T00:00:00Z"),
|
||||
});
|
||||
expect(result.version).toBe("0.1.7");
|
||||
expect(result.source).toBe("api");
|
||||
expect(result.cache.etag).toBe('W/"abc"');
|
||||
expect(result.cache.checkedAt).toBe("2026-10-29T00:00:00.000Z");
|
||||
});
|
||||
|
||||
it("reuses the version on 304 and refreshes the timestamp", async () => {
|
||||
const cache: UpdateCache = {
|
||||
version: "0.1.7",
|
||||
checkedAt: new Date("2026-10-01T00:00:00Z").toISOString(),
|
||||
etag: 'W/"abc"',
|
||||
};
|
||||
let sentEtag: string | undefined;
|
||||
const result = await resolveLatestVersion({
|
||||
cache,
|
||||
fetchImpl: (async (_input: unknown, init?: RequestInit) => {
|
||||
sentEtag = new Headers(init?.headers).get("if-none-match") ?? undefined;
|
||||
return new Response(null, { status: 304 });
|
||||
}) as unknown as typeof fetch,
|
||||
now: new Date("2026-10-29T00:00:00Z"),
|
||||
});
|
||||
expect(sentEtag).toBe('W/"abc"');
|
||||
expect(result.version).toBe("0.1.7");
|
||||
expect(result.source).toBe("api");
|
||||
expect(result.cache.etag).toBe('W/"abc"');
|
||||
expect(result.cache.checkedAt).toBe("2026-10-29T00:00:00.000Z");
|
||||
});
|
||||
|
||||
it("falls back to the releases/latest redirect when the API is rate-limited", async () => {
|
||||
const result = await resolveLatestVersion({
|
||||
fetchImpl: (async (input: unknown) => {
|
||||
const url = String(input);
|
||||
if (url.includes("api.github.com")) return new Response("rate limited", { status: 403 });
|
||||
return new Response(null, {
|
||||
status: 302,
|
||||
headers: { location: "https://github.com/zgs225/pi2d/releases/tag/v0.1.9" },
|
||||
});
|
||||
}) as unknown as typeof fetch,
|
||||
});
|
||||
expect(result.version).toBe("0.1.9");
|
||||
expect(result.source).toBe("redirect");
|
||||
});
|
||||
|
||||
it("reports both failures when nothing answers", async () => {
|
||||
await expect(
|
||||
resolveLatestVersion({
|
||||
fetchImpl: (async () => {
|
||||
throw new Error("network down");
|
||||
}) as unknown as typeof fetch,
|
||||
}),
|
||||
).rejects.toThrow(/could not reach GitHub/);
|
||||
});
|
||||
});
|
||||
Reference in new issue
Block a user