// Products kept from the archive ("food.product", food_model.dart): stored as the dashboard's db.save_product // does (uid "p:"), found again by id / barcode / name (folded), and their added sugar. import 'dart:io'; import 'package:dash_app/health/food_model.dart'; import 'package:dash_app/health/sugar.dart'; import 'package:dash_app/sync/store.dart'; import 'package:flutter_test/flutter_test.dart'; void main() { final store = SyncStore('${Directory.systemTemp.createTempSync('product').path}/d.db', 'test00000000'); final item = { 'id': 24032663211215, 'name': 'Zarte Haferflocken', 'brand': 'Kölln', 'barcode': '4032663211215', 'countries': 'germany', 'category': 'Kölln · Rolled oats', 'nutriscore': 'a', 'nova': 1, 'labels': null, 'sugars_in': '', 'dairy': 0, 'added_sugar': null, 'per100': {'kcal': 372, 'protein': 13.5, 'carbs': 58.7, 'sugar': 0.7, 'fat': 7}, 'portions': [{'label': 'serving (40 g)', 'grams': 40}], }; test('kept, found again, synced under p:', () { final f = keepProduct(store, item)!; expect(f.foodId, 24032663211215); expect(f.portions.single.grams, 40); expect(store.row(tProduct, 'p:24032663211215')!['food_id'], 24032663211215); expect(keptProduct(store, 24032663211215)!.name, 'Zarte Haferflocken'); expect(keptByBarcode(store, '4032663211215').single.brand, 'Kölln'); expect(keptProducts(store, 'kolln hafer', fold: fold).single.foodId, 24032663211215); expect(keptProducts(store, 'muesli', fold: fold), isEmpty); }); test('not a product: refused', () { expect(keepProduct(store, {...item, 'id': 123}), isNull); expect(keepProduct(store, {...item, 'name': ' '}), isNull); }); test('added sugar: the label figure first, else its ingredients', () { final oats = keepProduct(store, item)!; expect(foodPart(oats, 0.7, 100), 0); // sugars_in "" → natural final cereal = keepProduct(store, {...item, 'id': 20000000000017, 'name': 'Choco cereal', 'sugars_in': 'sugar', 'added_sugar': 20, 'per100': {'kcal': 390, 'sugar': 25}})!; expect(foodPart(cereal, 25, 100), 20); // the label's 20 of 25 g final zero = keepProduct(store, {...item, 'id': 20000000000018, 'name': 'Wafer', 'nova': 4, 'sugars_in': 'sugar', 'added_sugar': 0, 'per100': {'kcal': 500, 'sugar': 30}})!; expect(foodPart(zero, 30, 100), 30); // "0 added" yet sugar in the ingredients: the 0 is ignored }); }