games_app/prediqo_mobile/test/account_section_load_test.dart

279 lines
8.0 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: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/wallet/presentation/account_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';
class _ControllableAuth implements AuthRepository {
Completer<UserProfile>? hold;
UserProfile? cached;
var getProfileCalls = 0;
Object? failWith;
_ControllableAuth({this.cached});
@override
UserProfile? peekLastProfile() => cached;
@override
void invalidateProfile() => cached = null;
@override
Future<UserProfile> getProfile({bool force = false}) async {
getProfileCalls++;
if (failWith != null) throw failWith!;
if (hold != null) return hold!.future;
return cached ??
const UserProfile(
firstName: 'Ada',
lastName: 'Lovelace',
email: 'ada@prediqo.test',
countryOfResidence: 'FR',
gender: 'female',
totpEnabled: false,
);
}
@override
List<CountryOption>? peekLastCountries() => null;
@override
void invalidateCountries() {}
@override
Future<List<CountryOption>> getCountries({bool force = false}) async =>
const [CountryOption(code: 'FR', name: 'France')];
@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<bool> isLoggedIn() async => true;
@override
Future<({String? userId, String? role})> getStoredUser() async =>
(userId: 'u1', role: 'user');
@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<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(AuthRepository auth) 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);
sl.registerLazySingleton<AuthRepository>(() => auth);
}
Widget _wrap(AuthRepository auth) {
return RepositoryProvider<AuthRepository>.value(
value: auth,
child: BlocProvider(
create: (_) => AuthBloc(auth),
child: MaterialApp(
theme: AppTheme.dark,
home: const AccountScreen(),
),
),
);
}
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<AppConfig>()) await sl.reset();
});
test('AuthProfileStore peekLast under 100ms', () {
final store = AuthProfileStore();
final sw = Stopwatch()..start();
for (var i = 0; i < 100; i++) {
store.peekLast();
}
sw.stop();
expect(sw.elapsedMilliseconds, lessThan(100));
});
testWidgets('Account peeks cached profile while refresh loads', (tester) async {
_tallSurface(tester);
final hold = Completer<UserProfile>();
final auth = _ControllableAuth(
cached: const UserProfile(
firstName: 'Cached',
lastName: 'User',
email: 'cached@prediqo.test',
countryOfResidence: 'FR',
gender: 'male',
),
)..hold = hold;
await _boot(auth);
await tester.pumpWidget(_wrap(auth));
await tester.pump();
expect(find.text('Cached User'), findsOneWidget);
expect(find.text('Update profile'), findsOneWidget);
expect(find.byType(PredSkeleton), findsNothing);
hold.complete(const UserProfile(
firstName: 'Fresh',
lastName: 'User',
email: 'fresh@prediqo.test',
countryOfResidence: 'FR',
gender: 'male',
));
await tester.pumpAndSettle();
expect(find.text('Fresh User'), findsOneWidget);
});
testWidgets('Account shows skeleton then paints when no cache', (tester) async {
_tallSurface(tester);
final hold = Completer<UserProfile>();
final auth = _ControllableAuth()..hold = hold;
await _boot(auth);
await tester.pumpWidget(_wrap(auth));
await tester.pump();
expect(find.byType(PredSkeleton), findsWidgets);
expect(find.text('Update profile'), findsOneWidget);
hold.complete(const UserProfile(
firstName: 'Ada',
lastName: 'Lovelace',
email: 'ada@prediqo.test',
countryOfResidence: 'FR',
gender: 'female',
));
await tester.pumpAndSettle();
expect(find.text('Ada Lovelace'), findsOneWidget);
expect(find.byType(PredSkeleton), findsNothing);
});
testWidgets('Account shows SectionError without blanking tiles', (tester) async {
_tallSurface(tester);
final auth = _ControllableAuth()..failWith = Exception('down');
await _boot(auth);
await tester.pumpWidget(_wrap(auth));
await tester.pumpAndSettle();
expect(find.byType(SectionError), findsOneWidget);
expect(find.text('Update profile'), findsOneWidget);
expect(find.text('Log out'), findsOneWidget);
});
testWidgets('Account retry does not launch parallel duplicate fetches', (tester) async {
_tallSurface(tester);
final auth = _ControllableAuth()..failWith = Exception('down');
await _boot(auth);
await tester.pumpWidget(_wrap(auth));
await tester.pump();
await tester.pump(const Duration(milliseconds: 50));
expect(find.byType(SectionError), findsOneWidget);
final before = auth.getProfileCalls;
auth.failWith = null;
auth.hold = Completer<UserProfile>();
final retry = find.byKey(const Key('section_error_retry'));
expect(retry, findsOneWidget);
await tester.ensureVisible(retry);
await tester.tap(retry);
await tester.pump();
// Second tap while in-flight must not start another fetch.
if (retry.evaluate().isNotEmpty) {
await tester.tap(retry);
await tester.pump();
}
expect(auth.getProfileCalls, before + 1);
auth.hold!.complete(const UserProfile(
firstName: 'Ada',
lastName: 'Lovelace',
email: 'ada@prediqo.test',
countryOfResidence: 'FR',
gender: 'female',
));
await tester.pumpAndSettle();
expect(find.text('Ada Lovelace'), findsOneWidget);
});
}