games_app/prediqo_mobile/test/splash_section_test.dart

256 lines
7.4 KiB
Dart

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<bool>? hold;
@override
Future<bool> 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<LoginResult> login(
String email,
String password, {
String? totpCode,
String? captchaToken,
}) async =>
throw UnimplementedError();
@override
Future<RegisterResult> register(RegisterRequest request) async =>
throw UnimplementedError();
@override
Future<({bool valid, String? reason})> validateReferralCode(String code) async =>
(valid: true, reason: null);
@override
Future<void> resendVerificationEmail(String email) async {}
@override
Future<void> logout() async {}
@override
Future<Map<String, dynamic>> setup2FA({String? password, String? totpCode}) async =>
{};
@override
Future<List<String>> enable2FA(String code) async => const [];
@override
Future<void> disable2FA({required String password, required String code}) async {}
@override
Future<List<String>> regenerateRecoveryCodes(String totpCode) async => const [];
@override
Future<UserProfile> getProfile({bool force = false}) async =>
throw UnimplementedError();
@override
UserProfile? peekLastProfile() => null;
@override
void invalidateProfile() {}
@override
Future<List<CountryOption>> getCountries({bool force = false}) async => const [];
@override
List<CountryOption>? peekLastCountries() => null;
@override
void invalidateCountries() {}
@override
Future<void> 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<void> changePassword({
required String currentPassword,
required String newPassword,
}) async {}
@override
Future<void> forgotPassword(String email) async {}
}
Future<void> _boot() async {
configureE2eTestEnvironment();
if (sl.isRegistered<AppConfig>()) await sl.reset();
sl.registerSingleton<AppConfig>(
const AppConfig(apiBaseUrl: 'http://localhost', isProduction: false, wsEnabled: false),
);
final locale = LocaleController();
SharedPreferences.setMockInitialValues({'app_locale': 'en'});
await locale.load();
sl.registerSingleton<LocaleController>(locale);
}
void main() {
tearDown(() async {
if (sl.isRegistered<AppConfig>()) 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<bool>();
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<bool>();
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);
});
}