This commit is contained in:
@@ -113,6 +113,48 @@ test('offline cached scans work and OFF/network failures fail gracefully', async
|
||||
}
|
||||
});
|
||||
|
||||
test('stale product data refreshes online and remains usable offline', async () => {
|
||||
const stale = {
|
||||
...off({ product_name: 'Old name', allergens_tags: ['en:soybeans'] }),
|
||||
fetchedAt: '2020-01-01T00:00:00.000Z', price: 3.5,
|
||||
sustainability: { originCountry: 'Schweiz' }
|
||||
};
|
||||
const originalFetch = globalThis.fetch;
|
||||
let requests = 0;
|
||||
try {
|
||||
globalThis.fetch = async () => {
|
||||
requests += 1;
|
||||
return { ok: true, json: async () => ({ status: 1, product: {
|
||||
code: stale.barcode, product_name: 'New name', allergens_tags: ['en:milk'],
|
||||
origins: 'Frankreich', countries_tags: ['en:switzerland']
|
||||
} }) };
|
||||
};
|
||||
const refreshed = await lookupProductByBarcode(stale.barcode, { [stale.barcode]: stale });
|
||||
assert.equal(requests, 1);
|
||||
assert.equal(refreshed.product.name, 'New name');
|
||||
assert.equal(refreshed.product.price, 3.5);
|
||||
assert.equal(refreshed.product.sustainability.originCountry, 'Frankreich');
|
||||
assert.equal(refreshed.product.sustainability.originSource, 'ingredient-origin');
|
||||
assert.ok(Date.parse(refreshed.product.fetchedAt) > Date.parse(stale.fetchedAt));
|
||||
|
||||
const fresh = await lookupProductByBarcode(stale.barcode, { [stale.barcode]: refreshed.product });
|
||||
assert.equal(requests, 1);
|
||||
assert.equal(fresh.product.name, 'New name');
|
||||
|
||||
const offline = await lookupProductByBarcode(stale.barcode, { [stale.barcode]: stale }, true);
|
||||
assert.equal(requests, 1);
|
||||
assert.equal(offline.product.name, 'Old name');
|
||||
assert.equal(offline.product.cacheStale, true);
|
||||
|
||||
globalThis.fetch = async () => { throw new Error('offline'); };
|
||||
const fallback = await lookupProductByBarcode(stale.barcode, { [stale.barcode]: stale });
|
||||
assert.equal(fallback.product.cacheStale, true);
|
||||
assert.equal(fallback.product.name, 'Old name');
|
||||
} finally {
|
||||
globalThis.fetch = originalFetch;
|
||||
}
|
||||
});
|
||||
|
||||
test('ADEME import failure cannot replace the last good local snapshot', async () => {
|
||||
let wrote = false;
|
||||
await assert.rejects(importAgribalyse({
|
||||
|
||||
@@ -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
|
||||
|
||||
Reference in New Issue
Block a user