games_app/prediqo_mobile/test/prediction_interval_switch_test.dart
2026-10-25 20:49:23 +02:00

235 lines
6.3 KiB
Dart

import 'dart:async';
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 _interval1 = IntervalModel(
id: 1,
intervalMinutes: 1,
label: '1 min',
isActive: true,
isDefault: true,
payoutMultiplier: 1.8,
chartCandleLimit: 60,
);
const _interval3 = IntervalModel(
id: 2,
intervalMinutes: 3,
label: '3 min',
isActive: true,
isDefault: false,
payoutMultiplier: 1.8,
chartCandleLimit: 60,
);
GameStateModel _gs(int interval) {
const config = PredictConfigModel(
stakeAmounts: [1, 5, 10],
minStakeAmount: 1,
maxStakeAmount: 100,
payoutMultiplier: 1.8,
predictionWindowSeconds: 55,
isActive: true,
);
final meta = interval == 1 ? _interval1 : _interval3;
return GameStateModel(
selectedIntervalMinutes: interval,
config: config,
intervals: const [_interval1, _interval3],
interval: meta,
multiLevels: const [],
round: RoundModel(
id: 'r-$interval',
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 _IntervalRepo implements PredictionRepository {
Completer<CandlesPageModel>? hold3;
var candlesCalls = 0;
@override
Future<GameStateModel> getState({
int? intervalMinutes,
bool force = false,
}) async =>
_gs(intervalMinutes ?? 1);
@override
Future<CandlesPageModel> getCandlesPage({
int? intervalMinutes,
int limit = 60,
int? beforeOpenMs,
}) async {
candlesCalls++;
final m = intervalMinutes ?? 1;
if (m == 3 && hold3 != null) await hold3!.future;
return CandlesPageModel(
items: [_candle(m * 1_000_000)],
hasMore: false,
);
}
@override
Future<PredictionsPageModel> getMyBets({
int page = 1,
int pageSize = 5,
int? intervalMinutes,
String? stakeAccountKind,
String? status,
}) async =>
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(_IntervalRepo 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('revisiting an interval paints cached candles before network returns', () async {
final repo = _IntervalRepo();
final bloc = _bloc(repo);
bloc.add(const PredictionLoadRequested());
await bloc.stream.firstWhere((s) => s.candles.isNotEmpty);
expect(bloc.state.candles.single.openMs, 1_000_000);
repo.hold3 = Completer<CandlesPageModel>();
bloc.add(const PredictionIntervalChanged(3));
await bloc.stream.firstWhere((s) => s.selectedIntervalMinutes == 3);
bloc.add(const PredictionIntervalChanged(1));
final revisit = await bloc.stream.firstWhere(
(s) => s.selectedIntervalMinutes == 1 && s.candles.isNotEmpty,
);
expect(revisit.candles.single.openMs, 1_000_000);
expect(revisit.isLoadingOlderCandles, isFalse);
repo.hold3!.complete(
CandlesPageModel(items: [_candle(3_000_000)], hasMore: false),
);
await bloc.close();
});
test('history filter chip updates before slow fetch completes', () async {
final repo = _RecordingSlowHistory();
final bloc = _bloc(repo);
bloc.add(const PredictionLoadRequested());
await bloc.stream.firstWhere((s) => s.status == PredictionStatus.ready);
repo.hold = Completer<PredictionsPageModel>();
bloc.add(const PredictionHistoryFilterChanged(walletFilter: 'main'));
final chip = await bloc.stream.firstWhere(
(s) => s.historyWalletFilter == 'main',
);
expect(chip.isLoadingHistory, isTrue);
expect(chip.history, isEmpty);
repo.hold!.complete(
PredictionsPageModel(
items: [
PredictionModel(
id: 'm1',
direction: 'long',
stakeDisplay: 1,
status: 'won',
payoutDisplay: 1.9,
createdAt: '2026-09-24T18:00:00.000Z',
stakeAccountKind: 'main',
),
],
total: 1,
page: 1,
totalPages: 1,
),
);
await bloc.stream.firstWhere((s) => s.history.any((p) => p.id == 'm1'));
await bloc.close();
});
}
class _RecordingSlowHistory extends _IntervalRepo {
Completer<PredictionsPageModel>? hold;
@override
Future<PredictionsPageModel> getMyBets({
int page = 1,
int pageSize = 5,
int? intervalMinutes,
String? stakeAccountKind,
String? status,
}) async {
if (hold != null) return hold!.future;
return super.getMyBets(
page: page,
pageSize: pageSize,
intervalMinutes: intervalMinutes,
stakeAccountKind: stakeAccountKind,
status: status,
);
}
}