Cotton code
61 lines · 2.6 KB · TypeScriptRaw
import { describe, expect, it } from "vitest";import {  MAC_RUNNER_ARCHS,  MAC_RUNNER_CACHE_CONTROL,  MAC_RUNNER_CONTENT_TYPE,  MAC_RUNNER_PREFIX,  macRunnerKey,  macRunnerName,  macRunnerPath,  macRunnerUrl,} from "../macRunner.ts";describe("macRunnerKey", () => {  it("takes the file name a published build is keyed by", () => {    expect(macRunnerKey("/mac/runner/forgejo-runner-13.0.0-darwin-arm64")).toBe(      "forgejo-runner-13.0.0-darwin-arm64",    );    expect(macRunnerKey("/mac/runner/forgejo-runner-13.0.0-darwin-amd64")).toBe(      "forgejo-runner-13.0.0-darwin-amd64",    );    expect(macRunnerKey("/mac/runner/forgejo-runner-100.2.30-darwin-arm64")).toBe(      "forgejo-runner-100.2.30-darwin-arm64",    );  });  it("refuses anything the build recipe never wrote", () => {    // Another program entirely — the one that was dropped for handing the job    // token to the actions host.    expect(macRunnerKey("/mac/runner/gitea-runner-3.5.0-darwin-arm64")).toBeNull();    // Not a darwin build, not an architecture a Mac is.    expect(macRunnerKey("/mac/runner/forgejo-runner-13.0.0-linux-amd64")).toBeNull();    expect(macRunnerKey("/mac/runner/forgejo-runner-13.0.0-darwin-riscv64")).toBeNull();    // A partial version, and an extension that would change the content type.    expect(macRunnerKey("/mac/runner/forgejo-runner-13.0-darwin-arm64")).toBeNull();    expect(macRunnerKey("/mac/runner/forgejo-runner-13.0.0-darwin-arm64.xz")).toBeNull();    // THE GRAMMAR IS THE GUARD: no path segments, no traversal, no bare prefix.    expect(macRunnerKey("/mac/runner/nested/forgejo-runner-13.0.0-darwin-arm64")).toBeNull();    expect(macRunnerKey("/mac/runner/../../secret")).toBeNull();    expect(macRunnerKey("/mac/runner/")).toBeNull();    // A different surface entirely.    expect(macRunnerKey("/mac/download/CottonDB-0.9.2.zip")).toBeNull();  });});describe("the one place the name is spelled", () => {  it("agrees with itself across name, path and url", () => {    for (const arch of MAC_RUNNER_ARCHS) {      const name = macRunnerName("13.0.0", arch);      expect(macRunnerPath("13.0.0", arch)).toBe(`${MAC_RUNNER_PREFIX}${name}`);      expect(macRunnerUrl("13.0.0", arch)).toBe(`https://cottondb.com${MAC_RUNNER_PREFIX}${name}`);      // And every one of them is a path the Worker will actually serve.      expect(macRunnerKey(macRunnerPath("13.0.0", arch))).toBe(name);    }  });  it("serves an extension-less Mach-O as bytes, cached for a year", () => {    expect(MAC_RUNNER_CONTENT_TYPE).toBe("application/octet-stream");    expect(MAC_RUNNER_CACHE_CONTROL).toContain("immutable");  });});