316 lines
11 KiB
Dart
316 lines
11 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/network/api_client.dart';
|
|
import 'package:prediqo_mobile/core/network/connectivity_controller.dart';
|
|
import 'package:prediqo_mobile/core/realtime/game_socket_service.dart';
|
|
import 'package:prediqo_mobile/core/theme/app_theme.dart';
|
|
import 'package:prediqo_mobile/data/models/advertisement_models.dart';
|
|
import 'package:prediqo_mobile/data/models/game_catalog_models.dart';
|
|
import 'package:prediqo_mobile/data/models/notification_models.dart';
|
|
import 'package:prediqo_mobile/data/models/referral_models.dart';
|
|
import 'package:prediqo_mobile/data/repositories/advertisement_repository_impl.dart';
|
|
import 'package:prediqo_mobile/data/repositories/game_catalog_repository_impl.dart';
|
|
import 'package:prediqo_mobile/data/repositories/wallet_repository_impl.dart';
|
|
import 'package:prediqo_mobile/domain/repositories/advertisement_repository.dart';
|
|
import 'package:prediqo_mobile/domain/repositories/game_catalog_repository.dart';
|
|
import 'package:prediqo_mobile/domain/repositories/health_repository.dart';
|
|
import 'package:prediqo_mobile/domain/repositories/notification_repository.dart';
|
|
import 'package:prediqo_mobile/domain/repositories/prediction_repository.dart';
|
|
import 'package:prediqo_mobile/domain/repositories/referral_repository.dart';
|
|
import 'package:prediqo_mobile/domain/repositories/repositories.dart';
|
|
import 'package:prediqo_mobile/features/home/presentation/home_screen.dart';
|
|
import 'package:prediqo_mobile/features/prediction/bloc/prediction_bloc.dart';
|
|
import 'package:prediqo_mobile/features/wallet/bloc/wallet_bloc.dart';
|
|
import 'package:prediqo_mobile/l10n/locale_controller.dart';
|
|
import 'package:prediqo_mobile/shared/widgets/pred_skeleton.dart';
|
|
import 'package:shared_preferences/shared_preferences.dart';
|
|
|
|
import 'support/e2e_fixtures.dart';
|
|
|
|
class _ControllableAds implements AdvertisementRepository {
|
|
Completer<List<AdvertisementModel>>? hold;
|
|
final List<AdvertisementModel> cached;
|
|
var listCalls = 0;
|
|
Object? failWith;
|
|
|
|
_ControllableAds({this.cached = const []});
|
|
|
|
@override
|
|
List<AdvertisementModel>? peekCached({String? placement}) =>
|
|
cached.isEmpty ? null : cached;
|
|
|
|
@override
|
|
List<AdvertisementModel>? peekLast({String? placement}) =>
|
|
cached.isEmpty ? null : cached;
|
|
|
|
@override
|
|
void invalidate({String? placement}) {}
|
|
|
|
@override
|
|
Future<List<AdvertisementModel>> list({String? placement, bool force = false}) async {
|
|
listCalls++;
|
|
if (failWith != null) throw failWith!;
|
|
if (hold != null) return hold!.future;
|
|
return cached.isEmpty
|
|
? [
|
|
const AdvertisementModel(
|
|
id: 'a1',
|
|
placement: 'home_hero',
|
|
title: 'Bonus',
|
|
accentColor: '#10B981',
|
|
sortOrder: 0,
|
|
),
|
|
]
|
|
: cached;
|
|
}
|
|
}
|
|
|
|
class _ControllableGames implements GameCatalogRepository {
|
|
Completer<List<GameCatalogItem>>? hold;
|
|
final List<GameCatalogItem> cached;
|
|
var listCalls = 0;
|
|
Object? failWith;
|
|
|
|
_ControllableGames({this.cached = const []});
|
|
|
|
@override
|
|
List<GameCatalogItem>? peekCached({bool featured = false}) =>
|
|
cached.isEmpty ? null : cached;
|
|
|
|
@override
|
|
List<GameCatalogItem>? peekLast({bool featured = false}) =>
|
|
cached.isEmpty ? null : cached;
|
|
|
|
@override
|
|
void invalidate() {}
|
|
|
|
@override
|
|
Future<List<GameCatalogItem>> list({bool? featured, bool force = false}) async {
|
|
listCalls++;
|
|
if (failWith != null) throw failWith!;
|
|
if (hold != null) return hold!.future;
|
|
return cached.isEmpty
|
|
? [
|
|
const GameCatalogItem(
|
|
id: 'g1',
|
|
slug: 'candle-predict',
|
|
title: 'Candle Predict',
|
|
category: 'prediction',
|
|
accentColor: '#10B981',
|
|
status: 'active',
|
|
isFeatured: true,
|
|
sortOrder: 0,
|
|
implementationKey: 'candle-predict',
|
|
),
|
|
]
|
|
: cached;
|
|
}
|
|
}
|
|
|
|
Future<void> _bootHome({
|
|
required AdvertisementRepository ads,
|
|
required GameCatalogRepository games,
|
|
ReferralRepository? referral,
|
|
}) async {
|
|
configureE2eTestEnvironment();
|
|
if (sl.isRegistered<AppConfig>()) await sl.reset();
|
|
|
|
sl.registerSingleton<AppConfig>(
|
|
const AppConfig(apiBaseUrl: 'http://localhost', isProduction: false, wsEnabled: false),
|
|
);
|
|
sl.registerSingleton<ConnectivityController>(ConnectivityController());
|
|
final locale = LocaleController();
|
|
SharedPreferences.setMockInitialValues({'app_locale': 'en'});
|
|
await locale.load();
|
|
sl.registerSingleton<LocaleController>(locale);
|
|
sl.registerLazySingleton<ApiClient>(() => ApiClient(config: sl()));
|
|
sl.registerLazySingleton<GameSocketService>(() => GameSocketService(sl(), sl()));
|
|
sl.registerLazySingleton<AdvertisementRepository>(() => ads);
|
|
sl.registerLazySingleton<GameCatalogRepository>(() => games);
|
|
sl.registerLazySingleton<ReferralRepository>(
|
|
() => referral ?? FakeReferralRepository(),
|
|
);
|
|
sl.registerLazySingleton<NotificationRepository>(() => FakeNotificationRepository());
|
|
sl.registerLazySingleton<AuthRepository>(() => FakeAuthRepository(startLoggedIn: true));
|
|
sl.registerLazySingleton<PredictionRepository>(() => FakePredictionRepository());
|
|
sl.registerLazySingleton<HealthRepository>(() => FakeHealthRepository());
|
|
sl.registerLazySingleton<WalletRepositoryImpl>(() => WalletRepositoryImpl(sl()));
|
|
sl.registerLazySingleton<WalletRepository>(() => sl<WalletRepositoryImpl>());
|
|
sl.registerFactory<WalletBloc>(
|
|
() => WalletBloc(sl<WalletRepository>(), sl(), sl()),
|
|
);
|
|
sl.registerFactory<PredictionBloc>(() => PredictionBloc(sl(), sl(), sl()));
|
|
}
|
|
|
|
void main() {
|
|
tearDown(() async {
|
|
if (sl.isRegistered<AppConfig>()) await sl.reset();
|
|
});
|
|
|
|
test('AdvertisementStore / GameCatalogStore peekLast under 100ms', () {
|
|
final ads = AdvertisementStore();
|
|
final games = GameCatalogStore();
|
|
final sw = Stopwatch()..start();
|
|
for (var i = 0; i < 100; i++) {
|
|
ads.peekLast();
|
|
games.peekLast();
|
|
}
|
|
sw.stop();
|
|
expect(sw.elapsedMilliseconds, lessThan(100));
|
|
});
|
|
|
|
testWidgets('Home paints deposit shortcut while ads still loading', (tester) async {
|
|
final adsHold = Completer<List<AdvertisementModel>>();
|
|
final ads = _ControllableAds()..hold = adsHold;
|
|
final games = _ControllableGames(
|
|
cached: const [
|
|
GameCatalogItem(
|
|
id: 'g1',
|
|
slug: 'candle-predict',
|
|
title: 'Candle Predict',
|
|
category: 'prediction',
|
|
accentColor: '#10B981',
|
|
status: 'active',
|
|
isFeatured: true,
|
|
sortOrder: 0,
|
|
implementationKey: 'candle-predict',
|
|
),
|
|
],
|
|
);
|
|
await _bootHome(ads: ads, games: games);
|
|
|
|
await tester.pumpWidget(
|
|
MultiBlocProvider(
|
|
providers: [
|
|
BlocProvider(create: (_) => sl<WalletBloc>()),
|
|
BlocProvider(create: (_) => sl<PredictionBloc>()),
|
|
],
|
|
child: MaterialApp(
|
|
theme: AppTheme.dark,
|
|
home: const HomeScreen(),
|
|
),
|
|
),
|
|
);
|
|
await tester.pump();
|
|
|
|
expect(find.byKey(const Key('home_deposit_shortcut')), findsOneWidget);
|
|
expect(find.text('Candle Predict'), findsOneWidget);
|
|
expect(find.byType(PredSkeletonCarousel), findsOneWidget);
|
|
|
|
adsHold.complete(const [
|
|
AdvertisementModel(
|
|
id: 'a1',
|
|
placement: 'home_hero',
|
|
title: 'Welcome bonus',
|
|
accentColor: '#10B981',
|
|
sortOrder: 0,
|
|
),
|
|
]);
|
|
await tester.pump();
|
|
await tester.pump(const Duration(milliseconds: 500));
|
|
expect(find.text('Welcome bonus'), findsOneWidget);
|
|
expect(find.byType(PredSkeletonCarousel), findsNothing);
|
|
});
|
|
|
|
testWidgets('Home soft-fails ads without blanking games', (tester) async {
|
|
final ads = _ControllableAds()..failWith = Exception('ads down');
|
|
final games = _ControllableGames(
|
|
cached: const [
|
|
GameCatalogItem(
|
|
id: 'g1',
|
|
slug: 'candle-predict',
|
|
title: 'Candle Predict',
|
|
category: 'prediction',
|
|
accentColor: '#10B981',
|
|
status: 'active',
|
|
isFeatured: true,
|
|
sortOrder: 0,
|
|
implementationKey: 'candle-predict',
|
|
),
|
|
],
|
|
);
|
|
await _bootHome(ads: ads, games: games);
|
|
|
|
await tester.pumpWidget(
|
|
MultiBlocProvider(
|
|
providers: [
|
|
BlocProvider(create: (_) => sl<WalletBloc>()),
|
|
BlocProvider(create: (_) => sl<PredictionBloc>()),
|
|
],
|
|
child: MaterialApp(
|
|
theme: AppTheme.dark,
|
|
home: const HomeScreen(),
|
|
),
|
|
),
|
|
);
|
|
await tester.pump();
|
|
await tester.pump(const Duration(milliseconds: 500));
|
|
|
|
// Soft-fail: empty promo placeholder, games remain; no ads SectionError.
|
|
expect(find.textContaining('promotions coming soon'), findsOneWidget);
|
|
expect(find.text('Candle Predict'), findsOneWidget);
|
|
expect(find.byKey(const Key('home_deposit_shortcut')), findsOneWidget);
|
|
});
|
|
|
|
testWidgets('Home ads in-flight dedupes parallel list calls', (tester) async {
|
|
final adsHold = Completer<List<AdvertisementModel>>();
|
|
final ads = _ControllableAds()..hold = adsHold;
|
|
final games = _ControllableGames(
|
|
cached: const [
|
|
GameCatalogItem(
|
|
id: 'g1',
|
|
slug: 'candle-predict',
|
|
title: 'Candle Predict',
|
|
category: 'prediction',
|
|
accentColor: '#10B981',
|
|
status: 'active',
|
|
isFeatured: true,
|
|
sortOrder: 0,
|
|
implementationKey: 'candle-predict',
|
|
),
|
|
],
|
|
);
|
|
await _bootHome(ads: ads, games: games);
|
|
|
|
await tester.pumpWidget(
|
|
MultiBlocProvider(
|
|
providers: [
|
|
BlocProvider(create: (_) => sl<WalletBloc>()),
|
|
BlocProvider(create: (_) => sl<PredictionBloc>()),
|
|
],
|
|
child: MaterialApp(
|
|
theme: AppTheme.dark,
|
|
home: const HomeScreen(),
|
|
),
|
|
),
|
|
);
|
|
await tester.pump();
|
|
await tester.pump(const Duration(milliseconds: 50));
|
|
expect(ads.listCalls, 1);
|
|
|
|
// Pull-to-refresh while first fetch still held reuses the in-flight future.
|
|
await tester.fling(find.byType(CustomScrollView), const Offset(0, 400), 1000);
|
|
await tester.pump();
|
|
await tester.pump(const Duration(milliseconds: 100));
|
|
expect(ads.listCalls, 1);
|
|
|
|
adsHold.complete(const [
|
|
AdvertisementModel(
|
|
id: 'a1',
|
|
placement: 'home_hero',
|
|
title: 'Welcome bonus',
|
|
accentColor: '#10B981',
|
|
sortOrder: 0,
|
|
),
|
|
]);
|
|
await tester.pump();
|
|
await tester.pump(const Duration(milliseconds: 500));
|
|
expect(find.text('Welcome bonus'), findsOneWidget);
|
|
});
|
|
}
|