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); });