Files
TREK/shared/src/budget/receipt-scan.schema.spec.ts
T
Maurice 237f42a493 fix(llm-parse): harden photo reading and finish the receipt scan
A photo is now decoded only when its header says it is within 40 megapixels,
and bytes TREK cannot shrink, such as a WebP, are sent only up to 3.5 MB. A
scanned PDF page is drawn by its long edge, and pdf.js skips an embedded image
above the same pixel cap instead of decoding it.

The receipt reading is held to bounds in the shared contract, keeps the minus
sign of a discount and leaves such lines out so the Ticket split can save, and
its prompt states the JSON shape, so a server without json_schema still answers
something the parser reads. The model lookup sends the configured key, the
import error names the photo formats, the scanned photo is attached only for
someone who may upload files, and the phone settlement reloads after a receipt
is saved from the trip sheets. Test ids that clashed after the rebase are
renumbered, and the wiki describes the image setting, photo import and receipt
scan.
2026-09-26 15:59:05 +02:00

61 lines
2.5 KiB
TypeScript

import {
RECEIPT_AMOUNT_MAX,
RECEIPT_LINES_MAX,
RECEIPT_TEXT_MAX,
receiptReadSchema,
receiptScanResultSchema,
} from './receipt-scan.schema';
import { describe, it, expect } from 'vitest';
const read = {
merchant: 'Café de Flore',
date: '2026-09-20',
total: 23.4,
currency: 'EUR',
items: [{ name: 'Croque', price: 15.4 }],
};
describe('receiptReadSchema', () => {
it('takes a reading, and one where nothing but the total was read', () => {
expect(receiptReadSchema.safeParse(read).success).toBe(true);
expect(
receiptReadSchema.safeParse({ merchant: null, date: null, total: 5, currency: null, items: [] }).success,
).toBe(true);
});
it('refuses a line that is not a charge: a discount or a free item stops the Ticket split', () => {
for (const price of [0, -2]) {
expect(receiptReadSchema.safeParse({ ...read, items: [{ name: 'Discount', price }] }).success).toBe(false);
}
});
it('holds names, lines and amounts to their bounds', () => {
const long = 'x'.repeat(RECEIPT_TEXT_MAX + 1);
expect(receiptReadSchema.safeParse({ ...read, merchant: long }).success).toBe(false);
expect(receiptReadSchema.safeParse({ ...read, items: [{ name: long, price: 1 }] }).success).toBe(false);
const lines = Array.from({ length: RECEIPT_LINES_MAX + 1 }, () => ({ name: 'Tea', price: 1 }));
expect(receiptReadSchema.safeParse({ ...read, items: lines }).success).toBe(false);
expect(receiptReadSchema.safeParse({ ...read, items: lines.slice(1) }).success).toBe(true);
expect(receiptReadSchema.safeParse({ ...read, total: RECEIPT_AMOUNT_MAX + 1 }).success).toBe(false);
expect(receiptReadSchema.safeParse({ ...read, total: Number.POSITIVE_INFINITY }).success).toBe(false);
expect(
receiptReadSchema.safeParse({ ...read, items: [{ name: 'Gold', price: RECEIPT_AMOUNT_MAX + 1 }] }).success,
).toBe(false);
});
it('refuses a date or a currency in any other form', () => {
expect(receiptReadSchema.safeParse({ ...read, date: '20/09/2026' }).success).toBe(false);
expect(receiptReadSchema.safeParse({ ...read, currency: 'eur' }).success).toBe(false);
});
});
describe('receiptScanResultSchema', () => {
it('carries a reading, or none with the reason', () => {
expect(receiptScanResultSchema.safeParse({ receipt: read, warnings: [] }).success).toBe(true);
expect(
receiptScanResultSchema.safeParse({ receipt: null, warnings: ['bill.jpg: no receipt could be read'] }).success,
).toBe(true);
});
});