Add barcode and nutrition label scanning
All checks were successful
Verify and publish Calio container / build-and-push (push) Successful in 1m2s
All checks were successful
Verify and publish Calio container / build-and-push (push) Successful in 1m2s
Barcode scanning - Scan barcode in the food sheet reads UPC-A/E and EAN-13/8 live from the camera with ZXing WebAssembly (bundled, precached, CSP allows wasm), with photo and typed-digit fallbacks - Lookups check saved foods first (offline), then local copies of USDA FoodData Central Branded Foods and Open Food Facts, then the live Open Food Facts API; API answers and misses are cached - Server imports refresh in a low-priority child process: USDA when a new archive is published, Open Food Facts weekly by streaming its CSV export; each import builds a new table and swaps it in atomically - Every available nutrient is kept: foods and entries gain optional barcode (GTIN-14) and extra nutrients, shown under More nutrients and included in JSON exports - Settings shows the size, date and any error for each product copy Label scanning - Scan label sends a downscaled photo to the server, read with PaddleOCR PP-OCRv5 (server detection, English recognition) on ONNX Runtime CPU, about 1.3 s median - Parser handles US/Canadian, linear, Spanish and EU per-100 g panels, tilted photos, OCR misspellings and %DV cross-checks - Models are pinned and checksum-verified at image build time Container - Runtime moves to Debian slim for ONNX Runtime's glibc build; other platforms' binaries are pruned - Smoke tests now read a fixture label inside the container - README and Unraid docs cover disk use, network access and refresh Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
parent
8f6981c8a2
commit
80fc48b9f1
39 changed files with 4434 additions and 33 deletions
37
tests/barcode.test.js
Normal file
37
tests/barcode.test.js
Normal file
|
|
@ -0,0 +1,37 @@
|
|||
import test from "node:test";
|
||||
import assert from "node:assert/strict";
|
||||
import {
|
||||
databaseGtin,
|
||||
displayBarcode,
|
||||
expandUpcE,
|
||||
normalizeBarcode,
|
||||
} from "../shared/barcode.js";
|
||||
|
||||
test("UPC-A, EAN-13 and GTIN-14 forms normalize to one GTIN", () => {
|
||||
assert.equal(normalizeBarcode("016000275287"), "00016000275287");
|
||||
assert.equal(normalizeBarcode("0016000275287"), "00016000275287");
|
||||
assert.equal(normalizeBarcode("00016000275287"), "00016000275287");
|
||||
assert.equal(normalizeBarcode("3017620422003"), "03017620422003");
|
||||
assert.equal(normalizeBarcode("96385074"), "00000096385074");
|
||||
});
|
||||
|
||||
test("invalid check digits and lengths are rejected", () => {
|
||||
assert.equal(normalizeBarcode("016000275288"), null);
|
||||
assert.equal(normalizeBarcode("12345"), null);
|
||||
assert.equal(normalizeBarcode("abc"), null);
|
||||
});
|
||||
|
||||
test("UPC-E expands to UPC-A", () => {
|
||||
assert.equal(expandUpcE("04252614"), "042100005264");
|
||||
assert.equal(normalizeBarcode("04252614", "upc_e"), "00042100005264");
|
||||
assert.equal(normalizeBarcode("425261"), "00042100005264");
|
||||
});
|
||||
|
||||
test("database codes pad without validation; display uses the short form", () => {
|
||||
assert.equal(databaseGtin("28400090858"), "00028400090858");
|
||||
assert.equal(databaseGtin("123"), null);
|
||||
assert.equal(databaseGtin("00000000000000"), null);
|
||||
assert.equal(displayBarcode("00016000275287"), "016000275287");
|
||||
assert.equal(displayBarcode("03017620422003"), "3017620422003");
|
||||
assert.equal(displayBarcode("00000096385074"), "96385074");
|
||||
});
|
||||
BIN
tests/fixtures/label.png
vendored
Normal file
BIN
tests/fixtures/label.png
vendored
Normal file
Binary file not shown.
|
After Width: | Height: | Size: 55 KiB |
142
tests/label.test.js
Normal file
142
tests/label.test.js
Normal file
|
|
@ -0,0 +1,142 @@
|
|||
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");
|
||||
});
|
||||
209
tests/products.test.js
Normal file
209
tests/products.test.js
Normal file
|
|
@ -0,0 +1,209 @@
|
|||
import test from "node:test";
|
||||
import assert from "node:assert/strict";
|
||||
import { Readable } from "node:stream";
|
||||
import { gzipSync } from "node:zlib";
|
||||
import { openProducts } from "../server/products/db.js";
|
||||
import {
|
||||
fetchOffProduct,
|
||||
importOffCsv,
|
||||
offNutrients,
|
||||
} from "../server/products/off.js";
|
||||
import { csvRecords, nutrientMapper } from "../server/products/usda.js";
|
||||
import { productToFood, titleCase } from "../shared/products.js";
|
||||
import { recordSchema } from "../shared/domain.js";
|
||||
|
||||
test("Open Food Facts grams become Calio units", () => {
|
||||
const nutrients = offNutrients([
|
||||
["energy-kj", "1046"],
|
||||
["fat", "8"],
|
||||
["salt", "1"],
|
||||
["vitamin-c", "0.0036"],
|
||||
["vitamin-d", "0.000005"],
|
||||
["cholesterol", "0.012"],
|
||||
["nutrition-score-fr", "12"],
|
||||
["alcohol", "5"],
|
||||
["proteins", "250"],
|
||||
["fiber", ""],
|
||||
]);
|
||||
assert.equal(Math.round(nutrients["energy-kcal"]), 250);
|
||||
assert.equal(nutrients.fat, 8);
|
||||
assert.equal(nutrients.sodium, 400);
|
||||
assert.ok(Math.abs(nutrients["vitamin-c"] - 3.6) < 1e-9);
|
||||
assert.ok(Math.abs(nutrients["vitamin-d"] - 5) < 1e-9);
|
||||
assert.ok(Math.abs(nutrients.cholesterol - 12) < 1e-9);
|
||||
for (const key of ["nutrition-score-fr", "alcohol", "proteins", "fiber"])
|
||||
assert.equal(nutrients[key], undefined, key);
|
||||
});
|
||||
|
||||
test("products become per-serving foods with every nutrient kept", () => {
|
||||
const food = productToFood({
|
||||
gtin: "00016000275287",
|
||||
name: "Cheerios",
|
||||
brand: "General Mills",
|
||||
serving: "1 cup (28 g)",
|
||||
servingQty: 28,
|
||||
servingUnit: "g",
|
||||
nutrients: {
|
||||
"energy-kcal": 357,
|
||||
proteins: 12.5,
|
||||
sodium: 500,
|
||||
calcium: 357,
|
||||
"palmitic-acid": 0.5,
|
||||
},
|
||||
source: "usda",
|
||||
});
|
||||
assert.equal(food.name, "General Mills Cheerios");
|
||||
assert.equal(food.serving, "1 cup (28 g)");
|
||||
assert.equal(food.nutrients.calories, 100);
|
||||
assert.equal(food.nutrients.sodium, 140);
|
||||
assert.equal(food.nutrients.fat, 0);
|
||||
assert.deepEqual(food.extra, { calcium: 100, "palmitic-acid": 0.14 });
|
||||
const record = {
|
||||
id: "food-1",
|
||||
type: "food",
|
||||
updatedAt: new Date().toISOString(),
|
||||
deleted: false,
|
||||
data: {
|
||||
name: food.name,
|
||||
serving: food.serving,
|
||||
nutrients: food.nutrients,
|
||||
barcode: food.barcode,
|
||||
extra: food.extra,
|
||||
},
|
||||
};
|
||||
assert.equal(recordSchema.safeParse(record).success, true);
|
||||
record.data.barcode = "123";
|
||||
assert.equal(recordSchema.safeParse(record).success, false);
|
||||
});
|
||||
|
||||
test("products without a serving size are shown per 100 g", () => {
|
||||
const food = productToFood({
|
||||
gtin: "03017620422003",
|
||||
name: "Nutella",
|
||||
brand: "Nutella",
|
||||
nutrients: { "energy-kcal": 539 },
|
||||
});
|
||||
assert.equal(food.name, "Nutella");
|
||||
const coke = productToFood({
|
||||
gtin: "05449000000996",
|
||||
name: "Coca-Cola",
|
||||
brand: "COCA-COLA SERVICES SA/NV",
|
||||
nutrients: {},
|
||||
});
|
||||
assert.equal(coke.name, "Coca-Cola");
|
||||
assert.equal(food.serving, "100 g");
|
||||
assert.equal(food.nutrients.calories, 539);
|
||||
assert.equal(titleCase("CHEERIOS CEREAL"), "Cheerios Cereal");
|
||||
assert.equal(titleCase("Mixed case"), "Mixed case");
|
||||
});
|
||||
|
||||
test("the CSV reader handles quotes, escapes and multi-line fields", async () => {
|
||||
const stream = Readable.from(['a,"b ""c""",', '"multi\nline"\r\n1,', "2,3"]);
|
||||
const records = [];
|
||||
for await (const record of csvRecords(stream)) records.push(record);
|
||||
assert.deepEqual(records, [
|
||||
["a", 'b "c"', "multi\nline"],
|
||||
["1", "2", "3"],
|
||||
]);
|
||||
});
|
||||
|
||||
test("USDA nutrients map to catalog keys and units", () => {
|
||||
const map = nutrientMapper([
|
||||
{ id: 1008, name: "Energy", unit: "KCAL" },
|
||||
{ id: 1099, name: "Fluoride, F", unit: "UG" },
|
||||
{ id: 1110, name: "Vitamin D (D2 + D3), International Units", unit: "IU" },
|
||||
{ id: 1210, name: "Tryptophan", unit: "G" },
|
||||
{ id: 1023, name: "pH", unit: "PH" },
|
||||
]);
|
||||
assert.equal(map.get(1008).key, "energy-kcal");
|
||||
assert.equal(map.get(1099).convert(500), 0.5); // µg → mg
|
||||
assert.equal(map.get(1110).convert(400), 10); // IU → µg
|
||||
assert.equal(map.get(1110).fallback, true);
|
||||
assert.equal(map.get(1210).key, "tryptophan");
|
||||
assert.equal(map.has(1023), false);
|
||||
});
|
||||
|
||||
test("imports swap in atomically and USDA wins over Open Food Facts", async () => {
|
||||
const products = openProducts(":memory:");
|
||||
const header = [
|
||||
"code",
|
||||
"product_name",
|
||||
"brands",
|
||||
"serving_size",
|
||||
"serving_quantity",
|
||||
"no_nutrition_data",
|
||||
"energy-kcal_100g",
|
||||
"proteins_100g",
|
||||
];
|
||||
const rows = [
|
||||
header,
|
||||
["16000275287", "My Bff", "Cheerios", "1 cup (28g)", "28", "", "350", "12"],
|
||||
["5449000000996", "Coca-Cola", "Coca-Cola", "330 ml", "330", "", "42", "0"],
|
||||
["123", "Too short", "", "", "", "", "100", "1"],
|
||||
["4000000000006", "No data", "", "", "", "on", "", ""],
|
||||
];
|
||||
const body = gzipSync(rows.map((row) => row.join("\t")).join("\n"));
|
||||
const off = products.beginImport("off");
|
||||
assert.equal(await importOffCsv(new Response(body), off), 2);
|
||||
off.commit("v1");
|
||||
const usda = products.beginImport("usda");
|
||||
usda.add({
|
||||
gtin: "00016000275287",
|
||||
name: "CHEERIOS CEREAL",
|
||||
brand: "Cheerios",
|
||||
serving: "1 cup (28 g)",
|
||||
servingQty: 28,
|
||||
servingUnit: "g",
|
||||
nutrients: { "energy-kcal": 357 },
|
||||
});
|
||||
usda.commit("2026-04-30");
|
||||
const cheerios = products.lookup("00016000275287");
|
||||
assert.equal(cheerios.source, "usda");
|
||||
assert.equal(cheerios.name, "Cheerios Cereal");
|
||||
assert.equal(products.lookup("05449000000996").servingUnit, "ml");
|
||||
assert.equal(products.lookup("00000000000123"), undefined);
|
||||
products.remember("00000000000123", null);
|
||||
assert.equal(products.lookup("00000000000123"), null);
|
||||
assert.deepEqual(
|
||||
products
|
||||
.status()
|
||||
.map((row) => [row.source, row.rows])
|
||||
.sort(),
|
||||
[
|
||||
["off", 2],
|
||||
["usda", 1],
|
||||
],
|
||||
);
|
||||
products.close();
|
||||
});
|
||||
|
||||
test("the Open Food Facts API answer is shaped like an import row", async () => {
|
||||
let url;
|
||||
const fetchStub = async (address) => {
|
||||
url = address;
|
||||
return Response.json({
|
||||
status: 1,
|
||||
product: {
|
||||
product_name: "Diet Coke",
|
||||
brands: "Coca-Cola",
|
||||
serving_size: "1 can (355 ml)",
|
||||
serving_quantity: 355,
|
||||
nutriments: {
|
||||
"energy-kcal_100g": 0.4,
|
||||
sodium_100g: 0.012,
|
||||
caffeine_100g: 0.0129,
|
||||
},
|
||||
},
|
||||
});
|
||||
};
|
||||
const product = await fetchOffProduct("00049000028911", fetchStub);
|
||||
assert.match(url, /\/product\/0049000028911\.json/);
|
||||
assert.equal(product.source, "api");
|
||||
assert.equal(product.servingUnit, "ml");
|
||||
assert.equal(product.nutrients.sodium, 12);
|
||||
assert.ok(Math.abs(product.nutrients.caffeine - 12.9) < 1e-9);
|
||||
const missing = await fetchOffProduct("00049000028911", async () =>
|
||||
Response.json({ status: 0 }),
|
||||
);
|
||||
assert.equal(missing, null);
|
||||
});
|
||||
160
tests/scan-api.test.js
Normal file
160
tests/scan-api.test.js
Normal file
|
|
@ -0,0 +1,160 @@
|
|||
import test from "node:test";
|
||||
import assert from "node:assert/strict";
|
||||
import { createApp } from "../server/app.js";
|
||||
import { openDatabase } from "../server/db.js";
|
||||
import { hashPassword } from "../server/auth.js";
|
||||
import { openProducts } from "../server/products/db.js";
|
||||
|
||||
const password = "test-only-password-123";
|
||||
const origin = "http://localhost:3000";
|
||||
async function fixture(t, { fetchProduct, ocr } = {}) {
|
||||
const store = openDatabase(":memory:");
|
||||
const products = openProducts(":memory:");
|
||||
const app = createApp({
|
||||
store,
|
||||
username: "test",
|
||||
passwordHash: hashPassword(password),
|
||||
origin,
|
||||
secureCookies: false,
|
||||
dist: ".local/nonexistent",
|
||||
products,
|
||||
fetchProduct,
|
||||
ocr,
|
||||
});
|
||||
const server = app.listen(0, "127.0.0.1");
|
||||
await new Promise((resolve) => server.once("listening", resolve));
|
||||
const base = `http://127.0.0.1:${server.address().port}/api`;
|
||||
t.after(
|
||||
() =>
|
||||
new Promise((resolve) =>
|
||||
server.close(() => {
|
||||
store.close();
|
||||
products.close();
|
||||
resolve();
|
||||
}),
|
||||
),
|
||||
);
|
||||
const login = await fetch(base + "/login", {
|
||||
method: "POST",
|
||||
headers: {
|
||||
"Content-Type": "application/json",
|
||||
Origin: origin,
|
||||
"X-Calio-Request": "1",
|
||||
},
|
||||
body: JSON.stringify({ username: "test", password }),
|
||||
});
|
||||
const cookie = login.headers.get("set-cookie").split(";")[0];
|
||||
const request = (path, init = {}) =>
|
||||
fetch(base + path, {
|
||||
...init,
|
||||
headers: {
|
||||
Origin: origin,
|
||||
"X-Calio-Request": "1",
|
||||
Cookie: cookie,
|
||||
...init.headers,
|
||||
},
|
||||
});
|
||||
return { products, request };
|
||||
}
|
||||
|
||||
test("barcode lookups use local data first, then Open Food Facts once", async (t) => {
|
||||
let calls = 0;
|
||||
const { products, request } = await fixture(t, {
|
||||
fetchProduct: async (gtin) => {
|
||||
calls++;
|
||||
return gtin === "05449000000996"
|
||||
? {
|
||||
gtin,
|
||||
name: "Coca-Cola",
|
||||
brand: "Coca-Cola",
|
||||
serving: "1 can (330 ml)",
|
||||
servingQty: 330,
|
||||
servingUnit: "ml",
|
||||
nutrients: { "energy-kcal": 42, sugars: 10.6 },
|
||||
source: "api",
|
||||
}
|
||||
: null;
|
||||
},
|
||||
});
|
||||
const usda = products.beginImport("usda");
|
||||
usda.add({
|
||||
gtin: "00016000275287",
|
||||
name: "CHEERIOS",
|
||||
brand: "General Mills",
|
||||
serving: "1 cup (28 g)",
|
||||
servingQty: 28,
|
||||
servingUnit: "g",
|
||||
nutrients: { "energy-kcal": 357, calcium: 357 },
|
||||
});
|
||||
usda.commit("test");
|
||||
|
||||
const local = await request("/products/016000275287");
|
||||
assert.equal(local.status, 200);
|
||||
const { food } = await local.json();
|
||||
assert.equal(food.barcode, "00016000275287");
|
||||
assert.equal(food.source, "usda");
|
||||
assert.equal(food.nutrients.calories, 100);
|
||||
assert.equal(food.extra.calcium, 100);
|
||||
assert.equal(calls, 0);
|
||||
|
||||
const remote = await request("/products/5449000000996");
|
||||
assert.equal((await remote.json()).food.serving, "1 can (330 ml)");
|
||||
await request("/products/5449000000996");
|
||||
assert.equal(calls, 1, "the API answer is cached");
|
||||
|
||||
assert.equal((await request("/products/4006381333931")).status, 404);
|
||||
assert.equal((await request("/products/4006381333931")).status, 404);
|
||||
assert.equal(calls, 2, "misses are cached too");
|
||||
assert.equal((await request("/products/12345")).status, 400);
|
||||
assert.equal(
|
||||
(
|
||||
await fetch(
|
||||
new URL("/api/products/016000275287", (await request("/health")).url),
|
||||
)
|
||||
).status,
|
||||
401,
|
||||
);
|
||||
});
|
||||
|
||||
test("an unreachable Open Food Facts is reported, not cached", async (t) => {
|
||||
const { request } = await fixture(t, {
|
||||
fetchProduct: async () => {
|
||||
throw new Error("offline");
|
||||
},
|
||||
});
|
||||
const response = await request("/products/5449000000996");
|
||||
assert.equal(response.status, 502);
|
||||
assert.match((await response.json()).error, /couldn’t be reached/);
|
||||
});
|
||||
|
||||
test("label photos are read and parsed", async (t) => {
|
||||
let received;
|
||||
const { request } = await fixture(t, {
|
||||
ocr: async (image) => {
|
||||
received = image;
|
||||
const row = (text, y) => ({ text, x0: 0, x1: 300, y0: y, y1: y + 20 });
|
||||
return [
|
||||
row("Serving size 1 cup (55g)", 0),
|
||||
row("Calories 230", 30),
|
||||
row("Total Fat 8g 10%", 60),
|
||||
row("Sodium 160mg 7%", 90),
|
||||
];
|
||||
},
|
||||
});
|
||||
const response = await request("/label", {
|
||||
method: "POST",
|
||||
headers: { "Content-Type": "image/jpeg" },
|
||||
body: new Uint8Array([0xff, 0xd8, 0xff]),
|
||||
});
|
||||
assert.equal(response.status, 200);
|
||||
const { label } = await response.json();
|
||||
assert.equal(received.length, 3);
|
||||
assert.equal(label.serving, "1 cup (55 g)");
|
||||
assert.deepEqual(label.nutrients, { calories: 230, fat: 8, sodium: 160 });
|
||||
const wrongType = await request("/label", {
|
||||
method: "POST",
|
||||
headers: { "Content-Type": "text/plain" },
|
||||
body: "hello",
|
||||
});
|
||||
assert.equal(wrongType.status, 415);
|
||||
});
|
||||
Loading…
Add table
Add a link
Reference in a new issue