|
| 1 | +import { describe, expect, it } from "vitest"; |
| 2 | +import { |
| 3 | + BatchId, |
| 4 | + generateFriendlyId, |
| 5 | + generateKsuidId, |
| 6 | + RunId, |
| 7 | +} from "@trigger.dev/core/v3/isomorphic"; |
| 8 | +import { isValidFriendlyId, makeFriendlyIdValidator } from "./friendlyId"; |
| 9 | + |
| 10 | +describe("isValidFriendlyId", () => { |
| 11 | + it("accepts every id generation the real generators produce", () => { |
| 12 | + // nanoid (legacy V1), cuid (run-engine), ksuid (run-ops split) |
| 13 | + expect(isValidFriendlyId(generateFriendlyId("run"), "run")).toBe(true); |
| 14 | + expect(isValidFriendlyId(RunId.generate().friendlyId, "run")).toBe(true); |
| 15 | + expect(isValidFriendlyId(RunId.toFriendlyId(generateKsuidId()), "run")).toBe(true); |
| 16 | + |
| 17 | + expect(isValidFriendlyId(generateFriendlyId("batch"), "batch")).toBe(true); |
| 18 | + expect(isValidFriendlyId(BatchId.generate().friendlyId, "batch")).toBe(true); |
| 19 | + expect(isValidFriendlyId(BatchId.toFriendlyId(generateKsuidId()), "batch")).toBe(true); |
| 20 | + }); |
| 21 | + |
| 22 | + it("accepts each valid body length (21 nanoid, 25 cuid, 27 ksuid)", () => { |
| 23 | + expect(isValidFriendlyId("run_" + "a".repeat(21), "run")).toBe(true); |
| 24 | + expect(isValidFriendlyId("run_" + "a".repeat(25), "run")).toBe(true); |
| 25 | + expect(isValidFriendlyId("run_" + "a".repeat(27), "run")).toBe(true); |
| 26 | + }); |
| 27 | + |
| 28 | + it("accepts mixed-case (uppercase) ksuid bodies", () => { |
| 29 | + expect(isValidFriendlyId("run_2ABCdefGHI0123456789jklMN", "run")).toBe(true); |
| 30 | + }); |
| 31 | + |
| 32 | + it("rejects the wrong prefix", () => { |
| 33 | + expect(isValidFriendlyId(RunId.generate().friendlyId, "batch")).toBe(false); |
| 34 | + expect(isValidFriendlyId("batch_" + "a".repeat(25), "run")).toBe(false); |
| 35 | + }); |
| 36 | + |
| 37 | + it("rejects a bare (unprefixed) id", () => { |
| 38 | + expect(isValidFriendlyId("a".repeat(25), "run")).toBe(false); |
| 39 | + }); |
| 40 | + |
| 41 | + it("rejects body lengths that match no generator", () => { |
| 42 | + for (const len of [0, 20, 22, 24, 26, 28]) { |
| 43 | + expect(isValidFriendlyId("run_" + "a".repeat(len), "run")).toBe(false); |
| 44 | + } |
| 45 | + }); |
| 46 | + |
| 47 | + it("rejects non-base62 characters in the body", () => { |
| 48 | + expect(isValidFriendlyId("run_" + "-".repeat(25), "run")).toBe(false); |
| 49 | + expect(isValidFriendlyId("run_" + "!".repeat(25), "run")).toBe(false); |
| 50 | + // an underscore in the body is not base62 |
| 51 | + expect(isValidFriendlyId("run_" + "a".repeat(24) + "_", "run")).toBe(false); |
| 52 | + }); |
| 53 | + |
| 54 | + it("does not treat the prefix separator as optional", () => { |
| 55 | + // "runX..." shares the "run" prefix but not the "run_" marker |
| 56 | + expect(isValidFriendlyId("run" + "a".repeat(25), "run")).toBe(false); |
| 57 | + }); |
| 58 | +}); |
| 59 | + |
| 60 | +describe("makeFriendlyIdValidator", () => { |
| 61 | + const validateRunId = makeFriendlyIdValidator("run", "Run"); |
| 62 | + const validateBatchId = makeFriendlyIdValidator("batch", "Batch"); |
| 63 | + |
| 64 | + it("returns undefined for a valid id of any generation", () => { |
| 65 | + expect(validateRunId(generateFriendlyId("run"))).toBeUndefined(); |
| 66 | + expect(validateRunId(RunId.generate().friendlyId)).toBeUndefined(); |
| 67 | + expect(validateRunId(RunId.toFriendlyId(generateKsuidId()))).toBeUndefined(); |
| 68 | + expect(validateBatchId(BatchId.toFriendlyId(generateKsuidId()))).toBeUndefined(); |
| 69 | + }); |
| 70 | + |
| 71 | + it("reports a wrong prefix distinctly from a wrong shape", () => { |
| 72 | + expect(validateRunId("batch_" + "a".repeat(25))).toBe("Run IDs start with 'run_'"); |
| 73 | + expect(validateRunId("run_" + "a".repeat(20))).toBe("That doesn't look like a valid run ID"); |
| 74 | + }); |
| 75 | + |
| 76 | + it("derives the marker and label per entity", () => { |
| 77 | + const validateWaitpointId = makeFriendlyIdValidator("waitpoint", "Waitpoint"); |
| 78 | + expect(validateWaitpointId("run_" + "a".repeat(25))).toBe( |
| 79 | + "Waitpoint IDs start with 'waitpoint_'" |
| 80 | + ); |
| 81 | + expect(validateWaitpointId("waitpoint_" + "a".repeat(20))).toBe( |
| 82 | + "That doesn't look like a valid waitpoint ID" |
| 83 | + ); |
| 84 | + }); |
| 85 | +}); |
0 commit comments