games_app/prediqo_mobile/test/server_adjusted_countdown_test.dart

227 lines
6.4 KiB
Dart

import 'package:flutter_test/flutter_test.dart';
import 'package:prediqo_mobile/data/models/prediction_models.dart';
import 'package:prediqo_mobile/features/prediction/utils/server_adjusted_countdown.dart';
RoundModel _round({
required String id,
required String status,
int secondsToBettingEnd = 0,
int secondsToBettingStart = 0,
int secondsToClose = 0,
int? serverNowMs,
int? candleOpenMs,
}) {
return RoundModel(
id: id,
status: status,
secondsToBettingEnd: secondsToBettingEnd,
secondsToBettingStart: secondsToBettingStart,
secondsToClose: secondsToClose,
serverNowMs: serverNowMs,
candleOpenMs: candleOpenMs,
bettingEndMs: candleOpenMs,
);
}
void main() {
group('isIncomingRoundStale', () {
test('keeps the live betting window over a late settled HTTP snapshot', () {
final live = _round(
id: 'next',
status: 'betting',
secondsToBettingEnd: 55,
serverNowMs: 1_000_000,
candleOpenMs: 1_055_000,
);
final lateHttp = _round(
id: 'prev',
status: 'settled',
serverNowMs: 990_000,
candleOpenMs: 995_000,
);
expect(isIncomingRoundStale(live, lateHttp), isTrue);
});
test('accepts the next cycle when candleOpenMs is newer', () {
final current = _round(
id: 'prev',
status: 'settled',
candleOpenMs: 995_000,
);
final next = _round(
id: 'next',
status: 'betting',
secondsToBettingEnd: 55,
candleOpenMs: 1_055_000,
);
expect(isIncomingRoundStale(current, next), isFalse);
});
test('same round id is never stale', () {
final current = _round(
id: 'same',
status: 'betting',
secondsToBettingEnd: 40,
candleOpenMs: 1_055_000,
);
final tick = _round(
id: 'same',
status: 'betting',
secondsToBettingEnd: 39,
candleOpenMs: 1_055_000,
);
expect(isIncomingRoundStale(current, tick), isFalse);
});
test('rejects a non-betting snapshot when live betting has no candle time', () {
final live = _round(id: 'next', status: 'betting', secondsToBettingEnd: 52);
final late = _round(id: 'prev', status: 'settled');
expect(isIncomingRoundStale(live, late), isTrue);
});
});
group('mergeRound', () {
test('keeps previous timing when WS omits serverNowMs on the same round', () {
final prev = _round(
id: 'r1',
status: 'betting',
secondsToBettingEnd: 55,
serverNowMs: 1_000_000,
candleOpenMs: 1_055_000,
);
final incoming = _round(
id: 'r1',
status: 'betting',
secondsToBettingEnd: 40,
candleOpenMs: 1_055_000,
);
final merged = mergeRound(prev, incoming);
expect(merged.secondsToBettingEnd, 55);
expect(merged.serverNowMs, 1_000_000);
});
});
group('serverAdjustedCountdown', () {
test('counts down locally from one frozen snapshot', () {
final snap = _round(
id: 'r1',
status: 'betting',
secondsToBettingEnd: 55,
serverNowMs: 1_000_000,
candleOpenMs: 1_055_000,
);
expect(
serverAdjustedCountdown(snap, clientNowMs: 1_000_000).secondsToBettingEnd,
55,
);
expect(
serverAdjustedCountdown(snap, clientNowMs: 1_003_000).secondsToBettingEnd,
52,
);
expect(
serverAdjustedCountdown(snap, clientNowMs: 1_055_000).secondsToBettingEnd,
0,
);
});
});
group('countdownNeedsStateResync / countdownStuckAtZero', () {
test('does not resync mid open window', () {
final snap = _round(
id: 'r1',
status: 'betting',
secondsToBettingEnd: 40,
serverNowMs: 1_000_000,
);
expect(
countdownNeedsStateResync(snap, clientNowMs: 1_000_000),
isFalse,
);
expect(countdownStuckAtZero(snap, clientNowMs: 1_000_000), isFalse);
});
test('resyncs when open window is about to expire', () {
final snap = _round(
id: 'r1',
status: 'betting',
secondsToBettingEnd: 55,
serverNowMs: 1_000_000,
);
expect(
countdownNeedsStateResync(snap, clientNowMs: 1_054_000),
isTrue,
);
expect(countdownStuckAtZero(snap, clientNowMs: 1_054_000), isFalse);
expect(countdownStuckAtZero(snap, clientNowMs: 1_055_000), isTrue);
});
test('resyncs when wait window is exhausted', () {
final snap = _round(
id: 'r1',
status: 'locked',
secondsToBettingStart: 10,
serverNowMs: 1_000_000,
);
expect(
countdownNeedsStateResync(snap, clientNowMs: 1_009_000),
isTrue,
);
expect(countdownStuckAtZero(snap, clientNowMs: 1_010_000), isTrue);
});
});
group('multi-cycle countdown 55→0→5→0→55', () {
test('open expires then wait prep then next open', () {
final open = _round(
id: 'r1',
status: 'betting',
secondsToBettingEnd: 55,
serverNowMs: 1_000_000,
candleOpenMs: 1_055_000,
);
expect(
serverAdjustedCountdown(open, clientNowMs: 1_000_000).secondsToBettingEnd,
55,
);
expect(
serverAdjustedCountdown(open, clientNowMs: 1_055_000).secondsToBettingEnd,
0,
);
expect(countdownStuckAtZero(open, clientNowMs: 1_055_000), isTrue);
// Fresh locked snapshot for the 5s prep window (serverNextBettingStart).
final wait = _round(
id: 'r1',
status: 'locked',
secondsToBettingStart: 5,
serverNowMs: 1_055_000,
candleOpenMs: 1_055_000,
);
expect(
serverAdjustedCountdown(wait, clientNowMs: 1_055_000).secondsToBettingStart,
5,
);
expect(countdownStuckAtZero(wait, clientNowMs: 1_055_000), isFalse);
expect(
serverAdjustedCountdown(wait, clientNowMs: 1_060_000).secondsToBettingStart,
0,
);
expect(countdownStuckAtZero(wait, clientNowMs: 1_060_000), isTrue);
final next = _round(
id: 'r2',
status: 'betting',
secondsToBettingEnd: 55,
serverNowMs: 1_060_000,
candleOpenMs: 1_115_000,
);
expect(isIncomingRoundStale(wait, next), isFalse);
expect(
serverAdjustedCountdown(next, clientNowMs: 1_060_000).secondsToBettingEnd,
55,
);
expect(countdownStuckAtZero(next, clientNowMs: 1_060_000), isFalse);
});
});
}