Files
lunch/tests/receipt-read.test.ts
kai ffee757b81
release / which version (push) Successful in 3s
release / olares chart · sanity (push) Successful in 4s
release / app (push) Successful in 1m5s
release / tag and release (push) Successful in 3s
26.11.33: Bons – nicht zum Kochen, Preis je 100 g (#34)
2026-11-07 12:08:36 +00:00

122 lines
6.1 KiB
TypeScript
Raw Permalink Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
import { describe, expect, it } from "vitest";
import { fromReceiptReply, matchesProduct, normalizeLine, packFromText, parseReceiptDate, pricePer, receiptFromText, unitPrice } from "@/lib/receipt-read";
describe("reading receipts", () => {
const today = new Date("2026-11-08T12:00:00Z");
it("parses the printed date and refuses implausible ones", () => {
expect(parseReceiptDate("07.11.26", today)).toBe("2026-11-07");
expect(parseReceiptDate("Datum 07.11.2026 18:32", today)).toBe("2026-11-07");
expect(parseReceiptDate("31.02.26", today)).toBeNull();
expect(parseReceiptDate("07.11.24", today)).toBeNull(); // zu alt
expect(parseReceiptDate(null, today)).toBeNull();
});
it("takes only sane lines and prices from the model", () => {
const r = fromReceiptReply({
store: "REWE Markt",
date: "07.11.26",
total: 12.47,
lines: [
{ text: " RINDERHACK 500G ", qty: null, unit_price: null, total_price: 4.99 },
{ text: "", total_price: 1 },
{ text: "BANANEN", qty: 2, unit_price: 0.35, total_price: -3 },
],
});
expect(r?.lines).toEqual([
{ text: "RINDERHACK 500G", qty: null, unitPrice: null, totalPrice: 4.99 },
{ text: "BANANEN", qty: 2, unitPrice: 0.35, totalPrice: null },
]);
expect(r?.store).toBe("REWE Markt");
expect(fromReceiptReply({ lines: [] })).toBeNull();
expect(fromReceiptReply("kaputt")).toBeNull();
});
it("reads pack sizes and normalizes lines for learning", () => {
expect(packFromText("RINDERHACK 500G")).toEqual({ qty: 500, unit: "g" });
expect(packFromText("H-MILCH 1,5% 1L")).toEqual({ qty: 1000, unit: "ml" });
expect(packFromText("EIER BODENH. 10ST")).toEqual({ qty: 10, unit: "piece" });
expect(packFromText("BANANEN")).toBeNull();
expect(normalizeLine("RINDERHACK 500G 4,99 A")).toBe("rinderhack 500g");
});
it("recognises a product from its abbreviation on the receipt", () => {
expect(matchesProduct("BARILLA SPAGH N5 500G", "Barilla Spaghetti Nr. 5")).toBe(true);
expect(matchesProduct("BARILLA PENNE 500G", "Barilla Spaghetti Nr. 5")).toBe(false);
expect(matchesProduct("SPAGHETTI", "Barilla Spaghetti Nr. 5")).toBe(false);
});
});
// Ein Aldi-Bon (Kai, 7.11.2026), nachgebaut — das Foto bleibt draußen. Aldi druckt
// Menge und Gewicht über den Artikel, Pfand und Leergut als eigene Zeilen, die
// Steuerklasse hinter den Preis; und er ist lang.
describe("an Aldi receipt", () => {
const printed = [
["HAUSHALTSROLLEN 4X64", null, null, 2.65],
["2 x 1,49 €", null, null, null],
["DINKEL/KARTOFFELKRÜSTC", null, null, 2.98],
["PAULANER SPEZI 6X0,33L", null, null, 3.99],
["PFANDWERT 1,50", null, null, 1.5],
["3 x 0,59 €", null, null, null],
["H-KOCHSAHNE 12%", null, null, 1.77],
["2 x 8,79 €", null, null, null],
["RINDERHACKFLEI. XXL - 17,58 € 1", null, null, 17.58],
["BIO MÖHREN NL", null, null, 1.69],
["0,416 kg x 2,79 €/kg", null, null, null],
["MANDARINEN KG-WARE", null, null, 1.16],
["-41 x 0,25 €", null, null, null],
["LEERGUTRÜCKNAHME 19%", null, null, -10.25],
];
it("puts quantity and weight lines into the article below and drops deposits", () => {
const r = fromReceiptReply({ store: "ALDI", date: null, total: 96.67, lines: printed });
expect(r?.lines).toEqual([
{ text: "HAUSHALTSROLLEN 4X64", qty: null, unitPrice: null, totalPrice: 2.65 },
{ text: "DINKEL/KARTOFFELKRÜSTC", qty: 2, unitPrice: 1.49, totalPrice: 2.98 },
{ text: "PAULANER SPEZI 6X0,33L", qty: null, unitPrice: null, totalPrice: 3.99 },
{ text: "H-KOCHSAHNE 12%", qty: 3, unitPrice: 0.59, totalPrice: 1.77 },
{ text: "RINDERHACKFLEI. XXL -", qty: 2, unitPrice: 8.79, totalPrice: 17.58 },
{ text: "BIO MÖHREN NL", qty: null, unitPrice: null, totalPrice: 1.69 },
{ text: "MANDARINEN KG-WARE", qty: 0.416, unitPrice: 2.79, totalPrice: 1.16, weighed: true },
]);
expect(r?.total).toBe(96.67);
expect(r?.date).toBeNull(); // auf dem Foto abgeschnitten — dann gilt der Tag des Hochladens
});
it("keeps a quantity the model already set", () => {
const r = fromReceiptReply({ lines: [["2 x 1,49 €", null, null, null], ["DINKEL/KARTOFFELKRÜSTC", 2, 1.49, 2.98]] });
expect(r?.lines).toEqual([{ text: "DINKEL/KARTOFFELKRÜSTC", qty: 2, unitPrice: 1.49, totalPrice: 2.98 }]);
});
it("rescues every complete line when the reply breaks off mid-way", () => {
const whole = JSON.stringify({ store: "ALDI", date: "07.11.26", total: 96.67, lines: printed });
const cut = whole.slice(0, whole.indexOf("BIO MÖHREN") + 4); // mitten in einer Zeile abgebrochen
const r = receiptFromText(`<think>lang</think>${cut}`);
expect(r?.partial).toBe(true);
expect(r?.store).toBe("ALDI");
expect(r?.total).toBe(96.67);
expect(r?.lines.map((l) => l.text)).toEqual(["HAUSHALTSROLLEN 4X64", "DINKEL/KARTOFFELKRÜSTC", "PAULANER SPEZI 6X0,33L", "H-KOCHSAHNE 12%", "RINDERHACKFLEI. XXL -"]);
});
it("reads a complete reply as complete, also in the old object form", () => {
const r = receiptFromText("```json\n" + JSON.stringify({ lines: [{ text: "TOMATENMARK", qty: null, unit_price: null, total_price: 0.89 }] }) + "\n```");
expect(r).toEqual({ store: null, date: null, total: null, lines: [{ text: "TOMATENMARK", qty: null, unitPrice: null, totalPrice: 0.89 }] });
expect(receiptFromText("Ich kann den Bon nicht lesen.")).toBeNull();
});
});
describe("price per unit (26.11.33)", () => {
it("divides the line by count times pack", () => {
const hack = unitPrice(17.58, 2, 400)!; // 2 × 400 g
expect(pricePer(hack, "g").price).toBeCloseTo(2.1975, 4);
expect(pricePer(hack, "g").per).toBe("100g");
const mandarins = unitPrice(1.16, 0.416, 1000)!; // gewogen: 0,416 kg, Packung 1000 g
expect(mandarins * 1000).toBeCloseTo(2.79, 2);
expect(pricePer(unitPrice(2.49, null, 1)!, "piece")).toEqual({ price: 2.49, per: "piece" });
});
it("gives no price without a pack size or a price", () => {
expect(unitPrice(17.58, 2, null)).toBeNull();
expect(unitPrice(null, 1, 500)).toBeNull();
expect(unitPrice(1.19, 1, 0)).toBeNull();
});
});