games_app/prediqo_mobile/test/prediction_candle_sections_test.dart

185 lines
4.8 KiB
Dart

import 'package:dio/dio.dart';
import 'package:flutter_test/flutter_test.dart';
import 'package:prediqo_mobile/core/config/app_config.dart';
import 'package:prediqo_mobile/core/network/api_client.dart';
import 'package:prediqo_mobile/core/realtime/game_socket_service.dart';
import 'package:prediqo_mobile/data/models/prediction_models.dart';
import 'package:prediqo_mobile/data/repositories/wallet_repository_impl.dart';
import 'package:prediqo_mobile/domain/repositories/prediction_repository.dart';
import 'package:prediqo_mobile/features/prediction/bloc/prediction_bloc.dart';
const _interval = IntervalModel(
id: 1,
intervalMinutes: 1,
label: '1 min',
isActive: true,
isDefault: true,
payoutMultiplier: 1.8,
chartCandleLimit: 60,
);
GameStateModel _gs() {
const config = PredictConfigModel(
stakeAmounts: [1, 5, 10],
minStakeAmount: 1,
maxStakeAmount: 100,
payoutMultiplier: 1.8,
predictionWindowSeconds: 55,
isActive: true,
);
return GameStateModel(
selectedIntervalMinutes: 1,
config: config,
intervals: const [_interval],
interval: _interval,
multiLevels: const [],
round: RoundModel(
id: 'r1',
status: 'betting',
secondsToBettingEnd: 55,
secondsToBettingStart: 0,
secondsToClose: 60,
serverNowMs: 1_000_000,
candleOpenMs: 1_055_000,
),
);
}
OhlcCandleModel _candle(int openMs) => OhlcCandleModel(
time: openMs ~/ 1000,
open: 100,
high: 101,
low: 99,
close: 100.5,
candleOpenMs: openMs,
);
class _ControllableRepo implements PredictionRepository {
Object? candlesFail;
Object? historyFail;
@override
Future<GameStateModel> getState({int? intervalMinutes}) async => _gs();
@override
Future<CandlesPageModel> getCandlesPage({
int? intervalMinutes,
int limit = 60,
int? beforeOpenMs,
}) async {
if (candlesFail != null) throw candlesFail!;
return CandlesPageModel(
items: [_candle(1_000_000)],
hasMore: false,
);
}
@override
Future<PredictionsPageModel> getMyBets({
int page = 1,
int pageSize = 5,
int? intervalMinutes,
String? stakeAccountKind,
String? status,
}) async {
if (historyFail != null) throw historyFail!;
return const PredictionsPageModel(
items: [],
total: 0,
page: 1,
totalPages: 1,
);
}
@override
Future<void> placeBet({
required String direction,
required double stakeAmount,
required int intervalMinutes,
String stakeAccountKind = 'main',
}) async {}
@override
Future<void> placeMultiBet({
required int level,
required List<String> sequence,
required double stakeAmount,
required int intervalMinutes,
String stakeAccountKind = 'main',
}) async {}
@override
Future<AutoSettingsModel> saveAutoSettings(Map<String, dynamic> body) async =>
throw UnimplementedError();
}
class _OkInterceptor extends Interceptor {
@override
void onRequest(RequestOptions options, RequestInterceptorHandler handler) {
handler.resolve(
Response(requestOptions: options, data: <dynamic>[]),
);
}
}
PredictionBloc _bloc(_ControllableRepo repo) {
const config = AppConfig(
apiBaseUrl: 'http://localhost',
isProduction: false,
wsEnabled: false,
);
final api = ApiClient(config: config);
api.dio.interceptors.insert(0, _OkInterceptor());
return PredictionBloc(
repo,
GameSocketService(config, api),
WalletRepositoryImpl(api),
);
}
void main() {
TestWidgetsFlutterBinding.ensureInitialized();
test('candles failure sets section error without clearing game state', () async {
final repo = _ControllableRepo()..candlesFail = Exception('chart down');
final bloc = _bloc(repo);
bloc.add(const PredictionLoadRequested());
await bloc.stream.firstWhere(
(s) => s.gameState != null && s.candlesError != null,
);
expect(bloc.state.gameState, isNotNull);
expect(bloc.state.candles, isEmpty);
expect(bloc.state.candlesError, contains('chart down'));
repo.candlesFail = null;
bloc.add(const PredictionCandlesReloadRequested());
await bloc.stream.firstWhere((s) => s.candles.isNotEmpty);
expect(bloc.state.candlesError, isNull);
await bloc.close();
});
test('history failure sets section error independently', () async {
final repo = _ControllableRepo()..historyFail = Exception('history down');
final bloc = _bloc(repo);
bloc.add(const PredictionLoadRequested());
await bloc.stream.firstWhere(
(s) => s.gameState != null && s.historyError != null,
);
expect(bloc.state.gameState, isNotNull);
expect(bloc.state.historyError, isNotEmpty);
repo.historyFail = null;
bloc.add(const PredictionHistoryPageRequested(1));
await bloc.stream.firstWhere(
(s) => s.historyError == null && !s.isLoadingHistory,
);
await bloc.close();
});
}