Cotton code
78 lines · 3.4 KB · TypeScriptRaw
import { describe, expect, it } from "vitest";import type { Env } from "../env.ts";import worker from "../index.ts";/** * `/mac/cotton/…` — Cotton for the Mac's update feed and builds, out of R2 (`src/shared/macCotton.ts`). * * The failure this guards (23 Nov 2026): the installed app asked https://cottondb.com/mac/cotton/appcast.xml and * got the site's 404 page, so it could never update itself. A published feed is XML 200, a published zip is 200 * with its length, and anything else is a plain-text 404 naming the key — never an HTML page. */const FEED = '<?xml version="1.0"?><rss version="2.0"><channel><title>Cotton</title></channel></rss>';const ZIP = "Cotton-0.4.0-4.zip";function env(objects: Record<string, string> = { "cotton/appcast.xml": FEED, [`cotton/${ZIP}`]: "zip bytes" }): Env {  const object = (key: string) =>    key in objects ? { size: objects[key].length, httpEtag: `"etag-${key}"`, body: objects[key] } : null;  return {    PUBLIC_ORIGIN: "https://cottondb.com",    ASSETS: { fetch: async () => new Response("<!doctype html><p>404</p>", { status: 404, headers: { "Content-Type": "text/html" } }) },    MAC_RELEASES: {      get: async (key: string) => object(key),      head: async (key: string) => {        const found = object(key);        return found ? { size: found.size, httpEtag: found.httpEtag } : null;      },    },  } as unknown as Env;}function get(path: string, method = "GET", e = env()) {  return worker.fetch(new Request(`https://cottondb.com${path}`, { method }), e);}describe("Cotton for the Mac's update feed", () => {  it("serves the published feed as XML with a short cache", async () => {    const res = await get("/mac/cotton/appcast.xml");    expect(res.status).toBe(200);    expect(res.headers.get("Content-Type")).toBe("application/xml; charset=utf-8");    expect(res.headers.get("Cache-Control")).toBe("public, max-age=300");    expect(await res.text()).toBe(FEED);  });  it("says plainly that no feed is published, never with a page", async () => {    const res = await get("/mac/cotton/appcast.xml", "GET", env({}));    expect(res.status).toBe(404);    expect(res.headers.get("Content-Type")).toBe("text/plain; charset=utf-8");    expect(await res.text()).toBe("cotton/appcast.xml is not published");  });});describe("Cotton for the Mac's builds", () => {  it("serves a published zip with its length, immutable", async () => {    const res = await get(`/mac/cotton/${ZIP}`);    expect(res.status).toBe(200);    expect(res.headers.get("Content-Type")).toBe("application/zip");    expect(res.headers.get("Content-Length")).toBe(String("zip bytes".length));    expect(res.headers.get("Cache-Control")).toBe("public, max-age=31536000, immutable");  });  it("answers HEAD without the bytes", async () => {    const res = await get(`/mac/cotton/${ZIP}`, "HEAD");    expect(res.status).toBe(200);    expect(res.headers.get("Content-Length")).toBe(String("zip bytes".length));  });  it("refuses names nobody publishes, and other methods", async () => {    for (const path of ["/mac/cotton/", "/mac/cotton/../runner/x", "/mac/cotton/Cotton.zip", "/mac/cotton/cotton/appcast.xml"]) {      const res = await get(path);      expect(res.status, path).toBe(404);      expect(res.headers.get("Content-Type"), path).toBe("text/plain; charset=utf-8");    }    expect((await get(`/mac/cotton/${ZIP}`, "POST")).status).toBe(405);    expect((await get("/mac/cotton/Cotton-0.4.1-5.zip")).status).toBe(404);  });});