34 lines · 1.3 KB
import { describe, expect, mock, spyOn, test } from "bun:test";

mock.module("cloudflare:workers", () => ({ env: {}, waitUntil: () => undefined }));

const { deletionHalt, sweepMode } = await import("./artifacts-sweep");

describe("orphan sweep delete caps (GIT-P3)", () => {
  test("a few orphans on a large page may be deleted", () => {
    expect(deletionHalt(5, 200, 0)).toBeNull();
    expect(deletionHalt(3, 10, 0)).toBeNull();
  });

  test("more than 5% of a page halts", () => {
    expect(deletionHalt(11, 200, 0)).toContain("over 5%");
    expect(deletionHalt(4, 10, 0)).toContain("over 5%");
  });

  test("more than 20 in a run halts", () => {
    expect(deletionHalt(5, 200, 16)).toContain("more than 20");
  });
});

describe("sweepMode", () => {
  test("delete needs an exclusive namespace, a real host and no dev flag", () => {
    const log = spyOn(console, "error").mockImplementation(() => undefined);
    const base = { mode: "delete", exclusive: "true", publicUrl: "https://gitfor.dev" };
    expect(sweepMode(base)).toBe("delete");
    expect(sweepMode({ ...base, local: "true" })).toBe("report");
    expect(sweepMode({ ...base, publicUrl: "http://localhost:3000" })).toBe("report");
    expect(sweepMode({ ...base, exclusive: "false" })).toBe("report");
    log.mockRestore();
  });
});