feat: basic trainer support
This commit is contained in:
71
test/model/simulation_test.dart
Normal file
71
test/model/simulation_test.dart
Normal 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);
|
||||
});
|
||||
}
|
||||
65
test/service/shifter_settings_test.dart
Normal file
65
test/service/shifter_settings_test.dart
Normal file
@ -0,0 +1,65 @@
|
||||
import 'dart:convert';
|
||||
import 'package:abawo_bt_app/model/shifter_types.dart';
|
||||
import 'package:abawo_bt_app/model/simulation.dart';
|
||||
import 'package:abawo_bt_app/service/shifter_service.dart';
|
||||
import 'package:flutter_test/flutter_test.dart';
|
||||
import '../support/settings_bluetooth.dart';
|
||||
|
||||
void main() {
|
||||
late SettingsBluetooth bluetooth;
|
||||
late ShifterService service;
|
||||
setUp(() {
|
||||
bluetooth = SettingsBluetooth();
|
||||
service = ShifterService(bluetooth: bluetooth, buttonDeviceId: 'test');
|
||||
});
|
||||
tearDown(() async {
|
||||
await service.dispose();
|
||||
await bluetooth.close();
|
||||
});
|
||||
|
||||
test('selection is confirmed before address and connection command',
|
||||
() async {
|
||||
final result = await service.connectButtonToTrainer(
|
||||
const TrainerAddress(flags: 0, bytes: [1, 2, 3, 4, 5, 6]),
|
||||
protocol: 'yesoul');
|
||||
expect(result.isOk(), isTrue);
|
||||
expect(bluetooth.steps, [
|
||||
'write:$trainerProtocolCharacteristicUuid',
|
||||
'read:$trainerProtocolCharacteristicUuid',
|
||||
'write:$universalShifterConnectToAddrCharacteristicUuid',
|
||||
'write:$universalShifterCommandCharacteristicUuid'
|
||||
]);
|
||||
});
|
||||
test('rejected protocol does not connect or write address', () async {
|
||||
bluetooth.rejectWrites = true;
|
||||
final result = await service.connectButtonToTrainer(
|
||||
const TrainerAddress(flags: 0, bytes: [1, 2, 3, 4, 5, 6]),
|
||||
protocol: 'yesoul');
|
||||
expect(result.isErr(), isTrue);
|
||||
expect(bluetooth.steps.length, 2);
|
||||
expect(utf8.decode(bluetooth.values[trainerProtocolCharacteristicUuid]!),
|
||||
'auto');
|
||||
});
|
||||
test('legacy connection does not require optional protocol characteristic',
|
||||
() async {
|
||||
bluetooth.values.remove(trainerProtocolCharacteristicUuid);
|
||||
final result = await service.connectButtonToTrainer(
|
||||
const TrainerAddress(flags: 0, bytes: [1, 2, 3, 4, 5, 6]));
|
||||
expect(result.isOk(), isTrue);
|
||||
expect(bluetooth.steps.length, 2);
|
||||
});
|
||||
test('simulation saves require matching read-back', () async {
|
||||
const config =
|
||||
SimulationConfig(policy: SimulationPolicy.force, riderWeight: 80.25);
|
||||
expect((await service.writeSimulationConfig(config)).isOk(), isTrue);
|
||||
expect((await service.readSimulationConfig()).unwrap().riderWeight, 80.25);
|
||||
bluetooth.rejectWrites = true;
|
||||
expect(
|
||||
(await service.writeSimulationConfig(const SimulationConfig())).isErr(),
|
||||
isTrue);
|
||||
});
|
||||
test('malformed configuration is a recoverable read error', () async {
|
||||
bluetooth.values[simulationConfigCharacteristicUuid] = [1];
|
||||
expect((await service.readSimulationConfig()).isErr(), isTrue);
|
||||
});
|
||||
}
|
||||
52
test/support/settings_bluetooth.dart
Normal file
52
test/support/settings_bluetooth.dart
Normal file
@ -0,0 +1,52 @@
|
||||
import 'dart:async';
|
||||
import 'dart:convert';
|
||||
import 'package:abawo_bt_app/controller/bluetooth.dart';
|
||||
import 'package:abawo_bt_app/model/shifter_types.dart';
|
||||
import 'package:abawo_bt_app/model/simulation.dart';
|
||||
import 'package:anyhow/anyhow.dart';
|
||||
|
||||
class SettingsBluetooth implements BluetoothController {
|
||||
final values = <String, List<int>>{
|
||||
trainerProtocolCharacteristicUuid: utf8.encode('auto'),
|
||||
simulationConfigCharacteristicUuid: const SimulationConfig().encode(),
|
||||
simulationModelsCharacteristicUuid: utf8.encode('1:fe-c;2:track-only'),
|
||||
simulationStatusCharacteristicUuid: [1, 2, 1, 0, 145, 0],
|
||||
};
|
||||
final steps = <String>[];
|
||||
final scan = StreamController<List<int>>.broadcast();
|
||||
final simulation = StreamController<List<int>>.broadcast();
|
||||
bool rejectWrites = false;
|
||||
|
||||
@override
|
||||
Future<Result<List<int>>> readCharacteristic(
|
||||
String deviceId, String serviceUuid, String uuid) async {
|
||||
steps.add('read:$uuid');
|
||||
return values.containsKey(uuid)
|
||||
? Ok(values[uuid]!)
|
||||
: bail('Characteristic not found');
|
||||
}
|
||||
|
||||
@override
|
||||
Future<Result<void>> writeCharacteristic(
|
||||
String deviceId, String serviceUuid, String uuid, List<int> value,
|
||||
{bool withResponse = true}) async {
|
||||
steps.add('write:$uuid');
|
||||
if (!rejectWrites) values[uuid] = List.of(value);
|
||||
return const Ok(null);
|
||||
}
|
||||
|
||||
@override
|
||||
Stream<List<int>> subscribeToCharacteristic(
|
||||
String deviceId, String serviceUuid, String uuid) =>
|
||||
uuid == universalShifterScanResultCharacteristicUuid
|
||||
? scan.stream
|
||||
: simulation.stream;
|
||||
|
||||
@override
|
||||
dynamic noSuchMethod(Invocation invocation) => super.noSuchMethod(invocation);
|
||||
|
||||
Future<void> close() async {
|
||||
await scan.close();
|
||||
await simulation.close();
|
||||
}
|
||||
}
|
||||
190
test/trainer_ui_test.dart
Normal file
190
test/trainer_ui_test.dart
Normal file
@ -0,0 +1,190 @@
|
||||
import 'package:abawo_bt_app/model/shifter_types.dart';
|
||||
import 'package:abawo_bt_app/model/trainer_model.dart';
|
||||
import 'package:abawo_bt_app/pages/trainer_model_search_page.dart';
|
||||
import 'package:abawo_bt_app/service/shifter_service.dart';
|
||||
import 'package:abawo_bt_app/theme/app_theme.dart';
|
||||
import 'package:abawo_bt_app/widgets/bike_scan_dialog.dart';
|
||||
import 'package:abawo_bt_app/widgets/simulation_card.dart';
|
||||
import 'package:flutter/material.dart';
|
||||
import 'package:flutter_test/flutter_test.dart';
|
||||
import 'support/settings_bluetooth.dart';
|
||||
|
||||
void main() {
|
||||
TestWidgetsFlutterBinding.ensureInitialized();
|
||||
test('offline catalog includes all selectable profiles and search aliases',
|
||||
() async {
|
||||
final models = await TrainerModel.load();
|
||||
expect(models.length, 128);
|
||||
expect(models.map((m) => m.id).toSet().length, 128);
|
||||
expect(
|
||||
models.where((m) => m.matches('kettler c12')).single.id, 'kettler-c12');
|
||||
expect(
|
||||
models.where((m) => m.matches('NordicTrack')).length, greaterThan(1));
|
||||
expect(models.any((m) => m.id == 'schwinn-170' || m.id == 'ifit-model-97'),
|
||||
isFalse);
|
||||
});
|
||||
|
||||
testWidgets(
|
||||
'scan includes proprietary and broadcast trainers on a small phone',
|
||||
(tester) async {
|
||||
tester.view.physicalSize = const Size(360, 740);
|
||||
tester.view.devicePixelRatio = 1;
|
||||
addTearDown(tester.view.resetPhysicalSize);
|
||||
addTearDown(tester.view.resetDevicePixelRatio);
|
||||
final bluetooth = SettingsBluetooth();
|
||||
final service =
|
||||
ShifterService(bluetooth: bluetooth, buttonDeviceId: 'test');
|
||||
await tester.pumpWidget(MaterialApp(
|
||||
theme: AppTheme.light(),
|
||||
home: Scaffold(
|
||||
body: Builder(
|
||||
builder: (context) => TextButton(
|
||||
onPressed: () =>
|
||||
BikeScanDialog.show(context, shifter: service),
|
||||
child: const Text('Open'))))));
|
||||
await tester.tap(find.text('Open'));
|
||||
await tester.pump();
|
||||
await tester.pump(const Duration(milliseconds: 300));
|
||||
bluetooth.scan.add(
|
||||
[1, 1, 0, 0, 1, 2, 3, 4, 5, 6, 190, 0x10, 6, ...'Keiser'.codeUnits]);
|
||||
await tester.pump();
|
||||
expect(find.text('Keiser'), findsOneWidget);
|
||||
expect(find.text('Supported'), findsOneWidget);
|
||||
expect(find.text("I can't find my trainer"), findsOneWidget);
|
||||
expect(tester.takeException(), isNull);
|
||||
bluetooth.scan.add([1, 2, 0]);
|
||||
await tester.pump();
|
||||
await tester.tap(find.text("I can't find my trainer"));
|
||||
await tester.pumpAndSettle();
|
||||
expect(find.text('Find trainer model'), findsOneWidget);
|
||||
await tester.pumpWidget(const SizedBox());
|
||||
await service.dispose();
|
||||
await bluetooth.close();
|
||||
});
|
||||
|
||||
testWidgets(
|
||||
'simulation loads status, collapses advanced settings and saves weight',
|
||||
(tester) async {
|
||||
final bluetooth = SettingsBluetooth();
|
||||
final service =
|
||||
ShifterService(bluetooth: bluetooth, buttonDeviceId: 'test');
|
||||
await tester.pumpWidget(MaterialApp(
|
||||
theme: AppTheme.light(),
|
||||
home: Scaffold(
|
||||
body: SingleChildScrollView(
|
||||
child: SimulationCard(shifter: service, enabled: true)))));
|
||||
await tester.pumpAndSettle();
|
||||
expect(find.text('Emulated simulation'), findsOneWidget);
|
||||
expect(find.text('Target power: 145 W'), findsOneWidget);
|
||||
expect(find.byType(PowerPreviewChart), findsNothing);
|
||||
await tester.enterText(
|
||||
find.widgetWithText(TextFormField, 'Rider weight'), '80');
|
||||
await tester.pump();
|
||||
await tester.ensureVisible(find.text('Save changes'));
|
||||
await tester.tap(find.text('Save changes'));
|
||||
await tester.pumpAndSettle();
|
||||
expect(find.text('Simulation settings saved.'), findsOneWidget);
|
||||
await tester.ensureVisible(find.text('Advanced settings'));
|
||||
await tester.tap(find.text('Advanced settings'));
|
||||
await tester.pumpAndSettle();
|
||||
expect(find.byType(PowerPreviewChart), findsNWidgets(2));
|
||||
expect(tester.takeException(), isNull);
|
||||
await tester.pumpWidget(const SizedBox());
|
||||
await service.dispose();
|
||||
await bluetooth.close();
|
||||
});
|
||||
|
||||
testWidgets('older firmware keeps unsupported settings non-editable',
|
||||
(tester) async {
|
||||
final bluetooth = SettingsBluetooth()
|
||||
..values.remove(simulationConfigCharacteristicUuid);
|
||||
final service =
|
||||
ShifterService(bluetooth: bluetooth, buttonDeviceId: 'test');
|
||||
await tester.pumpWidget(MaterialApp(
|
||||
home: Scaffold(body: SimulationCard(shifter: service, enabled: true))));
|
||||
await tester.pumpAndSettle();
|
||||
expect(find.textContaining('Older firmware'), findsOneWidget);
|
||||
expect(find.text('Save changes'), findsNothing);
|
||||
await tester.pumpWidget(const SizedBox());
|
||||
await service.dispose();
|
||||
await bluetooth.close();
|
||||
});
|
||||
|
||||
testWidgets('model search shows exact model and firmware requirements',
|
||||
(tester) async {
|
||||
final models = await tester.runAsync(() => TrainerModel.load());
|
||||
await tester.pumpWidget(MaterialApp(
|
||||
theme: AppTheme.light(),
|
||||
home: TrainerModelSearchPage(
|
||||
canSelect: false, models: Future.value(models!))));
|
||||
await tester.pump();
|
||||
await tester.pump(const Duration(milliseconds: 300));
|
||||
await tester.enterText(find.byType(TextField), 'Kettler C12');
|
||||
await tester.pump();
|
||||
expect(find.text('Kettler C12'), findsWidgets);
|
||||
await tester.tap(find.byType(ListTile));
|
||||
await tester.pump();
|
||||
await tester.pump(const Duration(milliseconds: 300));
|
||||
expect(find.text('Firmware update required'), findsOneWidget);
|
||||
expect(find.text('Data only'), findsOneWidget);
|
||||
expect(find.text('Find nearby trainer'), findsNothing);
|
||||
expect(tester.takeException(), isNull);
|
||||
});
|
||||
|
||||
testWidgets('scan controls remain reachable in landscape with larger text',
|
||||
(tester) async {
|
||||
tester.view.physicalSize = const Size(740, 360);
|
||||
tester.view.devicePixelRatio = 1;
|
||||
addTearDown(tester.view.resetPhysicalSize);
|
||||
addTearDown(tester.view.resetDevicePixelRatio);
|
||||
final bluetooth = SettingsBluetooth();
|
||||
final service =
|
||||
ShifterService(bluetooth: bluetooth, buttonDeviceId: 'test');
|
||||
await tester.pumpWidget(MaterialApp(
|
||||
theme: AppTheme.dark(),
|
||||
builder: (context, child) => MediaQuery(
|
||||
data: MediaQuery.of(context)
|
||||
.copyWith(textScaler: const TextScaler.linear(1.4)),
|
||||
child: child!),
|
||||
home: Scaffold(
|
||||
body: Builder(
|
||||
builder: (context) => TextButton(
|
||||
onPressed: () =>
|
||||
BikeScanDialog.show(context, shifter: service),
|
||||
child: const Text('Open'))))));
|
||||
await tester.tap(find.text('Open'));
|
||||
await tester.pump();
|
||||
await tester.pump(const Duration(milliseconds: 300));
|
||||
expect(find.text("I can't find my trainer"), findsOneWidget);
|
||||
expect(tester.takeException(), isNull);
|
||||
await tester.pumpWidget(const SizedBox());
|
||||
await service.dispose();
|
||||
await bluetooth.close();
|
||||
});
|
||||
|
||||
testWidgets('simulation changes are disabled during firmware update',
|
||||
(tester) async {
|
||||
final bluetooth = SettingsBluetooth();
|
||||
final service =
|
||||
ShifterService(bluetooth: bluetooth, buttonDeviceId: 'test');
|
||||
Widget app(bool enabled) => MaterialApp(
|
||||
home: Scaffold(
|
||||
body: SingleChildScrollView(
|
||||
child: SimulationCard(shifter: service, enabled: enabled))));
|
||||
await tester.pumpWidget(app(true));
|
||||
await tester.pumpAndSettle();
|
||||
await tester.enterText(
|
||||
find.widgetWithText(TextFormField, 'Rider weight'), '82');
|
||||
await tester.pumpWidget(app(false));
|
||||
await tester.pump();
|
||||
final save = tester.widget<FilledButton>(
|
||||
find.widgetWithText(FilledButton, 'Save changes'));
|
||||
expect(save.onPressed, isNull);
|
||||
final count = bluetooth.steps.length;
|
||||
await tester.pump(const Duration(seconds: 1));
|
||||
expect(bluetooth.steps.length, count);
|
||||
await tester.pumpWidget(const SizedBox());
|
||||
await service.dispose();
|
||||
await bluetooth.close();
|
||||
});
|
||||
}
|
||||
Reference in New Issue
Block a user