import { describe, expect, it } from "vitest"; import { fromBullMqJobId, toBullMqJobId } from "./job-id.js"; describe("BullMQ job id mapping", () => { it("escapes colons so BullMQ accepts idempotency keys as custom ids", () => { expect(toBullMqJobId("portal-deletion-confirmation:3d1c2427")).toBe("portal-deletion-confirmation%3A3d1c2427"); }); it("prefixes integer-only keys, which BullMQ rejects as custom ids", () => { expect(toBullMqJobId("12345")).toBe("job-12345"); }); it("keeps the lossy integer prefix visible instead of guessing the original key", () => { // `42` and `job-42` both encode to `job-42`, so decoding cannot tell them // apart. Showing the wire form is honest; stripping would display a key // that may never have been enqueued. expect(toBullMqJobId("42")).toBe(toBullMqJobId("job-42")); expect(fromBullMqJobId("job-42")).toBe("job-42"); }); it("shows operators the original key instead of the wire form", () => { // The admin jobs list rendered "portal-deletion-confirmation%3A3d1c2427". expect(fromBullMqJobId("portal-deletion-confirmation%3A3d1c2427")).toBe("portal-deletion-confirmation:3d1c2427"); }); it("round-trips keys that contain colons, percent signs and literal escape sequences", () => { for (const key of ["plain-key", "a:b:c", "100%-key", "literal-%3A-inside", "literal-%25-inside", "mixed:%3A:%25", "job-abc:1"]) { expect(fromBullMqJobId(toBullMqJobId(key))).toBe(key); } }); it("leaves a key without escape sequences untouched in both directions", () => { expect(toBullMqJobId("outbox-abc-1")).toBe("outbox-abc-1"); expect(fromBullMqJobId("outbox-abc-1")).toBe("outbox-abc-1"); }); it("keeps a hyphenated job prefix that is not an escaped integer", () => { expect(fromBullMqJobId("job-abc")).toBe("job-abc"); }); });