mirror of
https://github.com/MBombeck/HealthLog.git
synced 2026-11-19 08:12:52 +00:00
On a host whose CPU cannot run the page renderer (x64 without AVX2, arm64 musl without LSE/RCpc), every AI read of a PDF for a provider that only takes images failed with "Scanning PDFs needs a Claude vision provider", which says nothing about the cause. Most medical PDFs are produced digitally and carry every word in their text layer, which pdf.js reads without rendering. `prepareVisionInput` now answers a text read (`mode: "text"`) when the renderer is unavailable on this host, the provider cannot take the PDF whole and the PDF has a usable text layer. Summary, suggest and the background summary send that text through their existing text mode; "Read with AI" and the index backfill write it as the local text index it is, with no provider call; the automatic index leaves it to its local path. `planVisionReads` does the same for the extraction and the lab scan: text reads by page group, with anchored provenance, and the pages without a text layer reported as `extractionIncomplete` on a document and as `unreadPages` on a lab scan. A supplement label PDF is read from its text layer too. A scan on such a host now answers `documents.inbound.pdfRendererUnavailable` and `labs.ocr.pdfRendererUnavailable` (422): this server's processor cannot run the page renderer, so a scanned PDF can only be read by a provider that takes PDF files directly, or as photos. The `pdfNeedsAnthropic` codes are no longer emitted, since with a working renderer every provider reads a PDF; the recorded index outcome keeps its `pdf-needs-anthropic` value with corrected copy. The extraction path no longer folds a render failure of one document into the capability error: like `prepareVisionInput` since #776 it reports "The PDF pages couldn't be rendered for scanning." (`extractFailed`), and the lab scan answers `labs.ocr.extractFailed`. Hosts that render, and providers that take PDFs whole, are unchanged. Refs #1311
821 lines
25 KiB
TypeScript
821 lines
25 KiB
TypeScript
/**
|
|
* The lab scan read the background worker runs (v1.40), moved out of the
|
|
* route with the guarantees the route's own tests used to pin: the budget is
|
|
* settled against what the provider reported, a PDF is rendered and read a
|
|
* page at a time, a long PDF says which pages were read, the reads fit the
|
|
* run's deadline, and a provider failure is named as one without the upstream
|
|
* body reaching the stored failure or the event stream.
|
|
*/
|
|
import { beforeEach, describe, expect, it, vi } from "vitest";
|
|
import { jsPDF } from "jspdf";
|
|
|
|
vi.mock("@/lib/logging/context", () => ({ annotate: vi.fn() }));
|
|
vi.mock("@/lib/documents/rasterize-pdf", async (importOriginal) => ({
|
|
...(await importOriginal<typeof import("@/lib/documents/rasterize-pdf")>()),
|
|
rasterizePdf: vi.fn(),
|
|
}));
|
|
vi.mock("@/lib/ai/coach/budget", () => ({
|
|
reconcileSpend: vi.fn().mockResolvedValue(undefined),
|
|
reserveBudget: vi.fn(),
|
|
OPERATOR_COST_CAP: 200_000,
|
|
USER_PLAN_CAP: 2_000_000,
|
|
}));
|
|
vi.mock("@/lib/labs/ocr-extract", async () => {
|
|
const actual = await vi.importActual<typeof import("@/lib/labs/ocr-extract")>(
|
|
"@/lib/labs/ocr-extract",
|
|
);
|
|
return { ...actual, runOcrExtraction: vi.fn() };
|
|
});
|
|
|
|
import { reconcileSpend, reserveBudget } from "@/lib/ai/coach/budget";
|
|
import { rasterizePdf } from "@/lib/documents/rasterize-pdf";
|
|
import {
|
|
OCR_MAX_PAGES,
|
|
OCR_TEXT_READ_CHARS,
|
|
OCR_TEXT_READ_MAX_CHARS,
|
|
planPageReads,
|
|
splitOcrText,
|
|
} from "@/lib/documents/ocr-plan";
|
|
import { OcrExtractError } from "@/lib/documents/ocr-read";
|
|
import { runOcrExtraction } from "@/lib/labs/ocr-extract";
|
|
import type { RunOcrExtractionArgs } from "@/lib/labs/ocr-extract";
|
|
import { executeOcrExtraction } from "@/lib/labs/ocr-run";
|
|
import { AiUnavailableError } from "@/lib/ai/capabilities/refusal";
|
|
import { annotate } from "@/lib/logging/context";
|
|
|
|
const budget = {
|
|
reserved: 1500,
|
|
owner: "operator" as const,
|
|
dateKey: "2027-01-15",
|
|
};
|
|
const textPick = {
|
|
entry: { providerType: "openai", instance: {} as never },
|
|
providerType: "openai",
|
|
} as never;
|
|
const codexPick = {
|
|
entry: { providerType: "codex", instance: {} as never },
|
|
providerType: "codex",
|
|
pdfSupported: false,
|
|
} as never;
|
|
const anthropicPick = {
|
|
entry: { providerType: "anthropic", instance: {} as never },
|
|
providerType: "anthropic",
|
|
pdfSupported: true,
|
|
} as never;
|
|
|
|
const text = (t = "Hb 14") => ({
|
|
mode: "text" as const,
|
|
text: t,
|
|
pick: textPick,
|
|
});
|
|
const pdf = (pick: never = codexPick) => ({
|
|
mode: "vision" as const,
|
|
bytes: Buffer.from("%PDF-1.4"),
|
|
mime: "application/pdf" as const,
|
|
pick,
|
|
});
|
|
const pages = (n: number) =>
|
|
Array.from({ length: n }, (_, i) => ({
|
|
mediaType: "image/jpeg" as const,
|
|
dataBase64: `page${i + 1}`,
|
|
}));
|
|
/** A read that spends `spent` tokens (of them `cached` from the cache). */
|
|
const extraction =
|
|
(spent = 0, cached = 0) =>
|
|
async (args: RunOcrExtractionArgs) => {
|
|
args.meter!.spent += spent;
|
|
args.meter!.cached += cached;
|
|
return {
|
|
data: { rows: [] } as never,
|
|
spentTokens: spent,
|
|
cachedTokens: cached,
|
|
};
|
|
};
|
|
const schedule = (maxModelCalls: number) => ({
|
|
maxModelCalls: () => maxModelCalls,
|
|
arm: vi.fn().mockResolvedValue(undefined),
|
|
});
|
|
/** A run whose deadline leaves room for every read. */
|
|
const noDeadline = () => schedule(Number.POSITIVE_INFINITY);
|
|
|
|
beforeEach(() => {
|
|
vi.clearAllMocks();
|
|
});
|
|
|
|
describe("planning the reads", () => {
|
|
it("reads one page per call when the deadline allows it", () => {
|
|
expect(planPageReads(3, Number.POSITIVE_INFINITY)).toEqual([[1], [2], [3]]);
|
|
});
|
|
|
|
it("shares the pages out when the deadline allows fewer reads", () => {
|
|
expect(planPageReads(10, 4)).toEqual([
|
|
[1, 2, 3],
|
|
[4, 5, 6],
|
|
[7, 8, 9],
|
|
[10],
|
|
]);
|
|
expect(planPageReads(5, 1)).toEqual([[1, 2, 3, 4, 5]]);
|
|
expect(planPageReads(5, 0)).toEqual([[1, 2, 3, 4, 5]]);
|
|
});
|
|
|
|
it("keeps a photo's text as one read", () => {
|
|
expect(splitOcrText("Hb 14\nMCV 90", Number.POSITIVE_INFINITY)).toEqual([
|
|
"Hb 14\nMCV 90",
|
|
]);
|
|
});
|
|
|
|
it("splits long text at line boundaries, every line kept once", () => {
|
|
const lines = Array.from(
|
|
{ length: 400 },
|
|
(_, i) => `Marker ${i} 1,5 mg/dl`,
|
|
);
|
|
const whole = lines.join("\n");
|
|
const parts = splitOcrText(whole, Number.POSITIVE_INFINITY);
|
|
expect(parts.length).toBeGreaterThan(1);
|
|
for (const part of parts) {
|
|
expect(part.length).toBeLessThanOrEqual(OCR_TEXT_READ_CHARS);
|
|
}
|
|
expect(parts.join("\n")).toBe(whole);
|
|
});
|
|
|
|
it("never splits text into more reads than the deadline allows", () => {
|
|
const whole = Array.from({ length: 400 }, (_, i) => `Marker ${i}`).join(
|
|
"\n",
|
|
);
|
|
const parts = splitOcrText(whole, 2);
|
|
expect(parts).toHaveLength(2);
|
|
expect(parts.join("\n")).toBe(whole);
|
|
});
|
|
|
|
it("folds the remainder in when lines do not share out evenly", () => {
|
|
// Seven long lines into three reads: packing two lines a read leaves a
|
|
// fourth read, which is folded into the third.
|
|
const whole = Array.from({ length: 7 }, (_, i) =>
|
|
String(i).repeat(5000),
|
|
).join("\n");
|
|
const parts = splitOcrText(whole, 3);
|
|
expect(parts).toHaveLength(3);
|
|
expect(parts.join("\n")).toBe(whole);
|
|
});
|
|
});
|
|
|
|
describe("a scan is always planned against a schedule", () => {
|
|
it("cannot be started without one", () => {
|
|
// The scan only runs in the worker, which hands it the run's deadline.
|
|
// A caller with no schedule would read a twenty-page report as forty
|
|
// model calls, so the signature leaves no such call to write.
|
|
const start = () =>
|
|
// @ts-expect-error -- `schedule` is required
|
|
executeOcrExtraction({ userId: "u1", input: text(), budget });
|
|
expect(typeof start).toBe("function");
|
|
});
|
|
});
|
|
|
|
describe("text mode", () => {
|
|
it("charges what the provider reported to the provider that served", async () => {
|
|
vi.mocked(runOcrExtraction).mockImplementation(extraction(2345));
|
|
const out = await executeOcrExtraction({
|
|
userId: "u1",
|
|
input: text(),
|
|
budget,
|
|
schedule: noDeadline(),
|
|
});
|
|
expect(out).toEqual({ ok: true, data: { rows: [] } });
|
|
expect(reconcileSpend).toHaveBeenCalledWith(
|
|
"u1",
|
|
1500,
|
|
2345,
|
|
"2027-01-15",
|
|
0,
|
|
{ servedBy: "openai", reservedOwner: "operator" },
|
|
);
|
|
});
|
|
|
|
it("charges the reservation when the provider reported nothing", async () => {
|
|
vi.mocked(runOcrExtraction).mockImplementation(extraction(0));
|
|
await executeOcrExtraction({
|
|
userId: "u1",
|
|
input: text(),
|
|
budget,
|
|
schedule: noDeadline(),
|
|
});
|
|
expect(reconcileSpend).toHaveBeenCalledWith(
|
|
"u1",
|
|
1500,
|
|
1500,
|
|
"2027-01-15",
|
|
0,
|
|
{ servedBy: "openai", reservedOwner: "operator" },
|
|
);
|
|
});
|
|
|
|
it("sends long text in parts and moves the deadline to cover them", async () => {
|
|
vi.mocked(runOcrExtraction).mockImplementation(extraction());
|
|
// ~8.8k characters: three reads of at most OCR_TEXT_READ_CHARS.
|
|
const long = Array.from(
|
|
{ length: 400 },
|
|
(_, i) => `Marker ${i} 1,5 mg/dl`,
|
|
).join("\n");
|
|
const s = schedule(20);
|
|
await executeOcrExtraction({
|
|
userId: "u1",
|
|
input: text(long),
|
|
budget,
|
|
schedule: s,
|
|
});
|
|
const chunks = vi.mocked(runOcrExtraction).mock.calls[0][0].chunks;
|
|
expect(chunks).toHaveLength(3);
|
|
expect(chunks.every((c) => c.pages.length === 0)).toBe(true);
|
|
expect(s.arm).toHaveBeenCalledWith(6);
|
|
});
|
|
|
|
it("passes the cached share of what was spent to the ledger", async () => {
|
|
vi.mocked(runOcrExtraction).mockImplementation(extraction(3000, 1200));
|
|
await executeOcrExtraction({
|
|
userId: "u1",
|
|
input: text(),
|
|
budget,
|
|
schedule: noDeadline(),
|
|
});
|
|
expect(reconcileSpend).toHaveBeenCalledWith(
|
|
"u1",
|
|
1500,
|
|
3000,
|
|
"2027-01-15",
|
|
1200,
|
|
{ servedBy: "openai", reservedOwner: "operator" },
|
|
);
|
|
});
|
|
|
|
it("reserves every read after the first, and settles against all of it", async () => {
|
|
vi.mocked(reserveBudget).mockResolvedValue({
|
|
allowed: true,
|
|
reserved: 8000,
|
|
} as never);
|
|
vi.mocked(runOcrExtraction).mockImplementation(async (args) => {
|
|
expect(await args.reserveRead!()).toBe(true);
|
|
expect(await args.reserveRead!()).toBe(true);
|
|
return extraction(20_000)(args);
|
|
});
|
|
await executeOcrExtraction({
|
|
userId: "u1",
|
|
input: text(),
|
|
budget,
|
|
schedule: noDeadline(),
|
|
});
|
|
expect(reserveBudget).toHaveBeenCalledWith(
|
|
"u1",
|
|
8000,
|
|
"2027-01-15",
|
|
200_000,
|
|
"operator",
|
|
"coach",
|
|
undefined,
|
|
{ countMessage: false },
|
|
);
|
|
expect(reconcileSpend).toHaveBeenCalledWith(
|
|
"u1",
|
|
1500 + 2 * 8000,
|
|
20_000,
|
|
"2027-01-15",
|
|
0,
|
|
{ servedBy: "openai", reservedOwner: "operator" },
|
|
);
|
|
});
|
|
|
|
it("judges the next read by the owner's cap, the one the route judged the first by", async () => {
|
|
vi.mocked(reserveBudget).mockResolvedValue({
|
|
allowed: true,
|
|
reserved: 8000,
|
|
} as never);
|
|
vi.mocked(runOcrExtraction).mockImplementation(async (args) => {
|
|
await args.reserveRead!();
|
|
return extraction(1000)(args);
|
|
});
|
|
await executeOcrExtraction({
|
|
userId: "u1",
|
|
input: text(),
|
|
budget: { ...budget, owner: "user" },
|
|
schedule: noDeadline(),
|
|
});
|
|
expect(vi.mocked(reserveBudget).mock.calls[0][3]).toBe(2_000_000);
|
|
expect(vi.mocked(reserveBudget).mock.calls[0][4]).toBe("user");
|
|
});
|
|
|
|
it("books what the reads so far overspent before judging the next one", async () => {
|
|
// A day's counter against the operator's cap, with reserveBudget's own
|
|
// rule: a reservation is allowed while the spend before it is under cap.
|
|
let counter = 190_000;
|
|
vi.mocked(reserveBudget).mockImplementation(async (...a) => {
|
|
const [, tokens, , cap] = a as unknown as [
|
|
string,
|
|
number,
|
|
string,
|
|
number,
|
|
];
|
|
const prior = counter;
|
|
if (prior >= cap) return { allowed: false, reserved: tokens } as never;
|
|
counter += tokens;
|
|
return { allowed: true, reserved: tokens } as never;
|
|
});
|
|
let answer: boolean | undefined;
|
|
vi.mocked(runOcrExtraction).mockImplementation(async (args) => {
|
|
// The first page cost 20 000 against the 1 500 the route reserved.
|
|
args.meter!.spent += 20_000;
|
|
answer = await args.reserveRead!();
|
|
return { data: { rows: [] } as never, spentTokens: 0, cachedTokens: 0 };
|
|
});
|
|
await executeOcrExtraction({
|
|
userId: "u1",
|
|
input: text(),
|
|
budget,
|
|
schedule: noDeadline(),
|
|
});
|
|
// 18 500 caught up first (allowed, the day stood at 190 000); the next
|
|
// read is then judged at 208 500 and refused. Booking only the 8 000
|
|
// ceiling, or both in one reservation, would have let it through.
|
|
expect(answer).toBe(false);
|
|
expect(vi.mocked(reserveBudget).mock.calls.map((c) => c[1])).toEqual([
|
|
18_500, 8000,
|
|
]);
|
|
expect(reconcileSpend).toHaveBeenCalledWith(
|
|
"u1",
|
|
20_000,
|
|
20_000,
|
|
"2027-01-15",
|
|
0,
|
|
{ servedBy: "openai", reservedOwner: "operator" },
|
|
);
|
|
});
|
|
|
|
it("judges the overspend net of the cached share, as the settlement books it", async () => {
|
|
vi.mocked(reserveBudget).mockResolvedValue({
|
|
allowed: true,
|
|
reserved: 1,
|
|
} as never);
|
|
vi.mocked(runOcrExtraction).mockImplementation(async (args) => {
|
|
args.meter!.spent += 20_000;
|
|
args.meter!.cached += 4000;
|
|
await args.reserveRead!();
|
|
return { data: { rows: [] } as never, spentTokens: 0, cachedTokens: 0 };
|
|
});
|
|
await executeOcrExtraction({
|
|
userId: "u1",
|
|
input: text(),
|
|
budget,
|
|
schedule: noDeadline(),
|
|
});
|
|
// 20 000 spent, 4 000 of it cached: 16 000 net against 1 500 reserved.
|
|
expect(vi.mocked(reserveBudget).mock.calls[0][1]).toBe(14_500);
|
|
});
|
|
|
|
it("stops rather than takes the larger cap when the reservation names no known owner", async () => {
|
|
let answer: boolean | undefined;
|
|
vi.mocked(runOcrExtraction).mockImplementation(async (args) => {
|
|
answer = await args.reserveRead!();
|
|
return { data: { rows: [] } as never, spentTokens: 0, cachedTokens: 0 };
|
|
});
|
|
await executeOcrExtraction({
|
|
userId: "u1",
|
|
input: text(),
|
|
budget: { ...budget, owner: undefined as never },
|
|
schedule: noDeadline(),
|
|
});
|
|
expect(answer).toBe(false);
|
|
expect(reserveBudget).not.toHaveBeenCalled();
|
|
});
|
|
|
|
it("answers a scan stopped before any row with the stop's own reason", async () => {
|
|
vi.mocked(runOcrExtraction).mockRejectedValueOnce(
|
|
new OcrExtractError("x", "budget"),
|
|
);
|
|
expect(
|
|
await executeOcrExtraction({
|
|
userId: "u1",
|
|
input: text(),
|
|
budget,
|
|
schedule: noDeadline(),
|
|
}),
|
|
).toMatchObject({ status: 429, errorCode: "labs.ocr.budgetExceeded" });
|
|
|
|
vi.mocked(runOcrExtraction).mockRejectedValueOnce(
|
|
new OcrExtractError(
|
|
"x",
|
|
"unavailable",
|
|
new AiUnavailableError("labsOcr", "consent_required"),
|
|
),
|
|
);
|
|
expect(
|
|
await executeOcrExtraction({
|
|
userId: "u1",
|
|
input: text(),
|
|
budget,
|
|
schedule: noDeadline(),
|
|
}),
|
|
).toMatchObject({ ok: false, errorCode: "consent.ai.required" });
|
|
|
|
vi.mocked(runOcrExtraction).mockRejectedValueOnce(
|
|
new OcrExtractError("x", "error"),
|
|
);
|
|
expect(
|
|
await executeOcrExtraction({
|
|
userId: "u1",
|
|
input: text(),
|
|
budget,
|
|
schedule: noDeadline(),
|
|
}),
|
|
).toMatchObject({ status: 500, errorCode: "aiRuns.failed" });
|
|
});
|
|
|
|
it("tells the read to stop when the day's budget refuses the next one", async () => {
|
|
vi.mocked(reserveBudget).mockResolvedValue({
|
|
allowed: false,
|
|
reserved: 8000,
|
|
} as never);
|
|
let answer: boolean | undefined;
|
|
vi.mocked(runOcrExtraction).mockImplementation(async (args) => {
|
|
answer = await args.reserveRead!();
|
|
return extraction(4000)(args);
|
|
});
|
|
await executeOcrExtraction({
|
|
userId: "u1",
|
|
input: text(),
|
|
budget,
|
|
schedule: noDeadline(),
|
|
});
|
|
expect(answer).toBe(false);
|
|
// The refused reservation was handed back by reserveBudget itself; the
|
|
// run settles against the first read's alone.
|
|
expect(reconcileSpend).toHaveBeenCalledWith(
|
|
"u1",
|
|
1500,
|
|
4000,
|
|
"2027-01-15",
|
|
0,
|
|
{ servedBy: "openai", reservedOwner: "operator" },
|
|
);
|
|
});
|
|
|
|
it("refuses text one read cannot return up front, and refunds", async () => {
|
|
// One unbroken line: it cannot be split, and is past what a read holds.
|
|
const out = await executeOcrExtraction({
|
|
userId: "u1",
|
|
input: text("x".repeat(OCR_TEXT_READ_MAX_CHARS + 1)),
|
|
budget,
|
|
schedule: noDeadline(),
|
|
});
|
|
expect(out).toMatchObject({
|
|
ok: false,
|
|
status: 422,
|
|
errorCode: "labs.ocr.tooLarge",
|
|
});
|
|
expect(runOcrExtraction).not.toHaveBeenCalled();
|
|
expect(reconcileSpend).toHaveBeenCalledWith(
|
|
"u1",
|
|
1500,
|
|
0,
|
|
"2027-01-15",
|
|
0,
|
|
{ servedBy: null, reservedOwner: "operator" },
|
|
);
|
|
});
|
|
|
|
it("charges a failed scan for the calls it made, refunding only the rest", async () => {
|
|
vi.mocked(runOcrExtraction).mockImplementation(async (args) => {
|
|
args.meter!.spent += 900;
|
|
throw new OcrExtractError("x");
|
|
});
|
|
await executeOcrExtraction({
|
|
userId: "u1",
|
|
input: text(),
|
|
budget,
|
|
schedule: noDeadline(),
|
|
});
|
|
expect(reconcileSpend).toHaveBeenCalledWith(
|
|
"u1",
|
|
1500,
|
|
900,
|
|
"2027-01-15",
|
|
0,
|
|
{ servedBy: "openai", reservedOwner: "operator" },
|
|
);
|
|
});
|
|
|
|
it("refunds the reservation in full when a failed scan spent nothing", async () => {
|
|
vi.mocked(runOcrExtraction).mockRejectedValue(new OcrExtractError("x"));
|
|
const out = await executeOcrExtraction({
|
|
userId: "u1",
|
|
input: text(),
|
|
budget,
|
|
schedule: noDeadline(),
|
|
});
|
|
expect(out).toMatchObject({
|
|
ok: false,
|
|
status: 422,
|
|
errorCode: "labs.ocr.extractFailed",
|
|
});
|
|
expect(reconcileSpend).toHaveBeenCalledWith(
|
|
"u1",
|
|
1500,
|
|
0,
|
|
"2027-01-15",
|
|
0,
|
|
{ servedBy: null, reservedOwner: "operator" },
|
|
);
|
|
});
|
|
|
|
it("names a read that stopped at the output limit as exactly that", async () => {
|
|
vi.mocked(runOcrExtraction).mockRejectedValue(
|
|
new OcrExtractError("x", "output_limit"),
|
|
);
|
|
const out = await executeOcrExtraction({
|
|
userId: "u1",
|
|
input: text(),
|
|
budget,
|
|
schedule: noDeadline(),
|
|
});
|
|
expect(out).toMatchObject({
|
|
ok: false,
|
|
status: 422,
|
|
errorCode: "labs.ocr.outputLimit",
|
|
});
|
|
});
|
|
|
|
it("names a provider failure and never keeps the upstream body", async () => {
|
|
vi.mocked(runOcrExtraction).mockRejectedValue(
|
|
Object.assign(new Error("Local AI request failed (403)"), {
|
|
httpStatus: 403,
|
|
model: "llama3",
|
|
bodyExcerpt: "ami-id iam/security-credentials/admin",
|
|
}),
|
|
);
|
|
const out = await executeOcrExtraction({
|
|
userId: "u1",
|
|
input: text(),
|
|
budget,
|
|
schedule: noDeadline(),
|
|
});
|
|
expect(out).toMatchObject({ ok: false, status: 502 });
|
|
expect(JSON.stringify(out)).toContain("configured AI provider");
|
|
expect(JSON.stringify(out)).not.toContain("iam/security-credentials");
|
|
expect(JSON.stringify(vi.mocked(annotate).mock.calls)).not.toContain(
|
|
"iam/security-credentials",
|
|
);
|
|
expect(annotate).toHaveBeenCalledWith(
|
|
expect.objectContaining({
|
|
meta: expect.objectContaining({ upstreamStatus: 403, model: "llama3" }),
|
|
}),
|
|
);
|
|
});
|
|
});
|
|
|
|
describe("vision mode", () => {
|
|
it("reads a rendered PDF one page per call", async () => {
|
|
vi.mocked(rasterizePdf).mockResolvedValue({
|
|
ok: true,
|
|
images: pages(3),
|
|
pageCount: 3,
|
|
});
|
|
vi.mocked(runOcrExtraction).mockImplementation(extraction());
|
|
const s = schedule(Number.POSITIVE_INFINITY);
|
|
const out = await executeOcrExtraction({
|
|
userId: "u1",
|
|
input: pdf(),
|
|
budget,
|
|
schedule: s,
|
|
});
|
|
expect(out).toEqual({ ok: true, data: { rows: [] } });
|
|
expect(rasterizePdf).toHaveBeenCalledWith(
|
|
expect.anything(),
|
|
OCR_MAX_PAGES,
|
|
{ withText: true },
|
|
);
|
|
const args = vi.mocked(runOcrExtraction).mock.calls[0][0];
|
|
expect(args.totalPages).toBe(3);
|
|
expect(args.chunks).toEqual([
|
|
{ images: [pages(3)[0]], documents: [], pages: [1], pageTexts: [] },
|
|
{ images: [pages(3)[1]], documents: [], pages: [2], pageTexts: [] },
|
|
{ images: [pages(3)[2]], documents: [], pages: [3], pageTexts: [] },
|
|
]);
|
|
expect(s.arm).toHaveBeenCalledWith(6);
|
|
});
|
|
|
|
it("sends each page's text layer with it, headed by page when a read holds several", async () => {
|
|
vi.mocked(rasterizePdf).mockResolvedValue({
|
|
ok: true,
|
|
images: pages(4),
|
|
pageCount: 4,
|
|
texts: ["Hb 14,2 g/dl", "", "TSH 1,2", "Na 140"],
|
|
});
|
|
vi.mocked(runOcrExtraction).mockImplementation(extraction());
|
|
await executeOcrExtraction({
|
|
userId: "u1",
|
|
input: pdf(),
|
|
budget,
|
|
schedule: schedule(4),
|
|
});
|
|
const args = vi.mocked(runOcrExtraction).mock.calls[0][0];
|
|
expect(args.chunks.map((c) => c.pageTexts)).toEqual([
|
|
[{ page: 1, text: "Hb 14,2 g/dl" }],
|
|
[
|
|
{ page: 3, text: "TSH 1,2" },
|
|
{ page: 4, text: "Na 140" },
|
|
],
|
|
]);
|
|
});
|
|
|
|
it("reads a multi-page PDF page by page on a provider that reads PDFs too", async () => {
|
|
vi.mocked(rasterizePdf).mockResolvedValue({
|
|
ok: true,
|
|
images: pages(4),
|
|
pageCount: 4,
|
|
});
|
|
vi.mocked(runOcrExtraction).mockImplementation(extraction());
|
|
await executeOcrExtraction({
|
|
userId: "u1",
|
|
input: pdf(anthropicPick),
|
|
budget,
|
|
schedule: noDeadline(),
|
|
});
|
|
const args = vi.mocked(runOcrExtraction).mock.calls[0][0];
|
|
expect(args.chunks.map((c) => c.pages)).toEqual([[1], [2], [3], [4]]);
|
|
expect(args.chunks.every((c) => c.documents?.length === 0)).toBe(true);
|
|
});
|
|
|
|
it("keeps the native document for a one-page PDF, or one that cannot be rendered", async () => {
|
|
vi.mocked(runOcrExtraction).mockImplementation(extraction());
|
|
vi.mocked(rasterizePdf).mockResolvedValueOnce({
|
|
ok: true,
|
|
images: pages(1),
|
|
pageCount: 1,
|
|
});
|
|
vi.mocked(rasterizePdf).mockResolvedValueOnce({
|
|
ok: false,
|
|
reason: "unsupported",
|
|
});
|
|
for (let i = 0; i < 2; i += 1) {
|
|
await executeOcrExtraction({
|
|
userId: "u1",
|
|
input: pdf(anthropicPick),
|
|
budget,
|
|
schedule: noDeadline(),
|
|
});
|
|
const args = vi.mocked(runOcrExtraction).mock.calls[i][0];
|
|
expect(args.chunks).toHaveLength(1);
|
|
expect(args.chunks[0].documents).toHaveLength(1);
|
|
expect(args.chunks[0].images).toEqual([]);
|
|
}
|
|
});
|
|
|
|
it("puts several pages in a read when the deadline allows fewer reads", async () => {
|
|
vi.mocked(rasterizePdf).mockResolvedValue({
|
|
ok: true,
|
|
images: pages(10),
|
|
pageCount: 10,
|
|
});
|
|
vi.mocked(runOcrExtraction).mockImplementation(extraction());
|
|
// Five sequential calls fit: two reads with their retries.
|
|
const s = schedule(5);
|
|
await executeOcrExtraction({
|
|
userId: "u1",
|
|
input: pdf(),
|
|
budget,
|
|
schedule: s,
|
|
});
|
|
const args = vi.mocked(runOcrExtraction).mock.calls[0][0];
|
|
expect(args.chunks.map((c) => c.pages)).toEqual([
|
|
[1, 2, 3, 4, 5],
|
|
[6, 7, 8, 9, 10],
|
|
]);
|
|
expect(s.arm).toHaveBeenCalledWith(4);
|
|
});
|
|
|
|
it("says when only the first pages of a long PDF were read", async () => {
|
|
vi.mocked(rasterizePdf).mockResolvedValue({
|
|
ok: true,
|
|
images: pages(OCR_MAX_PAGES),
|
|
pageCount: 23,
|
|
});
|
|
vi.mocked(runOcrExtraction).mockImplementation(extraction());
|
|
const out = await executeOcrExtraction({
|
|
userId: "u1",
|
|
input: pdf(),
|
|
budget,
|
|
schedule: noDeadline(),
|
|
});
|
|
expect(out).toEqual({
|
|
ok: true,
|
|
data: { rows: [], pageCoverage: { read: OCR_MAX_PAGES, total: 23 } },
|
|
});
|
|
});
|
|
|
|
it("reports a render failure of this PDF and refunds", async () => {
|
|
vi.mocked(rasterizePdf).mockResolvedValue({
|
|
ok: false,
|
|
reason: "render-failed",
|
|
});
|
|
const out = await executeOcrExtraction({
|
|
userId: "u1",
|
|
input: pdf(),
|
|
budget,
|
|
schedule: noDeadline(),
|
|
});
|
|
expect(out).toMatchObject({
|
|
ok: false,
|
|
status: 422,
|
|
errorCode: "labs.ocr.extractFailed",
|
|
});
|
|
expect(runOcrExtraction).not.toHaveBeenCalled();
|
|
expect(reconcileSpend).toHaveBeenCalledWith(
|
|
"u1",
|
|
1500,
|
|
0,
|
|
"2027-01-15",
|
|
0,
|
|
{ servedBy: null, reservedOwner: "operator" },
|
|
);
|
|
});
|
|
});
|
|
|
|
describe("a PDF on a server that cannot render PDF pages (#1311)", () => {
|
|
const PNG =
|
|
"iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAYAAAAfFcSJAAAADUlEQVR42mNkYPhfDwAChwGA60e6kgAAAABJRU5ErkJggg==";
|
|
/** `true` a lab page with a text layer, `false` a scanned page. */
|
|
const report = (layout: boolean[]) => {
|
|
const doc = new jsPDF();
|
|
layout.forEach((hasText, i) => {
|
|
if (i > 0) doc.addPage();
|
|
if (hasText) {
|
|
doc.text(`Haemoglobin 14.${i} g/dl 13.5 - 17.5`, 10, 15);
|
|
doc.text("Leukocytes 6.1 /nl 4.0 - 10.0", 10, 23);
|
|
} else {
|
|
doc.addImage(`data:image/png;base64,${PNG}`, "PNG", 10, 10, 180, 250);
|
|
}
|
|
});
|
|
return Buffer.from(doc.output("arraybuffer"));
|
|
};
|
|
|
|
beforeEach(() => {
|
|
vi.mocked(rasterizePdf).mockResolvedValue({
|
|
ok: false,
|
|
reason: "unsupported",
|
|
});
|
|
});
|
|
|
|
it("reads the text layer a page at a time and names the scanned pages as unread", async () => {
|
|
vi.mocked(runOcrExtraction).mockImplementation(extraction());
|
|
const out = await executeOcrExtraction({
|
|
userId: "u1",
|
|
input: { ...pdf(), bytes: report([true, false, true]) },
|
|
budget,
|
|
schedule: noDeadline(),
|
|
});
|
|
expect(out).toEqual({
|
|
ok: true,
|
|
data: { rows: [], unreadPages: [2] },
|
|
});
|
|
const args = vi.mocked(runOcrExtraction).mock.calls[0][0];
|
|
expect(args.mode).toBe("text");
|
|
expect(args.totalPages).toBe(3);
|
|
expect(args.chunks.map((c) => c.pages)).toEqual([[1], [3]]);
|
|
expect(args.chunks[0].text).toContain("Haemoglobin 14.0 g/dl");
|
|
expect(args.chunks.every((c) => !c.images && !c.documents)).toBe(true);
|
|
}, 30_000);
|
|
|
|
it("answers a scan with pdfRendererUnavailable and refunds", async () => {
|
|
const out = await executeOcrExtraction({
|
|
userId: "u1",
|
|
input: { ...pdf(), bytes: report([false]) },
|
|
budget,
|
|
schedule: noDeadline(),
|
|
});
|
|
expect(out).toMatchObject({
|
|
ok: false,
|
|
status: 422,
|
|
errorCode: "labs.ocr.pdfRendererUnavailable",
|
|
});
|
|
expect(runOcrExtraction).not.toHaveBeenCalled();
|
|
expect(reconcileSpend).toHaveBeenCalledWith(
|
|
"u1",
|
|
1500,
|
|
0,
|
|
expect.anything(),
|
|
0,
|
|
expect.anything(),
|
|
);
|
|
}, 30_000);
|
|
|
|
it("still sends the PDF whole to a provider that takes PDFs", async () => {
|
|
vi.mocked(runOcrExtraction).mockImplementation(extraction());
|
|
await executeOcrExtraction({
|
|
userId: "u1",
|
|
input: { ...pdf(anthropicPick), bytes: report([false]) },
|
|
budget,
|
|
schedule: noDeadline(),
|
|
});
|
|
const args = vi.mocked(runOcrExtraction).mock.calls[0][0];
|
|
expect(args.mode).toBe("vision");
|
|
expect(args.chunks[0].documents).toHaveLength(1);
|
|
}, 30_000);
|
|
});
|