135 lines
4.8 KiB
JavaScript
135 lines
4.8 KiB
JavaScript
import test from 'node:test';
|
|
import assert from 'node:assert/strict';
|
|
|
|
import { calculateNutriScore } from '../src/services/nutriScoreService.js';
|
|
import { calculateProductRating } from '../src/services/ratingService.js';
|
|
import { lookupProductByBarcode } from '../src/services/productApi.js';
|
|
|
|
const preferences = { avoidAllergens: [], preferredDiet: 'none' };
|
|
|
|
function product(nutrition, categoryTags = []) {
|
|
return { id: 'test', name: 'Testprodukt', categoryTags, nutrition };
|
|
}
|
|
|
|
test('calculates a good food grade from per-100g nutrients', () => {
|
|
const item = product({
|
|
energyKjPer100g: 500,
|
|
saturatedFatPer100g: 0,
|
|
sugarPer100g: 0,
|
|
saltPer100g: 0,
|
|
proteinPer100g: 10,
|
|
fiberPer100g: 10,
|
|
fruitVegetableLegumePercent: 0
|
|
});
|
|
assert.equal(calculateNutriScore(item)?.grade, 'A');
|
|
assert.match(calculateProductRating(item, preferences).categoryScores.nutrition.message, /Berechneter Nutri-Score A/);
|
|
});
|
|
|
|
test('high sugar, salt and saturated fat produce E even when positive data is absent', () => {
|
|
const item = product({
|
|
energyKjPer100g: 2000,
|
|
saturatedFatPer100g: 10,
|
|
sugarPer100g: 55,
|
|
saltPer100g: 5
|
|
});
|
|
assert.equal(calculateNutriScore(item)?.grade, 'E');
|
|
});
|
|
|
|
test('uses beverage thresholds and sweetener penalty', () => {
|
|
const nutrition = {
|
|
energyKjPer100g: 0,
|
|
saturatedFatPer100g: 0,
|
|
sugarPer100g: 0,
|
|
saltPer100g: 0,
|
|
proteinPer100g: 0,
|
|
fiberPer100g: 0,
|
|
fruitVegetableLegumePercent: 0,
|
|
hasNonNutritiveSweeteners: false
|
|
};
|
|
assert.equal(calculateNutriScore(product(nutrition), true, false)?.grade, 'B');
|
|
assert.equal(calculateNutriScore(product(nutrition), true, true)?.grade, 'A');
|
|
assert.equal(calculateNutriScore(product({ ...nutrition, hasNonNutritiveSweeteners: true }), true, false)?.grade, 'C');
|
|
});
|
|
|
|
test('does not apply cheese rules to a cheese sandwich', () => {
|
|
const nutrition = {
|
|
energyKjPer100g: 2000,
|
|
saturatedFatPer100g: 5,
|
|
sugarPer100g: 10,
|
|
saltPer100g: 0.5,
|
|
proteinPer100g: 20,
|
|
fiberPer100g: 0,
|
|
fruitVegetableLegumePercent: 0
|
|
};
|
|
assert.equal(calculateNutriScore(product(nutrition, ['en:cheese-sandwiches']))?.grade, 'D');
|
|
assert.equal(calculateNutriScore(product(nutrition, ['en:cheeses']))?.grade, 'C');
|
|
});
|
|
|
|
test('fat products use energy from saturated fat and do not need a total energy field', () => {
|
|
const item = product({
|
|
saturatedFatPer100g: 5, fatPer100g: 80, sugarPer100g: 0, saltPer100g: 0,
|
|
proteinPer100g: 0, fiberPer100g: 0, fruitVegetableLegumePercent: 0
|
|
}, ['en:vegetable-oils']);
|
|
assert.equal(calculateNutriScore(item)?.grade, 'B');
|
|
});
|
|
|
|
test('does not calculate a grade for excluded products', () => {
|
|
const item = product({
|
|
energyKjPer100g: 100, saturatedFatPer100g: 0, sugarPer100g: 0,
|
|
saltPer100g: 0, proteinPer100g: 0, fiberPer100g: 0,
|
|
fruitVegetableLegumePercent: 0
|
|
}, ['en:alcoholic-beverages']);
|
|
assert.equal(calculateNutriScore(item, true), null);
|
|
});
|
|
|
|
test('missing or ambiguous nutrient values do not create a grade or skew overall rating', () => {
|
|
const item = product({ sugarPer100g: 1 });
|
|
assert.equal(calculateNutriScore(item), null);
|
|
assert.equal(calculateProductRating(item, preferences).categoryScores.nutrition.hasKnownFacts, false);
|
|
|
|
const ambiguous = product({ energyKjPer100g: 500, saturatedFatPer100g: 0, sugarPer100g: 0, saltPer100g: 0 });
|
|
assert.equal(calculateNutriScore(ambiguous), null);
|
|
});
|
|
|
|
test('keeps a supplied Nutri-Score authoritative', () => {
|
|
const item = product({ nutriScore: 'D', sugarPer100g: 0 });
|
|
const nutritionRating = calculateProductRating(item, preferences).categoryScores.nutrition;
|
|
assert.match(nutritionRating.message, /Nutri-Score D/);
|
|
assert.doesNotMatch(nutritionRating.message, /Berechneter/);
|
|
});
|
|
|
|
test('maps the Open Food Facts fields required by the fallback', async () => {
|
|
const originalFetch = globalThis.fetch;
|
|
globalThis.fetch = async () => ({
|
|
ok: true,
|
|
json: async () => ({ status: 1, product: {
|
|
code: '12345',
|
|
categories_tags: ['en:beverages'],
|
|
additives_tags: ['en:e955'],
|
|
nutrition_grades: 'b',
|
|
environmental_score_grade: 'a',
|
|
nutriments: {
|
|
energy_100g: 100,
|
|
sugars_100g: 2,
|
|
'saturated-fat_100g': 0,
|
|
salt_100g: 0,
|
|
proteins_100g: 0,
|
|
fiber_100g: 0,
|
|
'fruits-vegetables-legumes-estimate-from-ingredients_100g': 0
|
|
}
|
|
} })
|
|
});
|
|
|
|
try {
|
|
const result = await lookupProductByBarcode('12345');
|
|
assert.equal(result.product.nutrition.energyKjPer100g, 100);
|
|
assert.equal(result.product.nutrition.fiberPer100g, 0);
|
|
assert.equal(result.product.nutrition.hasNonNutritiveSweeteners, true);
|
|
assert.equal(result.product.nutrition.nutriScore, 'B');
|
|
assert.equal(result.product.sustainability.ecoScore, 'A');
|
|
assert.equal(calculateNutriScore(result.product, true)?.grade, 'D');
|
|
} finally {
|
|
globalThis.fetch = originalFetch;
|
|
}
|
|
});
|