This commit is contained in:
+103
-35
@@ -16,20 +16,20 @@ test('shows unknown when no category has reliable facts', () => {
|
||||
assert.equal(rating.overallStatus, 'unknown');
|
||||
});
|
||||
|
||||
test('missing budget and allergen data do not contribute neutral placeholder scores', () => {
|
||||
test('missing goal and allergen data do not contribute placeholder scores', () => {
|
||||
const rating = rate({ nutrition: { nutriScore: 'A' } });
|
||||
assert.equal(rating.categoryScores.budget.hasKnownFacts, false);
|
||||
assert.equal(rating.categoryScores.goals.hasKnownFacts, false);
|
||||
assert.equal(rating.categoryScores.allergies.hasKnownFacts, false);
|
||||
assert.equal(rating.overallScore, 77);
|
||||
assert.equal(rating.categoryScores.budget, undefined);
|
||||
assert.equal(rating.limitedByMissingData, true);
|
||||
});
|
||||
|
||||
test('profile priorities actually change the category weighting', () => {
|
||||
test('sustainability preferences no longer change the rating', () => {
|
||||
const item = { nutrition: { nutriScore: 'A' }, sustainability: { ecoScore: 'E' },
|
||||
ingredientAnalysis: { palmOil: false, additives: [] }, ingredients: ['Hafer'], dataCompleteness: { additives: true } };
|
||||
const nutritionFirst = rate(item, { nutritionPriority: 'high', sustainabilityPriority: 'low' });
|
||||
const sustainabilityFirst = rate(item, { nutritionPriority: 'low', sustainabilityPriority: 'high' });
|
||||
assert.ok(nutritionFirst.overallScore > sustainabilityFirst.overallScore);
|
||||
assert.equal(nutritionFirst.overallScore, sustainabilityFirst.overallScore);
|
||||
});
|
||||
|
||||
test('ingredient text without animal terms is not treated as vegan proof', () => {
|
||||
@@ -89,11 +89,11 @@ test('selling countries do not stand in for ingredient origin', () => {
|
||||
const rating = rate(product);
|
||||
assert.equal(product.sustainability.originCountry, undefined);
|
||||
assert.deepEqual(product.sustainability.sellingCountries, ['en:switzerland']);
|
||||
assert.equal(rating.categoryScores.sustainability.status, 'unknown');
|
||||
assert.equal(rating.categoryScores.sustainability, undefined);
|
||||
const legacyCache = rate({
|
||||
dataSource: 'cache', sustainability: { originCountry: 'Schweiz' }
|
||||
});
|
||||
assert.equal(legacyCache.categoryScores.sustainability.status, 'unknown');
|
||||
assert.equal(legacyCache.categoryScores.sustainability, undefined);
|
||||
});
|
||||
|
||||
test('stale cached facts cannot keep a green recommendation', () => {
|
||||
@@ -105,7 +105,7 @@ test('stale cached facts cannot keep a green recommendation', () => {
|
||||
dataCompleteness: { additives: true },
|
||||
sustainability: { ecoScore: 'A' }
|
||||
};
|
||||
assert.equal(rate({ ...product, cacheStale: false }).overallStatus, 'good');
|
||||
assert.ok(rate({ ...product, cacheStale: false }).overallScore >= rate(product).overallScore);
|
||||
const rating = rate(product);
|
||||
assert.equal(rating.overallScore, 77);
|
||||
assert.equal(rating.staleDataReviewRequired, true);
|
||||
@@ -152,32 +152,32 @@ test('flavoured water with sugar is not treated as plain water', () => {
|
||||
assert.notEqual(rating.categoryScores.nutrition.score, 98);
|
||||
});
|
||||
|
||||
test('country of origin alone cannot establish sustainability', () => {
|
||||
test('country of origin alone cannot affect the rating', () => {
|
||||
const peru = rate({ sustainability: { originCountry: 'Peru' } });
|
||||
const swiss = rate({ sustainability: { originCountry: 'Schweiz' } });
|
||||
assert.equal(peru.categoryScores.sustainability.score, null);
|
||||
assert.equal(swiss.categoryScores.sustainability.score, null);
|
||||
assert.equal(peru.overallScore, swiss.overallScore);
|
||||
});
|
||||
|
||||
test('Eco-Score is not counted again through its environmental indicators', () => {
|
||||
test('Eco-Score does not affect the health rating', () => {
|
||||
const rating = rate({ sustainability: {
|
||||
ecoScore: 'E', carbonFootprintKgCo2ePerKg: 0.1,
|
||||
originCountry: 'Schweiz', packaging: 'Glas'
|
||||
} });
|
||||
assert.equal(rating.categoryScores.sustainability.score, 15);
|
||||
assert.equal(rating.categoryScores.sustainability, undefined);
|
||||
assert.equal(rating.overallScore, null);
|
||||
});
|
||||
|
||||
test('alternatives present unverified allergens only with a clear review path', async () => {
|
||||
const source = {
|
||||
id: 'source', barcode: '1', name: 'Original', category: 'Snacks', categoryTags: ['en:snacks'],
|
||||
id: 'source', barcode: '1', name: 'Schokolade', category: 'Snacks', categoryTags: ['en:snacks', 'en:chocolates'],
|
||||
allergens: ['Milch'], dataCompleteness: { allergens: true }
|
||||
};
|
||||
const unknown = {
|
||||
id: 'unknown', barcode: '2', name: 'Unklar', category: 'Snacks', categoryTags: ['en:snacks'],
|
||||
id: 'unknown', barcode: '2', name: 'Schokolade A', category: 'Snacks', categoryTags: ['en:snacks', 'en:chocolates'],
|
||||
allergens: [], dataCompleteness: { allergens: false }, nutrition: { nutriScore: 'A' }
|
||||
};
|
||||
const verified = {
|
||||
id: 'verified', barcode: '3', name: 'Bestätigt', category: 'Snacks', categoryTags: ['en:snacks'],
|
||||
id: 'verified', barcode: '3', name: 'Schokolade B', category: 'Snacks', categoryTags: ['en:snacks', 'en:chocolates'],
|
||||
allergens: ['Soja'], dataCompleteness: { allergens: true, allergensVerified: true }, nutrition: { nutriScore: 'A' }
|
||||
};
|
||||
const alternatives = await findAlternatives(source, {
|
||||
@@ -190,9 +190,9 @@ test('alternatives present unverified allergens only with a clear review path',
|
||||
});
|
||||
|
||||
test('allergy-review alternatives remain discoverable when both displayed scores are capped', async () => {
|
||||
const source = { id: 'source', barcode: '10', name: 'Original', categoryTags: ['en:snacks'],
|
||||
const source = { id: 'source', barcode: '10', name: 'Schokolade', categoryTags: ['en:snacks', 'en:chocolates'],
|
||||
nutrition: { nutriScore: 'C' }, sustainability: { ecoScore: 'A' }, allergens: [] };
|
||||
const candidate = { id: 'candidate', barcode: '11', name: 'Besser', categoryTags: ['en:snacks'],
|
||||
const candidate = { id: 'candidate', barcode: '11', name: 'Bessere Schokolade', categoryTags: ['en:snacks', 'en:chocolates'],
|
||||
nutrition: { nutriScore: 'A' }, sustainability: { ecoScore: 'A' }, allergens: [] };
|
||||
const pref = { ...preferences, avoidAllergens: ['Milch'], offlineModeEnabled: true };
|
||||
const alternatives = await findAlternatives(source, pref, { candidate });
|
||||
@@ -221,28 +221,26 @@ test('coverage counts underlying facts and ignores packaging as a score', () =>
|
||||
const rating = rate({ nutrition: { nutriScore: 'A' }, ingredientAnalysis: { palmOil: false },
|
||||
sustainability: { packaging: 'Glas' } });
|
||||
assert.ok(rating.dataCoverage < 80);
|
||||
assert.equal(rating.categoryScores.sustainability.score, null);
|
||||
assert.equal(rating.categoryScores.sustainability, undefined);
|
||||
assert.notEqual(rating.overallStatus, 'good');
|
||||
});
|
||||
|
||||
test('budget includes open list prices and treats unknown prices as uncertainty', () => {
|
||||
test('budget and open list prices do not affect the rating', () => {
|
||||
const item = { price: 5, nutrition: { nutriScore: 'A' } };
|
||||
const pref = { maxBudgetPerShoppingTrip: 20, budgetPeriod: 'shoppingTrip' };
|
||||
const list = { items: [{ estimatedPrice: 18, quantity: 1, checked: false }] };
|
||||
const exceeded = calculateProductRating(item, { ...preferences, ...pref }, { shoppingList: list });
|
||||
assert.equal(exceeded.categoryScores.budget.status, 'critical');
|
||||
assert.match(exceeded.categoryScores.budget.message, /überschreitet/);
|
||||
assert.equal(exceeded.categoryScores.budget, undefined);
|
||||
const missing = calculateProductRating(item, { ...preferences, ...pref },
|
||||
{ shoppingList: { items: [{ quantity: 1, checked: false }] } });
|
||||
assert.equal(missing.budgetReviewRequired, true);
|
||||
assert.notEqual(missing.overallStatus, 'good');
|
||||
assert.equal(missing.budgetReviewRequired, false);
|
||||
assert.equal(exceeded.overallScore, missing.overallScore);
|
||||
});
|
||||
|
||||
test('a category-average footprint has partial coverage', () => {
|
||||
test('a category-average footprint does not create health coverage', () => {
|
||||
const rating = rate({ sustainability: { carbonFootprintKgCo2ePerKg: 0.5,
|
||||
footprint: { status: 'estimated' } } });
|
||||
assert.ok(rating.dataCoverage > 0);
|
||||
assert.ok(rating.dataCoverage < 50);
|
||||
assert.equal(rating.dataCoverage, 0);
|
||||
});
|
||||
|
||||
test('online alternatives search category pages for healthier drinks', async () => {
|
||||
@@ -285,10 +283,10 @@ test('online alternatives search category pages for healthier drinks', async ()
|
||||
}
|
||||
});
|
||||
|
||||
test('offline alternatives use only saved products', async () => {
|
||||
const source = { id: 'source', barcode: '1', categoryTags: ['en:beverages'],
|
||||
test('offline alternatives use only saved products of the same type', async () => {
|
||||
const source = { id: 'source', barcode: '1', name: 'Cola', categoryTags: ['en:beverages', 'en:sodas'],
|
||||
nutrition: { nutriScore: 'E' } };
|
||||
const cached = { id: 'cached', barcode: '2', name: 'Wasser', categoryTags: ['en:beverages'],
|
||||
const cached = { id: 'cached', barcode: '2', name: 'Cola light', categoryTags: ['en:beverages', 'en:sodas'],
|
||||
nutrition: { nutriScore: 'A' } };
|
||||
const originalFetch = globalThis.fetch;
|
||||
try {
|
||||
@@ -301,11 +299,81 @@ test('offline alternatives use only saved products', async () => {
|
||||
}
|
||||
});
|
||||
|
||||
test('a sweet soda suggests a lower-sugar soda but never water', async () => {
|
||||
const source = { id: 'source', barcode: '1', name: 'Süsse Cola',
|
||||
categoryTags: ['en:beverages', 'en:sodas'],
|
||||
nutrition: { nutriScore: 'D', sugarPer100g: 20 } };
|
||||
const lowerSugar = { id: 'lower-sugar', barcode: '2', name: 'Cola mit weniger Zucker',
|
||||
categoryTags: ['en:beverages', 'en:sodas'],
|
||||
nutrition: { nutriScore: 'D', sugarPer100g: 8 } };
|
||||
const water = { id: 'water', barcode: '3', name: 'Wasser',
|
||||
categoryTags: ['en:beverages', 'en:waters'],
|
||||
nutrition: { nutriScore: 'A', sugarPer100g: 0 } };
|
||||
const alternatives = await findAlternatives(source, { ...preferences, offlineModeEnabled: true },
|
||||
{ lowerSugar, water }, {}, { preview: true });
|
||||
assert.deepEqual(alternatives.map((item) => item.product.id), ['lower-sugar']);
|
||||
assert.match(alternatives[0].reason, /8 statt 20 g/);
|
||||
});
|
||||
|
||||
test('automatic preview continues to a related category when the first tag is empty', async () => {
|
||||
const source = { id: 'spread', barcode: '1', name: 'Haselnussaufstrich',
|
||||
category: 'Sweet spreads',
|
||||
categoryTags: ['en:spreads', 'en:sweet-spreads', 'en:chocolate-spreads', 'en:hazelnut-spreads'],
|
||||
nutrition: { nutriScore: 'E', sugarPer100g: 45 } };
|
||||
const requests = [];
|
||||
const originalFetch = globalThis.fetch;
|
||||
try {
|
||||
globalThis.fetch = async (url) => {
|
||||
const parsed = new URL(url);
|
||||
const tag = parsed.searchParams.get('categories_tags');
|
||||
requests.push(tag);
|
||||
return { ok: true, json: async () => ({ products: tag === 'en:chocolate-spreads'
|
||||
&& !parsed.searchParams.has('nutrition_grades_tags')
|
||||
? [{ code: '2', product_name: 'Schokoaufstrich mit weniger Zucker',
|
||||
categories_tags: ['en:spreads', 'en:chocolate-spreads'], nutriscore_grade: 'c',
|
||||
nutriments: { sugars_100g: 15 } }] : [] }) };
|
||||
};
|
||||
const alternatives = await findAlternatives(source, { ...preferences, offlineModeEnabled: false },
|
||||
{}, {}, { preview: true });
|
||||
assert.deepEqual(alternatives.map((item) => item.product.barcode), ['2']);
|
||||
assert.ok(requests.includes('en:hazelnut-spreads'));
|
||||
assert.ok(requests.includes('en:chocolate-spreads'));
|
||||
} finally {
|
||||
globalThis.fetch = originalFetch;
|
||||
}
|
||||
});
|
||||
|
||||
test('automatic preview searches by product type when only a broad category is available', async () => {
|
||||
const source = { id: 'cola', barcode: '1', name: 'Cola Original',
|
||||
category: 'Beverages', categoryTags: ['en:beverages'],
|
||||
nutrition: { nutriScore: 'D', sugarPer100g: 20 } };
|
||||
const queries = [];
|
||||
const originalFetch = globalThis.fetch;
|
||||
try {
|
||||
globalThis.fetch = async (url) => {
|
||||
const parsed = new URL(url);
|
||||
if (parsed.pathname.endsWith('/cgi/search.pl')) {
|
||||
queries.push(parsed.searchParams.get('search_terms'));
|
||||
return { ok: true, json: async () => ({ products: [{ code: '2',
|
||||
product_name: 'Cola mit weniger Zucker', categories_tags: ['en:beverages', 'en:sodas'],
|
||||
nutriscore_grade: 'd', nutriments: { sugars_100g: 8 } }] }) };
|
||||
}
|
||||
return { ok: true, json: async () => ({ products: [] }) };
|
||||
};
|
||||
const alternatives = await findAlternatives(source, { ...preferences, offlineModeEnabled: false },
|
||||
{}, {}, { preview: true });
|
||||
assert.ok(queries.includes('cola'));
|
||||
assert.deepEqual(alternatives.map((item) => item.product.barcode), ['2']);
|
||||
} finally {
|
||||
globalThis.fetch = originalFetch;
|
||||
}
|
||||
});
|
||||
|
||||
test('different brands with the same product name remain separate alternatives', async () => {
|
||||
const source = { id: 'source', barcode: '1', categoryTags: ['en:beverages'],
|
||||
const source = { id: 'source', barcode: '1', name: 'Cola', categoryTags: ['en:beverages', 'en:sodas'],
|
||||
nutrition: { nutriScore: 'E' } };
|
||||
const first = { id: 'first', barcode: '2', name: 'Mineralwasser', brand: 'A',
|
||||
categoryTags: ['en:beverages'], nutrition: { nutriScore: 'A' } };
|
||||
const first = { id: 'first', barcode: '2', name: 'Cola light', brand: 'A',
|
||||
categoryTags: ['en:beverages', 'en:sodas'], nutrition: { nutriScore: 'A' } };
|
||||
const second = { ...first, id: 'second', barcode: '3', brand: 'B' };
|
||||
const alternatives = await findAlternatives(source,
|
||||
{ ...preferences, offlineModeEnabled: true }, { first, second });
|
||||
@@ -313,7 +381,7 @@ test('different brands with the same product name remain separate alternatives',
|
||||
});
|
||||
|
||||
test('online search broadens to products without an official grade and reports a complete outage', async () => {
|
||||
const source = { id: 'source', barcode: '1', categoryTags: ['en:beverages'],
|
||||
const source = { id: 'source', barcode: '1', name: 'Cola', categoryTags: ['en:beverages', 'en:sodas'],
|
||||
nutrition: { nutriScore: 'E' } };
|
||||
const originalFetch = globalThis.fetch;
|
||||
const requests = [];
|
||||
@@ -322,7 +390,7 @@ test('online search broadens to products without an official grade and reports a
|
||||
const parsed = new URL(url);
|
||||
requests.push(parsed);
|
||||
return { ok: true, json: async () => ({ products: parsed.searchParams.has('nutrition_grades_tags')
|
||||
? [] : [{ code: '2', product_name: 'Wasser', categories_tags: ['en:beverages'],
|
||||
? [] : [{ code: '2', product_name: 'Cola light', categories_tags: ['en:beverages', 'en:sodas'],
|
||||
nutriscore_grade: 'a' }] }) };
|
||||
};
|
||||
const alternatives = await findAlternatives(source, { ...preferences, offlineModeEnabled: false });
|
||||
|
||||
Reference in New Issue
Block a user