|
| 1 | +import { describe, expect, it, vi } from "vitest"; |
| 2 | + |
| 3 | +vi.mock("~/db.server", () => ({ |
| 4 | + prisma: { taskRun: { findFirst: vi.fn(async () => null) } }, |
| 5 | + $replica: { taskRun: { findFirst: vi.fn(async () => null) } }, |
| 6 | +})); |
| 7 | + |
| 8 | +import { mutateWithFallback } from "~/v3/mollifier/mutateWithFallback.server"; |
| 9 | +import type { MollifierBuffer, MutateSnapshotResult } from "@trigger.dev/redis-worker"; |
| 10 | +import type { TaskRun } from "@trigger.dev/database"; |
| 11 | + |
| 12 | +type FindFirst = ReturnType<typeof vi.fn>; |
| 13 | +type PrismaStub = { taskRun: { findFirst: FindFirst } }; |
| 14 | + |
| 15 | +function fakePrisma(rows: Array<TaskRun | null>): PrismaStub { |
| 16 | + const fn = vi.fn(); |
| 17 | + for (const r of rows) fn.mockResolvedValueOnce(r); |
| 18 | + fn.mockResolvedValue(null); |
| 19 | + return { taskRun: { findFirst: fn } }; |
| 20 | +} |
| 21 | + |
| 22 | +function bufferReturning(result: MutateSnapshotResult): MollifierBuffer { |
| 23 | + return { |
| 24 | + mutateSnapshot: vi.fn(async () => result), |
| 25 | + } as unknown as MollifierBuffer; |
| 26 | +} |
| 27 | + |
| 28 | +const fakeRun = (overrides: Partial<TaskRun> = {}): TaskRun => |
| 29 | + ({ |
| 30 | + id: "pg_id", |
| 31 | + friendlyId: "run_1", |
| 32 | + runtimeEnvironmentId: "env_a", |
| 33 | + ...overrides, |
| 34 | + }) as TaskRun; |
| 35 | + |
| 36 | +const baseInput = { |
| 37 | + runId: "run_1", |
| 38 | + environmentId: "env_a", |
| 39 | + organizationId: "org_1", |
| 40 | + bufferPatch: { type: "append_tags" as const, tags: ["x"] }, |
| 41 | +}; |
| 42 | + |
| 43 | +describe("mutateWithFallback", () => { |
| 44 | + it("hits replica → calls pgMutation, returns pg outcome", async () => { |
| 45 | + const row = fakeRun(); |
| 46 | + const pgMutation = vi.fn(async () => "pg-response"); |
| 47 | + const synthesisedResponse = vi.fn(() => "snapshot-response"); |
| 48 | + |
| 49 | + const result = await mutateWithFallback({ |
| 50 | + ...baseInput, |
| 51 | + pgMutation, |
| 52 | + synthesisedResponse, |
| 53 | + prismaReplica: fakePrisma([row]) as unknown as typeof import("~/db.server").$replica, |
| 54 | + prismaWriter: fakePrisma([]) as unknown as typeof import("~/db.server").prisma, |
| 55 | + getBuffer: () => bufferReturning("applied_to_snapshot"), |
| 56 | + }); |
| 57 | + |
| 58 | + expect(result).toEqual({ kind: "pg", response: "pg-response" }); |
| 59 | + expect(pgMutation).toHaveBeenCalledWith(row); |
| 60 | + expect(synthesisedResponse).not.toHaveBeenCalled(); |
| 61 | + }); |
| 62 | + |
| 63 | + it("replica miss + buffer applied_to_snapshot → synthesisedResponse", async () => { |
| 64 | + const pgMutation = vi.fn(async () => "pg"); |
| 65 | + const result = await mutateWithFallback({ |
| 66 | + ...baseInput, |
| 67 | + pgMutation, |
| 68 | + synthesisedResponse: () => "snap", |
| 69 | + prismaReplica: fakePrisma([null]) as unknown as typeof import("~/db.server").$replica, |
| 70 | + prismaWriter: fakePrisma([]) as unknown as typeof import("~/db.server").prisma, |
| 71 | + getBuffer: () => bufferReturning("applied_to_snapshot"), |
| 72 | + }); |
| 73 | + expect(result).toEqual({ kind: "snapshot", response: "snap" }); |
| 74 | + expect(pgMutation).not.toHaveBeenCalled(); |
| 75 | + }); |
| 76 | + |
| 77 | + it("replica miss + buffer not_found + writer miss → not_found", async () => { |
| 78 | + const result = await mutateWithFallback({ |
| 79 | + ...baseInput, |
| 80 | + pgMutation: async () => "pg", |
| 81 | + synthesisedResponse: () => "snap", |
| 82 | + prismaReplica: fakePrisma([null]) as unknown as typeof import("~/db.server").$replica, |
| 83 | + prismaWriter: fakePrisma([null]) as unknown as typeof import("~/db.server").prisma, |
| 84 | + getBuffer: () => bufferReturning("not_found"), |
| 85 | + }); |
| 86 | + expect(result).toEqual({ kind: "not_found" }); |
| 87 | + }); |
| 88 | + |
| 89 | + it("replica miss + buffer not_found + writer hit → pgMutation (replica-lag recovery)", async () => { |
| 90 | + const row = fakeRun({ friendlyId: "run_1" }); |
| 91 | + const pgMutation = vi.fn(async () => "pg-recovered"); |
| 92 | + const result = await mutateWithFallback({ |
| 93 | + ...baseInput, |
| 94 | + pgMutation, |
| 95 | + synthesisedResponse: () => "snap", |
| 96 | + prismaReplica: fakePrisma([null]) as unknown as typeof import("~/db.server").$replica, |
| 97 | + prismaWriter: fakePrisma([row]) as unknown as typeof import("~/db.server").prisma, |
| 98 | + getBuffer: () => bufferReturning("not_found"), |
| 99 | + }); |
| 100 | + expect(result).toEqual({ kind: "pg", response: "pg-recovered" }); |
| 101 | + expect(pgMutation).toHaveBeenCalledWith(row); |
| 102 | + }); |
| 103 | + |
| 104 | + it("replica miss + buffer busy + writer resolves mid-wait → pgMutation", async () => { |
| 105 | + const row = fakeRun(); |
| 106 | + const pgMutation = vi.fn(async () => "pg-after-wait"); |
| 107 | + // Replica misses; writer misses twice, then hits. |
| 108 | + const writer = fakePrisma([null, null, row]); |
| 109 | + let nowValue = 0; |
| 110 | + const result = await mutateWithFallback({ |
| 111 | + ...baseInput, |
| 112 | + pgMutation, |
| 113 | + synthesisedResponse: () => "snap", |
| 114 | + prismaReplica: fakePrisma([null]) as unknown as typeof import("~/db.server").$replica, |
| 115 | + prismaWriter: writer as unknown as typeof import("~/db.server").prisma, |
| 116 | + getBuffer: () => bufferReturning("busy"), |
| 117 | + sleep: async () => { |
| 118 | + nowValue += 20; |
| 119 | + }, |
| 120 | + now: () => nowValue, |
| 121 | + safetyNetMs: 2000, |
| 122 | + pollStepMs: 20, |
| 123 | + pgTimeoutMs: 50, |
| 124 | + }); |
| 125 | + expect(result).toEqual({ kind: "pg", response: "pg-after-wait" }); |
| 126 | + expect(pgMutation).toHaveBeenCalledWith(row); |
| 127 | + // Writer should have been polled 3 times before the hit. |
| 128 | + expect(writer.taskRun.findFirst).toHaveBeenCalledTimes(3); |
| 129 | + }); |
| 130 | + |
| 131 | + it("replica miss + buffer busy + drainer never resolves → timed_out", async () => { |
| 132 | + let nowValue = 0; |
| 133 | + const result = await mutateWithFallback({ |
| 134 | + ...baseInput, |
| 135 | + pgMutation: async () => "pg", |
| 136 | + synthesisedResponse: () => "snap", |
| 137 | + prismaReplica: fakePrisma([null]) as unknown as typeof import("~/db.server").$replica, |
| 138 | + prismaWriter: fakePrisma([null, null, null, null, null]) as unknown as typeof import("~/db.server").prisma, |
| 139 | + getBuffer: () => bufferReturning("busy"), |
| 140 | + sleep: async () => { |
| 141 | + nowValue += 20; |
| 142 | + }, |
| 143 | + now: () => nowValue, |
| 144 | + safetyNetMs: 60, |
| 145 | + pollStepMs: 20, |
| 146 | + pgTimeoutMs: 5, |
| 147 | + }); |
| 148 | + expect(result).toEqual({ kind: "timed_out" }); |
| 149 | + }); |
| 150 | + |
| 151 | + it("abort signal during wait → timed_out without further polls", async () => { |
| 152 | + const writer = fakePrisma([null, null, null]); |
| 153 | + const controller = new AbortController(); |
| 154 | + let nowValue = 0; |
| 155 | + const result = await mutateWithFallback({ |
| 156 | + ...baseInput, |
| 157 | + pgMutation: async () => "pg", |
| 158 | + synthesisedResponse: () => "snap", |
| 159 | + prismaReplica: fakePrisma([null]) as unknown as typeof import("~/db.server").$replica, |
| 160 | + prismaWriter: writer as unknown as typeof import("~/db.server").prisma, |
| 161 | + getBuffer: () => bufferReturning("busy"), |
| 162 | + sleep: async () => { |
| 163 | + nowValue += 20; |
| 164 | + controller.abort(); |
| 165 | + }, |
| 166 | + now: () => nowValue, |
| 167 | + safetyNetMs: 2000, |
| 168 | + pollStepMs: 20, |
| 169 | + pgTimeoutMs: 5, |
| 170 | + abortSignal: controller.signal, |
| 171 | + }); |
| 172 | + expect(result).toEqual({ kind: "timed_out" }); |
| 173 | + // One poll happened before the sleep+abort. |
| 174 | + expect(writer.taskRun.findFirst).toHaveBeenCalledTimes(1); |
| 175 | + }); |
| 176 | + |
| 177 | + it("buffer is null (mollifier disabled) → not_found after replica miss", async () => { |
| 178 | + const result = await mutateWithFallback({ |
| 179 | + ...baseInput, |
| 180 | + pgMutation: async () => "pg", |
| 181 | + synthesisedResponse: () => "snap", |
| 182 | + prismaReplica: fakePrisma([null]) as unknown as typeof import("~/db.server").$replica, |
| 183 | + prismaWriter: fakePrisma([]) as unknown as typeof import("~/db.server").prisma, |
| 184 | + getBuffer: () => null, |
| 185 | + }); |
| 186 | + expect(result).toEqual({ kind: "not_found" }); |
| 187 | + }); |
| 188 | +}); |
0 commit comments