From efaa371b553ae9751cc2e8a23a1dddf795796afe Mon Sep 17 00:00:00 2001 From: Mohammad Abdul Sahil <127765312+abdulsaheel@users.noreply.github.com> Date: Sat, 28 Nov 2026 06:02:59 +0530 Subject: [PATCH] sleep: dont publish a night still going at midnight as a finished one --- lib/compute/substrate.dart | 13 +++- test/sleep_unfinished_at_midnight_test.dart | 68 +++++++++++++++++++++ 2 files changed, 79 insertions(+), 2 deletions(-) create mode 100644 test/sleep_unfinished_at_midnight_test.dart diff --git a/lib/compute/substrate.dart b/lib/compute/substrate.dart index 8e1403f06..cf49d38c5 100644 --- a/lib/compute/substrate.dart +++ b/lib/compute/substrate.dart @@ -1330,6 +1330,9 @@ List calendarDays( // full set of sessions that can legitimately end today. final searchStart = math.max(dataStart, dayStart - kNocturnalSearchLookbackSec); final searchEnd = math.min(dataEnd, dayEnd); + // Data carries on past this day's midnight cut: a sleep still going at the + // cut belongs to the day that sees it end, not to this one. + final recordContinues = dataEnd > dayEnd; final loS = _lowerBound(sub.tsSec, searchStart); final hiS = _lowerBound(sub.tsSec, searchEnd); @@ -1426,6 +1429,7 @@ List calendarDays( // the auto night at the band's own last SLEEP (AUTO path only — // never the override or the HR-led fallback below). bandSleepState: sub.bandSleepStateSlice(loS, hiS), + recordContinues: recordContinues, ); } else { // Not an error and not "no sleep" — just no accel evidence. Fall @@ -1437,13 +1441,18 @@ List calendarDays( s = ana.SleepSegmentation.absent; } src = 'auto'; - if (!s.present) { + // A deferred night is not a miss: the fallback would propose the same + // cut-off stretch. + if (!s.present && !s.unfinished) { // Approach 2: accel-led detection found nothing → HR-led fallback. // Propose the longest sustained nocturnal HR dip, then STAGE it via the // forced-window path. Marked low-confidence for a "is this right?" prompt. final tsSlice = [for (var i = loS; i < hiS; i++) sub.tsSec[i]]; final cand = - ana.hrLedSleepWindow(hrSlice, tsSlice, hrBaseline: hrBaseline); + ana.hrLedSleepWindow(hrSlice, tsSlice, + hrBaseline: hrBaseline, + recordContinues: recordContinues, + tzOffsetResolver: tzOffset); if (cand != null && fallbackNightAcceptable(cand.onsetSec, cand.offsetSec, tzOffsetSec: tzOffset(cand.onsetSec), diff --git a/test/sleep_unfinished_at_midnight_test.dart b/test/sleep_unfinished_at_midnight_test.dart new file mode 100644 index 000000000..5de58ed1d --- /dev/null +++ b/test/sleep_unfinished_at_midnight_test.dart @@ -0,0 +1,68 @@ +// An early bedtime (20:00) is still going at the day's midnight cut. The day +// that ends at midnight must not publish a truncated night ending at 00:00; +// only the next day, which sees the wake, owns it. +// +// Fixed local `DateTime(...)` literals, never `DateTime.now()`. + +import 'dart:math' as math; + +import 'package:flutter_test/flutter_test.dart'; +import 'package:openstrap_edge/compute/substrate.dart'; + +int _local(int y, int mo, int d, [int h = 0, int mi = 0]) => + DateTime(y, mo, d, h, mi).millisecondsSinceEpoch ~/ 1000; + +Substrate _build(int start, int n, bool Function(int t) asleepAt) { + final ts = [], hr = []; + final ax = [], ay = [], az = []; + for (var i = 0; i < n; i++) { + final t = start + i; + final asleep = asleepAt(t); + ts.add(t); + hr.add(asleep ? 50 : 72); + if (asleep) { + ax.add(0.0); + ay.add(0.0); + az.add(1.0); + } else { + final rad = (i % 9) * 10.0 * math.pi / 180.0; + ax.add(math.cos(rad)); + ay.add(0.0); + az.add(math.sin(rad)); + } + } + return Substrate( + tsSec: ts, + hr: hr, + rrTsMs: const [], + rrMs: const [], + ax: ax, + ay: ay, + az: az, + spo2Red: List.filled(n, 0), + spo2Ir: List.filled(n, 0), + skinTemp: List.filled(n, 0), + skinContact: List.filled(n, 0), + ); +} + +void main() { + test('a night still going at midnight is owned only by the day it ends in', + () { + final start = _local(2025, 6, 15, 12); + final bed = _local(2025, 6, 15, 20); + final wake = _local(2025, 6, 16, 6); + final end = _local(2025, 6, 16, 11); + final days = calendarDays( + _build(start, end - start, (t) => t >= bed && t < wake)); + + final slept = days.where((d) => d.sleep.present).toList(); + expect(slept, hasLength(1)); + final night = slept.single; + expect(night.date, '2025-06-16'); + final onSec = night.sleep.window!.onsetMs! ~/ 1000; + final offSec = night.sleep.window!.offsetMs! ~/ 1000; + expect((onSec - bed).abs(), lessThan(3600)); + expect((offSec - wake).abs(), lessThan(3600)); + }); +}