import { describe, expect, mock, test } from "bun:test"; import { concatBytes, FLUSH_PKT, packetText, parseAllPackets, pktLine } from "./pktline"; const env: Record = { ARTIFACTS_SHARDS: "1" }; mock.module("cloudflare:workers", () => ({ env, waitUntil: () => undefined, WorkflowEntrypoint: class {}, DurableObject: class {}, WorkerEntrypoint: class {}, })); const { lsRefsRequest, parseLsRefs } = await import("./refs"); const shards = await import("./artifacts-shards"); const { tokenLifetimeSeconds, tokenSecret } = await import("./artifacts"); const A = "a".repeat(40); const B = "b".repeat(40); const C = "c".repeat(40); describe("ls-refs", () => { test("requests only HEAD, branches and tags", () => { const lines = parseAllPackets(lsRefsRequest()).map((packet) => packet.kind === "data" ? packetText(packet) : packet.kind, ); expect(lines).toEqual([ "command=ls-refs", "agent=git/gitfor", "delim", "symrefs", "peel", "ref-prefix HEAD", "ref-prefix refs/heads/", "ref-prefix refs/tags/", "flush", ]); }); test("parses HEAD, branches, peeled tags and ignores hidden refs", () => { const body = concatBytes([ pktLine(`${A} HEAD symref-target:refs/heads/main\n`), pktLine(`${B} refs/heads/dev\n`), pktLine(`${A} refs/heads/main\n`), pktLine(`${C} refs/pull/1/head\n`), pktLine(`${C} refs/tags/v1 peeled:${A}\n`), pktLine(`${B} refs/tags/v2\n`), FLUSH_PKT, ]); expect(parseLsRefs(body)).toEqual({ head: "main", branches: [ { name: "dev", sha: B }, { name: "main", sha: A }, ], tags: [ { name: "v2", sha: B }, { name: "v1", sha: A }, ], }); }); test("an empty repository has no refs", () => { expect(parseLsRefs(FLUSH_PKT)).toEqual({ head: null, branches: [], tags: [] }); }); test("ERR lines fail so the caller falls back", () => { expect(() => parseLsRefs(concatBytes([pktLine("ERR nope\n"), FLUSH_PKT]))).toThrow("nope"); }); }); describe("artifacts shards", () => { test("legacy names resolve to shard 0", () => { expect(shards.shardOfName("r-abc123")).toBe(0); expect(shards.nameInShard("abc123", 0)).toBe("r-abc123"); }); test("shard is encoded in the name", () => { expect(shards.nameInShard("abc123", 3)).toBe("r3-abc123"); expect(shards.shardOfName("r3-abc123")).toBe(3); expect(shards.bindingNameForShard(3)).toBe("ARTIFACTS_3"); expect(shards.bindingNameForShard(0)).toBe("ARTIFACTS"); }); test("picks a stable shard within range", () => { expect(shards.pickShard("abc", 1)).toBe(0); const picked = shards.pickShard("abc", 4); expect(picked).toBe(shards.pickShard("abc", 4)); expect(picked).toBeGreaterThanOrEqual(0); expect(picked).toBeLessThan(4); }); test("invalid shard counts fall back to one", () => { expect(shards.configuredShardCount(undefined)).toBe(1); expect(shards.configuredShardCount("0")).toBe(1); expect(shards.configuredShardCount("x")).toBe(1); expect(shards.configuredShardCount("3")).toBe(3); }); test("new names default to shard 0 with one shard configured", () => { expect(shards.artifactsNameFor("abc")).toBe("r-abc"); }); test("a missing shard binding fails loudly", () => { expect(() => shards.bindingForShard(2)).toThrow("ARTIFACTS_2"); }); }); describe("token plaintext", () => { test("splits the secret and reads the expiry", () => { const plaintext = `art_v1_${"0".repeat(40)}?expires=2000`; expect(tokenSecret(plaintext)).toBe(`art_v1_${"0".repeat(40)}`); expect(tokenLifetimeSeconds(plaintext, 1_000_000)).toBe(1000); expect(tokenLifetimeSeconds("art_v1_x")).toBeNull(); }); }); describe("orphan sweep", () => { test("only flags our old, unknown names in the right shard", async () => { const { orphanCandidates } = await import("./artifacts-sweep"); const now = Date.parse("2026-10-10T00:00:00Z"); const old = "2026-10-01T00:00:00Z"; const repos = [ { name: "r-known", createdAt: old }, { name: "r-orphan", createdAt: old }, { name: "r-young", createdAt: "2026-10-09T12:00:00Z" }, { name: "r2-elsewhere", createdAt: old }, { name: "someone-else", createdAt: old }, ]; expect(orphanCandidates(repos, 0, new Set(["r-known"]), now)).toEqual(["r-orphan"]); }); });