Cotton code
76 lines · 3.6 KB · TypeScriptRaw
import { describe, expect, it } from "vitest";/** A two-build feed in the shape the serving Mac answers, newest first. */const APPCAST = `<?xml version="1.0" standalone="yes"?><rss xmlns:sparkle="http://www.andymatuschak.org/xml-namespaces/sparkle" version="2.0"><channel><item><sparkle:shortVersionString>0.29.96</sparkle:shortVersionString><enclosure url="https://cottondb.com/mac/download/CottonDB-0.29.96.zip" length="368063845"/></item><item><sparkle:shortVersionString>0.29.95</sparkle:shortVersionString><enclosure url="https://cottondb.com/mac/download/CottonDB-0.29.95.zip" length="368063762"/></item></channel></rss>`;import {  downloadPathForEnclosure,  isMacLatestPath,  latestRelease,  MAC_DOWNLOAD_LATEST_PATH,  macDownloadKey,  macDownloadPath,} from "../macDownloads.ts";/** The Mac download's grammar, and how the `latest` pointer reads the feed. */describe("which paths are downloads", () => {  it("accepts exactly the names the release lane writes", () => {    expect(macDownloadKey("/mac/download/CottonDB-0.4.6.zip")).toBe("CottonDB-0.4.6.zip");    expect(macDownloadKey("/mac/download/CottonDB-10.20.30.zip")).toBe("CottonDB-10.20.30.zip");  });  it("refuses anything else, so a request cannot name an object nobody published", () => {    expect(macDownloadKey("/mac/download/CottonDB-0.4.6.zip.bak")).toBeNull();    expect(macDownloadKey("/mac/download/cottondb-0.4.6.zip")).toBeNull();    expect(macDownloadKey("/mac/download/CottonDB-0.4.zip")).toBeNull();    expect(macDownloadKey("/mac/download/nested/CottonDB-0.4.6.zip")).toBeNull();    expect(macDownloadKey("/mac/download/../../secret")).toBeNull();    expect(macDownloadKey("/mac/download/")).toBeNull();    expect(macDownloadKey("/downloads/CottonDB-0.4.6.zip")).toBeNull();  });  it("names one un-versioned pointer, with no aliases", () => {    expect(isMacLatestPath(MAC_DOWNLOAD_LATEST_PATH)).toBe(true);    expect(isMacLatestPath("/mac/download/latest/")).toBe(false);    expect(isMacLatestPath("/mac/download/Latest")).toBe(false);  });  it("builds the path a version is served at", () => {    expect(macDownloadPath("0.4.6")).toBe("/mac/download/CottonDB-0.4.6.zip");  });});describe("reading the appcast", () => {  it("takes the newest entry — the first item, which is how the feed is written", () => {    const latest = latestRelease(APPCAST);    expect(latest).not.toBeNull();    expect(latest?.version).toBe("0.29.96");    expect(latest?.url).toBe(`https://cottondb.com${macDownloadPath(latest?.version ?? "")}`);    expect(latest?.length).toBeGreaterThan(0);  });  it("answers null for a feed with no item — an empty channel is a correct answer", () => {    expect(latestRelease('<?xml version="1.0"?><rss><channel/></rss>')).toBeNull();  });  it("answers null for an entry missing its enclosure or version", () => {    expect(latestRelease("<item><sparkle:shortVersionString>0.1.0</sparkle:shortVersionString></item>")).toBeNull();    expect(latestRelease('<item><enclosure url="https://cottondb.com/mac/download/CottonDB-0.1.0.zip" length="1"/></item>')).toBeNull();  });  it("takes only the PATH of an enclosure, so a feed can never redirect visitors off this site", () => {    expect(downloadPathForEnclosure("https://cottondb.com/mac/download/CottonDB-0.4.6.zip")).toBe(      "/mac/download/CottonDB-0.4.6.zip",    );    expect(downloadPathForEnclosure("https://elsewhere.example/mac/download/CottonDB-0.4.6.zip")).toBe(      "/mac/download/CottonDB-0.4.6.zip",    );    expect(downloadPathForEnclosure("https://cottondb.com/anything-else.zip")).toBeNull();    expect(downloadPathForEnclosure("not a url")).toBeNull();  });});