34 lines · 1.3 KB
import { describe, expect, test } from "bun:test";
import { handleGoGet } from "./go-get";

const get = (path: string, method = "GET") => {
  const url = new URL(`https://gitfor.dev${path}`);
  return handleGoGet(new Request(url, { method }), url);
};

describe("go get discovery", () => {
  test("maps the module root and subpackages to the repository's git URL", async () => {
    for (const path of ["/taigrr/gfd?go-get=1", "/taigrr/gfd/cmd/gfd?go-get=1", "/taigrr/gfd.git?go-get=1"]) {
      const body = await get(path)?.text();
      expect(body).toContain(
        '<meta name="go-import" content="gitfor.dev/taigrr/gfd git https://gitfor.dev/taigrr/gfd.git">',
      );
    }
  });

  test("ignores normal page loads, app routes and malformed paths", () => {
    expect(get("/taigrr/gfd")).toBeNull();
    expect(get("/settings/tokens?go-get=1")).toBeNull();
    expect(get("/taigrr?go-get=1")).toBeNull();
    expect(get("/taigrr/%22%3E%3Cscript%3E?go-get=1")).toBeNull();
    expect(get("/taigrr/gfd?go-get=1", "POST")).toBeNull();
  });
});

describe("project-reserved owners", () => {
  test("the gitfor org's repositories resolve as Go modules", async () => {
    const body = await get("/gitfor/platform?go-get=1")?.text();
    expect(body).toContain('content="gitfor.dev/gitfor/platform git https://gitfor.dev/gitfor/platform.git"');
  });
});