|
| 1 | +jest.mock("../malegislature", () => ({ |
| 2 | + getDocument: jest.fn(), |
| 3 | + getDocumentPdf: jest.fn() |
| 4 | +})) |
| 5 | +jest.mock("./pdfText", () => ({ |
| 6 | + extractBillTextFromPdf: jest.fn() |
| 7 | +})) |
| 8 | + |
| 9 | +import { getDocumentWithPdfTextFallback } from "./documentTextFallback" |
| 10 | +import { extractBillTextFromPdf } from "./pdfText" |
| 11 | + |
| 12 | +const mockedApi = jest.requireMock("../malegislature") as { |
| 13 | + getDocument: jest.Mock |
| 14 | + getDocumentPdf: jest.Mock |
| 15 | +} |
| 16 | +const mockedExtractBillTextFromPdf = |
| 17 | + extractBillTextFromPdf as jest.MockedFunction<typeof extractBillTextFromPdf> |
| 18 | + |
| 19 | +describe("getDocumentWithPdfTextFallback", () => { |
| 20 | + beforeEach(() => { |
| 21 | + jest.resetAllMocks() |
| 22 | + }) |
| 23 | + |
| 24 | + it("does not fetch a PDF when API text is present", async () => { |
| 25 | + mockedApi.getDocument.mockResolvedValue({ DocumentText: "API text" }) |
| 26 | + |
| 27 | + await expect( |
| 28 | + getDocumentWithPdfTextFallback(194, "H1") |
| 29 | + ).resolves.toMatchObject({ |
| 30 | + content: { DocumentText: "API text" }, |
| 31 | + documentTextSource: "api" |
| 32 | + }) |
| 33 | + expect(mockedApi.getDocumentPdf).not.toHaveBeenCalled() |
| 34 | + }) |
| 35 | + |
| 36 | + it("sets DocumentText when PDF extraction succeeds", async () => { |
| 37 | + mockedApi.getDocument.mockResolvedValue({ DocumentText: null }) |
| 38 | + mockedApi.getDocumentPdf.mockResolvedValue(Buffer.from("pdf")) |
| 39 | + mockedExtractBillTextFromPdf.mockResolvedValue({ |
| 40 | + status: "extracted", |
| 41 | + text: "PDF text", |
| 42 | + pageCount: 1, |
| 43 | + charCount: 7 |
| 44 | + }) |
| 45 | + |
| 46 | + await expect( |
| 47 | + getDocumentWithPdfTextFallback(194, "H1") |
| 48 | + ).resolves.toMatchObject({ |
| 49 | + content: { DocumentText: "PDF text" }, |
| 50 | + documentTextSource: "pdf", |
| 51 | + pdfTextExtraction: { status: "extracted" } |
| 52 | + }) |
| 53 | + }) |
| 54 | + |
| 55 | + it("leaves DocumentText absent when PDF has no text", async () => { |
| 56 | + mockedApi.getDocument.mockResolvedValue({ DocumentText: null }) |
| 57 | + mockedApi.getDocumentPdf.mockResolvedValue(Buffer.from("pdf")) |
| 58 | + mockedExtractBillTextFromPdf.mockResolvedValue({ |
| 59 | + status: "no-text", |
| 60 | + pageCount: 1, |
| 61 | + charCount: 0 |
| 62 | + }) |
| 63 | + |
| 64 | + const result = await getDocumentWithPdfTextFallback(194, "H18") |
| 65 | + |
| 66 | + expect(result.content).not.toHaveProperty("DocumentText") |
| 67 | + expect(result.pdfTextExtraction).toMatchObject({ status: "no-text" }) |
| 68 | + }) |
| 69 | + |
| 70 | + it("leaves DocumentText absent when PDF fetch fails", async () => { |
| 71 | + mockedApi.getDocument.mockResolvedValue({ DocumentText: null }) |
| 72 | + mockedApi.getDocumentPdf.mockRejectedValue(new Error("not found")) |
| 73 | + |
| 74 | + const result = await getDocumentWithPdfTextFallback(194, "H18") |
| 75 | + |
| 76 | + expect(result.content).not.toHaveProperty("DocumentText") |
| 77 | + expect(result.pdfTextExtraction).toMatchObject({ |
| 78 | + status: "fetch-error", |
| 79 | + error: "not found" |
| 80 | + }) |
| 81 | + }) |
| 82 | +}) |
0 commit comments