Cotton code
92 lines · 3.6 KB · TypeScriptRaw
import { describe, expect, it } from "vitest";import { macRunnerPath } from "../../shared/macRunner.ts";import type { Env } from "../env.ts";import worker from "../index.ts";/** * `/mac/runner/…` — the darwin `forgejo-runner` the Mac app bundles. * * `macDownload.test.ts`'s sibling, against the same binding. The two answers * that matter are the same two: a published build is **200 with its length**, * and one that is not there is **404 in plain text** — never a redirect to * somebody's login, which is exactly what the forge answers for a private * repository's release asset and the whole reason this route exists. */const ARM = "forgejo-runner-13.0.0-darwin-arm64";const SIZE = 17955826;function env(objects: Record<string, number> = { [ARM]: SIZE }): Env {  const object = (key: string) =>    key in objects      ? { size: objects[key], httpEtag: `"etag-${key}"`, body: `bytes of ${key}` }      : null;  return {    PUBLIC_ORIGIN: "https://cottondb.com",    ASSETS: { fetch: async () => new Response("no", { status: 404 }) },    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") {  return worker.fetch(new Request(`https://cottondb.com${path}`, { method }), env());}describe("a published runner binary", () => {  it("is served as bytes, with its length and an immutable cache", async () => {    const res = await get(macRunnerPath("13.0.0", "arm64"));    expect(res.status).toBe(200);    // A Mach-O with no extension: bytes, never something a browser renders.    expect(res.headers.get("Content-Type")).toBe("application/octet-stream");    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(ARM);  });  it("answers HEAD without moving 18 MiB — what the fetch script's check does", async () => {    const res = await get(macRunnerPath("13.0.0", "arm64"), "HEAD");    expect(res.status).toBe(200);    expect(res.headers.get("Content-Length")).toBe(String(SIZE));    expect(await res.text()).toBe("");  });});describe("a runner build that is not published", () => {  it("is a plain-text 404 naming the file, not a login page and not the site", async () => {    // What a pin bump that was never published through    // `publish-mac-runner.yml` looks like from a build Mac.    const res = await get(macRunnerPath("14.0.0", "arm64"));    expect(res.status).toBe(404);    expect(res.headers.get("Content-Type")).toBe("text/plain; charset=utf-8");    expect(await res.text()).toContain("forgejo-runner-14.0.0-darwin-arm64");  });  it("refuses a path that is not a runner name at all", async () => {    // An encoded traversal survives URL parsing (a literal `../` does not —    // the URL collapses it first), so this is the shape that actually reaches    // the key grammar.    for (const path of [      "/mac/runner/%2e%2e%2fsecret",      "/mac/runner/nested/forgejo-runner-13.0.0-darwin-arm64",      "/mac/runner/gitea-runner-3.5.0-darwin-arm64",      "/mac/runner/",    ]) {      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(macRunnerPath("13.0.0", "arm64"), "POST");    expect(res.status).toBe(405);    expect(res.headers.get("Allow")).toBe("GET, HEAD");  });});