cyberhybridhub/test/question_source_settings_test.dart
nathanAndrinoid 4459852f83
Some checks failed
Admin portal tests / test (push) Has been cancelled
spelling
2026-06-05 03:22:35 +00:00

140 lines
4.4 KiB
Dart

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