diff --git a/lib/import/noop_backup_import.dart b/lib/import/noop_backup_import.dart index 441d28f58..c377d6217 100644 --- a/lib/import/noop_backup_import.dart +++ b/lib/import/noop_backup_import.dart @@ -376,8 +376,10 @@ class NoopBackupImporter { try { r = await src.rawQuery( 'SELECT ts FROM $t WHERE deviceId = ? AND ts >= ? ' - 'ORDER BY ts LIMIT 1', - [d, to]); + 'AND ts <= ? ORDER BY ts LIMIT 1', + // Same upper bound as [_span]: one microsecond-scaled + // row would otherwise land here and overflow DateTime. + [d, to, noopMaxPlausibleTs()]); } catch (e) { if (!_isCorruptPageError(e)) rethrow; corrupt.add(t); diff --git a/test/noop_backup_import_test.dart b/test/noop_backup_import_test.dart index defe111b7..fb7f8789f 100644 --- a/test/noop_backup_import_test.dart +++ b/test/noop_backup_import_test.dart @@ -392,6 +392,31 @@ void main() { expect(res.rows, 300, reason: 'the good rows import, the corrupt one does not'); }, timeout: const Timeout(Duration(minutes: 5))); + test('a microsecond timestamp after the last day does not crash the walk', + () async { + // The jump-to-next-day probe had no upper bound, so a trailing 1.7e15 row + // reached DateTime and threw a RangeError before the last day derived. + const t0 = 1787256000; // 2026-08-20 noon UTC, a day no other fixture uses + final path = p.join(tmp.path, 'micro.sqlite'); + if (File(path).existsSync()) File(path).deleteSync(); + final src = await databaseFactory.openDatabase(path); + await src.execute('CREATE TABLE hrSample (deviceId TEXT, ts INTEGER, ' + 'bpm INTEGER, PRIMARY KEY (deviceId, ts))'); + final b = src.batch(); + for (var i = 0; i < 300; i++) { + b.insert('hrSample', {'deviceId': 'd', 'ts': t0 + i, 'bpm': 62}); + } + b.insert('hrSample', {'deviceId': 'd', 'ts': 1700000000000000, 'bpm': 60}); + await b.commit(noResult: true); + await src.close(); + final bak = writeBackup('micro.noopbak', path); + + final res = await NoopImporter.importFile( + bak, const Profile(), DerivationEngine()); + expect(res.days, greaterThan(0)); + expect(res.rows, 300); + }, timeout: const Timeout(Duration(minutes: 5))); + test('a long gap between blocks still derives the later day', () async { // The prior-evening buffer used to be retained across ANY gap, handing the // next day a Substrate spanning the whole thing. `calendarDays` walks that