add better eco ratings
Android Release Build / build-android (push) Successful in 13m35s

This commit is contained in:
Frederik Spirgi
2026-10-26 11:42:28 +01:00
parent f12a9d47b6
commit 1d32aeaf29
9 changed files with 255 additions and 35 deletions
+66 -1
View File
@@ -3,6 +3,7 @@ import assert from 'node:assert/strict';
import { calculateProductRating } from '../src/services/ratingService.js';
import { findAlternatives } from '../src/services/alternativesService.js';
import { mapOpenFoodFactsProduct } from '../src/services/productApi.js';
const preferences = { avoidAllergens: [], preferredDiet: 'none' };
const rate = (product, customPreferences = {}) => calculateProductRating(
@@ -45,6 +46,70 @@ test('a confirmed avoided allergen still blocks the recommendation', () => {
assert.equal(rating.limitedByMissingData, false);
});
test('an OFF allergen list does not prove a different allergen is absent', () => {
const product = mapOpenFoodFactsProduct({
code: '123', allergens_tags: ['en:soybeans'], traces_tags: [], nutriscore_grade: 'a'
});
const rating = rate(product, { avoidAllergens: ['Milch'] });
assert.equal(rating.categoryScores.allergies.status, 'unknown');
assert.equal(rating.allergenReviewRequired, true);
assert.notEqual(rating.overallStatus, 'good');
});
test('possible traces of a selected allergen require review', () => {
const product = mapOpenFoodFactsProduct({
code: '124', allergens_tags: ['en:soybeans'], traces_tags: ['en:milk'], nutriscore_grade: 'a'
});
const rating = rate(product, { avoidAllergens: ['Milch'] });
assert.equal(rating.categoryScores.allergies.status, 'unknown');
assert.match(rating.allergenReviewMessage, /Spuren von Milch/);
assert.equal(rating.allergenReviewRequired, true);
});
test('a verified absence can contribute to the allergy score', () => {
const rating = rate({
allergens: ['Soja'], allergenTraces: [], dataCompleteness: { allergensVerified: true }
}, { avoidAllergens: ['Milch'] });
assert.equal(rating.categoryScores.allergies.score, 100);
assert.equal(rating.allergenReviewRequired, false);
});
test('empty additive tags without ingredients do not earn ingredient points', () => {
const product = mapOpenFoodFactsProduct({ code: '125', additives_tags: [] });
const rating = rate(product);
assert.equal(product.dataCompleteness.additives, false);
assert.equal(rating.categoryScores.ingredients.status, 'unknown');
});
test('selling countries do not stand in for ingredient origin', () => {
const product = mapOpenFoodFactsProduct({
code: '126', countries_tags: ['en:switzerland'], categories_tags: ['en:unknown-food']
});
const rating = rate(product);
assert.equal(product.sustainability.originCountry, undefined);
assert.deepEqual(product.sustainability.sellingCountries, ['en:switzerland']);
assert.equal(rating.categoryScores.sustainability.status, 'unknown');
const legacyCache = rate({
dataSource: 'cache', sustainability: { originCountry: 'Schweiz' }
});
assert.equal(legacyCache.categoryScores.sustainability.status, 'unknown');
});
test('stale cached facts cannot keep a green recommendation', () => {
const product = {
cacheStale: true,
nutrition: { nutriScore: 'A' },
ingredients: ['Hafer'],
ingredientAnalysis: { palmOil: false, additives: [] },
dataCompleteness: { additives: true },
sustainability: { ecoScore: 'A' }
};
assert.equal(rate({ ...product, cacheStale: false }).overallStatus, 'good');
const rating = rate(product);
assert.equal(rating.overallScore, 77);
assert.equal(rating.staleDataReviewRequired, true);
});
test('Nutri-Score nutrients are not penalized twice', () => {
const plain = rate({ nutrition: { nutriScore: 'C' } });
const flagged = rate({ nutrition: { nutriScore: 'C', nutrientLevels: { sugars: 'high' } } });
@@ -109,7 +174,7 @@ test('alternatives exclude products whose allergens need review', async () => {
};
const verified = {
id: 'verified', barcode: '3', name: 'Bestätigt', category: 'Snacks', categoryTags: ['en:snacks'],
allergens: ['Soja'], dataCompleteness: { allergens: true }, nutrition: { nutriScore: 'A' }
allergens: ['Soja'], dataCompleteness: { allergens: true, allergensVerified: true }, nutrition: { nutriScore: 'A' }
};
const alternatives = await findAlternatives(source, {
...preferences, avoidAllergens: ['Milch'], offlineModeEnabled: true