|
| 1 | +import { beforeEach, describe, expect, it, vi } from "vitest"; |
| 2 | + |
| 3 | +vi.mock("server-only", () => ({})); |
| 4 | + |
| 5 | +const mockLimit = vi.fn(); |
| 6 | +const mockWhere = vi.fn(); |
| 7 | +const mockFrom = vi.fn(); |
| 8 | +const mockSelect = vi.fn(); |
| 9 | + |
| 10 | +let selectCallCount = 0; |
| 11 | + |
| 12 | +vi.mock("~/server/db", () => ({ |
| 13 | + db: { |
| 14 | + select: (...args: unknown[]) => mockSelect(...args), |
| 15 | + }, |
| 16 | +})); |
| 17 | + |
| 18 | +describe("buildTransmittedPdfData", () => { |
| 19 | + beforeEach(() => { |
| 20 | + vi.resetAllMocks(); |
| 21 | + selectCallCount = 0; |
| 22 | + }); |
| 23 | + |
| 24 | + function setupMockDb( |
| 25 | + company: Record<string, unknown> | null, |
| 26 | + declaration: Record<string, unknown> | null, |
| 27 | + opinions: unknown[] = [], |
| 28 | + cseFiles: unknown[] = [], |
| 29 | + jointFiles: unknown[] = [], |
| 30 | + ) { |
| 31 | + // 5 select calls: |
| 32 | + // 1) company, 2) declaration (parallel Promise.all) |
| 33 | + // 3) opinions, 4) cseFiles, 5) jointFiles (parallel Promise.all) |
| 34 | + const results = [ |
| 35 | + company ? [company] : [], |
| 36 | + declaration ? [declaration] : [], |
| 37 | + opinions, |
| 38 | + cseFiles, |
| 39 | + jointFiles, |
| 40 | + ]; |
| 41 | + |
| 42 | + mockLimit.mockImplementation(() => { |
| 43 | + return Promise.resolve(results[selectCallCount - 1] ?? []); |
| 44 | + }); |
| 45 | + mockWhere.mockImplementation(() => { |
| 46 | + const res = results[selectCallCount - 1] ?? []; |
| 47 | + return Object.assign(Promise.resolve(res), { limit: mockLimit }); |
| 48 | + }); |
| 49 | + mockFrom.mockReturnValue({ where: mockWhere }); |
| 50 | + mockSelect.mockImplementation(() => { |
| 51 | + selectCallCount++; |
| 52 | + return { from: mockFrom }; |
| 53 | + }); |
| 54 | + } |
| 55 | + |
| 56 | + it("returns assembled PDF data with declaration lookup", async () => { |
| 57 | + setupMockDb( |
| 58 | + { name: "ACME Corp", siren: "123456789" }, |
| 59 | + { id: "decl-1" }, |
| 60 | + [ |
| 61 | + { |
| 62 | + declarationNumber: 1, |
| 63 | + type: "accuracy", |
| 64 | + opinion: "favorable", |
| 65 | + opinionDate: "2026-01-15", |
| 66 | + gapConsulted: null, |
| 67 | + }, |
| 68 | + ], |
| 69 | + [{ fileName: "avis.pdf", uploadedAt: new Date("2026-03-01") }], |
| 70 | + [{ fileName: "eval.pdf", uploadedAt: new Date("2026-03-02") }], |
| 71 | + ); |
| 72 | + |
| 73 | + const { buildTransmittedPdfData } = await import( |
| 74 | + "../buildTransmittedPdfData" |
| 75 | + ); |
| 76 | + const result = await buildTransmittedPdfData( |
| 77 | + "123456789", |
| 78 | + new Date("2026-03-15"), |
| 79 | + ); |
| 80 | + |
| 81 | + expect(result.companyName).toBe("ACME Corp"); |
| 82 | + expect(result.siren).toBe("123456789"); |
| 83 | + expect(result.opinions).toHaveLength(1); |
| 84 | + expect(result.cseFiles).toHaveLength(1); |
| 85 | + expect(result.jointEvaluationFile).not.toBeNull(); |
| 86 | + expect(result.jointEvaluationFile?.fileName).toBe("eval.pdf"); |
| 87 | + }); |
| 88 | + |
| 89 | + it("throws when company is not found", async () => { |
| 90 | + setupMockDb(null, { id: "decl-1" }); |
| 91 | + |
| 92 | + const { buildTransmittedPdfData } = await import( |
| 93 | + "../buildTransmittedPdfData" |
| 94 | + ); |
| 95 | + |
| 96 | + await expect( |
| 97 | + buildTransmittedPdfData("999999999", new Date()), |
| 98 | + ).rejects.toThrow("Entreprise introuvable"); |
| 99 | + }); |
| 100 | + |
| 101 | + it("throws when declaration is not found", async () => { |
| 102 | + setupMockDb({ name: "ACME", siren: "123456789" }, null); |
| 103 | + |
| 104 | + const { buildTransmittedPdfData } = await import( |
| 105 | + "../buildTransmittedPdfData" |
| 106 | + ); |
| 107 | + |
| 108 | + await expect( |
| 109 | + buildTransmittedPdfData("123456789", new Date()), |
| 110 | + ).rejects.toThrow("Déclaration introuvable"); |
| 111 | + }); |
| 112 | + |
| 113 | + it("returns null jointEvaluationFile when none exists", async () => { |
| 114 | + setupMockDb( |
| 115 | + { name: "ACME", siren: "123456789" }, |
| 116 | + { id: "decl-1" }, |
| 117 | + [], |
| 118 | + [], |
| 119 | + [], |
| 120 | + ); |
| 121 | + |
| 122 | + const { buildTransmittedPdfData } = await import( |
| 123 | + "../buildTransmittedPdfData" |
| 124 | + ); |
| 125 | + const result = await buildTransmittedPdfData( |
| 126 | + "123456789", |
| 127 | + new Date("2026-03-15"), |
| 128 | + ); |
| 129 | + |
| 130 | + expect(result.opinions).toEqual([]); |
| 131 | + expect(result.cseFiles).toEqual([]); |
| 132 | + expect(result.jointEvaluationFile).toBeNull(); |
| 133 | + }); |
| 134 | +}); |
0 commit comments