|
| 1 | +import { describe, expect, it } from "vitest"; |
| 2 | +import { Prisma } from "@trigger.dev/database"; |
| 3 | +import { DefaultQueueManager } from "../app/runEngine/concerns/queues.server.js"; |
| 4 | +import { ServiceValidationError } from "../app/v3/services/common.server.js"; |
| 5 | + |
| 6 | +// Minimal non-DEVELOPMENT environment so getWorkerQueue resolves a worker group |
| 7 | +// (DEVELOPMENT short-circuits before touching the DB). |
| 8 | +function productionEnv() { |
| 9 | + return { type: "PRODUCTION", projectId: "proj_test", id: "env_test" } as any; |
| 10 | +} |
| 11 | + |
| 12 | +describe("DefaultQueueManager.getWorkerQueue — writer DB error handling", () => { |
| 13 | + it("rethrows a Prisma connectivity error unchanged instead of wrapping it in a client-facing ServiceValidationError", async () => { |
| 14 | + // The exact production failure: getDefaultWorkerGroupForProject's writer |
| 15 | + // `project.findFirst` throws P1001 when the DB is unreachable. The raw |
| 16 | + // message carries the DB hostname and must NOT become a 422 with that text. |
| 17 | + const prisma = { |
| 18 | + project: { |
| 19 | + findFirst: async () => { |
| 20 | + throw new Prisma.PrismaClientKnownRequestError( |
| 21 | + "Invalid `prisma.project.findFirst()` invocation: Can't reach database server at host:5432", |
| 22 | + { code: "P1001", clientVersion: "6.14.0" } |
| 23 | + ); |
| 24 | + }, |
| 25 | + }, |
| 26 | + } as any; |
| 27 | + |
| 28 | + const queueManager = new DefaultQueueManager(prisma, {} as any); |
| 29 | + |
| 30 | + const result = await queueManager.getWorkerQueue(productionEnv()).then( |
| 31 | + () => ({ ok: true as const }), |
| 32 | + (error: unknown) => ({ ok: false as const, error }) |
| 33 | + ); |
| 34 | + |
| 35 | + expect(result.ok).toBe(false); |
| 36 | + if (!result.ok) { |
| 37 | + expect(result.error).toBeInstanceOf(Prisma.PrismaClientKnownRequestError); |
| 38 | + expect(result.error).not.toBeInstanceOf(ServiceValidationError); |
| 39 | + } |
| 40 | + }); |
| 41 | + |
| 42 | + it("still wraps a genuine domain failure (project not found) as a ServiceValidationError", async () => { |
| 43 | + const prisma = { |
| 44 | + project: { findFirst: async () => null }, |
| 45 | + } as any; |
| 46 | + |
| 47 | + const queueManager = new DefaultQueueManager(prisma, {} as any); |
| 48 | + |
| 49 | + await expect(queueManager.getWorkerQueue(productionEnv())).rejects.toBeInstanceOf( |
| 50 | + ServiceValidationError |
| 51 | + ); |
| 52 | + }); |
| 53 | +}); |
0 commit comments