dashboard/app/test/food_follow_test.dart
bashynx 4193de4262 Food, sync and warmer updates from 2026-10-26
- Food: "All products" lists what's here first (My foods, the food database, kept
  products), then Open Food Facts; the start of a barcode (4+ digits) finds the
  products starting with it (dashboard, app, server search).
- Food: My foods barcodes; entries follow edits of their food or meal, and meals
  made with a changed food are worked out again (nested meals too); a save that
  changes nothing isn't written.
- Food: the week strip shows protein / carbs / fat per day, over-target calories
  in red; the header's arrows go by week, days swap in place with prefetch on
  hover; the app's arrows go by week, a sideways swipe by day.
- Food search: stomach / žalúdok / pipotă find gizzards, kurací / pui chicken,
  morčací / curcan turkey.
- Sync: tables a plugin makes after start-up are prepared at the next sync and
  their file read from the start once.
- Warmers build pages as each admin account and wait while someone is using
  the dashboard; slow page builds are logged.
- Stations: impossible sensor readings are dropped; chart ticks never fail.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-10-26 15:45:32 +02:00

68 lines
4.4 KiB
Dart
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

// Entries logged from one of My foods follow its edits (food_model.dart _follow = the dashboard's db._follow):
// a meal's new recipe or a food's new values / name re-work them for their grams; others aren't touched.
import 'dart:io';
import 'package:dash_app/health/food_model.dart';
import 'package:dash_app/sync/store.dart';
import 'package:flutter_test/flutter_test.dart';
void main() {
final store = SyncStore('${Directory.systemTemp.createTempSync('follow').path}/d.db', 'test00000000');
test('editing a food re-works its entries', () {
final (u, err) = saveMyFood(store, name: 'Tvaroh', per100: {'kcal': 200, 'protein': 14, 'fat': 15}, portions: []);
expect(err, isNull);
final f = myFoodByUid(store, u!)!;
saveFromFood(store, u: 'e1', day: '2026-10-26', meal: 'lunch', food: f, amount: 100, unit: 'g');
saveFromFood(store, u: 'e2', day: '2026-10-27', meal: 'lunch', food: f, amount: 150, unit: 'g');
saveTotals(store, u: 'e3', day: '2026-10-27', meal: 'lunch', name: 'Tvaroh', values: {'kcal': 300});
final h3 = store.rows('food.entry').firstWhere((r) => r['_u'] == 'e3')['_h'];
saveMyFood(store, uid: u, name: 'Tvaroh (homemade)', per100: {'kcal': 212, 'protein': 14, 'fat': 16}, portions: []);
expect(store.row('food.entry', 'e1')!['kcal'], 212);
expect(store.row('food.entry', 'e2')!['kcal'], 318);
expect(store.row('food.entry', 'e2')!['fat'], 24);
expect(store.row('food.entry', 'e2')!['name'], 'Tvaroh (homemade)');
expect(store.row('food.entry', 'e3')!['kcal'], 300); // typed numbers, no food: left alone
expect(store.rows('food.entry').firstWhere((r) => r['_u'] == 'e3')['_h'], h3);
});
test('unchanged entries are not re-sent', () {
final (u, _) = saveMyFood(store, name: 'Casein', per100: {'kcal': 390}, portions: []);
saveFromFood(store, u: 'c1', day: '2026-10-26', meal: 'breakfast', food: myFoodByUid(store, u!)!, amount: 30, unit: 'g');
final h = store.rows('food.entry').firstWhere((r) => r['_u'] == 'c1')['_h'];
saveMyFood(store, uid: u, name: 'Casein', per100: {'kcal': 390}, portions: [], note: 'a note');
expect(store.rows('food.entry').firstWhere((r) => r['_u'] == 'c1')['_h'], h);
});
test('a meal’s new recipe re-works its entries', () {
final (oil, _) = saveMyFood(store, name: 'Oil', per100: {'kcal': 900, 'fat': 100}, portions: []);
final (veg, _) = saveMyFood(store, name: 'Veg', per100: {'kcal': 30, 'fat': 0}, portions: []);
Ingredient ing(String uid, double g) => Ingredient.food(myFoodByUid(store, uid)!, g, 'g');
final (soup, err) = saveMeal(store, name: 'Soup', ings: [ing(oil!, 30), ing(veg!, 970)], portions: []);
expect(err, isNull);
saveFromFood(store, u: 's1', day: '2026-10-27', meal: 'lunch', food: myFoodByUid(store, soup!)!, amount: 400, unit: 'g');
expect(store.row('food.entry', 's1')!['kcal'], closeTo(400 * (27000 + 29100) / 100000, 0.01));
saveMeal(store, uid: soup, name: 'Soup', ings: [ing(oil, 10), ing(veg, 970)], portions: []);
expect(store.row('food.entry', 's1')!['kcal'], closeTo(400 * (9000 + 29100) / 98000, 0.01));
});
test('a meal made with a changed food is worked out again, its entries too', () {
final (curd, _) = saveMyFood(store, name: 'Curd', per100: {'kcal': 100, 'protein': 12}, portions: []);
final (flour, _) = saveMyFood(store, name: 'Flour', per100: {'kcal': 350}, portions: []);
Ingredient ing(String uid, double g) => Ingredient.food(myFoodByUid(store, uid)!, g, 'g');
final (strudel, _) = saveMeal(store, name: 'Strudel', ings: [ing(curd!, 300), ing(flour!, 200)], portions: []);
final (plate, _) = saveMeal(store, name: 'Plate', ings: [ing(strudel!, 250), ing(flour, 50)], portions: []);
saveFromFood(store, u: 'p1', day: '2026-10-27', meal: 'dinner', food: myFoodByUid(store, plate!)!, amount: 150, unit: 'g');
expect(store.row('food.food', strudel)!['kcal'], closeTo((300 + 700) / 5, 0.001));
final hFlour = store.row('food.food', flour)!['_h'];
saveMyFood(store, uid: curd, name: 'Curd', per100: {'kcal': 200, 'protein': 12}, portions: []);
expect(store.row('food.food', strudel)!['kcal'], closeTo((600 + 700) / 5, 0.001));
final plateKcal = (250 * 260 / 100 + 175) * 100 / 300;
expect(store.row('food.food', plate)!['kcal'], closeTo(plateKcal, 0.001));
expect(store.row('food.entry', 'p1')!['kcal'], closeTo(plateKcal * 1.5, 0.01));
expect(store.row('food.food', flour)!['_h'], hFlour); // not an ingredient's meal: untouched
});
}