Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 14 additions & 9 deletions lib/compute/crossday_pipeline.dart
Original file line number Diff line number Diff line change
Expand Up @@ -199,6 +199,10 @@ Map<String, dynamic> buildCrossDayBundle(
final freeMidH = <double>[];
final workMidH = <double>[];
final freeDurH = <double>[];
// chronotype pairs each free night's mid-sleep with its duration, so a free
// night with a window but no TST (vendor-staged or manual) must sit out of
// BOTH lists, or one bad night leaves them unequal for the whole window.
final chronoMidH = <double>[];
final allDurH = <double>[];
for (final d in days) {
final onset = (d['onset_sec'] as num?)?.toDouble();
Expand All @@ -218,7 +222,10 @@ Map<String, dynamic> buildCrossDayBundle(
final free = _isFreeDay(d['date'] as String?);
if (free) {
freeMidH.add(midH);
if (dur != null) freeDurH.add(dur);
if (dur != null) {
freeDurH.add(dur);
chronoMidH.add(midH);
}
} else {
workMidH.add(midH);
}
Expand All @@ -227,7 +234,7 @@ Map<String, dynamic> buildCrossDayBundle(

final socialJetlag = ana.socialJetlag(freeMidH, workMidH);
final chronotype = ana.chronotype(
freeMidH,
chronoMidH,
freeDurH,
avgWeekSleepDurH: avgWeekDurH,
totalDaysObserved: allDurH.length,
Expand Down Expand Up @@ -1277,14 +1284,12 @@ bool _isNextDay(String a, String b) {
final end = (seg['end'] as num?)?.toDouble();
final stage = seg['stage'] as String?;
if (start == null || end == null) continue;
// Segment bounds are epoch SECONDS; map to clock-minute-of-day [0,1440).
// this was using `start % 86400` directly, which is the UTC
// time-of-day, not the user's wall clock - the exact mistake
// _localTodMin (right here in this same file, used correctly at
// line ~100) already exists to avoid.
final startMin = _localTodMin(start.round()).floor();
// Segment bounds are epoch SECONDS; map to clock-minute-of-day [0,1440)
// on the clock the night was LIVED on (see _nightClockMin), so a trip
// does not shift every earlier night against the later ones.
final startMin = _nightClockMin(start.round(), d).floor();
// end is exclusive of the segment's trailing edge; cover [start,end).
final endMinRaw = _localTodMin(end.round()).ceil();
final endMinRaw = _nightClockMin(end.round(), d).ceil();
// WRAP, don't clamp. Both bounds are clock-minutes-of-day in [0,1440),
// so a segment crossing local midnight reads e.g. start=1430,
// end=20 — and the old `for (m = startMin; m < endMin; m++)` simply
Expand Down
45 changes: 45 additions & 0 deletions test/crossday_pipeline_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -636,6 +636,51 @@ void _wiredFamilies() {
});
});

group('free night with a window but no TST', () {
test('does not make chronotype refuse for the whole window', () {
final days = _synthDays(30);
// 2024-01-06 is a Saturday: keep its window, drop its TST.
final sat = days.firstWhere((d) => d['date'] == '2024-01-06');
sat.remove('tst_min');
final chrono = (buildCrossDayBundle(days, const {})['chronotype'] as Map)
.cast<String, dynamic>();
expect(chrono['value'], isA<Map>(), reason: '${chrono['note']}');
});
});

group('sleep regularity across a time-zone change', () {
test('nights lived on the same clock read as perfectly regular', () {
// Every night is light 23:00-07:00 on the clock it was lived on. The
// first week was in a zone 3 h ahead, so on the phone's current clock
// those instants read 20:00-04:00; clock_shift_sec puts them back.
int at(int day, int h) =>
DateTime(2024, 3, 4 + day, h).millisecondsSinceEpoch ~/ 1000;
final days = [
for (var i = 0; i < 14; i++)
{
'date': '2024-03-${(5 + i).toString().padLeft(2, '0')}',
if (i < 7) 'clock_shift_sec': 3 * 3600,
'hypnogram': [
{
'start': at(i, i < 7 ? 20 : 23),
'end': at(i + 1, i < 7 ? 4 : 7),
'stage': 'light',
},
// awake the rest of the day, so every minute is observed
{
'start': at(i + 1, i < 7 ? 4 : 7),
'end': at(i + 1, i < 7 ? 20 : 23),
'stage': 'wake',
},
],
},
];
final reg = (buildCrossDayBundle(days, const {})['regularity'] as Map)
.cast<String, dynamic>();
expect((reg['value'] as Map)['sri'], closeTo(100, 1e-9));
});
});

group('sleep performance is last night only', () {
test('no TST last night → absent, not an older night\'s TST', () {
final days = _synthDays(30);
Expand Down
Loading