返回 CodeWhale
computer-use-release.test.ts
根目录 / web / lib / computer-use-release.test.ts
1 import { afterEach, describe, expect, it, vi } from "vitest";
2 import { COMPUTER_USE_REPO, getComputerUseRelease, qualifiedComputerUseRelease } from "./computer-use-release";
3
4 const archive = "Codewhale-Computer-Use-0.3.0-macos-universal.zip";
5 const sha256 = "a".repeat(64);
6 const asset = (name: string, size: number) => ({ name, size, state: "uploaded",
7 browser_download_url: `${COMPUTER_USE_REPO}/releases/download/v0.3.0/${name}`, digest: `sha256:${sha256}` });
8 const fixture = () => ({ tag_name: "v0.3.0", draft: false, prerelease: false,
9 published_at: "2026-09-13T00:00:00Z", html_url: `${COMPUTER_USE_REPO}/releases/tag/v0.3.0`,
10 assets: [asset(archive, 80000000), asset("release.json", 500)] });
11 const receipt = () => ({ version: "0.3.0", platform: "macos", arch: "universal", archive,
12 sha256, size: 80000000, notarized: true });
13 const image = "Codewhale-Computer-Use-0.3.0-macos-universal.dmg";
14 const imageSha = "c".repeat(64);
15 const imageAsset = () => ({ ...asset(image, 81000000), digest: `sha256:${imageSha}` });
16 const imageReceipt = () => ({ ...receipt(), dmg: { archive: image, size: 81000000, sha256: imageSha, notarized: true } });
17
18 const API_LATEST = "https://api.github.com/repos/codewhale-hq/codewhale-cu-plugin/releases/latest";
19 const WEB_RECEIPT = `${COMPUTER_USE_REPO}/releases/latest/download/release.json`;
20 const OBJECT_URL = "https://objects.githubusercontent.com/github-production-release-asset/1/release.json?X-Amz-Signature=x";
21 const status = (code: number) => new Response(null, { status: code });
22 const redirect = (location: string) => new Response(null, { status: 302, headers: { location } });
23 const stub = (...responses: unknown[]) => {
24 const fetcher = vi.fn();
25 for (const r of responses) {
26 if (r instanceof Error) fetcher.mockRejectedValueOnce(r); else fetcher.mockResolvedValueOnce(r);
27 }
28 vi.stubGlobal("fetch", fetcher);
29 return fetcher;
30 };
31
32 afterEach(() => { vi.unstubAllGlobals(); vi.unstubAllEnvs(); vi.restoreAllMocks(); });
33
34 describe("Computer Use download qualification", () => {
35 it("qualifies the canonical URLs returned after the repository transfer", () => {
36 const release = fixture();
37 release.html_url = "https://github.com/codewhale-hq/codewhale-cu-plugin/releases/tag/v0.3.0";
38 release.assets[0].browser_download_url = "https://github.com/codewhale-hq/codewhale-cu-plugin/releases/download/v0.3.0/Codewhale-Computer-Use-0.3.0-macos-universal.zip";
39 release.assets[1].browser_download_url = "https://github.com/codewhale-hq/codewhale-cu-plugin/releases/download/v0.3.0/release.json";
40 expect(qualifiedComputerUseRelease(release, receipt())).toMatchObject({
41 status: "ready", verification: "github-digest",
42 url: "https://github.com/codewhale-hq/codewhale-cu-plugin/releases/tag/v0.3.0",
43 downloadUrl: release.assets[0].browser_download_url,
44 });
45 });
46 it.each(["Hmbown", "codewhale-hq-lookalike"])("refuses release metadata naming noncanonical owner %s", owner => {
47 const release = fixture();
48 const repo = `https://github.com/${owner}/codewhale-cu-plugin`;
49 release.html_url = `${repo}/releases/tag/v0.3.0`;
50 release.assets = release.assets.map(a => ({
51 ...a, browser_download_url: `${repo}/releases/download/v0.3.0/${a.name}`,
52 }));
53 expect(qualifiedComputerUseRelease(release, receipt()).status).toBe("pending");
54 });
55 it("offers the exact archive when the release, receipt and GitHub digest agree", () => {
56 expect(qualifiedComputerUseRelease(fixture(), receipt())).toMatchObject({
57 status: "ready", version: "0.3.0", sha256, downloadUrl: asset(archive, 80000000).browser_download_url,
58 verification: "github-digest",
59 });
60 });
61 it.each([
62 { notarized: false }, { version: "0.2.2" }, { size: 1 }, { sha256: "b".repeat(64) },
63 { platform: "windows" }, { arch: "arm64" }, { archive: "unqualified.zip" },
64 ])("withholds mismatched or unqualified receipts: %j", change => {
65 expect(qualifiedComputerUseRelease(fixture(), { ...receipt(), ...change }).status).toBe("pending");
66 });
67 it("refuses drafts, prereleases, missing assets and foreign download URLs", () => {
68 const foreign = fixture(); foreign.assets[0].browser_download_url = "https://example.com/app.zip";
69 const duplicate = fixture(); duplicate.assets.push(duplicate.assets[0]);
70 const unsigned = fixture(); unsigned.assets[0].digest = "";
71 const tooLarge = fixture(); tooLarge.assets[0].size = 300 * 1024 * 1024;
72 for (const release of [{ ...fixture(), draft: true }, { ...fixture(), prerelease: true },
73 { ...fixture(), tag_name: "v0.3.0-rc1" }, { ...fixture(), assets: [] }, foreign, duplicate, unsigned, tooLarge]) {
74 expect(qualifiedComputerUseRelease(release, receipt()).status).toBe("pending");
75 }
76 });
77 it("loads only the canonical release and its matching receipt", async () => {
78 const fetcher = stub(Response.json(fixture()), Response.json(receipt()));
79 expect(await getComputerUseRelease()).toMatchObject({ status: "ready", verification: "github-digest" });
80 expect(fetcher.mock.calls.map(c => c[0])).toEqual([API_LATEST, `${COMPUTER_USE_REPO}/releases/download/v0.3.0/release.json`]);
81 });
82 it("sends the server-held token to the API exactly when one is passed, and never elsewhere", async () => {
83 const fetcher = stub(status(404), status(404), status(404));
84 await getComputerUseRelease("ghp_secret");
85 await getComputerUseRelease();
86 const headers = (call: number) => fetcher.mock.calls[call][1].headers as Record<string, string>;
87 expect(headers(0).Authorization).toBe("Bearer ghp_secret");
88 expect(headers(1)).not.toHaveProperty("Authorization");
89 await getComputerUseRelease("");
90 expect(headers(2)).not.toHaveProperty("Authorization");
91 });
92 it("reports no published installer when both the API and the release endpoint say so", async () => {
93 const fetcher = stub(status(404));
94 expect((await getComputerUseRelease()).status).toBe("pending");
95 expect(fetcher).toHaveBeenCalledTimes(1);
96 });
97 it("falls back to the release web endpoint when the API refuses, and stays honest when that fails too", async () => {
98 const error = vi.spyOn(console, "error").mockImplementation(() => {});
99 stub(status(403), status(404));
100 expect((await getComputerUseRelease()).status).toBe("pending");
101 expect(error).toHaveBeenCalledWith("computer-use release check", 403);
102 stub(status(503), new Error("offline"));
103 expect((await getComputerUseRelease("ghp_secret")).status).toBe("unavailable");
104 stub(new Error("offline"), status(404));
105 expect((await getComputerUseRelease("ghp_secret")).status).toBe("pending");
106 expect(error).toHaveBeenCalledWith("computer-use release check failed", "offline");
107 stub(status(403), status(500));
108 expect((await getComputerUseRelease("ghp_secret")).status).toBe("unavailable");
109 expect(error.mock.calls.flat().join(" ")).not.toContain("ghp_");
110 });
111 it("qualifies the download from the receipt when the API is unreachable", async () => {
112 vi.spyOn(console, "error").mockImplementation(() => {});
113 const fetcher = stub(status(403), redirect(OBJECT_URL), Response.json(receipt()), redirect(OBJECT_URL));
114 expect(await getComputerUseRelease()).toEqual({
115 status: "ready", version: "0.3.0", sha256, size: 80000000, verification: "receipt",
116 url: `${COMPUTER_USE_REPO}/releases/tag/v0.3.0`,
117 downloadUrl: `${COMPUTER_USE_REPO}/releases/download/v0.3.0/${archive}`,
118 receiptUrl: `${COMPUTER_USE_REPO}/releases/download/v0.3.0/release.json`,
119 });
120 expect(fetcher.mock.calls.map(c => [c[0], c[1].method ?? "GET", c[1].redirect])).toEqual([
121 [API_LATEST, "GET", undefined], [WEB_RECEIPT, "GET", "manual"], [OBJECT_URL, "GET", "manual"],
122 [`${COMPUTER_USE_REPO}/releases/download/v0.3.0/${archive}`, "HEAD", "manual"],
123 ]);
124 expect(fetcher.mock.calls.every(c => !("Authorization" in (c[1].headers ?? {})))).toBe(true);
125 });
126 it("accepts a directly served archive and a permanent redirect for the receipt", async () => {
127 vi.spyOn(console, "error").mockImplementation(() => {});
128 stub(status(403), new Response(null, { status: 301, headers: { location: OBJECT_URL } }), Response.json(receipt()), status(200));
129 expect(await getComputerUseRelease()).toMatchObject({ status: "ready", verification: "receipt" });
130 });
131 it.each([
132 ["a disallowed host", redirect("https://example.com/release.json")],
133 ["a redirect without a location", new Response(null, { status: 302 })],
134 ["plain http", redirect("http://objects.githubusercontent.com/release.json")],
135 ])("refuses a receipt redirect onto %s", async (_label, hop) => {
136 vi.spyOn(console, "error").mockImplementation(() => {});
137 const fetcher = stub(status(403), hop, Response.json(receipt()));
138 expect((await getComputerUseRelease()).status).toBe("unavailable");
139 expect(fetcher).toHaveBeenCalledTimes(2);
140 });
141 it("withholds the fallback when the receipt is unqualified or the archive is not served", async () => {
142 vi.spyOn(console, "error").mockImplementation(() => {});
143 stub(status(403), redirect(OBJECT_URL), Response.json({ ...receipt(), notarized: false }));
144 expect((await getComputerUseRelease()).status).toBe("unavailable");
145 stub(status(403), redirect(OBJECT_URL), Response.json(receipt()), status(404));
146 expect((await getComputerUseRelease()).status).toBe("unavailable");
147 const fetcher = stub(status(403), redirect(OBJECT_URL), redirect(OBJECT_URL), redirect(OBJECT_URL), redirect(OBJECT_URL), redirect(OBJECT_URL));
148 expect((await getComputerUseRelease()).status).toBe("unavailable");
149 expect(fetcher).toHaveBeenCalledTimes(5);
150 });
151 it("bounds malformed or oversized responses and does not fetch an unqualified receipt", async () => {
152 vi.spyOn(console, "error").mockImplementation(() => {});
153 const fetcher = stub(new Response("x".repeat(128 * 1024 + 1)), new Error("offline"),
154 Response.json({ ...fixture(), draft: true }));
155 expect((await getComputerUseRelease()).status).toBe("unavailable");
156 expect((await getComputerUseRelease()).status).toBe("pending");
157 expect(fetcher).toHaveBeenCalledTimes(3);
158 });
159 it("offers the disk image only when the receipt and GitHub's digest agree on it", () => {
160 const withImage = { ...fixture(), assets: [...fixture().assets, imageAsset()] };
161 expect(qualifiedComputerUseRelease(withImage, imageReceipt())).toMatchObject({
162 status: "ready", dmg: { downloadUrl: imageAsset().browser_download_url, size: 81000000, sha256: imageSha },
163 });
164 // A release without the image, a receipt without the entry, or a mismatch all fall back to the archive alone.
165 expect(qualifiedComputerUseRelease(fixture(), imageReceipt())).not.toHaveProperty("dmg");
166 expect(qualifiedComputerUseRelease(withImage, receipt())).not.toHaveProperty("dmg");
167 const mismatched = imageReceipt(); mismatched.dmg.sha256 = "d".repeat(64);
168 expect(qualifiedComputerUseRelease(withImage, mismatched)).toMatchObject({ status: "ready" });
169 expect(qualifiedComputerUseRelease(withImage, mismatched)).not.toHaveProperty("dmg");
170 const unnotarized = imageReceipt(); unnotarized.dmg.notarized = false;
171 expect(qualifiedComputerUseRelease(withImage, unnotarized)).not.toHaveProperty("dmg");
172 });
173 it("confirms the disk image is served before offering it from the receipt fallback", async () => {
174 vi.spyOn(console, "error").mockImplementation(() => {});
175 let fetcher = stub(status(403), redirect(OBJECT_URL), Response.json(imageReceipt()), redirect(OBJECT_URL), redirect(OBJECT_URL));
176 expect(await getComputerUseRelease()).toMatchObject({
177 status: "ready", verification: "receipt",
178 dmg: { downloadUrl: `${COMPUTER_USE_REPO}/releases/download/v0.3.0/${image}`, size: 81000000, sha256: imageSha },
179 });
180 expect(fetcher.mock.calls[4][0]).toBe(`${COMPUTER_USE_REPO}/releases/download/v0.3.0/${image}`);
181 expect(fetcher.mock.calls[4][1].method).toBe("HEAD");
182 fetcher = stub(status(403), redirect(OBJECT_URL), Response.json(imageReceipt()), redirect(OBJECT_URL), status(404));
183 const withoutImage = await getComputerUseRelease();
184 expect(withoutImage).toMatchObject({ status: "ready", verification: "receipt" });
185 expect(withoutImage).not.toHaveProperty("dmg");
186 expect(fetcher).toHaveBeenCalledTimes(5);
187 });
188 it("keeps production builds offline without claiming that a release is available", async () => {
189 vi.stubEnv("NEXT_PHASE", "phase-production-build");
190 const fetcher = vi.fn(); vi.stubGlobal("fetch", fetcher);
191 expect((await getComputerUseRelease()).status).toBe("unavailable");
192 expect(fetcher).not.toHaveBeenCalled();
193 });
194 });
195
195 lines TYPESCRIPT