|
| 1 | +import { describe, expect, it, vi } from "vitest"; |
| 2 | +import { classifyKind, mintWaitpointIdFor, resolveShard } from "@trigger.dev/core/v3/isomorphic"; |
| 3 | +import { resolveInheritedMintKind } from "./resolveInheritedMintKind.server"; |
| 4 | +import { |
| 5 | + mintAnchoredRunFriendlyId, |
| 6 | + mintFriendlyIdForKind, |
| 7 | +} from "./mintAnchoredRunFriendlyId.server"; |
| 8 | +import { batchIdForMintKind } from "./mintBatchFriendlyId.server"; |
| 9 | +import { resolveRunMintTarget } from "./resolveRunMintTarget.server"; |
| 10 | + |
| 11 | +// Gate off means resolveMintShard answers "new". Every assertion is "the id is what it was". |
| 12 | +const offShard = vi.fn().mockResolvedValue("new" as const); |
| 13 | +const environment = { organizationId: "org_1", id: "env_1", orgFeatureFlags: {} }; |
| 14 | + |
| 15 | +describe("gate off — run mint paths", () => { |
| 16 | + it("a root run on the run-ops path mints a gen-1 v1 id", async () => { |
| 17 | + const target = await resolveRunMintTarget({ |
| 18 | + environment, |
| 19 | + region: "us-east-1", |
| 20 | + deps: { |
| 21 | + resolveRunIdMintKind: vi.fn().mockResolvedValue("runOpsId"), |
| 22 | + resolveMintShard: offShard, |
| 23 | + }, |
| 24 | + }); |
| 25 | + const body = mintFriendlyIdForKind(target).slice(4); |
| 26 | + expect(body.length).toBe(26); |
| 27 | + expect(body[24]).toBe("e"); // the region char, as today |
| 28 | + expect(body[25]).toBe("1"); |
| 29 | + }); |
| 30 | + |
| 31 | + it("a root run on a non-cut-over org mints a cuid", async () => { |
| 32 | + const target = await resolveRunMintTarget({ |
| 33 | + environment, |
| 34 | + deps: { |
| 35 | + resolveRunIdMintKind: vi.fn().mockResolvedValue("cuid"), |
| 36 | + resolveMintShard: offShard, |
| 37 | + }, |
| 38 | + }); |
| 39 | + expect(mintFriendlyIdForKind(target).slice(4).length).toBe(25); |
| 40 | + }); |
| 41 | + |
| 42 | + it("a child of a gen-1 parent keeps the caller's region char", async () => { |
| 43 | + // The pre-split code passed the region on both arms; dropping it on the inherited arm would |
| 44 | + // silently stamp the default. |
| 45 | + const target = await resolveRunMintTarget({ |
| 46 | + environment, |
| 47 | + parentRunFriendlyId: `run_${"a".repeat(24)}01`, |
| 48 | + region: "us-east-1", |
| 49 | + deps: { |
| 50 | + resolveRunIdMintKind: vi.fn().mockResolvedValue("runOpsId"), |
| 51 | + resolveMintShard: offShard, |
| 52 | + }, |
| 53 | + }); |
| 54 | + const body = mintFriendlyIdForKind(target).slice(4); |
| 55 | + expect(body[24]).toBe("e"); |
| 56 | + expect(body[25]).toBe("1"); |
| 57 | + }); |
| 58 | + |
| 59 | + it("a gen-2 parent's shard still outranks the caller's region", async () => { |
| 60 | + const target = await resolveRunMintTarget({ |
| 61 | + environment, |
| 62 | + parentRunFriendlyId: `run_${"a".repeat(24)}a2`, |
| 63 | + region: "us-east-1", |
| 64 | + deps: { |
| 65 | + resolveRunIdMintKind: vi.fn().mockResolvedValue("runOpsId"), |
| 66 | + resolveMintShard: offShard, |
| 67 | + }, |
| 68 | + }); |
| 69 | + expect(mintFriendlyIdForKind(target).slice(4)[24]).toBe("a"); |
| 70 | + }); |
| 71 | + |
| 72 | + it("a child of a gen-1 parent mints a gen-1 v1 id", () => { |
| 73 | + const body = mintFriendlyIdForKind(resolveInheritedMintKind(`run_${"a".repeat(24)}01`)).slice( |
| 74 | + 4 |
| 75 | + ); |
| 76 | + expect(body[25]).toBe("1"); |
| 77 | + }); |
| 78 | + |
| 79 | + it("a child of a cuid parent mints a cuid", () => { |
| 80 | + expect( |
| 81 | + mintFriendlyIdForKind(resolveInheritedMintKind(`run_${"b".repeat(25)}`)).slice(4).length |
| 82 | + ).toBe(25); |
| 83 | + }); |
| 84 | +}); |
| 85 | + |
| 86 | +describe("gate off — batch and item paths", () => { |
| 87 | + it("a batch with no shard char mints a gen-1 v1 id", () => { |
| 88 | + const r = batchIdForMintKind({ kind: "runOpsId" }); |
| 89 | + expect(r.id.length).toBe(26); |
| 90 | + expect(r.id[25]).toBe("1"); |
| 91 | + expect(classifyKind(r.id)).toBe("runOpsId"); |
| 92 | + }); |
| 93 | + |
| 94 | + it("a batch on a non-cut-over org mints a cuid", () => { |
| 95 | + expect(batchIdForMintKind({ kind: "cuid" }).id.length).toBe(25); |
| 96 | + }); |
| 97 | + |
| 98 | + it("a batch item anchored on a gen-1 batch mints a gen-1 v1 id", () => { |
| 99 | + const body = mintAnchoredRunFriendlyId(`batch_${"a".repeat(24)}01`).slice(4); |
| 100 | + expect(body[25]).toBe("1"); |
| 101 | + }); |
| 102 | +}); |
| 103 | + |
| 104 | +describe("gate off — waitpoint paths", () => { |
| 105 | + it("every gen-1 or legacy anchor yields a cuid waitpoint id", () => { |
| 106 | + for (const anchor of [`${"a".repeat(24)}01`, "c".repeat(25), undefined]) { |
| 107 | + const r = mintWaitpointIdFor(anchor); |
| 108 | + expect(r.id.length).toBe(25); |
| 109 | + expect(resolveShard(r.id)).toBe("legacy"); |
| 110 | + } |
| 111 | + }); |
| 112 | +}); |
0 commit comments