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 } = {}): 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/); }); });