add features for manual product editing
Android Release Build / build-android (push) Successful in 18m57s
Android Release Build / build-android (push) Successful in 18m57s
This commit is contained in:
@@ -0,0 +1,57 @@
|
||||
import test from 'node:test';
|
||||
import assert from 'node:assert/strict';
|
||||
|
||||
import { createLocalProduct, saveManualProduct } from '../src/services/manualProduct.js';
|
||||
import { lookupProductByBarcode, mapOpenFoodFactsProduct } from '../src/services/productApi.js';
|
||||
import { calculateProductRating } from '../src/services/ratingService.js';
|
||||
import { defaultPreferences } from '../src/utils/constants.js';
|
||||
|
||||
test('an unknown barcode can be saved locally without inventing product facts', async () => {
|
||||
const blank = createLocalProduct('7612345678901');
|
||||
assert.equal(calculateProductRating(blank, defaultPreferences).overallStatus, 'unknown');
|
||||
const saved = saveManualProduct(blank, { name: 'Apfelschorle', nutrition: { sugarPer100g: 5 } });
|
||||
assert.equal(saved.dataSource, 'local');
|
||||
assert.equal(saved.nutrition.sugarPer100g, 5);
|
||||
assert.equal(saved.nutrition.nutriScore, undefined);
|
||||
const originalFetch = globalThis.fetch;
|
||||
globalThis.fetch = () => { throw new Error('Local product must not trigger a request'); };
|
||||
try {
|
||||
const result = await lookupProductByBarcode(saved.barcode, { [saved.barcode]: saved });
|
||||
assert.equal(result.status, 'found');
|
||||
assert.equal(result.product.name, 'Apfelschorle');
|
||||
assert.equal(result.product.dataSource, 'local');
|
||||
} finally {
|
||||
globalThis.fetch = originalFetch;
|
||||
}
|
||||
});
|
||||
|
||||
test('a barcode-only Open Food Facts entry keeps its missing fields visible', () => {
|
||||
const item = mapOpenFoodFactsProduct({ code: '7612345678903', nutriments: {} });
|
||||
assert.equal(item.dataCompleteness.name, false);
|
||||
assert.equal(item.dataCompleteness.nutrition, false);
|
||||
assert.equal(item.nutrition.nutriScore, undefined);
|
||||
});
|
||||
|
||||
test('manual label corrections survive an Open Food Facts refresh', async () => {
|
||||
const barcode = '7612345678902';
|
||||
const cached = saveManualProduct(mapOpenFoodFactsProduct({
|
||||
code: barcode, product_name: 'Alte Limonade', categories_tags: ['en:beverages'],
|
||||
nutriments: { sugars_100g: 12 }, ingredients_text: 'Wasser, Zucker', nutriscore_grade: 'D'
|
||||
}), { name: 'Etikettname', nutrition: { sugarPer100g: 4 } });
|
||||
cached.fetchedAt = '2020-01-01T00:00:00.000Z';
|
||||
const originalFetch = globalThis.fetch;
|
||||
globalThis.fetch = async () => ({ ok: true, json: async () => ({ status: 1, product: {
|
||||
code: barcode, product_name: 'Neuer OFF Name', categories_tags: ['en:beverages'],
|
||||
nutriments: { sugars_100g: 10, salt_100g: 0.1 }
|
||||
} }) });
|
||||
try {
|
||||
const result = await lookupProductByBarcode(barcode, { [barcode]: cached });
|
||||
assert.equal(result.product.name, 'Etikettname');
|
||||
assert.equal(result.product.nutrition.sugarPer100g, 4);
|
||||
assert.equal(result.product.nutrition.saltPer100g, 0.1);
|
||||
assert.equal(result.product.nutrition.nutriScore, undefined);
|
||||
assert.equal(result.product.manualOverrides.name, 'Etikettname');
|
||||
} finally {
|
||||
globalThis.fetch = originalFetch;
|
||||
}
|
||||
});
|
||||
Reference in New Issue
Block a user