// work_model.dart against the dashboard's own numbers (plugins/workload views.week / report) on the // same rows. The fixture is real data, so it isn't in the repo: make it on the Pi (rows in wire // format + the Pi's results) and run WORK_FIXTURE= flutter test test/work_model_test.dart import 'dart:convert'; import 'dart:io'; 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 path = Platform.environment['WORK_FIXTURE']; final skip = path == null ? 'WORK_FIXTURE not set' : null; late SyncStore store; late Map exp; late DateTime now; setUpAll(() { if (path == null) return; final fx = jsonDecode(File(path).readAsStringSync()) as Map; exp = fx['expect'] as Map; now = DateTime.parse(exp['today'] as String).add(const Duration(hours: 12)); final dir = Directory.systemTemp.createTempSync('work_test'); store = SyncStore('${dir.path}/data.db', 'test00000000'); for (final r in (fx['rows'] as List).cast()) { store.put(r[0] as String, r[1] as String, (r[2] as Map).cast()); } }); test('weeks match the Pi', () { final t = Tree(store); for (final MapEntry(key: off, value: w) in (exp['weeks'] as Map).entries) { final v = week(store, t, int.parse(off), now); expect(hm(v.minutes), w['total'], reason: 'week $off total'); expect(hm(v.billableMinutes), w['billable'], reason: 'week $off billable'); expect([for (final e in (v.amounts.entries.toList()..sort((a, b) => a.key.compareTo(b.key)))) money(e.value, e.key)], w['amounts'], reason: 'week $off amounts'); expect([for (final d in v.days) [d.billableMinutes > 0 ? hm(d.billableMinutes) : '', d.minutes - d.billableMinutes > 0 ? hm(d.minutes - d.billableMinutes) : '']], w['days'], reason: 'week $off days'); expect([for (final c in v.customers) [c.node.name, c.minutes > 0 ? hm(c.minutes) : '', [for (final e in (c.amounts.entries.toList()..sort((a, b) => a.key.compareTo(b.key)))) if (e.value != 0) money(e.value, e.key)].join(' · '), c.rate]], w['customers'], reason: 'week $off customers'); } }, skip: skip); test('reports match the Pi', () { final t = Tree(store); for (final MapEntry(key: p, value: r) in (exp['reports'] as Map).entries) { final (from, to) = presetRange(p, now)!; final v = report(store, t, from, to); String ams(Agg a) => [for (final e in (a.amounts.entries.toList()..sort((a, b) => a.key.compareTo(b.key)))) money(e.value, e.key)].join('|'); expect(hm(v.total.m), r['total'], reason: '$p total'); expect(hm(v.total.bm), r['billable'], reason: '$p billable'); expect(ams(v.total), (r['amounts'] as List).join('|'), reason: '$p amounts'); expect(v.items.length, r['n'], reason: '$p entries'); expect([for (final x in v.rows) [x.node.name, x.depth, hm(x.agg.m), hm(x.agg.bm), ams(x.agg)]], [for (final x in (r['rows'] as List).cast()) [x[0], x[1], x[2], x[3], (x[4] as List).join('|')]], reason: '$p rows'); } }, skip: skip); test('saving round-trips and validates', () { final t = Tree(store); final leaf = t.walk().last.$1; final f = EntryForm(node: leaf.id, day: DateTime(2030, 1, 1), startMin: 23 * 60, endMin: 60, billable: true, note: ' x '); final id = saveEntry(store, t, f); final r = store.row(tEntry, id)!; expect(r['start'], '2030-01-01 23:00'); expect(r['end'], '2030-01-02 01:00'); // past midnight → the next day expect(r['note'], 'x'); expect(() => saveEntry(store, t, EntryForm(node: leaf.id, day: DateTime(2030, 1, 1), startMin: 60, endMin: 60, endDay: DateTime(2030, 1, 1), billable: true)), throwsA(isA())); deleteEntry(store, id); expect(store.row(tEntry, id), isNull); // a node with booked time can't be deleted; a fresh one can, with its rates final top = t.walk().first.$1; expect(deleteNode(store, t, top.id), isFalse); final nid = saveNode(store, t, NodeForm(kind: 'task', parent: top.id, name: 'Test', rates: [('', '12,5'), ('2031-01-01', '15')])); final t2 = Tree(store); expect(t2.rate(nid, '2030-06-01').$1, 12.5); expect(t2.rate(nid, '2031-02-01').$1, 15); expect(store.row(tRate, '$nid:$epoch')!['rate'], 12.5); expect(() => saveNode(store, t2, NodeForm(kind: 'project', parent: nid, name: 'Bad')), throwsA(isA())); expect(deleteNode(store, t2, nid), isTrue); expect(store.row(tRate, '$nid:2031-01-01'), isNull); }, skip: skip); test('week numbers and blocks keep to the wall clock across summer time', () { expect(isoWeek(DateTime.utc(2026, 10, 19)), 43); expect(isoWeek(DateTime.utc(2026, 10, 26)), 44); // after the clocks went back on the 25th expect(isoWeek(DateTime.utc(2026, 3, 30)), 14); // after they went forward on the 29th expect(isoWeek(DateTime.utc(2026, 12, 28)), 53); expect(isoWeek(DateTime.utc(2027, 1, 4)), 1); final dir = Directory.systemTemp.createTempSync('dst'); final st = SyncStore('${dir.path}/d.db', 'dst000000000'); st.put(tNode, 'n', {'kind': 'client', 'parent_id': null, 'name': 'X', 'colour': '#ffb33a', 'billable': 1, 'currency': 'EUR', 'notes': '', 'archived': 0, 'created': ''}); st.put(tEntry, 'e', {'node_id': 'n', 'start': '2026-10-25 10:00', 'end': '2026-10-25 12:30', 'billable': 1, 'note': '', 'created': '', 'updated': ''}); final v = week(st, Tree(st), 0, DateTime(2026, 10, 21, 12)); final b = v.days[6].blocks.single; expect([b.a, b.b], [600, 750]); expect(hm(v.minutes), '2:30'); }); test('money and hours format like the Pi', () { expect(money(1234567.5, 'EUR'), '1 234 567.50 EUR'); expect(money(0, 'RON'), '0.00 RON'); expect(hm(90), '1:30'); expect(num0(20.0), '20'); expect(num0(22.5), '22.5'); }); }