obd_economy_test.dart 1.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  1. import 'package:cardash_launcher/features/home/obd_economy.dart';
  2. import 'package:flutter_test/flutter_test.dart';
  3. void main() {
  4. test('MAF and speed produce a rolling L/100 km value', () {
  5. final economy = ObdEconomy();
  6. final start = DateTime(2026, 10, 29, 12);
  7. for (var i = 0; i <= 40; i++) {
  8. economy.add(
  9. at: start.add(Duration(seconds: i)),
  10. speedKmh: 60,
  11. mafGps: 12,
  12. throttlePercent: 20,
  13. );
  14. }
  15. expect(economy.accumulatedKm, closeTo(40 * 60 / 3600, 0.12));
  16. expect(economy.litresPer100Km, closeTo(6.6, 0.2));
  17. });
  18. test('idle yields no economy value', () {
  19. final economy = ObdEconomy();
  20. final start = DateTime(2026);
  21. for (var i = 0; i < 10; i++) {
  22. economy.add(
  23. at: start.add(Duration(seconds: i)),
  24. speedKmh: 0,
  25. mafGps: 4,
  26. throttlePercent: 2,
  27. );
  28. }
  29. expect(economy.litresPer100Km, isNull);
  30. });
  31. test('fuel cut while coasting reads zero consumption', () {
  32. final economy = ObdEconomy();
  33. final start = DateTime(2026);
  34. for (var i = 0; i <= 35; i++) {
  35. economy.add(
  36. at: start.add(Duration(seconds: i)),
  37. speedKmh: 60,
  38. mafGps: 12,
  39. throttlePercent: 0,
  40. );
  41. }
  42. expect(economy.litresPer100Km, 0);
  43. });
  44. test('reset clears the rolling window', () {
  45. final economy = ObdEconomy();
  46. final start = DateTime(2026);
  47. for (var i = 0; i <= 35; i++) {
  48. economy.add(
  49. at: start.add(Duration(seconds: i)),
  50. speedKmh: 60,
  51. mafGps: 12,
  52. throttlePercent: 20,
  53. );
  54. }
  55. expect(economy.litresPer100Km, isNotNull);
  56. economy.reset();
  57. expect(economy.litresPer100Km, isNull);
  58. expect(economy.accumulatedKm, 0);
  59. });
  60. }