124 lines
7.2 KiB
JavaScript
124 lines
7.2 KiB
JavaScript
import test from 'node:test';
|
|
import assert from 'node:assert/strict';
|
|
import { importAgribalyse } from '../scripts/importAgribalyse.mjs';
|
|
|
|
import { mapOpenFoodFactsProduct, lookupProductByBarcode, searchProducts } from '../src/services/productApi.js';
|
|
import { enrichEnvironmentalProduct, productSpecificFootprint, ENVIRONMENTAL_RULES_VERSION } from '../src/services/environmentalService.js';
|
|
|
|
const off = (overrides = {}) => mapOpenFoodFactsProduct({
|
|
code: '12345678', product_name: 'Banana', categories_tags: ['en:fruits', 'en:bananas'],
|
|
quantity: '500 g', ...overrides
|
|
});
|
|
|
|
test('product-specific OFF footprint and Green-Score take priority over category impact', () => {
|
|
const product = off({
|
|
environmental_score_grade: 'b', environmental_score_score: 70,
|
|
categories_properties: { 'agribalyse_food_code:en': '13005' },
|
|
nutriments: { 'carbon-footprint_100g': 120 }
|
|
});
|
|
assert.equal(product.sustainability.ecoScore, 'B');
|
|
assert.equal(product.sustainability.ecoScoreScore, 70);
|
|
assert.equal(product.sustainability.footprint.status, 'product-specific');
|
|
assert.equal(product.sustainability.footprint.valueKgCo2ePerKg, 1.2);
|
|
assert.equal(product.sustainability.footprint.packageKgCo2e, 0.6);
|
|
});
|
|
|
|
test('missing product footprint falls back to verified AGRIBALYSE entry with indicators', () => {
|
|
const product = off({ categories_properties: { 'agribalyse_food_code:en': '13005' } });
|
|
const footprint = product.sustainability.footprint;
|
|
assert.equal(footprint.status, 'estimated');
|
|
assert.equal(footprint.sourceId, '13005');
|
|
assert.equal(footprint.valueKgCo2ePerKg, 0.909);
|
|
assert.equal(footprint.matchMethod, 'off-category-code');
|
|
assert.equal(footprint.datasetVersion, '3.2');
|
|
assert.equal(footprint.packageKgCo2e, 0.4545);
|
|
assert.equal(footprint.sourceUnit, 'kg CO2 eq/kg de produit');
|
|
assert.equal(footprint.indicators.landUse.unit, 'Pt/kg de produit');
|
|
assert.equal(footprint.indicators.waterScarcity.unit, 'm3 depriv./kg de produit');
|
|
assert.equal(footprint.indicators.singleScore.unit, 'mPt/kg de produit');
|
|
});
|
|
|
|
test('CO2 estimate does not create an official Green-Score', () => {
|
|
const product = off();
|
|
assert.equal(product.sustainability.footprint.status, 'estimated');
|
|
assert.equal(product.sustainability.ecoScore, undefined);
|
|
});
|
|
|
|
test('unrelated, conflicting and ambiguous categories remain unavailable', () => {
|
|
assert.equal(off({ categories_tags: ['en:unknown-food'], product_name: 'Mystery bar' }).sustainability.footprint.status, 'unavailable');
|
|
assert.equal(off({ categories_tags: ['en:apples', 'en:bananas'] }).sustainability.footprint.status, 'unavailable');
|
|
assert.equal(off({ product_name: 'Dried banana' }).sustainability.footprint.status, 'unavailable');
|
|
assert.equal(off({ product_name: 'Banana smoothie', categories_tags: ['en:bananas', 'en:smoothies'] }).sustainability.footprint.status, 'unavailable');
|
|
assert.equal(off({ product_name: 'Banana', categories_properties: { 'agribalyse_food_code:en': '20009' } }).sustainability.footprint.status, 'unavailable');
|
|
assert.equal(off({ ingredients_text: 'Banana, sugar' }).sustainability.footprint.status, 'unavailable');
|
|
});
|
|
|
|
test('similar names do not override structured categories', () => {
|
|
const banana = off();
|
|
const plantain = off({ product_name: 'Plantain banana', categories_tags: ['en:plantains'] });
|
|
const apple = off({ product_name: 'Apple', categories_tags: ['en:apples'] });
|
|
assert.equal(banana.sustainability.footprint.sourceId, '13005');
|
|
assert.equal(plantain.sustainability.footprint.status, 'unavailable');
|
|
assert.equal(apple.sustainability.footprint.sourceId, '13039');
|
|
});
|
|
|
|
test('exact name fallback is constrained by the food group and processing state', () => {
|
|
const tomato = off({ product_name: 'Tomato, raw', categories_tags: ['en:vegetables'] });
|
|
assert.equal(tomato.sustainability.footprint.sourceId, '20047_1');
|
|
assert.equal(tomato.sustainability.footprint.matchMethod, 'normalized-name-in-category');
|
|
assert.equal(off({ product_name: 'Tomato, raw', categories_tags: ['en:beverages'] }).sustainability.footprint.status, 'unavailable');
|
|
});
|
|
|
|
test('mass units and package reliability are handled without volume or drained-weight guesses', () => {
|
|
assert.equal(off({ quantity: '2 x 250 g' }).sustainability.footprint.packageQuantityGrams, 500);
|
|
assert.equal(off({ quantity: '1.5 kg' }).sustainability.footprint.packageQuantityGrams, 1500);
|
|
assert.equal(off({ quantity: '500 ml' }).sustainability.footprint.packageKgCo2e, undefined);
|
|
assert.equal(off({ quantity: '500 g, 350 g drained' }).sustainability.footprint.packageKgCo2e, undefined);
|
|
assert.equal(off({ quantity: 'unknown' }).sustainability.footprint.packageKgCo2e, undefined);
|
|
assert.equal(productSpecificFootprint({ 'carbon-footprint_100g': 0 }, '100 g').valueKgCo2ePerKg, 0);
|
|
assert.equal(productSpecificFootprint({ 'carbon-footprint_100g': 50 }, '500 ml').status, 'unavailable');
|
|
});
|
|
|
|
test('malformed and legacy OFF fields do not manufacture environmental values', () => {
|
|
assert.equal(productSpecificFootprint({ 'carbon-footprint_100g': 'bad' }, '100 g').status, 'unavailable');
|
|
const legacy = off({ ecoscore_grade: 'c', ecoscore_data: { agribalyse: { co2_total: 99 } }, categories_tags: [] });
|
|
assert.equal(legacy.sustainability.ecoScore, 'C');
|
|
assert.equal(legacy.sustainability.footprint.status, 'unavailable');
|
|
assert.equal(mapOpenFoodFactsProduct({ code: '9', categories_tags: null, ingredients_text: {}, ingredients_analysis_tags: null }).sustainability.footprint.status, 'unavailable');
|
|
});
|
|
|
|
test('cached product matches are reused and invalidated by rule/data version', () => {
|
|
const first = off();
|
|
assert.match(first.sustainability.environmentalCacheVersion, new RegExp(ENVIRONMENTAL_RULES_VERSION + '$'));
|
|
assert.strictEqual(enrichEnvironmentalProduct(first), first);
|
|
const stale = { ...first, sustainability: { ...first.sustainability, environmentalCacheVersion: 'old', footprint: { status: 'estimated', valueKgCo2ePerKg: 999 } } };
|
|
assert.equal(enrichEnvironmentalProduct(stale).sustainability.footprint.valueKgCo2ePerKg, 0.909);
|
|
const local = { id: 'sample-1', dataSource: 'local', sustainability: { carbonFootprintKgCo2ePerKg: 2 } };
|
|
assert.strictEqual(enrichEnvironmentalProduct(local), local);
|
|
});
|
|
|
|
test('offline cached scans work and OFF/network failures fail gracefully', async () => {
|
|
const cached = off();
|
|
const result = await lookupProductByBarcode(cached.barcode, { [cached.barcode]: cached }, true);
|
|
assert.equal(result.product.sustainability.footprint.status, 'estimated');
|
|
const originalFetch = globalThis.fetch;
|
|
try {
|
|
globalThis.fetch = async () => { throw new Error('offline'); };
|
|
assert.equal((await lookupProductByBarcode('987654321')).status, 'error');
|
|
assert.deepEqual(await searchProducts('banana', {}, false), []);
|
|
globalThis.fetch = async () => ({ ok: true, json: async () => null });
|
|
assert.equal((await lookupProductByBarcode('987654321')).status, 'notFound');
|
|
} finally {
|
|
globalThis.fetch = originalFetch;
|
|
}
|
|
});
|
|
|
|
test('ADEME import failure cannot replace the last good local snapshot', async () => {
|
|
let wrote = false;
|
|
await assert.rejects(importAgribalyse({
|
|
fetchJson: async () => { throw new Error('ADEME offline'); },
|
|
write: async () => { wrote = true; }
|
|
}), /ADEME offline/);
|
|
assert.equal(wrote, false);
|
|
});
|