|
| 1 | +import { test } from "node:test"; |
| 2 | +import assert from "node:assert/strict"; |
| 3 | +import { runIntake, type Extractor } from "./intake"; |
| 4 | +import type { Invoice } from "./schema"; |
| 5 | + |
| 6 | +/** |
| 7 | + * Intake tests — the "mock vision" coverage. The real extraction calls the |
| 8 | + * Anthropic vision API; here we inject a mock extractor so the whole intake path |
| 9 | + * (render → extract → reconcile-with-record / fail) is exercised offline. |
| 10 | + */ |
| 11 | + |
| 12 | +const SOURCE: Invoice = { |
| 13 | + invoiceNumber: "INV-9001", |
| 14 | + poNumber: "PO-9001", |
| 15 | + vendor: "Acme Corp", |
| 16 | + issueDate: "2026-05-01", |
| 17 | + currency: "USD", |
| 18 | + lineItems: [ |
| 19 | + { sku: "A-1", description: "Widget", qty: 2, unitPrice: 10, amount: 20 }, |
| 20 | + ], |
| 21 | + subtotal: 20, |
| 22 | + tax: null, |
| 23 | + total: 20, |
| 24 | +}; |
| 25 | + |
| 26 | +// A render stub so no PDF is actually generated. |
| 27 | +const render = async () => "ZmFrZS1wZGY="; |
| 28 | + |
| 29 | +/** An extractor that returns a given invoice (or failure). */ |
| 30 | +function mockExtractor(invoice: Invoice): Extractor { |
| 31 | + return async () => ({ ok: true, invoice }); |
| 32 | +} |
| 33 | + |
| 34 | +test("successful extraction returns the extracted invoice", async () => { |
| 35 | + const extracted: Invoice = { ...SOURCE, vendor: "Acme Corporation Ltd" }; |
| 36 | + const res = await runIntake(SOURCE, { |
| 37 | + extract: mockExtractor(extracted), |
| 38 | + render, |
| 39 | + }); |
| 40 | + assert.equal(res.ok, true); |
| 41 | + if (res.ok) { |
| 42 | + assert.equal(res.invoice.vendor, "Acme Corporation Ltd"); |
| 43 | + // Header reconciles (invoiceNumber + poNumber + total all match the record). |
| 44 | + assert.equal(res.matchesRecord, true); |
| 45 | + } |
| 46 | +}); |
| 47 | + |
| 48 | +test("extracted header differing from the record → matchesRecord false", async () => { |
| 49 | + const extracted: Invoice = { ...SOURCE, total: 999 }; |
| 50 | + const res = await runIntake(SOURCE, { |
| 51 | + extract: mockExtractor(extracted), |
| 52 | + render, |
| 53 | + }); |
| 54 | + assert.equal(res.ok, true); |
| 55 | + if (res.ok) assert.equal(res.matchesRecord, false); |
| 56 | +}); |
| 57 | + |
| 58 | +test("the pipeline runs on the EXTRACTED data, not the source", async () => { |
| 59 | + // The model reads a different unit price than the record — runIntake must |
| 60 | + // return the model's number (that's the whole point: data comes from the read). |
| 61 | + const extracted: Invoice = { |
| 62 | + ...SOURCE, |
| 63 | + lineItems: [{ ...SOURCE.lineItems[0]!, unitPrice: 11, amount: 22 }], |
| 64 | + subtotal: 22, |
| 65 | + total: 22, |
| 66 | + }; |
| 67 | + const res = await runIntake(SOURCE, { |
| 68 | + extract: mockExtractor(extracted), |
| 69 | + render, |
| 70 | + }); |
| 71 | + assert.equal(res.ok, true); |
| 72 | + if (res.ok) assert.equal(res.invoice.lineItems[0]?.unitPrice, 11); |
| 73 | +}); |
| 74 | + |
| 75 | +test("a validation failure surfaces as a failure (no fabricated data)", async () => { |
| 76 | + const res = await runIntake(SOURCE, { |
| 77 | + extract: async () => ({ |
| 78 | + ok: false, |
| 79 | + kind: "validation", |
| 80 | + issues: ["total: expected a number"], |
| 81 | + }), |
| 82 | + render, |
| 83 | + }); |
| 84 | + assert.equal(res.ok, false); |
| 85 | + if (!res.ok) assert.match(res.reason, /validation/i); |
| 86 | +}); |
| 87 | + |
| 88 | +test("a refusal surfaces as a failure", async () => { |
| 89 | + const res = await runIntake(SOURCE, { |
| 90 | + extract: async () => ({ ok: false, kind: "refusal" }), |
| 91 | + render, |
| 92 | + }); |
| 93 | + assert.equal(res.ok, false); |
| 94 | +}); |
| 95 | + |
| 96 | +test("a timeout surfaces as a failure (does not hang)", async () => { |
| 97 | + const res = await runIntake(SOURCE, { |
| 98 | + // Never resolves → the internal timeout must win. |
| 99 | + extract: () => new Promise(() => {}), |
| 100 | + render, |
| 101 | + timeoutMs: 20, |
| 102 | + }); |
| 103 | + assert.equal(res.ok, false); |
| 104 | +}); |
| 105 | + |
| 106 | +test("a thrown extractor is caught, not propagated", async () => { |
| 107 | + const res = await runIntake(SOURCE, { |
| 108 | + extract: async () => { |
| 109 | + throw new Error("network down"); |
| 110 | + }, |
| 111 | + render, |
| 112 | + }); |
| 113 | + assert.equal(res.ok, false); |
| 114 | +}); |
0 commit comments