import 'package:cardash_launcher/features/home/obd_economy.dart'; import 'package:flutter_test/flutter_test.dart'; void main() { test('MAF and speed produce a rolling L/100 km value', () { final economy = ObdEconomy(); final start = DateTime(2026, 10, 29, 12); for (var i = 0; i <= 40; i++) { economy.add( at: start.add(Duration(seconds: i)), speedKmh: 60, mafGps: 12, throttlePercent: 20, ); } expect(economy.accumulatedKm, closeTo(40 * 60 / 3600, 0.12)); expect(economy.litresPer100Km, closeTo(6.6, 0.2)); }); test('ECU fuel rate takes precedence over MAF', () { final economy = ObdEconomy(); final start = DateTime(2026); for (var i = 0; i <= 40; i++) { economy.add( at: start.add(Duration(seconds: i)), speedKmh: 60, mafGps: 12, throttlePercent: 20, fuelRateLph: 4, ); } // 4 L/h at 60 km/h is 6.67 L/100 km, not the 6.57 the MAF fallback gives. expect(economy.litresPer100Km, closeTo(6.67, 0.02)); }); test('ECU zero fuel rate during a fuel cut is kept', () { final economy = ObdEconomy(); final start = DateTime(2026); for (var i = 0; i <= 40; i++) { economy.add( at: start.add(Duration(seconds: i)), speedKmh: 60, mafGps: 12, throttlePercent: 20, fuelRateLph: 0, ); } expect(economy.litresPer100Km, 0); }); test('implausible ECU fuel rate falls back to MAF', () { final economy = ObdEconomy(); final start = DateTime(2026); for (var i = 0; i <= 40; i++) { economy.add( at: start.add(Duration(seconds: i)), speedKmh: 60, mafGps: 12, throttlePercent: 20, fuelRateLph: 500, ); } expect(economy.litresPer100Km, closeTo(6.6, 0.2)); }); test('idle yields no economy value', () { final economy = ObdEconomy(); final start = DateTime(2026); for (var i = 0; i < 10; i++) { economy.add( at: start.add(Duration(seconds: i)), speedKmh: 0, mafGps: 4, throttlePercent: 2, ); } expect(economy.litresPer100Km, isNull); }); test('fuel cut while coasting reads zero consumption', () { final economy = ObdEconomy(); final start = DateTime(2026); for (var i = 0; i <= 35; i++) { economy.add( at: start.add(Duration(seconds: i)), speedKmh: 60, mafGps: 12, throttlePercent: 0, ); } expect(economy.litresPer100Km, 0); }); test('reset clears the rolling window', () { final economy = ObdEconomy(); final start = DateTime(2026); for (var i = 0; i <= 35; i++) { economy.add( at: start.add(Duration(seconds: i)), speedKmh: 60, mafGps: 12, throttlePercent: 20, ); } expect(economy.litresPer100Km, isNotNull); economy.reset(); expect(economy.litresPer100Km, isNull); expect(economy.accumulatedKm, 0); }); }