games_app/prediqo_mobile/test/wallet_overview_section_test.dart
2026-10-25 16:42:52 +02:00

282 lines
8.8 KiB
Dart

import 'dart:async';
import 'package:dio/dio.dart';
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/theme/app_theme.dart';
import 'package:prediqo_mobile/data/models/balance_model.dart';
import 'package:prediqo_mobile/data/models/test_wallet_models.dart';
import 'package:prediqo_mobile/data/models/transaction_model.dart';
import 'package:prediqo_mobile/data/models/wallet_type_model.dart';
import 'package:prediqo_mobile/data/repositories/share_card_repository.dart';
import 'package:prediqo_mobile/data/repositories/wallet_repository_impl.dart';
import 'package:prediqo_mobile/domain/repositories/health_repository.dart';
import 'package:prediqo_mobile/domain/repositories/referral_repository.dart';
import 'package:prediqo_mobile/features/wallet/bloc/wallet_bloc.dart';
import 'package:prediqo_mobile/features/wallet/presentation/wallet_overview_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 _usdt = WalletTypeModel(
id: 1,
code: 'USDT_TRC20',
name: 'USDT',
network: 'TRON',
decimals: 6,
);
BalanceModel _balance() => BalanceModel(
walletType: _usdt,
available: '9290000',
locked: '0',
bonusAvailable: '0',
testAvailable: '5000000',
);
TransactionModel _tx(String id) => TransactionModel(
id: id,
walletTypeId: 1,
type: 'deposit',
amount: '1000000',
fee: '0',
status: 'completed',
createdAt: '2026-09-24T10:00:00.000Z',
);
class _ControllableWallet implements WalletRepository {
Completer<List<BalanceModel>>? balancesHold;
Completer<List<TransactionModel>>? depositsHold;
Object? balancesFail;
Object? depositsFail;
var balancesCalls = 0;
var txCalls = 0;
@override
Future<List<WalletTypeModel>> getWalletTypes() async => const [_usdt];
@override
Future<List<BalanceModel>> getBalances() async {
balancesCalls++;
if (balancesFail != null) throw balancesFail!;
if (balancesHold != null) return balancesHold!.future;
return [_balance()];
}
@override
Future<List<TransactionModel>> getTransactions({
int limit = 50,
int offset = 0,
String? category,
}) async {
txCalls++;
if (category == 'deposits') {
if (depositsHold != null) return depositsHold!.future;
if (depositsFail != null) throw depositsFail!;
}
return [_tx('d1')];
}
@override
Future<TestWalletStatusModel> getTestWalletStatus() async =>
const TestWalletStatusModel(
enabled: true,
balanceRaw: '5000000',
walletTypeId: 1,
decimals: 6,
creditAmountRaw: '10000000',
maxRequests: 3,
requestsUsed: 0,
canRequest: true,
);
@override
Future<TestWalletCreditResult> requestTestCredits() async {
throw UnimplementedError();
}
@override
Future<DepositAddressResult> getDepositAddress(String walletType) async {
throw UnimplementedError();
}
@override
Future<WithdrawResult> withdraw(
String walletType,
String toAddress,
String amount, {
String? totpCode,
}) async {
throw UnimplementedError();
}
@override
Future<TransferResult> transfer(
String toEmail,
String walletType,
String amount, {
String? totpCode,
}) async {
throw UnimplementedError();
}
}
class _ShareOkInterceptor extends Interceptor {
@override
void onRequest(RequestOptions options, RequestInterceptorHandler handler) {
handler.resolve(
Response(requestOptions: options, data: <String, dynamic>{}),
);
}
}
Future<void> _boot(_ControllableWallet wallet) 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();
// Reveal amounts in tests (product default remains privacy-first / hidden).
SharedPreferences.setMockInitialValues({
'app_locale': 'en',
'wallet_hide_balance': false,
});
await locale.load();
sl.registerSingleton<LocaleController>(locale);
final api = ApiClient(config: sl());
api.dio.interceptors.insert(0, _ShareOkInterceptor());
sl.registerLazySingleton<ApiClient>(() => api);
sl.registerLazySingleton<ShareCardRepository>(() => ShareCardRepository(sl()));
sl.registerLazySingleton<HealthRepository>(() => FakeHealthRepository());
sl.registerLazySingleton<ReferralRepository>(() => FakeReferralRepository());
sl.registerLazySingleton<WalletRepository>(() => wallet);
sl.registerLazySingleton<WalletRepositoryImpl>(() => WalletRepositoryImpl(sl()));
sl.registerFactory<WalletBloc>(
() => WalletBloc(wallet, sl(), sl(), shareCards: sl()),
);
}
Widget _app() => BlocProvider(
create: (_) => sl<WalletBloc>(),
child: MaterialApp(
theme: AppTheme.dark,
home: const WalletOverviewScreen(),
),
);
void main() {
tearDown(() async {
if (sl.isRegistered<AppConfig>()) await sl.reset();
});
testWidgets('shows wallet chrome + balance skeletons while soldes load', (tester) async {
final hold = Completer<List<BalanceModel>>();
final wallet = _ControllableWallet()..balancesHold = hold;
await _boot(wallet);
await tester.pumpWidget(_app());
await tester.pump();
await tester.pump(const Duration(milliseconds: 50));
expect(find.text('Wallet'), findsOneWidget);
expect(find.text('Activity'), findsOneWidget);
expect(find.byType(PredSkeletonCard), findsWidgets);
expect(find.text('Main Wallet'), findsNothing);
expect(
find.descendant(
of: find.byType(RefreshIndicator),
matching: find.byType(CircularProgressIndicator),
),
findsNothing,
);
hold.complete([_balance()]);
await tester.pump();
await tester.pump(const Duration(milliseconds: 80));
expect(find.text('Main Wallet'), findsOneWidget);
expect(find.textContaining('9.29'), findsOneWidget);
});
testWidgets('balance SectionError keeps Activity chrome and retries once', (tester) async {
final wallet = _ControllableWallet()..balancesFail = Exception('offline');
await _boot(wallet);
await tester.pumpWidget(_app());
await tester.pump();
await tester.pump(const Duration(milliseconds: 80));
expect(find.byType(SectionError), findsOneWidget);
expect(find.text('Activity'), findsOneWidget);
expect(find.text('Wallet'), findsOneWidget);
wallet.balancesFail = null;
final before = wallet.balancesCalls;
final retry = find.descendant(
of: find.byType(SectionError),
matching: find.byType(IconButton),
);
await tester.tap(retry);
await tester.pump();
await tester.pump(const Duration(milliseconds: 80));
expect(wallet.balancesCalls, before + 1);
expect(find.text('Main Wallet'), findsOneWidget);
expect(find.byType(SectionError), findsNothing);
});
testWidgets('Activity SectionError keeps balances visible on deposits failure', (tester) async {
final wallet = _ControllableWallet()..depositsFail = Exception('tx down');
await _boot(wallet);
// Tall surface so Activity slivers below the three wallet cards are built.
tester.view.physicalSize = const Size(400, 2000);
tester.view.devicePixelRatio = 1.0;
addTearDown(tester.view.resetPhysicalSize);
addTearDown(tester.view.resetDevicePixelRatio);
late WalletBloc bloc;
await tester.pumpWidget(
BlocProvider(
create: (_) {
bloc = sl<WalletBloc>();
return bloc;
},
child: MaterialApp(
theme: AppTheme.dark,
home: const WalletOverviewScreen(),
),
),
);
await tester.pump();
await tester.pump(const Duration(milliseconds: 200));
expect(find.text('Main Wallet'), findsOneWidget);
expect(bloc.state.deposits.error, isNotNull);
expect(find.byType(SectionError), findsOneWidget);
wallet.depositsFail = null;
final retry = find.descendant(
of: find.byType(SectionError),
matching: find.byType(IconButton),
);
await tester.tap(retry);
await tester.pump();
await tester.pump(const Duration(milliseconds: 100));
expect(find.byType(SectionError), findsNothing);
expect(find.text('Main Wallet'), findsOneWidget);
});
}