import 'package:cyberhybridhub/models/app_user.dart'; import 'package:cyberhybridhub/repositories/question_source_config_repository.dart'; import 'package:cyberhybridhub/screens/home_screen.dart'; import 'package:cyberhybridhub/widgets/question_source_settings_sheet.dart'; import 'package:cyberhybridhub/services/questions_hub_service.dart'; import 'package:cyberhybridhub/theme/app_theme.dart'; import 'package:flutter/material.dart'; import 'package:flutter_test/flutter_test.dart'; void main() { setUp(() { QuestionSourceConfigRepository.instance.resetToDefaults(); }); testWidgets('gear icon opens question source settings dialog', ( WidgetTester tester, ) async { final QuestionsHubService hub = QuestionsHubService.instance; hub.hasPendingQuestion.value = false; hub.pendingQuestionCount.value = 0; addTearDown(() { hub.hasPendingQuestion.value = false; hub.pendingQuestionCount.value = 0; }); await tester.pumpWidget( MaterialApp( theme: buildAppTheme(), home: const HomeScreen( user: AppUser(uid: 'u1', displayName: 'Test User'), ), ), ); expect(find.byKey(const Key('question-source-settings-button')), findsOneWidget); await tester.tap(find.byKey(const Key('question-source-settings-button'))); await tester.pumpAndSettle(); expect(find.byKey(const Key('question-source-settings-dialog')), findsOneWidget); expect(find.text('Question sources'), findsOneWidget); expect(find.text('Market question'), findsOneWidget); expect(find.text('Spelling'), findsOneWidget); expect(find.text('Coming soon'), findsNothing); expect(find.text('Geography'), findsNothing); expect(find.text('Weather'), findsNothing); expect(find.text('Trading'), findsNothing); expect( find.byKey(const Key('question-source-enabled-spelling')), findsOneWidget, ); final Switch spellingSwitch = tester.widget( find.byKey(const Key('question-source-enabled-spelling')), ); expect(spellingSwitch.onChanged, isNotNull); expect( find.byKey(const Key('question-source-ratio-spelling')), findsOneWidget, ); expect( find.byKey(const Key('question-source-edit-spelling')), findsOneWidget, ); expect( find.byKey(const Key('question-source-reset-spelling')), findsOneWidget, ); }); testWidgets('spelling edit opens spelling word list screen', ( WidgetTester tester, ) async { await tester.pumpWidget( MaterialApp( theme: buildAppTheme(), onGenerateRoute: (RouteSettings settings) { if (settings.name == '/spelling-words') { return MaterialPageRoute( settings: settings, builder: (_) => const Scaffold( body: Center(child: Text('Spelling words page')), ), ); } return null; }, home: Builder( builder: (BuildContext context) { return Scaffold( body: Center( child: FilledButton( onPressed: () => QuestionSourceSettingsSheet.show(context), child: const Text('Open'), ), ), ); }, ), ), ); await tester.tap(find.text('Open')); await tester.pumpAndSettle(); final Finder editSpelling = find.byKey(const Key('question-source-edit-spelling')); await tester.ensureVisible(editSpelling); await tester.pumpAndSettle(); await tester.tap(editSpelling); await tester.pumpAndSettle(); expect(find.text('Spelling words page'), findsOneWidget); }); testWidgets('saving question source settings updates repository', ( WidgetTester tester, ) async { await tester.pumpWidget( MaterialApp( theme: buildAppTheme(), home: const HomeScreen( user: AppUser(uid: 'u1', displayName: 'Test User'), ), ), ); await tester.tap(find.byKey(const Key('question-source-settings-button'))); await tester.pumpAndSettle(); await tester.drag( find.byKey(const Key('question-source-ratio-market_history')), const Offset(80, 0), ); await tester.pumpAndSettle(); await tester.tap(find.byKey(const Key('question-source-settings-save'))); await tester.pumpAndSettle(); expect( QuestionSourceConfigRepository.instance.current .firstWhere((source) => source.id == 'market_history') .ratio, isNot(50), ); expect(find.byKey(const Key('question-source-settings-dialog')), findsNothing); }); testWidgets('Question Source config top bar appears when panel is open and configures source preferences', ( WidgetTester tester, ) async { final QuestionsHubService hub = QuestionsHubService.instance; hub.hasPendingQuestion.value = true; hub.pendingQuestionCount.value = 1; hub.questionPanelOpen.value = true; addTearDown(() { hub.hasPendingQuestion.value = false; hub.pendingQuestionCount.value = 0; hub.questionPanelOpen.value = false; QuestionSourceConfigRepository.instance.resetToDefaults(); }); await tester.pumpWidget( MaterialApp( theme: buildAppTheme(), home: const HomeScreen( user: AppUser(uid: 'u1', displayName: 'Test User'), ), ), ); // Verify config top bar is displayed when panel is open. expect(find.byKey(const Key('question-source-config-top-bar')), findsOneWidget); expect(find.text('Question source configuration'), findsOneWidget); expect(find.text('Market question: 50%, Spelling: 50%'), findsOneWidget); // Click on the Configure button to open settings dialog. expect(find.byKey(const Key('question-source-config-top-bar-button')), findsOneWidget); await tester.tap(find.byKey(const Key('question-source-config-top-bar-button'))); await tester.pumpAndSettle(); // Verify settings dialog is open. expect(find.byKey(const Key('question-source-settings-dialog')), findsOneWidget); // Close settings dialog. await tester.tap(find.byKey(const Key('question-source-settings-close'))); await tester.pumpAndSettle(); expect(find.byKey(const Key('question-source-settings-dialog')), findsNothing); // Close panel and verify top bar is gone. hub.closeQuestionPanel(); await tester.pumpAndSettle(); expect(find.byKey(const Key('question-source-config-top-bar')), findsNothing); }); testWidgets('proportional slider balancing and manual text inputs', ( WidgetTester tester, ) async { await tester.pumpWidget( MaterialApp( theme: buildAppTheme(), home: const HomeScreen( user: AppUser(uid: 'u1', displayName: 'Test User'), ), ), ); // Open configuration dialog. await tester.tap(find.byKey(const Key('question-source-settings-button'))); await tester.pumpAndSettle(); // Verify initial state: both sources enabled and ratio is 50. final Finder marketInput = find.byKey(const Key('question-source-input-market_history')); final Finder spellingInput = find.byKey(const Key('question-source-input-spelling')); expect(marketInput, findsOneWidget); expect(spellingInput, findsOneWidget); expect(tester.widget(marketInput).controller?.text, '50'); expect(tester.widget(spellingInput).controller?.text, '50'); // 1. Enter 80 in market_history input. await tester.enterText(marketInput, '80'); await tester.pumpAndSettle(); // Verify spelling proportionally reduced to 20. expect(tester.widget(marketInput).controller?.text, '80'); expect(tester.widget(spellingInput).controller?.text, '20'); // 2. Put market_history to 100. await tester.enterText(marketInput, '100'); await tester.pumpAndSettle(); // Verify spelling is auto-disabled. final Switch spellingSwitch = tester.widget( find.byKey(const Key('question-source-enabled-spelling')), ); expect(spellingSwitch.value, isFalse); expect(find.byKey(const Key('question-source-input-spelling')), findsNothing); // 3. Reduce market_history to 70. await tester.enterText(marketInput, '70'); await tester.pumpAndSettle(); // Verify spelling auto-enabled and gets 30%. final Switch spellingSwitchAfter = tester.widget( find.byKey(const Key('question-source-enabled-spelling')), ); expect(spellingSwitchAfter.value, isTrue); expect(find.byKey(const Key('question-source-input-spelling')), findsOneWidget); expect(tester.widget(find.byKey(const Key('question-source-input-spelling'))).controller?.text, '30'); // 4. Manually disable spelling. final Finder enabledSpelling = find.byKey(const Key('question-source-enabled-spelling')); await tester.ensureVisible(enabledSpelling); await tester.pumpAndSettle(); await tester.tap(enabledSpelling); await tester.pumpAndSettle(); // Verify spelling is disabled and market_history automatically becomes 100%. expect(tester.widget(marketInput).controller?.text, '100'); // 5. Manually enable spelling. await tester.ensureVisible(enabledSpelling); await tester.pumpAndSettle(); await tester.tap(enabledSpelling); await tester.pumpAndSettle(); // Verify spelling gets enabled and shares the ratio evenly (50/50). expect(tester.widget(marketInput).controller?.text, '50'); expect(tester.widget(find.byKey(const Key('question-source-input-spelling'))).controller?.text, '50'); // 6. Enter a value > 100 (e.g. 150) in market_history input. await tester.enterText(marketInput, '150'); await tester.pumpAndSettle(); // Verify it clamps to 100. expect(tester.widget(marketInput).controller?.text, '100'); // Close dialog. await tester.tap(find.byKey(const Key('question-source-settings-close'))); await tester.pumpAndSettle(); }); }