From e47144581bf0c62d59c04bf6de416e05d3ab909a Mon Sep 17 00:00:00 2001 From: KuljeetSinghXDA <22261392+KuljeetSinghXDA@users.noreply.github.com> Date: Mon, 16 Nov 2026 17:28:31 +0530 Subject: [PATCH 1/3] steps: a counter day's screen total is the counter's, not the phone's `getDaySteps` presented a day from the strap's on-chip counter only when `live_coverage` held no rows for it. That is the rare case: the phone banks an hour row for every hour it covers, and the counter is chosen exactly when its calibrated total beat the whole coverage ladder. So on the ordinary gen5 day with phone steps on, the Steps screen fell through to the coverage branch and published the phone's smaller total as the day's number, labelled "Your phone", while Home showed the counter's figure for the same day. The bundle's `source` is the authority: a `strap_counter` day is presented from the counter's own hourly spans, which is also the clocked data the coverage rows could never justify. An older derive that stored no spans now says the times are missing, as that card already does for the empty case. --- lib/data/local_repository_impl.dart | 20 +++- lib/ui2/screens/day_steps.dart | 8 +- .../day_steps_counter_over_coverage_test.dart | 101 ++++++++++++++++++ 3 files changed, 121 insertions(+), 8 deletions(-) create mode 100644 test/day_steps_counter_over_coverage_test.dart diff --git a/lib/data/local_repository_impl.dart b/lib/data/local_repository_impl.dart index 5df419455..727522a2a 100644 --- a/lib/data/local_repository_impl.dart +++ b/lib/data/local_repository_impl.dart @@ -1520,11 +1520,21 @@ class LocalRepositoryImpl extends LocalRepository { // day's published total is the one mismatch this screen must not show. final st = _sub(await _bundle(date), 'steps'); final dayTotal = (st?['value'] as num?)?.toInt(); - // A day answered by the strap's on-chip counter has no coverage spans; - // its own hourly spans (stamped by the derive) stand in for them. - final counter = r.spans.isEmpty && - st?['source'] == 'strap_counter' && - st?['spans'] is List + // A day the strap's on-chip counter ANSWERED is presented from the counter, + // whether or not `live_coverage` also holds rows for it. + // + // The counter is a WHOLE-DAY alternative to the coverage ladder, and the + // derive only picks it when its calibrated total beat everything in + // `live_coverage` put together (see `_writeSteps`). So on a counter day + // those rows are, by construction, the count the day did NOT publish — and + // requiring `r.spans.isEmpty` here still read them on every day the phone + // had banked an hour, which is every gen5 day with phone steps switched on. + // The screen then published the phone's smaller coverage total as the day's + // number and named the phone as the sensor, against the counter's figure on + // Home. The counter's own hourly spans (stamped by the derive) are the + // clocked data for such a day; when an older derive stored none, the screen + // says so instead of showing another sensor's hours under this day's count. + final counter = st?['source'] == 'strap_counter' && st?['spans'] is List ? st!['spans'] as List : null; // Only for naming: a span that sits inside a session gets that session's diff --git a/lib/ui2/screens/day_steps.dart b/lib/ui2/screens/day_steps.dart index 03c9ab9e6..57fc197aa 100644 --- a/lib/ui2/screens/day_steps.dart +++ b/lib/ui2/screens/day_steps.dart @@ -93,9 +93,11 @@ class DayStepsData { /// The spans' own totals, from the ladder. Not re-summed here. final int total, strap, phone; - /// What the day actually published, and off which sensor. Usually [total] — - /// but a day whose only counter was the strap's on-chip one, derived before - /// its hourly spans were stored, publishes a figure with no spans behind it. + /// What the day actually published, and off which sensor. Always equal to + /// [total] once the reader has resolved the day — including a strap-counter + /// day, whose published figure is the counter's and not the coverage ladder's, + /// and one derived before its hourly spans were stored, which publishes a + /// figure with no spans behind it at all. final int? dayTotal; final String? daySource; diff --git a/test/day_steps_counter_over_coverage_test.dart b/test/day_steps_counter_over_coverage_test.dart new file mode 100644 index 000000000..6770c009b --- /dev/null +++ b/test/day_steps_counter_over_coverage_test.dart @@ -0,0 +1,101 @@ +// A day the strap's on-chip step counter ANSWERED is presented from that +// counter, whether or not `live_coverage` also holds phone rows for it. +// +// The counter is a whole-day alternative: the derive only picks it when its +// calibrated total beat every coverage row put together. So the coverage rows +// on such a day are the count the day did NOT publish, and publishing their +// total as the day's figure put a smaller number on this screen than the one +// Home shows for the same day — labelled with the phone, on a day the wrist +// answered. + +import 'dart:convert'; + +import 'package:flutter_test/flutter_test.dart'; +import 'package:path/path.dart' as p; +import 'package:sqflite_common_ffi/sqflite_ffi.dart'; + +import 'package:openstrap_edge/compute/derivation_engine.dart' show kAlgoVersion; +import 'package:openstrap_edge/data/day_label.dart'; +import 'package:openstrap_edge/data/db.dart'; +import 'package:openstrap_edge/data/local_repository_impl.dart'; + +void main() { + setUpAll(() async { + sqfliteFfiInit(); + databaseFactory = databaseFactoryFfi; + LocalDb.dbName = 'openstrap_day_steps_counter_over_coverage_test.db'; + final dir = await databaseFactory.getDatabasesPath(); + await databaseFactory.deleteDatabase(p.join(dir, LocalDb.dbName)); + }); + + tearDownAll(() async { + await LocalDb.close(); + final dir = await databaseFactory.getDatabasesPath(); + await databaseFactory.deleteDatabase(p.join(dir, LocalDb.dbName)); + }); + + test('a counter day that also has phone hours publishes the counter', () async { + const day = '2026-03-10'; + final midnight = localDayStartSec(day)!; + const counterTotal = 12000; + + // The strap's chip answered the day: 12,000 steps in two hourly spans. + await LocalDb.putDayResult( + dayId: day, + algoVersion: kAlgoVersion, + payloadJson: jsonEncode({ + 'steps': { + 'value': counterTotal, + 'band_measured': counterTotal, + 'source': 'strap_counter', + 'spans': [ + { + 'start_ts': midnight + 9 * 3600, + 'end_ts': midnight + 10 * 3600 - 1, + 'steps': 4000, + }, + { + 'start_ts': midnight + 10 * 3600, + 'end_ts': midnight + 11 * 3600 - 1, + 'steps': 8000, + }, + ], + }, + }), + windowJson: '{}', + finalized: true, + ); + + // …and the phone banked its own, smaller, hours. The ladder lost them to + // the counter (8,000 < 12,000), which is exactly the state that used to + // reach the screen as "8,000, counted by your phone". + await LocalDb.replacePhoneCoverageForDay(day, [ + for (var h = 9; h < 17; h++) + ( + startTs: midnight + h * 3600, + endTs: midnight + (h + 1) * 3600 - 1, + steps: 1000, + ), + ]); + expect(await LocalDb.phoneStepsForDay(day), 8000, + reason: 'the phone rows are really there, so the old reader saw them'); + + final out = + await LocalRepositoryImpl(getProfileMap: () => null).getDaySteps(day); + + // The day's number is the one it published, and the split sums to it. + expect(out['day_total'], counterTotal); + expect(out['day_source'], 'strap_counter'); + expect(out['total'], counterTotal, + reason: 'the coverage total was the count this day did not publish'); + expect(out['strap'], counterTotal); + expect(out['phone'], 0); + + // The clock comes from the counter's own spans, never the phone's. + final spans = (out['spans'] as List).cast(); + expect(spans.map((s) => s['steps']), [4000, 8000]); + expect(spans.map((s) => s['start_ts']), + [midnight + 9 * 3600, midnight + 10 * 3600]); + expect(spans.every((s) => s['source'] == LocalDb.kStepSourceBand), isTrue); + }); +} From 9daee882c41cb6a9b2a0453cf4c72e926d394617 Mon Sep 17 00:00:00 2001 From: KuljeetSinghXDA <22261392+KuljeetSinghXDA@users.noreply.github.com> Date: Mon, 16 Nov 2026 18:36:41 +0530 Subject: [PATCH 2/3] steps: a counter day with no stored spans is still a counter day MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit `getDaySteps` chose the counter branch only when the stored bundle also carried a `spans` List. A bundle written by an older derive has the counter's count and no span list, so exactly those days fell through to the coverage ladder: the phone's smaller total came back under the counter's name, or zero when the phone had no rows either, while Home showed the counter's figure for the same day. Select by `source` and treat absent (or null, or wrongly typed) spans as none, so such a day publishes the counter's total with no times behind it and the screen says so. `DayStepsData.load` also read `total` from the live coverage rows and `dayTotal` from the stored bundle. Those disagree while a re-derive is pending — coverage rows landed after the bundle was written — so the screen could print one number as the count and another as what the day published, against the documented equality. Both now come from one resolved value: the published figure when there is one (that is what Home shows), the ladder's sum otherwise. Tests: a counter day with no `spans` key and one with `spans: null`, each with phone coverage rows present, returning the counter total, its source and an empty span list; and `DayStepsData.load` keeping total == dayTotal both when coverage outran the bundle and when nothing was published. --- lib/data/local_repository_impl.dart | 20 ++- lib/ui2/screens/day_steps.dart | 26 +++- .../day_steps_counter_over_coverage_test.dart | 124 ++++++++++++++++++ 3 files changed, 156 insertions(+), 14 deletions(-) diff --git a/lib/data/local_repository_impl.dart b/lib/data/local_repository_impl.dart index 727522a2a..a16a20153 100644 --- a/lib/data/local_repository_impl.dart +++ b/lib/data/local_repository_impl.dart @@ -1529,13 +1529,19 @@ class LocalRepositoryImpl extends LocalRepository { // those rows are, by construction, the count the day did NOT publish — and // requiring `r.spans.isEmpty` here still read them on every day the phone // had banked an hour, which is every gen5 day with phone steps switched on. - // The screen then published the phone's smaller coverage total as the day's - // number and named the phone as the sensor, against the counter's figure on - // Home. The counter's own hourly spans (stamped by the derive) are the - // clocked data for such a day; when an older derive stored none, the screen - // says so instead of showing another sensor's hours under this day's count. - final counter = st?['source'] == 'strap_counter' && st?['spans'] is List - ? st!['spans'] as List + // The counter's own hourly spans (stamped by the derive) are the clocked + // data for such a day; when an older derive stored none, the screen says so + // instead of showing another sensor's hours under this day's count. + // + // SELECTED BY SOURCE, never by the presence of `spans`. An older bundle can + // carry the counter's count with no span list at all, and requiring a List + // here dropped exactly those days onto the coverage ladder: the phone's + // smaller total came back under the counter's name, or zero when the phone + // had no rows either. A counter day with no stored spans is still a counter + // day — it publishes the counter's figure with no times behind it. + final storedSpans = st?['spans']; + final counter = st?['source'] == 'strap_counter' + ? (storedSpans is List ? storedSpans : const []) : null; // Only for naming: a span that sits inside a session gets that session's // name. Cheap — one indexed read over one day. diff --git a/lib/ui2/screens/day_steps.dart b/lib/ui2/screens/day_steps.dart index 57fc197aa..6a3f4e2f2 100644 --- a/lib/ui2/screens/day_steps.dart +++ b/lib/ui2/screens/day_steps.dart @@ -93,11 +93,13 @@ class DayStepsData { /// The spans' own totals, from the ladder. Not re-summed here. final int total, strap, phone; - /// What the day actually published, and off which sensor. Always equal to - /// [total] once the reader has resolved the day — including a strap-counter - /// day, whose published figure is the counter's and not the coverage ladder's, - /// and one derived before its hourly spans were stored, which publishes a - /// figure with no spans behind it at all. + /// What the day actually published, and off which sensor. [load] resolves it + /// to the SAME value as [total] — the published figure when there is one, the + /// ladder's sum otherwise — so the two can never be read as two numbers for + /// one day. That holds for a strap-counter day, whose published figure is the + /// counter's and not the coverage ladder's, and for one derived before its + /// hourly spans were stored, which publishes a figure with no spans behind it + /// at all. final int? dayTotal; final String? daySource; @@ -126,6 +128,16 @@ class DayStepsData { final days = await repo.availableDays(); final day = pickDay(days, want, todayLabel()) ?? todayLabel(); final d = await repo.getDaySteps(day); + // ONE resolved total, used for both figures. The ladder's sum is recomputed + // from `live_coverage` while the day's published number comes out of the + // stored bundle, so the two disagree for as long as a re-derive is pending + // (coverage rows landed after the bundle was written). Printing one as the + // count and the other as what the day published is the mismatch these two + // fields exist to rule out, so the published figure wins — it is the one + // Home shows for the same day — and the ladder is the fallback only when + // nothing was published at all. + final ladder = (d['total'] as num?)?.toInt() ?? 0; + final total = (d['day_total'] as num?)?.toInt() ?? ladder; return DayStepsData( day: day, days: days, @@ -143,10 +155,10 @@ class DayStepsData { activity: s['activity'] as String?, ), ], - total: (d['total'] as num?)?.toInt() ?? 0, + total: total, strap: (d['strap'] as num?)?.toInt() ?? 0, phone: (d['phone'] as num?)?.toInt() ?? 0, - dayTotal: (d['day_total'] as num?)?.toInt(), + dayTotal: total, daySource: d['day_source'] as String?, note: d['note'] as String?, bandLabel: bandLabel, diff --git a/test/day_steps_counter_over_coverage_test.dart b/test/day_steps_counter_over_coverage_test.dart index 6770c009b..b72fb597d 100644 --- a/test/day_steps_counter_over_coverage_test.dart +++ b/test/day_steps_counter_over_coverage_test.dart @@ -17,7 +17,10 @@ import 'package:sqflite_common_ffi/sqflite_ffi.dart'; import 'package:openstrap_edge/compute/derivation_engine.dart' show kAlgoVersion; import 'package:openstrap_edge/data/day_label.dart'; import 'package:openstrap_edge/data/db.dart'; +import 'package:openstrap_edge/data/local_repository.dart'; import 'package:openstrap_edge/data/local_repository_impl.dart'; +import 'package:openstrap_edge/ui2/screens/day_steps.dart' + show DayStepsData; void main() { setUpAll(() async { @@ -98,4 +101,125 @@ void main() { [midnight + 9 * 3600, midnight + 10 * 3600]); expect(spans.every((s) => s['source'] == LocalDb.kStepSourceBand), isTrue); }); + + // An older bundle can carry the counter's count with no hourly spans stored. + // The day is still a counter day: selecting it by the presence of `spans` + // dropped it onto the coverage ladder, which returned the phone's smaller + // total under the counter's name — or zero when the phone had no rows either. + for (final (label, spans) in [ + ('no spans key at all', const {}), + ('spans: null', const {'spans': null}), + ]) { + test('a counter day with $label publishes the counter, not the phone', + () async { + final day = label == 'spans: null' ? '2026-03-12' : '2026-03-11'; + final midnight = localDayStartSec(day)!; + const counterTotal = 12500; + + await LocalDb.putDayResult( + dayId: day, + algoVersion: kAlgoVersion, + payloadJson: jsonEncode({ + 'steps': { + 'value': counterTotal, + 'band_measured': counterTotal, + 'source': 'strap_counter', + ...spans, + }, + }), + windowJson: '{}', + finalized: true, + ); + // The phone banked its own hours for the same day, so falling through to + // coverage is observable rather than a zero either way. + await LocalDb.replacePhoneCoverageForDay(day, [ + for (var h = 9; h < 13; h++) + ( + startTs: midnight + h * 3600, + endTs: midnight + (h + 1) * 3600 - 1, + steps: 1000, + ), + ]); + expect(await LocalDb.phoneStepsForDay(day), 4000, + reason: 'the phone rows are really there'); + + final out = await LocalRepositoryImpl(getProfileMap: () => null) + .getDaySteps(day); + + expect(out['day_source'], 'strap_counter'); + expect(out['day_total'], counterTotal); + expect(out['total'], counterTotal, + reason: 'the day published the counter\'s figure'); + expect(out['strap'], counterTotal); + expect(out['phone'], 0); + // No stored times: the screen says so instead of drawing the phone's + // hours under the counter's count. + expect(out['spans'], isEmpty); + }); + } + + group('DayStepsData.load', () { + test('a counter day with no stored spans keeps the counter total and its ' + 'source', () async { + final data = await DayStepsData.load(_StepsRepo({ + 'total': 12500, + 'strap': 12500, + 'phone': 0, + 'day_total': 12500, + 'day_source': 'strap_counter', + 'spans': const [], + })); + + expect(data.total, 12500); + expect(data.dayTotal, data.total, + reason: 'one resolved number, not two for the same day'); + expect(data.daySource, 'strap_counter'); + expect(data.spans, isEmpty, reason: 'the no-times card, not phone hours'); + }); + + test('total and dayTotal are one value when coverage rows outran the bundle', + () async { + // The bundle published 12,000; `live_coverage` now sums to 8,000 because + // the rows changed after the derive. Home shows the published figure, so + // this screen must not print 8,000 as the count and 12,000 as the day's. + final data = await DayStepsData.load(_StepsRepo({ + 'total': 8000, + 'strap': 0, + 'phone': 8000, + 'day_total': 12000, + 'day_source': 'phone', + 'spans': const [], + })); + + expect(data.total, 12000, reason: 'the day\'s own published figure'); + expect(data.dayTotal, 12000); + }); + + test('with nothing published, the ladder is the one resolved total', + () async { + final data = await DayStepsData.load(_StepsRepo({ + 'total': 6400, + 'strap': 0, + 'phone': 6400, + 'day_total': null, + 'day_source': 'phone', + 'spans': const [], + })); + + expect(data.total, 6400); + expect(data.dayTotal, 6400); + }); + }); +} + +/// A repository returning one already-resolved `getDaySteps` payload. +class _StepsRepo extends LocalRepository { + _StepsRepo(this.out); + final Map out; + + @override + Future> availableDays() async => const ['2026-03-10']; + + @override + Future> getDaySteps(String date) async => out; } From e47fd624cc4b82192741b2d5825a6138922887ac Mon Sep 17 00:00:00 2001 From: KuljeetSinghXDA <22261392+KuljeetSinghXDA@users.noreply.github.com> Date: Mon, 16 Nov 2026 18:47:24 +0530 Subject: [PATCH 3/3] steps: keep the ladder's total on a mixed day MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit `DayStepsData.load` resolved `total = day_total ?? ladder` for EVERY day, not just a counter day, while `strap` and `phone` still came from the ladder. On a mixed phone + strap day whose phone rows landed after the last derive — the ordinary state of today between drains — the header read "Counted 7,000 · Strap 3,000 · Your phone 6,000" over bars summing to 9,000: one day, two numbers, in the same card. The repository half already publishes `total == day_total` for a counter day, so the screen-side override was never needed for this bug. Drop it and read both fields as served: `total` stays the ladder's own sum, agreeing with `strap`, `phone` and the bars drawn from them, and `dayTotal` stays what the day actually published. On a counter day the two are the same figure by construction, which is what the screen needs for the no-times card. Tests: the mixed day now goes end to end through the real repository — a bundle publishing 7,000 (`mixed`) with 3,000 of wrist coverage and 6,000 of phone rows banked afterwards — and asserts `total == strap + phone == 9000` while `day_total` stays 7,000. The counter test also loads through `DayStepsData.load` and asserts the counter's figure as both values. The three fake-repo cases now pin each side of that rule: mixed days keep the ladder, counter days keep the counter, an unpublished day leaves `dayTotal` null. --- lib/ui2/screens/day_steps.dart | 28 ++-- .../day_steps_counter_over_coverage_test.dart | 138 ++++++++++++++---- 2 files changed, 120 insertions(+), 46 deletions(-) diff --git a/lib/ui2/screens/day_steps.dart b/lib/ui2/screens/day_steps.dart index 6a3f4e2f2..4796247d1 100644 --- a/lib/ui2/screens/day_steps.dart +++ b/lib/ui2/screens/day_steps.dart @@ -93,13 +93,13 @@ class DayStepsData { /// The spans' own totals, from the ladder. Not re-summed here. final int total, strap, phone; - /// What the day actually published, and off which sensor. [load] resolves it - /// to the SAME value as [total] — the published figure when there is one, the - /// ladder's sum otherwise — so the two can never be read as two numbers for - /// one day. That holds for a strap-counter day, whose published figure is the - /// counter's and not the coverage ladder's, and for one derived before its - /// hourly spans were stored, which publishes a figure with no spans behind it - /// at all. + /// What the day actually published, and off which sensor. Usually [total] — + /// but a day whose only counter was the strap's on-chip one, derived before + /// its hourly spans were stored, publishes a figure with no spans behind it. + /// + /// [total] stays the ladder's own sum, so it agrees with [strap] and [phone] + /// and with the bars drawn from them. On a counter day the repository + /// publishes the counter's figure as both, so the two still agree there. final int? dayTotal; final String? daySource; @@ -128,16 +128,6 @@ class DayStepsData { final days = await repo.availableDays(); final day = pickDay(days, want, todayLabel()) ?? todayLabel(); final d = await repo.getDaySteps(day); - // ONE resolved total, used for both figures. The ladder's sum is recomputed - // from `live_coverage` while the day's published number comes out of the - // stored bundle, so the two disagree for as long as a re-derive is pending - // (coverage rows landed after the bundle was written). Printing one as the - // count and the other as what the day published is the mismatch these two - // fields exist to rule out, so the published figure wins — it is the one - // Home shows for the same day — and the ladder is the fallback only when - // nothing was published at all. - final ladder = (d['total'] as num?)?.toInt() ?? 0; - final total = (d['day_total'] as num?)?.toInt() ?? ladder; return DayStepsData( day: day, days: days, @@ -155,10 +145,10 @@ class DayStepsData { activity: s['activity'] as String?, ), ], - total: total, + total: (d['total'] as num?)?.toInt() ?? 0, strap: (d['strap'] as num?)?.toInt() ?? 0, phone: (d['phone'] as num?)?.toInt() ?? 0, - dayTotal: total, + dayTotal: (d['day_total'] as num?)?.toInt(), daySource: d['day_source'] as String?, note: d['note'] as String?, bandLabel: bandLabel, diff --git a/test/day_steps_counter_over_coverage_test.dart b/test/day_steps_counter_over_coverage_test.dart index b72fb597d..107ef0b02 100644 --- a/test/day_steps_counter_over_coverage_test.dart +++ b/test/day_steps_counter_over_coverage_test.dart @@ -100,6 +100,16 @@ void main() { expect(spans.map((s) => s['start_ts']), [midnight + 9 * 3600, midnight + 10 * 3600]); expect(spans.every((s) => s['source'] == LocalDb.kStepSourceBand), isTrue); + + // …and the screen passes that one number through untouched: the counter's + // figure as both the count and what the day published. + final data = await DayStepsData.load( + LocalRepositoryImpl(getProfileMap: () => null), + want: day); + expect(data.total, counterTotal); + expect(data.dayTotal, counterTotal); + expect(data.strap, counterTotal); + expect(data.phone, 0); }); // An older bundle can carry the counter's count with no hourly spans stored. @@ -158,45 +168,119 @@ void main() { }); } + // The screen half: `DayStepsData.load` must not re-resolve a day's total. + // It did for one revision — `total = day_total ?? ladder` for EVERY day — and + // a mixed phone + strap day whose phone rows landed after the last derive + // then printed "Counted 7,000" over bars summing to 9,000: a header that + // disagreed with itself. End to end here, through the real repository, so the + // two halves are checked against one another and not against a fake. + test('a mixed phone + strap day keeps total == strap + phone when the ' + 'bundle is stale', () async { + const day = '2026-03-13'; + final midnight = localDayStartSec(day)!; + + // The derive published 7,000 off `mixed` — the phone had banked nothing yet. + await LocalDb.putDayResult( + dayId: day, + algoVersion: kAlgoVersion, + payloadJson: jsonEncode({ + 'steps': { + 'value': 7000, + 'band_measured': 3000, + 'source': 'mixed', + }, + }), + windowJson: '{}', + finalized: true, + ); + // The wrist covered 3,000 steps in one half hour… + await LocalDb.addLiveCoverage( + midnight + 9 * 3600, + midnight + 9 * 3600 + 1800, + 3000, + day, + ); + // …and the phone banked 6,000 more AFTER that bundle was written, which is + // the ordinary state of today between drains. + await LocalDb.replacePhoneCoverageForDay(day, [ + for (var h = 12; h < 18; h++) + ( + startTs: midnight + h * 3600, + endTs: midnight + (h + 1) * 3600 - 1, + steps: 1000, + ), + ]); + + final repo = LocalRepositoryImpl(getProfileMap: () => null); + final out = await repo.getDaySteps(day); + expect(out['total'], 9000, reason: 'the ladder: 3,000 + 6,000'); + expect(out['strap'], 3000); + expect(out['phone'], 6000); + expect(out['day_total'], 7000, + reason: 'the bundle is stale on purpose, so the two can disagree'); + + final data = await DayStepsData.load(repo, want: day); + + expect(data.total, 9000, reason: 'the count says what the bars sum to'); + expect(data.total, data.strap + data.phone); + expect(data.dayTotal, 7000, + reason: 'still readable as what the day published'); + }); + group('DayStepsData.load', () { - test('a counter day with no stored spans keeps the counter total and its ' - 'source', () async { + // The repository hands the screen one total for a counter day (`total`), + // and the ladder's own sum for every other day. The screen must not + // re-resolve either: doing so printed the published figure as the count + // while `strap`, `phone` and the bars stayed on the ladder, so the header + // disagreed with itself. + test('a mixed phone + strap day keeps total == strap + phone, so the ' + 'count and the bars agree', () async { + // The phone banked 6,000 after the last derive, so the bundle still + // publishes 7,000 off `mixed`. The count is the ladder's 9,000 and the + // bars under it sum to 9,000 too. final data = await DayStepsData.load(_StepsRepo({ - 'total': 12500, - 'strap': 12500, - 'phone': 0, - 'day_total': 12500, - 'day_source': 'strap_counter', + 'total': 9000, + 'strap': 3000, + 'phone': 6000, + 'day_total': 7000, + 'day_source': 'mixed', 'spans': const [], })); - expect(data.total, 12500); - expect(data.dayTotal, data.total, - reason: 'one resolved number, not two for the same day'); - expect(data.daySource, 'strap_counter'); - expect(data.spans, isEmpty, reason: 'the no-times card, not phone hours'); + expect(data.mixed, isTrue); + expect(data.total, 9000, reason: 'the ladder, not the stale bundle'); + expect(data.strap, 3000); + expect(data.phone, 6000); + expect(data.total, data.strap + data.phone, + reason: 'the header count and the bars are the same sum'); + expect(data.dayTotal, 7000, + reason: 'what the day published is still readable as such'); }); - test('total and dayTotal are one value when coverage rows outran the bundle', - () async { - // The bundle published 12,000; `live_coverage` now sums to 8,000 because - // the rows changed after the derive. Home shows the published figure, so - // this screen must not print 8,000 as the count and 12,000 as the day's. + test('a counter day keeps the counter total, which the repository returns ' + 'as both figures', () async { + // Even with phone hours on disk, the derive picked the counter, so the + // repository publishes the counter's figure as `total` and `day_total` + // alike. The screen passes both through untouched. final data = await DayStepsData.load(_StepsRepo({ - 'total': 8000, - 'strap': 0, - 'phone': 8000, - 'day_total': 12000, - 'day_source': 'phone', + 'total': 12500, + 'strap': 12500, + 'phone': 0, + 'day_total': 12500, + 'day_source': 'strap_counter', 'spans': const [], })); - expect(data.total, 12000, reason: 'the day\'s own published figure'); - expect(data.dayTotal, 12000); + expect(data.total, 12500, reason: 'the counter\'s figure, not 8,000'); + expect(data.dayTotal, 12500); + expect(data.total, data.dayTotal, + reason: 'one number for the day, said twice'); + expect(data.strap + data.phone, 12500); + expect(data.spans, isEmpty, reason: 'the no-times card, not phone hours'); }); - test('with nothing published, the ladder is the one resolved total', - () async { + test('with nothing published, dayTotal stays null and the ladder is the ' + 'count', () async { final data = await DayStepsData.load(_StepsRepo({ 'total': 6400, 'strap': 0, @@ -207,7 +291,7 @@ void main() { })); expect(data.total, 6400); - expect(data.dayTotal, 6400); + expect(data.dayTotal, isNull); }); }); }