import 'dart:async'; import 'package:flutter/material.dart'; import 'package:flutter_bloc/flutter_bloc.dart'; import 'package:flutter_test/flutter_test.dart'; import 'package:go_router/go_router.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/repositories/auth_repository_impl.dart'; import 'package:prediqo_mobile/domain/repositories/repositories.dart'; import 'package:prediqo_mobile/features/auth/bloc/auth_bloc.dart'; import 'package:prediqo_mobile/features/splash/presentation/splash_screen.dart'; import 'package:prediqo_mobile/l10n/locale_controller.dart'; import 'package:shared_preferences/shared_preferences.dart'; import 'support/e2e_fixtures.dart'; class _SlowAuth implements AuthRepository { _SlowAuth({required this.loggedIn, this.hold}); final bool loggedIn; Completer? hold; @override Future isLoggedIn() async { if (hold != null) return hold!.future.then((_) => loggedIn); return loggedIn; } @override Future<({String? userId, String? role})> getStoredUser() async => (userId: loggedIn ? 'u1' : null, role: loggedIn ? 'user' : null); @override Future login( String email, String password, { String? totpCode, String? captchaToken, }) async => throw UnimplementedError(); @override Future register(RegisterRequest request) async => throw UnimplementedError(); @override Future<({bool valid, String? reason})> validateReferralCode(String code) async => (valid: true, reason: null); @override Future resendVerificationEmail(String email) async {} @override Future logout() async {} @override Future> setup2FA({String? password, String? totpCode}) async => {}; @override Future> enable2FA(String code) async => const []; @override Future disable2FA({required String password, required String code}) async {} @override Future> regenerateRecoveryCodes(String totpCode) async => const []; @override Future getProfile({bool force = false}) async => throw UnimplementedError(); @override UserProfile? peekLastProfile() => null; @override void invalidateProfile() {} @override Future> getCountries({bool force = false}) async => const []; @override List? peekLastCountries() => null; @override void invalidateCountries() {} @override Future updateProfile({ String? firstName, String? middleName, String? lastName, String? dateOfBirth, required String email, String? phone, required String countryOfResidence, String? nationality, required String gender, }) async {} @override Future changePassword({ required String currentPassword, required String newPassword, }) async {} @override Future forgotPassword(String email) async {} } Future _boot() 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); } void main() { tearDown(() async { if (sl.isRegistered()) await sl.reset(); }); test('splashIsNewerVersion compares semver-ish tags', () { expect(splashIsNewerVersion('1.2.0', '1.1.9'), isTrue); expect(splashIsNewerVersion('1.1.9', '1.2.0'), isFalse); expect(splashIsNewerVersion('1.2.0', '1.2.0'), isFalse); expect(splashIsNewerVersion('2.0', '1.9.9'), isTrue); }); test('splashShouldWaitForAuth only for initial/loading', () { expect(splashShouldWaitForAuth(AuthStatus.initial), isTrue); expect(splashShouldWaitForAuth(AuthStatus.loading), isTrue); expect(splashShouldWaitForAuth(AuthStatus.submitting), isFalse); expect(splashShouldWaitForAuth(AuthStatus.authenticated), isFalse); expect(splashShouldWaitForAuth(AuthStatus.unauthenticated), isFalse); }); test('kSplashMinBrandingDelay is at least 1.2s', () { expect(kSplashMinBrandingDelay.inMilliseconds, greaterThanOrEqualTo(1200)); }); testWidgets('Splash waits branding delay + real auth before welcome', (tester) async { await _boot(); final hold = Completer(); final authRepo = _SlowAuth(loggedIn: false, hold: hold); final authBloc = AuthBloc(authRepo); final router = GoRouter( initialLocation: '/', routes: [ GoRoute(path: '/', builder: (_, __) => const SplashScreen()), GoRoute( path: '/welcome', builder: (_, __) => const Scaffold( key: Key('welcome_dest'), body: Text('welcome'), ), ), GoRoute( path: '/home', builder: (_, __) => const Scaffold( key: Key('home_dest'), body: Text('home'), ), ), ], ); await tester.pumpWidget( BlocProvider.value( value: authBloc, child: MaterialApp.router( theme: AppTheme.dark, routerConfig: router, ), ), ); await tester.pump(); expect(find.byKey(const Key('splash_screen')), findsOneWidget); // Auth still pending — must not navigate early. await tester.pump(const Duration(milliseconds: 1300)); expect(find.byKey(const Key('splash_screen')), findsOneWidget); expect(find.byKey(const Key('welcome_dest')), findsNothing); hold.complete(true); await tester.pump(); await tester.pump(const Duration(milliseconds: 100)); expect(find.byKey(const Key('welcome_dest')), findsOneWidget); expect(find.byKey(const Key('home_dest')), findsNothing); await tester.pumpWidget(const SizedBox.shrink()); await tester.pump(); addTearDown(authBloc.close); }); testWidgets('Splash routes to home only after real signed-in check', (tester) async { await _boot(); final hold = Completer(); final authRepo = _SlowAuth(loggedIn: true, hold: hold); final authBloc = AuthBloc(authRepo); final router = GoRouter( initialLocation: '/', routes: [ GoRoute(path: '/', builder: (_, __) => const SplashScreen()), GoRoute( path: '/welcome', builder: (_, __) => const Scaffold( key: Key('welcome_dest'), body: Text('welcome'), ), ), GoRoute( path: '/home', builder: (_, __) => const Scaffold( key: Key('home_dest'), body: Text('home'), ), ), ], ); await tester.pumpWidget( BlocProvider.value( value: authBloc, child: MaterialApp.router( theme: AppTheme.dark, routerConfig: router, ), ), ); await tester.pump(); await tester.pump(const Duration(milliseconds: 1300)); expect(find.byKey(const Key('splash_screen')), findsOneWidget); hold.complete(true); await tester.pump(); await tester.pump(const Duration(milliseconds: 100)); expect(find.byKey(const Key('home_dest')), findsOneWidget); await tester.pumpWidget(const SizedBox.shrink()); await tester.pump(); addTearDown(authBloc.close); }); }