import { describe, expect, it, vi } from "vitest"; import { attachPrismaTelemetry, noopPrismaTelemetryRecorder, type PrismaExtendableClient, type PrismaTelemetryEvent, } from "./telemetry.server.js"; type CapturedExtension = Parameters[0]; function makeStubClient() { let captured: CapturedExtension | undefined; const stub: PrismaExtendableClient = { $extends(extension) { captured = extension; return { __extended: true }; }, }; function extension(): CapturedExtension { if (!captured) throw new Error("$extends was never called"); return captured; } return { stub, extension }; } describe("attachPrismaTelemetry", () => { it("records a successful query with model, operation, client kind and duration", async () => { const events: PrismaTelemetryEvent[] = []; const { stub, extension } = makeStubClient(); const returned = attachPrismaTelemetry(stub, { client: "read", record: (event) => events.push(event), }); expect(returned).toEqual({ __extended: true }); expect(extension().name).toBe("rw-split-telemetry-read"); const handler = extension().query.$allModels.$allOperations; const result = await handler({ model: "User", operation: "findMany", args: { where: { id: "u1" } }, query: async (passed) => ({ echoed: passed }), }); expect(result).toEqual({ echoed: { where: { id: "u1" } } }); expect(events).toHaveLength(1); expect(events[0]).toMatchObject({ client: "read", model: "User", operation: "findMany", ok: true, }); expect(events[0]!.durationMs).toBeGreaterThanOrEqual(0); }); it("records a failed query and re-throws the underlying error unchanged", async () => { const events: PrismaTelemetryEvent[] = []; const { stub, extension } = makeStubClient(); attachPrismaTelemetry(stub, { client: "write", record: (event) => events.push(event) }); const handler = extension().query.$allModels.$allOperations; const boom = new Error("db down"); await expect( handler({ model: "OutboxEvent", operation: "create", args: { data: {} }, query: async () => { throw boom; }, }) ).rejects.toBe(boom); expect(events).toHaveLength(1); expect(events[0]).toMatchObject({ client: "write", model: "OutboxEvent", operation: "create", ok: false, }); }); it("falls back to model='$none' for raw operations that have no model context", async () => { const events: PrismaTelemetryEvent[] = []; const { stub, extension } = makeStubClient(); attachPrismaTelemetry(stub, { client: "read", record: (event) => events.push(event) }); await extension().query.$allModels.$allOperations({ model: undefined, operation: "$queryRaw", args: ["SELECT 1"], query: async () => [{ "?column?": 1 }], }); expect(events[0]).toMatchObject({ client: "read", model: "$none", operation: "$queryRaw", ok: true, }); }); it("invokes the recorder once per query and uses the provided client kind label", async () => { const record = vi.fn(); const { stub, extension } = makeStubClient(); attachPrismaTelemetry(stub, { client: "write", record }); const handler = extension().query.$allModels.$allOperations; await handler({ model: "Tenant", operation: "findUnique", args: {}, query: async () => null }); await handler({ model: "Tenant", operation: "update", args: {}, query: async () => null }); expect(record).toHaveBeenCalledTimes(2); expect(record.mock.calls.every((call) => (call[0] as PrismaTelemetryEvent).client === "write")).toBe(true); }); it("exposes a noop recorder for call-sites without a metrics sink", () => { expect(() => noopPrismaTelemetryRecorder({ client: "read", model: "X", operation: "y", durationMs: 1, ok: true })).not.toThrow(); }); });