// The dashboard's settings as they sync (prefs.dart = settingsync.py): values read with the dashboard's // defaults until they've come, the food split from its preset or the custom fields, the fields' checks. import 'dart:convert'; import 'dart:io'; import 'package:dash_app/app/prefs.dart'; import 'package:dash_app/health/cycle_model.dart'; import 'package:dash_app/health/food_model.dart'; import 'package:dash_app/sync/store.dart'; import 'package:dash_app/work/work_model.dart'; import 'package:flutter_test/flutter_test.dart'; void main() { final store = SyncStore('${Directory.systemTemp.createTempSync('prefs').path}/d.db', 'test00000000'); void fromDashboard(String k, Object? v) => store.put(tSetting, k, {'k': k, 'v': jsonEncode(v)}); test('defaults until the dashboard\'s values come', () { final f = FoodSettings.of(store); expect(f.split, (20, 50, 30)); expect(f.goalRate, 0); expect(CycleSettings.of(store).cycleLength, 28); expect(WorkSettings.of(store).dayStart, 7); }); test('the dashboard\'s values: presets, the custom split, numbers as stored', () { fromDashboard('food.macro_preset', 'high_protein'); fromDashboard('food.goal_rate', 0.25); fromDashboard('cycle.cycle_length', 30.0); fromDashboard('workload.currency', 'RON'); expect(FoodSettings.of(store).split, (30, 40, 30)); expect(FoodSettings.of(store).goalRate, 0.25); expect(CycleSettings.of(store).cycleLength, 30); expect(WorkSettings.of(store).currency, 'RON'); fromDashboard('food.macro_preset', 'custom'); fromDashboard('food.custom_protein', 35); fromDashboard('food.custom_carbs', 35); fromDashboard('food.custom_fat', 30); expect(FoodSettings.of(store).split, (35, 35, 30)); }); test('set writes only a change', () { final p = Prefs(store); p.set('food.sugar_max', 40); final h = store.rows(tSetting).firstWhere((r) => r['_u'] == 'food.sugar_max')['_h']; p.set('food.sugar_max', 40); expect(store.rows(tSetting).firstWhere((r) => r['_u'] == 'food.sugar_max')['_h'], h); expect(FoodSettings.of(store).sugarMax, 40); }); test('fields: sections in order, checks as the dashboard', () { void field(String k, int pos, Map spec) => store.put(tField, k, {'k': k, 'plugin': k.split('.').first, 'title': k.split('.').first, 'pos': pos, 'spec': jsonEncode(spec)}); field('cycle.cycle_length', 0, {'key': 'cycle_length', 'type': 'number', 'min': 15, 'max': 60, 'integer': true}); field('food.goal_rate', 1, {'key': 'goal_rate', 'type': 'select', 'options': [[0.0, 'Keep'], [0.25, 'Lose 0.25 %']]}); field('workload.currency', 2, {'key': 'currency', 'type': 'text', 'max': 3, 'pattern': '[A-Za-z]{3}'}); final secs = Prefs(store).sections(); expect([for (final s in secs) s.$1], ['cycle', 'food', 'workload']); final cl = secs[0].$3.single, gr = secs[1].$3.single, cur = secs[2].$3.single; expect(cl.check('29.6'), (29, null)); expect(cl.check('80').$2, 'Must be between 15 and 60.'); expect(gr.check(0.25), (0.25, null)); expect(gr.check('1').$2, 'Pick one of the options.'); expect(cur.check('eu1').$2, isNotNull); expect(cur.check('EUR'), ('EUR', null)); }); }