// @vitest-environment node import { describe, it, expect } from "vitest"; import { ScoreJobSchema, InMemoryJobQueue, jobDedupKey, type ScoreJob, } from "./queue"; const job = (over: Partial = {}): ScoreJob => ({ did: "did:plc:dev", pds: "https://pds.example", prUri: "at://did:plc:dev/sh.tangled.repo.pull/abc", round: 0, cid: "bafyCID", prTitle: "Add thing", sourceRepo: "octo/repo", ...over, }); describe("ScoreJobSchema", () => { it("validates a well-formed job payload", () => { expect(ScoreJobSchema.safeParse(job()).success).toBe(true); }); it("rejects a cid-less job (cid-less rounds are never enqueued)", () => { const { cid: _cid, ...noCid } = job(); expect(ScoreJobSchema.safeParse(noCid).success).toBe(false); }); it("rejects a negative round index", () => { expect(ScoreJobSchema.safeParse(job({ round: -1 })).success).toBe(false); }); it("treats sourceRepo and prTitle as optional", () => { const { sourceRepo: _s, prTitle: _t, ...lean } = job(); expect(ScoreJobSchema.safeParse(lean).success).toBe(true); }); }); describe("jobDedupKey", () => { it("is stable per (prUri, round)", () => { expect(jobDedupKey("at://x/p/1", 2)).toBe("enq:at://x/p/1#2"); }); }); describe("InMemoryJobQueue", () => { it("round-trips enqueue → consume → ack", async () => { const q = new InMemoryJobQueue(); const { enqueued, id } = await q.enqueue(job()); expect(enqueued).toBe(true); expect(id).toBeTruthy(); const batch = await q.consume(); expect(batch).toHaveLength(1); expect(batch[0].job.prUri).toBe(job().prUri); expect(q.unacked()).toHaveLength(1); // delivered, not yet acked await q.ack(batch[0].id); expect(q.unacked()).toHaveLength(0); }); it("de-duplicates a repeat enqueue of the same (prUri, round)", async () => { const q = new InMemoryJobQueue(); expect((await q.enqueue(job())).enqueued).toBe(true); expect((await q.enqueue(job())).enqueued).toBe(false); // same (prUri, round) expect((await q.enqueue(job({ round: 1 }))).enqueued).toBe(true); // different round const batch = await q.consume(); expect(batch).toHaveLength(2); }); it("respects a count bound on consume", async () => { const q = new InMemoryJobQueue(); await q.enqueue(job({ round: 0 })); await q.enqueue(job({ round: 1 })); await q.enqueue(job({ round: 2 })); const first = await q.consume({ count: 2 }); expect(first).toHaveLength(2); const rest = await q.consume(); expect(rest).toHaveLength(1); }); it("validates payloads on enqueue", async () => { const q = new InMemoryJobQueue(); // @ts-expect-error — deliberately invalid payload await expect(q.enqueue({ did: "x" })).rejects.toBeTruthy(); }); });