obd_economy_test.dart 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  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('ECU fuel rate takes precedence over MAF', () {
  19. final economy = ObdEconomy();
  20. final start = DateTime(2026);
  21. for (var i = 0; i <= 40; i++) {
  22. economy.add(
  23. at: start.add(Duration(seconds: i)),
  24. speedKmh: 60,
  25. mafGps: 12,
  26. throttlePercent: 20,
  27. fuelRateLph: 4,
  28. );
  29. }
  30. // 4 L/h at 60 km/h is 6.67 L/100 km, not the 6.57 the MAF fallback gives.
  31. expect(economy.litresPer100Km, closeTo(6.67, 0.02));
  32. });
  33. test('ECU zero fuel rate during a fuel cut is kept', () {
  34. final economy = ObdEconomy();
  35. final start = DateTime(2026);
  36. for (var i = 0; i <= 40; i++) {
  37. economy.add(
  38. at: start.add(Duration(seconds: i)),
  39. speedKmh: 60,
  40. mafGps: 12,
  41. throttlePercent: 20,
  42. fuelRateLph: 0,
  43. );
  44. }
  45. expect(economy.litresPer100Km, 0);
  46. });
  47. test('implausible ECU fuel rate falls back to MAF', () {
  48. final economy = ObdEconomy();
  49. final start = DateTime(2026);
  50. for (var i = 0; i <= 40; i++) {
  51. economy.add(
  52. at: start.add(Duration(seconds: i)),
  53. speedKmh: 60,
  54. mafGps: 12,
  55. throttlePercent: 20,
  56. fuelRateLph: 500,
  57. );
  58. }
  59. expect(economy.litresPer100Km, closeTo(6.6, 0.2));
  60. });
  61. test('idle yields no economy value', () {
  62. final economy = ObdEconomy();
  63. final start = DateTime(2026);
  64. for (var i = 0; i < 10; i++) {
  65. economy.add(
  66. at: start.add(Duration(seconds: i)),
  67. speedKmh: 0,
  68. mafGps: 4,
  69. throttlePercent: 2,
  70. );
  71. }
  72. expect(economy.litresPer100Km, isNull);
  73. });
  74. test('fuel cut while coasting reads zero consumption', () {
  75. final economy = ObdEconomy();
  76. final start = DateTime(2026);
  77. for (var i = 0; i <= 35; i++) {
  78. economy.add(
  79. at: start.add(Duration(seconds: i)),
  80. speedKmh: 60,
  81. mafGps: 12,
  82. throttlePercent: 0,
  83. );
  84. }
  85. expect(economy.litresPer100Km, 0);
  86. });
  87. test('reset clears the rolling window', () {
  88. final economy = ObdEconomy();
  89. final start = DateTime(2026);
  90. for (var i = 0; i <= 35; i++) {
  91. economy.add(
  92. at: start.add(Duration(seconds: i)),
  93. speedKmh: 60,
  94. mafGps: 12,
  95. throttlePercent: 20,
  96. );
  97. }
  98. expect(economy.litresPer100Km, isNotNull);
  99. economy.reset();
  100. expect(economy.litresPer100Km, isNull);
  101. expect(economy.accumulatedKm, 0);
  102. });
  103. }