|
| 1 | +import { describe, expect, it, vi } from "vitest"; |
| 2 | + |
| 3 | +// Stub `~/db.server` before importing the concern — the real module |
| 4 | +// eagerly calls `prisma.$connect()` at singleton construction, which |
| 5 | +// would fail without a database. The concern under test receives its |
| 6 | +// prisma via the constructor, so the stub is never used by the code path. |
| 7 | +vi.mock("~/db.server", () => ({ prisma: {}, $replica: {} })); |
| 8 | + |
| 9 | +// The IdempotencyKeyConcern resolves the pre-gate claim through the |
| 10 | +// global mollifier buffer (`getMollifierBuffer`), shared by both |
| 11 | +// `claimOrAwait` and `findBufferedRunWithIdempotency`. Control it via a |
| 12 | +// hoisted handle so each test can script the claim/lookup responses. |
| 13 | +const h = vi.hoisted(() => ({ buffer: null as unknown })); |
| 14 | +vi.mock("~/v3/mollifier/mollifierBuffer.server", () => ({ |
| 15 | + getMollifierBuffer: () => h.buffer, |
| 16 | +})); |
| 17 | + |
| 18 | +import type { MollifierBuffer } from "@trigger.dev/redis-worker"; |
| 19 | +import { IdempotencyKeyConcern } from "~/runEngine/concerns/idempotencyKeys.server"; |
| 20 | +import type { TriggerTaskRequest } from "~/runEngine/types"; |
| 21 | + |
| 22 | +function makeConcern(prisma: { findFirst: () => Promise<unknown> }) { |
| 23 | + return new IdempotencyKeyConcern( |
| 24 | + { taskRun: { findFirst: prisma.findFirst } } as never, |
| 25 | + {} as never, // engine — unused on this path |
| 26 | + {} as never, // traceEventConcern — unused on this path |
| 27 | + ); |
| 28 | +} |
| 29 | + |
| 30 | +function makeRequest(): TriggerTaskRequest { |
| 31 | + return { |
| 32 | + taskId: "my-task", |
| 33 | + environment: { id: "env_a", organizationId: "org_1" }, |
| 34 | + options: {}, |
| 35 | + body: { options: { idempotencyKey: "k-1" } }, |
| 36 | + } as unknown as TriggerTaskRequest; |
| 37 | +} |
| 38 | + |
| 39 | +describe("IdempotencyKeyConcern · claim resolution", () => { |
| 40 | + it("resolved-but-unfindable falls through to a fresh trigger (no cached run, no claim held)", async () => { |
| 41 | + // The claim slot holds a runId that is gone from both stores: the PG |
| 42 | + // findFirst misses and the buffer lookup misses. Regression guard for |
| 43 | + // the resolved-but-unfindable terminal case — the concern must fall |
| 44 | + // through to a fresh trigger rather than throw, hand back a bogus |
| 45 | + // cached run, or claim ownership it doesn't hold. |
| 46 | + const lookupIdempotency = vi.fn(async () => null); |
| 47 | + h.buffer = { |
| 48 | + claimIdempotency: vi.fn(async () => ({ kind: "resolved", runId: "run_gone" })), |
| 49 | + lookupIdempotency, |
| 50 | + } as unknown as MollifierBuffer; |
| 51 | + |
| 52 | + const findFirst = vi.fn(async () => null); // PG misses on every call |
| 53 | + const concern = makeConcern({ findFirst }); |
| 54 | + |
| 55 | + const result = await concern.handleTriggerRequest(makeRequest(), undefined); |
| 56 | + |
| 57 | + expect(result.isCached).toBe(false); |
| 58 | + if (result.isCached === false) { |
| 59 | + // No claim held — we resolved someone else's (stale) claim, we did |
| 60 | + // not win one. The caller must NOT publish/release on our behalf. |
| 61 | + expect(result.claim).toBeUndefined(); |
| 62 | + expect(result.idempotencyKey).toBe("k-1"); |
| 63 | + } |
| 64 | + // We attempted the buffer fallback before giving up. |
| 65 | + expect(lookupIdempotency).toHaveBeenCalled(); |
| 66 | + }); |
| 67 | + |
| 68 | + it("resolved-and-findable returns the existing run as a cached hit", async () => { |
| 69 | + // Guard the happy resolved path: when the claimed runId IS findable |
| 70 | + // (writer-side PG), the fall-through change must not swallow it. |
| 71 | + h.buffer = { |
| 72 | + claimIdempotency: vi.fn(async () => ({ kind: "resolved", runId: "run_winner" })), |
| 73 | + lookupIdempotency: vi.fn(async () => null), |
| 74 | + } as unknown as MollifierBuffer; |
| 75 | + |
| 76 | + const winner = { id: "run_winner", friendlyId: "run_winner" }; |
| 77 | + // First findFirst (initial existingRun check) misses so we enter the |
| 78 | + // claim path; the second (writer-side re-resolve) finds the winner. |
| 79 | + let calls = 0; |
| 80 | + const findFirst = vi.fn(async () => { |
| 81 | + calls += 1; |
| 82 | + return calls >= 2 ? winner : null; |
| 83 | + }); |
| 84 | + const concern = makeConcern({ findFirst }); |
| 85 | + |
| 86 | + const result = await concern.handleTriggerRequest(makeRequest(), undefined); |
| 87 | + |
| 88 | + expect(result.isCached).toBe(true); |
| 89 | + if (result.isCached === true) { |
| 90 | + expect(result.run).toBe(winner); |
| 91 | + } |
| 92 | + }); |
| 93 | +}); |
0 commit comments