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
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);
|
||||
});
|
||||
Loading…
Add table
Add a link
Reference in a new issue