import { describe, it, expect, vi, beforeEach, afterEach } from "vitest"; import os from "os"; import path from "path"; import fs from "fs"; import { DocumentCaptureService } from "@/src/services/document-capture-service"; import { DocumentExtractionService } from "@/src/services/document-extraction-service"; import { LocalBlobStore } from "@/src/adapters/local-blob-store"; function jsonResponse(body: unknown, opts: { status?: number; headers?: Record } = {}) { return { ok: (opts.status ?? 200) < 400, status: opts.status ?? 200, headers: { get: (k: string) => (opts.headers ?? {})[k] ?? null }, json: async () => body, text: async () => JSON.stringify(body), } as unknown as Response; } describe("DocumentExtractionService — evidence-based extraction", () => { let tmpDir: string; let aud0Calls: { url: string; body: Record }[]; beforeEach(() => { tmpDir = fs.mkdtempSync(path.join(os.tmpdir(), "0pt-blob-")); aud0Calls = []; }); afterEach(() => { fs.rmSync(tmpDir, { recursive: true, force: true }); vi.restoreAllMocks(); }); async function seedCapturedDocument(captureService: DocumentCaptureService) { const pdfBytes = Buffer.from("%PDF-1.4 fake invoice pdf"); const fetchMock = vi.fn(async (url: string) => { if (url.includes("login.microsoftonline.com")) return jsonResponse({ access_token: "tok", expires_in: 3600 }); if (url.includes("/messages?")) { return jsonResponse({ value: [{ id: "msg-1", receivedDateTime: "2026-07-01T09:00:00Z", hasAttachments: true, from: { emailAddress: {} }, toRecipients: [] }], }); } if (url.includes("/attachments")) { return jsonResponse({ value: [ { "@odata.type": "#microsoft.graph.fileAttachment", id: "att-1", name: "doc.pdf", contentType: "application/pdf", contentBytes: pdfBytes.toString("base64"), }, ], }); } throw new Error(`unexpected fetch during seed: ${url}`); }); vi.stubGlobal("fetch", fetchMock); const result = await captureService.captureFromMailbox( { mailbox: { tenantId: "t", clientId: "c", clientSecret: "s", mailboxAddress: "docs@customer.com" } }, { tenant_id: "tenant1", project_id: "proj1", actor_id: "actor1", correlation_id: "corr-seed" }, ); return result.captured_documents[0]; } it("never fabricates a value for a field Document Intelligence did not return, and preserves DI's confidence either way", async () => { const captureService = new DocumentCaptureService({ blobStore: new LocalBlobStore(tmpDir) }); const document = await seedCapturedDocument(captureService); const extractionService = new DocumentExtractionService(captureService, { blobStore: new LocalBlobStore(tmpDir), aud0BaseUrl: "https://aud0.example.com", documentIntelligence: { endpoint: "https://di.example.com", apiKey: "key123", pollIntervalMs: 1 }, }); const fetchMock = vi.fn(async (url: string, init?: RequestInit) => { if (url.includes(":analyze")) { return jsonResponse({}, { status: 202, headers: { "operation-location": "https://di.example.com/op/abc" } }); } if (url.includes("/op/abc")) { return jsonResponse({ status: "succeeded", analyzeResult: { content: "Acme AB\nTotal: 1234.50 SEK", pages: [{}], documents: [ { confidence: 0.9, fields: { VendorName: { type: "string", valueString: "Acme AB", confidence: 0.97 }, InvoiceTotal: { type: "currency", valueCurrency: { amount: 1234.5 }, confidence: 0.92 }, // DueDate was detected as a field region but DI could not read a value — // this is exactly the case that must NOT be fabricated into a fake date. DueDate: { type: "date", confidence: 0.31 }, // CustomerAddress is not present in the DI response at all. }, }, ], }, }); } if (url.includes("/v1/aud0/events") || url.includes("/v1/aud0/openlineage")) { aud0Calls.push({ url, body: init?.body ? JSON.parse(init.body as string) : null }); return jsonResponse({}); } throw new Error(`unexpected fetch during extraction: ${url}`); }); vi.stubGlobal("fetch", fetchMock); const extraction = await extractionService.extractDocument(document.document_id, { tenant_id: "tenant1", project_id: "proj1", actor_id: "actor1", correlation_id: "corr-extract", model_id: "prebuilt-invoice", }); expect(extraction.status).toBe("completed"); // Found fields carry the real value + DI's own confidence. expect(extraction.fields.VendorName).toEqual({ value: "Acme AB", confidence: 0.97, found: true }); expect(extraction.fields.InvoiceTotal).toEqual({ value: 1234.5, confidence: 0.92, found: true }); // Not-found field: value is null (never fabricated); confidence is whatever DI reported. expect(extraction.fields.DueDate).toEqual({ value: null, confidence: 0.31, found: false }); // A field DI never mentioned at all is simply absent — also never fabricated into existence. expect(extraction.fields.CustomerAddress).toBeUndefined(); expect(extraction.fields_total_count).toBe(3); expect(extraction.fields_found_count).toBe(2); // Average confidence is computed only over the found fields — DueDate's 0.31 excluded. expect(extraction.average_confidence).toBeCloseTo((0.97 + 0.92) / 2, 5); expect(extraction.document_confidence).toBe(0.9); expect(extraction.st0re_artifact_id).toContain("mock_artifact_"); expect(extraction.model_id).toBe("prebuilt-invoice"); const updatedDoc = await captureService.getDocument(document.document_id); expect(updatedDoc.status).toBe("extracted"); const extractedEvent = aud0Calls.find((c) => c.body?.event_type === "0pt.document_extracted") as { url: string; body: { event_type: string; capability_id: string; payload: { fields_found_count: number; fields_total_count: number } } } | undefined; expect(extractedEvent).toBeTruthy(); expect(extractedEvent!.body.capability_id).toBe("0pt.extract_document"); expect(extractedEvent!.body.payload.fields_found_count).toBe(2); expect(extractedEvent!.body.payload.fields_total_count).toBe(3); }); it("marks the extraction failed — never returns a fabricated success — when Document Intelligence errors", async () => { const captureService = new DocumentCaptureService({ blobStore: new LocalBlobStore(tmpDir) }); const document = await seedCapturedDocument(captureService); const extractionService = new DocumentExtractionService(captureService, { blobStore: new LocalBlobStore(tmpDir), documentIntelligence: { endpoint: "https://di.example.com", apiKey: "key123", pollIntervalMs: 1 }, }); const fetchMock = vi.fn(async (url: string) => { if (url.includes(":analyze")) return jsonResponse({}, { status: 500 }); throw new Error(`unexpected fetch: ${url}`); }); vi.stubGlobal("fetch", fetchMock); await expect( extractionService.extractDocument(document.document_id, { tenant_id: "tenant1", project_id: "proj1", actor_id: "actor1", correlation_id: "corr-fail", model_id: "prebuilt-invoice", }), ).rejects.toThrow(); const updatedDoc = await captureService.getDocument(document.document_id); expect(updatedDoc.status).toBe("extraction_failed"); }); });