import { afterEach, describe, expect, it, vi } from "vitest"; import { MAC_DOWNLOAD_LATEST_PATH } from "../../shared/macDownloads.ts"; import type { Env } from "../env.ts"; import worker from "../index.ts"; /** * `/mac/download/…` — the zip itself, STREAMED FROM THE SERVING MAC, which * reads it off the release record (`crates/cotton-server/src/routes/mac_feed.rs`). * * The serving Mac is mocked at `fetch` (the boundary the Worker talks to). Two * answers are load-bearing and both are asserted: a published build is **200 * with its length** (and a range is 206), and a build that is not there is * **404 in plain text** — never a login page and never the site. */ // RULE: cottondb.com's Mac download and feed are SERVED BY COTTON (architect, 10 Oct 2026) — the Worker forwards /mac/appcast.xml and /mac/download/ to the serving Mac named by MAC_FEED_ORIGIN and holds no copy of a Mac zip itself. const ZIP = "CottonDB-0.4.6.zip"; const SIZE = 111014762; const ORIGIN = "https://example.relay.cottondb.com"; const feed = ` 0.4.6 0.4.5 `; interface Mac { objects?: Record; appcast?: string | null; origin?: string | null; } const asked: { url: string; method: string; range: string | null }[] = []; function serving(mac: Mac = {}) { const objects = mac.objects ?? { [ZIP]: SIZE }; const appcast = mac.appcast === undefined ? feed : mac.appcast; asked.length = 0; vi.stubGlobal("fetch", async (input: string, init?: RequestInit) => { const url = new URL(input); const headers = new Headers(init?.headers); asked.push({ url: input, method: init?.method ?? "GET", range: headers.get("Range") }); if (url.pathname === "/mac/appcast.xml") { return appcast === null ? new Response("not found", { status: 404, headers: { "Content-Type": "application/json" } }) : new Response(appcast, { status: 200, headers: { "Content-Type": "application/xml; charset=utf-8" } }); } const name = decodeURIComponent(url.pathname.replace("/mac/download/", "")); if (!(name in objects)) return new Response('{"code":"not_found"}', { status: 404 }); if (headers.get("Range") === "bytes=0-2") { return new Response("byt", { status: 206, headers: { "Content-Length": "3", "Content-Range": `bytes 0-2/${objects[name]}` } }); } return new Response(init?.method === "HEAD" ? null : `bytes of ${name}`, { status: 200, headers: { "Content-Length": String(objects[name]), "Accept-Ranges": "bytes", "Content-Type": "application/zip" }, }); }); return { PUBLIC_ORIGIN: "https://cottondb.com", MAC_FEED_ORIGIN: mac.origin === undefined ? ORIGIN : (mac.origin ?? undefined), } as unknown as Env; } afterEach(() => vi.unstubAllGlobals()); function get(path: string, method = "GET", over?: Mac, range?: string) { const headers: Record = range ? { Range: range } : {}; return worker.fetch(new Request(`https://cottondb.com${path}`, { method, headers }), serving(over)); } describe("a published zip", () => { it("is streamed from the serving Mac as a zip, with its length and an immutable cache", async () => { const res = await get(`/mac/download/${ZIP}`); expect(res.status).toBe(200); expect(asked[0].url).toBe(`${ORIGIN}/mac/download/${ZIP}`); expect(res.headers.get("Content-Type")).toBe("application/zip"); expect(res.headers.get("Content-Length")).toBe(String(SIZE)); expect(res.headers.get("Cache-Control")).toBe("public, max-age=31536000, immutable"); expect(await res.text()).toContain(ZIP); }); it("answers HEAD without moving 110 MiB", async () => { const res = await get(`/mac/download/${ZIP}`, "HEAD"); expect(res.status).toBe(200); expect(asked[0].method).toBe("HEAD"); expect(res.headers.get("Content-Length")).toBe(String(SIZE)); expect(await res.text()).toBe(""); }); it("passes a range through, so an interrupted download resumes", async () => { const res = await get(`/mac/download/${ZIP}`, "GET", undefined, "bytes=0-2"); expect(asked[0].range).toBe("bytes=0-2"); expect(res.status).toBe(206); expect(res.headers.get("Content-Range")).toBe(`bytes 0-2/${SIZE}`); }); it("is a plain-text 503 when this site names no serving Mac", async () => { const res = await get(`/mac/download/${ZIP}`, "GET", { origin: null }); expect(res.status).toBe(503); expect(res.headers.get("Content-Type")).toBe("text/plain; charset=utf-8"); }); }); describe("a build that is not published", () => { it("is a plain-text 404, not a login page and not the site", async () => { const res = await get("/mac/download/CottonDB-9.9.9.zip"); expect(res.status).toBe(404); expect(res.headers.get("Content-Type")).toBe("text/plain; charset=utf-8"); expect(await res.text()).toContain("CottonDB-9.9.9.zip"); }); it("refuses a path that is not a zip name at all", async () => { // An encoded traversal survives URL parsing (a literal `../` does not — // the URL collapses it before the Worker ever sees it), so this is the // shape that actually reaches the key grammar. for (const path of ["/mac/download/%2e%2e%2fsecret", "/mac/download/nested/CottonDB-0.4.6.zip", "/mac/download/"]) { const res = await get(path); expect(res.status).toBe(404); expect(res.headers.get("Content-Type")).toBe("text/plain; charset=utf-8"); } }); it("refuses a write", async () => { const res = await get(`/mac/download/${ZIP}`, "POST"); expect(res.status).toBe(405); expect(res.headers.get("Allow")).toBe("GET, HEAD"); }); }); describe("latest", () => { it("redirects to the newest build the served feed names", async () => { const res = await get(MAC_DOWNLOAD_LATEST_PATH); expect(res.status).toBe(302); expect(res.headers.get("Location")).toBe(`https://cottondb.com/mac/download/${ZIP}`); // A pointer that is cached is a pointer that lies about which build is new. expect(res.headers.get("Cache-Control")).toBe("no-store"); }); it("404s when the serving Mac answers no feed, rather than sending a person nowhere", async () => { const res = await get(MAC_DOWNLOAD_LATEST_PATH, "GET", { appcast: null }); expect(res.status).toBe(404); expect(await res.text()).toContain("/mac/appcast.xml"); }); it("404s when the feed names a download this site does not serve", async () => { const elsewhere = feed.replace( `https://cottondb.com/mac/download/${ZIP}`, "https://git.example.org/decemberfm/cottondb/releases/download/mac-v0.4.6/CottonDB-0.4.6.zip", ); const res = await get(MAC_DOWNLOAD_LATEST_PATH, "GET", { appcast: elsewhere }); expect(res.status).toBe(404); }); });