From 70d6929a5c5f0c65af980a87c8dd4eaffc37500c Mon Sep 17 00:00:00 2001 From: Roland Geider Date: Tue, 27 Oct 2026 21:27:20 +0100 Subject: [PATCH] Count the chart range cutoffs in calendar days --- lib/features/measurements/charts/range.dart | 17 ++++--- lib/features/measurements/charts/spark.dart | 3 +- .../measurements/charts/range_test.dart | 24 +++++++-- .../widgets/measurement_tile_test.dart | 51 ++++++++++--------- 4 files changed, 58 insertions(+), 37 deletions(-) diff --git a/lib/features/measurements/charts/range.dart b/lib/features/measurements/charts/range.dart index 28e61d012..9e993257b 100644 --- a/lib/features/measurements/charts/range.dart +++ b/lib/features/measurements/charts/range.dart @@ -16,7 +16,9 @@ * along with this program. If not, see . */ +import 'package:clock/clock.dart'; import 'package:collection/collection.dart'; +import 'package:wger/features/measurements/charts/calendar.dart'; import 'package:wger/features/measurements/models/measurement_category.dart'; import 'package:wger/l10n/generated/app_localizations.dart'; @@ -44,8 +46,7 @@ enum ChartRange { if (days == null) { return null; } - final from = DateTime.now().subtract(Duration(days: days)); - return DateTime(from.year, from.month, from.day); + return shiftDays(dayOf(clock.now()), -days); } /// Days read beyond [cutoff], so the moving average of the first days in @@ -58,21 +59,21 @@ enum ChartRange { /// /// Reaches [_averageLeadDays] beyond [cutoff], and is rounded down to /// midnight like [countCutoff], for the same reason. - DateTime? get readCutoff => _atMidnight(Duration(days: _averageLeadDays)); + DateTime? get readCutoff => _atMidnight(_averageLeadDays); /// Oldest entry to count, null for the full history: the range itself, with /// no lead, for the queries that summarise exactly what is on screen. - DateTime? get countCutoff => _atMidnight(Duration.zero); + DateTime? get countCutoff => _atMidnight(0); - /// [cutoff] minus [lead], rounded down to midnight. + /// [cutoff] moved back [leadDays] calendar days, at midnight. /// /// The rounding is deliberate: these identify a provider and have to stay /// the same across rebuilds, which a bound derived from the current instant /// would not. - DateTime? _atMidnight(Duration lead) { - final from = cutoff?.subtract(lead); + DateTime? _atMidnight(int leadDays) { + final from = cutoff; - return from == null ? null : DateTime(from.year, from.month, from.day); + return from == null ? null : shiftDays(from, -leadDays); } /// Label for the selector, counted rather than one string per range, so a diff --git a/lib/features/measurements/charts/spark.dart b/lib/features/measurements/charts/spark.dart index 4eb3abed0..3b5ddbd85 100644 --- a/lib/features/measurements/charts/spark.dart +++ b/lib/features/measurements/charts/spark.dart @@ -23,6 +23,7 @@ library; import 'dart:math'; +import 'package:clock/clock.dart'; import 'package:collection/collection.dart'; import 'package:wger/features/measurements/charts/calendar.dart'; import 'package:wger/features/measurements/charts/series.dart'; @@ -93,7 +94,7 @@ class SparkWindow { /// the heatmap follows it within its own, tighter legibility bounds. The /// delta window gets one slot beyond its weeks for the running week. SparkWindow sparkWindowFor(SparkKind kind, {required DateTime? cutoff, DateTime? today}) { - final now = dayOf(today ?? DateTime.now()); + final now = dayOf(today ?? clock.now()); final days = cutoff == null ? null : daysBetween(dayOf(cutoff), now) + 1; int weeksOf(int? days) => days == null ? sparkMaxWeeks : min(sparkMaxWeeks, (days / 7).ceil()); diff --git a/test/features/measurements/charts/range_test.dart b/test/features/measurements/charts/range_test.dart index 1ad1c6454..78bc5f35a 100644 --- a/test/features/measurements/charts/range_test.dart +++ b/test/features/measurements/charts/range_test.dart @@ -16,6 +16,7 @@ * along with this program. If not, see . */ +import 'package:clock/clock.dart'; import 'package:flutter_test/flutter_test.dart'; import 'package:wger/features/measurements/charts/range.dart'; @@ -31,12 +32,25 @@ void main() { }); test('the week covers today plus the six days before it', () { - // Seven calendar days in all: the count cutoff sits at midnight six - // days back, so a weekday axis gets one letter per day - final count = ChartRange.lastWeek.countCutoff!; - final now = DateTime.now(); + // Seven calendar days, so a weekday axis gets one letter per day. Next to + // the European DST switches, where counting elapsed hours lands a day off + final weekStarts = { + DateTime(2026, 10, 26, 23, 30): DateTime(2026, 10, 20), + DateTime(2026, 3, 30, 0, 30): DateTime(2026, 3, 24), + }; - expect(DateTime(now.year, now.month, now.day - 6), count); + for (final MapEntry(key: now, value: start) in weekStarts.entries) { + withClock(Clock.fixed(now), () { + expect(ChartRange.lastWeek.countCutoff, start, reason: 'at $now'); + }); + } + }); + + test('the read cutoff counts its lead in calendar days', () { + // The 30-day lead from 4 April reaches back across the European spring DST switch + withClock(Clock.fixed(DateTime(2026, 4, 10, 12)), () { + expect(ChartRange.lastWeek.readCutoff, DateTime(2026, 3, 5)); + }); }); test('the cutoff sits at the same midnight as the count cutoff', () { diff --git a/test/features/measurements/widgets/measurement_tile_test.dart b/test/features/measurements/widgets/measurement_tile_test.dart index 93ab54c6f..9d60352ee 100644 --- a/test/features/measurements/widgets/measurement_tile_test.dart +++ b/test/features/measurements/widgets/measurement_tile_test.dart @@ -16,6 +16,7 @@ * along with this program. If not, see . */ +import 'package:clock/clock.dart'; import 'package:flutter/rendering.dart'; import 'package:flutter_riverpod/flutter_riverpod.dart'; import 'package:flutter_test/flutter_test.dart'; @@ -40,7 +41,7 @@ import 'measurement_tile_test.mocks.dart'; /// The calendar day [daysAgo] days back, at midnight like a day bucket. DateTime _day(int daysAgo) { - final now = DateTime.now(); + final now = clock.now(); return DateTime(now.year, now.month, now.day - daysAgo); } @@ -236,31 +237,35 @@ void main() { }); testWidgets('the range filter decides between daily and weekly slots', (tester) async { - final steps = MeasurementCategory( - id: 'steps', - name: 'Steps', - metricType: MetricType.steps, - ); - final entries = { - 'steps': [_entry('steps', 8000, _day(1)), _entry('steps', 6000, _day(2))], - }; + // Late evening the day after the European autumn DST switch, where counting the + // week in elapsed hours drops a day + await withClock(Clock.fixed(DateTime(2026, 10, 26, 23, 30)), () async { + final steps = MeasurementCategory( + id: 'steps', + name: 'Steps', + metricType: MetricType.steps, + ); + final entries = { + 'steps': [_entry('steps', 8000, _day(1)), _entry('steps', 6000, _day(2))], + }; - // A week is seven daily slots and a month one per day; three months - // come as calendar weeks - await tester.pumpWidget(_wrap(steps, entries: entries, range: ChartRange.lastWeek)); - await tester.pumpAndSettle(); - var chart = tester.widget(find.byType(SparkBarChart)); - expect(chart.data.slotCount, 7); + // A week is seven daily slots and a month one per day; three months + // come as calendar weeks + await tester.pumpWidget(_wrap(steps, entries: entries, range: ChartRange.lastWeek)); + await tester.pumpAndSettle(); + var chart = tester.widget(find.byType(SparkBarChart)); + expect(chart.data.slotCount, 7); - await tester.pumpWidget(_wrap(steps, entries: entries, range: ChartRange.lastMonth)); - await tester.pumpAndSettle(); - chart = tester.widget(find.byType(SparkBarChart)); - expect(chart.data.slotCount, 31); + await tester.pumpWidget(_wrap(steps, entries: entries, range: ChartRange.lastMonth)); + await tester.pumpAndSettle(); + chart = tester.widget(find.byType(SparkBarChart)); + expect(chart.data.slotCount, 31); - await tester.pumpWidget(_wrap(steps, entries: entries, range: ChartRange.last3Months)); - await tester.pumpAndSettle(); - chart = tester.widget(find.byType(SparkBarChart)); - expect(chart.data.slotCount, 13); + await tester.pumpWidget(_wrap(steps, entries: entries, range: ChartRange.last3Months)); + await tester.pumpAndSettle(); + chart = tester.widget(find.byType(SparkBarChart)); + expect(chart.data.slotCount, 13); + }); }); testWidgets('blood pressure tile: the reading quoted high over low', (tester) async {