import 'dart:async'; import 'package:flutter/material.dart'; import 'package:flutter_test/flutter_test.dart'; import 'package:prediqo_mobile/core/config/app_config.dart'; import 'package:prediqo_mobile/core/di/injection.dart'; import 'package:prediqo_mobile/core/theme/app_theme.dart'; import 'package:prediqo_mobile/data/models/faq_models.dart'; import 'package:prediqo_mobile/data/repositories/faq_repository_impl.dart'; import 'package:prediqo_mobile/domain/repositories/faq_repository.dart'; import 'package:prediqo_mobile/features/faq/presentation/faq_screen.dart'; import 'package:prediqo_mobile/l10n/locale_controller.dart'; import 'package:prediqo_mobile/shared/widgets/pred_skeleton.dart'; import 'package:prediqo_mobile/shared/widgets/section_error.dart'; import 'package:shared_preferences/shared_preferences.dart'; import 'support/e2e_fixtures.dart'; const _sample = FaqEntryModel( id: '1', question: 'How do predictions work?', answer: 'Choose long or short before the round locks.', category: 'game', ); class _ControllableFaq implements FaqRepository { Completer>? hold; List? cached; var listCalls = 0; Object? failWith; String? lastSearch; _ControllableFaq({this.cached}); @override List? peekLast({String? search, String? category}) => (search == null || search.isEmpty) ? cached : null; @override void invalidate() => cached = null; @override Future> listEntries({ String? search, String? category, bool force = false, }) async { listCalls++; lastSearch = search; if (failWith != null) throw failWith!; if (hold != null) return hold!.future; if (search != null && search.isNotEmpty) { return [ FaqEntryModel( id: '2', question: 'Search hit for $search', answer: 'Answer', category: 'game', ), ]; } return cached ?? const [_sample]; } } Future _boot(FaqRepository faq) async { configureE2eTestEnvironment(); if (sl.isRegistered()) await sl.reset(); sl.registerSingleton( const AppConfig(apiBaseUrl: 'http://localhost', isProduction: false, wsEnabled: false), ); final locale = LocaleController(); SharedPreferences.setMockInitialValues({'app_locale': 'en'}); await locale.load(); sl.registerSingleton(locale); sl.registerLazySingleton(() => faq); } void _tallSurface(WidgetTester tester) { tester.view.physicalSize = const Size(400, 2400); tester.view.devicePixelRatio = 1.0; addTearDown(tester.view.resetPhysicalSize); addTearDown(tester.view.resetDevicePixelRatio); } void main() { tearDown(() async { if (sl.isRegistered()) await sl.reset(); }); test('FaqListStore peekLast under 100ms', () { final store = FaqListStore(); final sw = Stopwatch()..start(); for (var i = 0; i < 100; i++) { store.peekLast(); } sw.stop(); expect(sw.elapsedMilliseconds, lessThan(100)); }); testWidgets('FAQ peeks cached entries while refresh loads', (tester) async { _tallSurface(tester); final hold = Completer>(); final faq = _ControllableFaq(cached: const [_sample])..hold = hold; await _boot(faq); await tester.pumpWidget( MaterialApp(theme: AppTheme.dark, home: const FaqScreen()), ); await tester.pump(); expect(find.text('How do predictions work?'), findsOneWidget); expect(find.byKey(const Key('faq_search')), findsOneWidget); expect(find.byType(PredSkeletonCard), findsNothing); hold.complete(const [ FaqEntryModel( id: '1', question: 'Fresh FAQ question', answer: 'Fresh answer', category: 'game', ), ]); await tester.pumpAndSettle(); expect(find.text('Fresh FAQ question'), findsOneWidget); }); testWidgets('FAQ shows skeletons without blanking search field', (tester) async { _tallSurface(tester); final hold = Completer>(); final faq = _ControllableFaq()..hold = hold; await _boot(faq); await tester.pumpWidget( MaterialApp(theme: AppTheme.dark, home: const FaqScreen()), ); await tester.pump(); expect(find.byKey(const Key('faq_search')), findsOneWidget); expect(find.byType(PredSkeletonCard), findsWidgets); hold.complete(const [_sample]); await tester.pumpAndSettle(); expect(find.text('How do predictions work?'), findsOneWidget); }); testWidgets('FAQ search keeps previous entries visible while loading', (tester) async { _tallSurface(tester); final searchHold = Completer>(); final faq = _ControllableFaq(cached: const [_sample]); await _boot(faq); await tester.pumpWidget( MaterialApp(theme: AppTheme.dark, home: const FaqScreen()), ); await tester.pumpAndSettle(); expect(find.text('How do predictions work?'), findsOneWidget); faq.hold = searchHold; await tester.enterText(find.byKey(const Key('faq_search')), 'wallet'); await tester.testTextInput.receiveAction(TextInputAction.done); await tester.pump(); expect(find.text('How do predictions work?'), findsOneWidget); expect(find.byType(LinearProgressIndicator), findsOneWidget); searchHold.complete(const [ FaqEntryModel( id: '2', question: 'Search hit for wallet', answer: 'Answer', category: 'game', ), ]); await tester.pumpAndSettle(); expect(find.text('Search hit for wallet'), findsOneWidget); }); testWidgets('FAQ shows SectionError without blanking search', (tester) async { _tallSurface(tester); final faq = _ControllableFaq()..failWith = Exception('down'); await _boot(faq); await tester.pumpWidget( MaterialApp(theme: AppTheme.dark, home: const FaqScreen()), ); await tester.pumpAndSettle(); expect(find.byType(SectionError), findsOneWidget); expect(find.byKey(const Key('faq_search')), findsOneWidget); }); testWidgets('FAQ retry does not launch parallel duplicate fetches', (tester) async { _tallSurface(tester); final faq = _ControllableFaq()..failWith = Exception('down'); await _boot(faq); await tester.pumpWidget( MaterialApp(theme: AppTheme.dark, home: const FaqScreen()), ); await tester.pump(); await tester.pump(const Duration(milliseconds: 50)); expect(find.byType(SectionError), findsOneWidget); final before = faq.listCalls; faq.failWith = null; faq.hold = Completer>(); final retry = find.byKey(const Key('section_error_retry')); expect(retry, findsOneWidget); await tester.tap(retry); await tester.pump(); if (retry.evaluate().isNotEmpty) { await tester.tap(retry); await tester.pump(); } expect(faq.listCalls, before + 1); faq.hold!.complete(const [_sample]); await tester.pumpAndSettle(); expect(find.text('How do predictions work?'), findsOneWidget); }); }