Cotton code
59 lines · 2.4 KB · TypeScriptRaw
import { afterEach, describe, expect, it, vi } from "vitest";import { isMacAppcastPath, MAC_APPCAST_PATH } from "../../shared/macUpdates.ts";import type { Env } from "../env.ts";import worker from "../index.ts";/** * The Mac app's update feed — answered by the SERVING MAC off its release * records, forwarded here. * * The failure this guards is silent by construction: a feed answered as HTML * (a relay error page, the site's SPA shell) makes Sparkle say "could not check * for updates" while the site looks healthy. So only an XML 200 from the * serving Mac is passed on, and everything else is plain text naming the path. */function envServing(body: string | null, type = "application/xml; charset=utf-8"): Env {  vi.stubGlobal("fetch", async () =>    body === null      ? new Response("<!doctype html><html><body>an error page</body></html>", {          status: 200,          headers: { "Content-Type": "text/html; charset=utf-8" },        })      : new Response(body, { status: 200, headers: { "Content-Type": type } }),  );  return { PUBLIC_ORIGIN: "https://cottondb.com", MAC_FEED_ORIGIN: "https://example.relay.cottondb.com" } as unknown as Env;}afterEach(() => vi.unstubAllGlobals());const feed = '<?xml version="1.0"?><rss version="2.0"><channel/></rss>';describe("the Mac update feed", () => {  it("serves the serving Mac's feed as XML", async () => {    const res = await worker.fetch(      new Request(`https://cottondb.com${MAC_APPCAST_PATH}`),      envServing(feed),    );    expect(res.status).toBe(200);    expect(res.headers.get("Content-Type")).toBe("application/xml; charset=utf-8");    expect(await res.text()).toContain("<rss");  });  it("refuses in plain text rather than handing an updater HTML", async () => {    const res = await worker.fetch(      new Request(`https://cottondb.com${MAC_APPCAST_PATH}`),      envServing(null),    );    expect(res.status).toBe(502);    expect(res.headers.get("Content-Type")).toBe("text/plain; charset=utf-8");    expect(await res.text()).toContain(MAC_APPCAST_PATH);  });  it("is one exact path, with no aliases a release could drift onto", () => {    expect(isMacAppcastPath("/mac/appcast.xml")).toBe(true);    expect(isMacAppcastPath("/mac/appcast.xml/")).toBe(false);    expect(isMacAppcastPath("/mac/appcast")).toBe(false);    expect(isMacAppcastPath("/appcast.xml")).toBe(false);  });});