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( 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(); }); }