import 'package:drift/drift.dart' as drift; import 'package:drift/native.dart'; import 'package:flutter_riverpod/flutter_riverpod.dart'; import 'package:flutter_test/flutter_test.dart'; import 'package:motorcycle_dash/database/database.dart'; import 'package:motorcycle_dash/features/garage/garage_providers.dart'; void main() { test('garage best times use valid completed runs for the selected motorcycle', () async { final db = AppDatabase.forTesting(NativeDatabase.memory()); addTearDown(db.close); Future addRide(int motorcycleId) { return db .into(db.rides) .insert(RidesCompanion.insert(startTime: DateTime(2026, 9, 7), motorcycleId: drift.Value(motorcycleId))); } Future addRun({ required int rideId, required String type, required double timeMs, required double topSpeedReached, }) async { await db .into(db.performanceRuns) .insert( PerformanceRunsCompanion.insert( rideId: drift.Value(rideId), type: type, timeMs: timeMs, topSpeedReached: topSpeedReached, timestamp: DateTime(2026, 9, 7), ), ); } final selectedRideId = await addRide(7); final otherRideId = await addRide(8); await addRun(rideId: selectedRideId, type: '0-60', timeMs: 900, topSpeedReached: 62); await addRun(rideId: selectedRideId, type: '0-60', timeMs: 1800, topSpeedReached: 59); await addRun(rideId: selectedRideId, type: '0-60', timeMs: 2400, topSpeedReached: 61); await addRun(rideId: selectedRideId, type: '0-60', timeMs: 2200, topSpeedReached: 60); await addRun(rideId: selectedRideId, type: '0-100', timeMs: 2900, topSpeedReached: 101); await addRun(rideId: selectedRideId, type: '0-100', timeMs: 6100, topSpeedReached: 99); await addRun(rideId: selectedRideId, type: '0-100', timeMs: 6500, topSpeedReached: 101); await addRun(rideId: otherRideId, type: '0-60', timeMs: 1900, topSpeedReached: 62); final container = ProviderContainer(overrides: [databaseProvider.overrideWithValue(db)]); addTearDown(container.dispose); final stats = await container.read(motorcyclePerformanceStatsProvider(7).future); expect(stats.rideCount, 1); expect(stats.best0to60Ms, 2200); expect(stats.best0to100Ms, 6500); }); }