import test from "node:test"; import assert from "node:assert/strict"; import { clean, parseLabel, rows } from "../server/ocr/label.js"; // Lays out label rows as OCR boxes: each row is [text, ...more boxes]. function boxes(lines, { angle = 0 } = {}) { const result = []; lines.forEach((parts, row) => { let x = 0; for (const text of [].concat(parts)) { const width = text.length * 10; const cx = x + width / 2; const cy = row * 30 + 10 + cx * Math.tan(angle); result.push({ text, angle, cx, cy, height: 20, x0: x, x1: x + width, y0: cy - 10, y1: cy + 10, }); x += width + 40; } }); return result; } test("a US Nutrition Facts panel is read in full", () => { const label = parseLabel( boxes([ "Nutrition Facts", "About 31 servings per container", ["Serving size", "1 cup (54g)"], "Amount per serving", ["Calories", "230"], "% Daily Value*", ["Total Fat 5g", "6%"], ["Saturated Fat 0.5g", "3%"], "Trans Fat 0g", ["Cholesterol Omg", "0%"], ["Sodium 1,230mg", "53%"], ["Total Carbohy ydrate 42g", "15%"], ["Dietary Fiber <1g", "2%"], "Total Sugars 15g", ["Includes 10g Added Sugars", "20%"], "Protein 4g", ["Vitamin D 2mcg 10%", "Calcium 260mg 20%"], ["Iron 8mg 45%", "Potassium 235mg 6%"], ["Thiamin", "60%"], ]), ); assert.equal(label.serving, "1 cup (54 g)"); assert.deepEqual(label.nutrients, { calories: 230, fat: 5, sodium: 1230, carbs: 42, fiber: 0.5, sugar: 15, protein: 4, }); assert.equal(label.extra["saturated-fat"], 0.5); assert.equal(label.extra["trans-fat"], 0); assert.equal(label.extra.cholesterol, 0); assert.equal(label.extra["added-sugars"], 10); assert.equal(label.extra["vitamin-d"], 2); assert.equal(label.extra.calcium, 260); assert.equal(label.extra.iron, 8); assert.equal(label.extra.potassium, 235); assert.equal(label.extra["vitamin-b1"], 0.72); // 60% of 1.2 mg }); test("misread units are corrected by the %DV", () => { const label = parseLabel( boxes([ ["Total Fat 89", "10%"], // 8g with the g read as 9 ["Sodium 160", "7%"], ["Calcium 20mg", "0%"], // under 2% legitimately rounds to 0 ]), ); assert.equal(label.nutrients.fat, 8); assert.equal(label.nutrients.sodium, 160); assert.equal(label.extra.calcium, 20); }); test("a large calorie figure on its own row is found", () => { const label = parseLabel( boxes(["Amount per serving 130", "Calories", ["Total Fat 6g", "8%"]]), ); assert.equal(label.nutrients.calories, 130); }); test("rows follow the text angle of a tilted photo", () => { const tilted = boxes( [ ["Sodium", "160mg", "7%"], ["Total Carbohydrate", "37g", "13%"], ], { angle: 0.12 }, ); assert.deepEqual(rows(tilted), [ "Sodium 160mg 7%", "Total Carbohydrate 37g 13%", ]); }); test("an EU per-100 g table converts salt to sodium", () => { const label = parseLabel( boxes([ "Nutrition per 100g", "Energy 1046kJ / 250kcal", "Fat 8,0g", "of which saturates 1,2g", "Carbohydrate 37g", "of which sugars 12g", "Fibre 4g", "Protein 3g", "Salt 0,5g", ]), ); assert.equal(label.serving, "100 g"); assert.deepEqual(label.nutrients, { calories: 250, fat: 8, carbs: 37, sugar: 12, fiber: 4, protein: 3, sodium: 200, }); assert.equal(label.extra["saturated-fat"], 1.2); }); test("OCR letter/digit confusions are undone", () => { assert.equal(clean("Trans Fat Og"), "Trans Fat 0g"); assert.equal(clean("Iron 1O.8mg"), "Iron 10.8mg"); assert.equal(clean("Vitamin D 2µg"), "Vitamin D 2mcg"); assert.equal(clean("Sugars less than 1g"), "Sugars < 1g"); });