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(); await tester.tap(find.byKey(const Key('question-source-edit-spelling'))); 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); }); }