Calio/tests/scan-api.test.js
Elijah 80fc48b9f1
All checks were successful
Verify and publish Calio container / build-and-push (push) Successful in 1m2s
Add barcode and nutrition label scanning
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>
2026-10-22 13:41:44 -07:00

160 lines
4.7 KiB
JavaScript
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 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);
});