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({
|
||||
|
||||
Reference in New Issue
Block a user