feat: basic trainer support

This commit is contained in:
2026-11-24 20:02:05 +01:00
parent 30b25784c1
commit 9dece10e29
17 changed files with 3048 additions and 58 deletions

View File

@ -0,0 +1,71 @@
import 'dart:convert';
import 'package:abawo_bt_app/model/simulation.dart';
import 'package:flutter_test/flutter_test.dart';
void main() {
test('default configuration matches firmware bytes', () {
const config = SimulationConfig();
expect(config.encode(),
[1, 0, 1, 0, 0x4c, 0x1d, 0xe8, 3, 0xe8, 3, 0xe8, 3, 0xe8, 3]);
expect(SimulationConfig.decode(config.encode()).encode(), config.encode());
});
test('configuration validates schema, reserved byte and ranges', () {
for (final change in [(0, 2), (1, 3), (2, 0), (3, 1), (5, 255), (9, 255)]) {
final bytes = const SimulationConfig().encode().toList();
bytes[change.$1] = change.$2;
expect(() => SimulationConfig.decode(bytes), throwsFormatException);
}
expect(() => SimulationConfig.decode([1]), throwsFormatException);
expect(() => const SimulationConfig(riderWeight: double.nan).encode(),
throwsFormatException);
expect(() => const SimulationConfig(bikeWeight: 101).encode(),
throwsFormatException);
expect(() => const SimulationConfig(gradeMultiplier: 3.001).encode(),
throwsFormatException);
const boundary =
SimulationConfig(riderWeight: 1, bikeWeight: 100, windMultiplier: 3);
expect(
SimulationConfig.decode(boundary.encode()).encode(), boundary.encode());
});
test('preview follows firmware FE-C formula and percent grade', () {
const config = SimulationConfig();
final expected =
(85 * 0.05 * 9.81 + 85 * 0.004 * 9.8 + 0.5 * 0.51 * 100) * 10;
expect(config.previewWatts(36, 5), closeTo(expected, 0.00001));
expect(config.previewWatts(0, 5), 0);
expect(config.previewWatts(15, -5), 0);
expect(config.previewWatts(15, 0), closeTo(32.3295138889, 0.0001));
expect(const SimulationConfig(modelId: 2).previewWatts(36, 0),
closeTo(33.32, 0.00001));
expect(config.copyWith(windMultiplier: 0).previewWatts(36, 0),
closeTo(33.32, 0.00001));
expect(config.copyWith(gradeMultiplier: 0).previewWatts(15, 5),
config.previewWatts(15, 0));
});
test('model catalog supports stable future IDs without inventing previews',
() {
expect(decodeSimulationModels(utf8.encode('1:fe-c;2:track-only;9:future')),
{1: 'fe-c', 2: 'track-only', 9: 'future'});
expect(() => decodeSimulationModels(utf8.encode('1:fe-c;1:other')),
throwsFormatException);
expect(() => const SimulationConfig(modelId: 9).previewWatts(15, 0),
throwsFormatException);
});
test('runtime status handles sentinels, faults and stale speed', () {
final inactive = SimulationStatus.decode([1, 0, 0, 0, 255, 255]);
expect(inactive.watts, isNull);
expect(inactive.label, 'Inactive');
final running = SimulationStatus.decode([1, 2, 1, 0, 145, 0]);
expect(running.details, 'Target power: 145 W');
expect(SimulationStatus.decode([1, 2, 0, 0, 145, 0]).details,
'Waiting for trainer speed');
expect(SimulationStatus.decode([1, 3, 0, 5, 255, 255]).details,
contains('denied'));
expect(() => SimulationStatus.decode([2, 0, 0, 0, 0, 0]),
throwsFormatException);
});
}