From 4d257db42afb0d5ce5a35fd835108c1b5d365321 Mon Sep 17 00:00:00 2001 From: fretuks Date: Wed, 18 Nov 2026 19:27:01 +0100 Subject: [PATCH] add features for manual product editing --- tests/manualProduct.test.mjs | 57 ++++++++++++++++++++++++++++++++++++ 1 file changed, 57 insertions(+) create mode 100644 tests/manualProduct.test.mjs diff --git a/tests/manualProduct.test.mjs b/tests/manualProduct.test.mjs new file mode 100644 index 0000000..a066f05 --- /dev/null +++ b/tests/manualProduct.test.mjs @@ -0,0 +1,57 @@ +import test from 'node:test'; +import assert from 'node:assert/strict'; + +import { createLocalProduct, saveManualProduct } from '../src/services/manualProduct.js'; +import { lookupProductByBarcode, mapOpenFoodFactsProduct } from '../src/services/productApi.js'; +import { calculateProductRating } from '../src/services/ratingService.js'; +import { defaultPreferences } from '../src/utils/constants.js'; + +test('an unknown barcode can be saved locally without inventing product facts', async () => { + const blank = createLocalProduct('7612345678901'); + assert.equal(calculateProductRating(blank, defaultPreferences).overallStatus, 'unknown'); + const saved = saveManualProduct(blank, { name: 'Apfelschorle', nutrition: { sugarPer100g: 5 } }); + assert.equal(saved.dataSource, 'local'); + assert.equal(saved.nutrition.sugarPer100g, 5); + assert.equal(saved.nutrition.nutriScore, undefined); + const originalFetch = globalThis.fetch; + globalThis.fetch = () => { throw new Error('Local product must not trigger a request'); }; + try { + const result = await lookupProductByBarcode(saved.barcode, { [saved.barcode]: saved }); + assert.equal(result.status, 'found'); + assert.equal(result.product.name, 'Apfelschorle'); + assert.equal(result.product.dataSource, 'local'); + } finally { + globalThis.fetch = originalFetch; + } +}); + +test('a barcode-only Open Food Facts entry keeps its missing fields visible', () => { + const item = mapOpenFoodFactsProduct({ code: '7612345678903', nutriments: {} }); + assert.equal(item.dataCompleteness.name, false); + assert.equal(item.dataCompleteness.nutrition, false); + assert.equal(item.nutrition.nutriScore, undefined); +}); + +test('manual label corrections survive an Open Food Facts refresh', async () => { + const barcode = '7612345678902'; + const cached = saveManualProduct(mapOpenFoodFactsProduct({ + code: barcode, product_name: 'Alte Limonade', categories_tags: ['en:beverages'], + nutriments: { sugars_100g: 12 }, ingredients_text: 'Wasser, Zucker', nutriscore_grade: 'D' + }), { name: 'Etikettname', nutrition: { sugarPer100g: 4 } }); + cached.fetchedAt = '2020-01-01T00:00:00.000Z'; + const originalFetch = globalThis.fetch; + globalThis.fetch = async () => ({ ok: true, json: async () => ({ status: 1, product: { + code: barcode, product_name: 'Neuer OFF Name', categories_tags: ['en:beverages'], + nutriments: { sugars_100g: 10, salt_100g: 0.1 } + } }) }); + try { + const result = await lookupProductByBarcode(barcode, { [barcode]: cached }); + assert.equal(result.product.name, 'Etikettname'); + assert.equal(result.product.nutrition.sugarPer100g, 4); + assert.equal(result.product.nutrition.saltPer100g, 0.1); + assert.equal(result.product.nutrition.nutriScore, undefined); + assert.equal(result.product.manualOverrides.name, 'Etikettname'); + } finally { + globalThis.fetch = originalFetch; + } +});