import 'package:flutter_test/flutter_test.dart';
import 'package:smplayer/features/player/session/stall_detector.dart';

void main() {
  group('StallDetector', () {
    test('有字节统计时下载继续便不判定停滞', () {
      final detector = StallDetector(threshold: const Duration(seconds: 10));

      expect(
        detector.update(
          position: const Duration(seconds: 5),
          bufferedPosition: const Duration(seconds: 5),
          bytesDownloaded: 100,
          isActivelyPlaying: true,
          now: Duration.zero,
        ),
        isFalse,
      );
      expect(
        detector.update(
          position: const Duration(seconds: 5),
          bufferedPosition: const Duration(seconds: 5),
          bytesDownloaded: 200,
          isActivelyPlaying: true,
          now: const Duration(seconds: 12),
        ),
        isFalse,
      );
    });

    test('无字节统计时缓冲终点继续前进便不判定停滞', () {
      final detector = StallDetector(threshold: const Duration(seconds: 10));

      detector.update(
        position: const Duration(seconds: 5),
        bufferedPosition: const Duration(seconds: 5),
        bytesDownloaded: null,
        isActivelyPlaying: true,
        now: Duration.zero,
      );
      expect(
        detector.update(
          position: const Duration(seconds: 5),
          bufferedPosition: const Duration(seconds: 8),
          bytesDownloaded: null,
          isActivelyPlaying: true,
          now: const Duration(seconds: 12),
        ),
        isFalse,
      );
    });

    test('累计字节首次变为可用时视为传输进展', () {
      final detector = StallDetector(threshold: const Duration(seconds: 10));

      detector.update(
        position: const Duration(seconds: 5),
        bufferedPosition: const Duration(seconds: 5),
        bytesDownloaded: null,
        isActivelyPlaying: true,
        now: Duration.zero,
      );
      expect(
        detector.update(
          position: const Duration(seconds: 5),
          bufferedPosition: const Duration(seconds: 5),
          bytesDownloaded: 100,
          isActivelyPlaying: true,
          now: const Duration(seconds: 12),
        ),
        isFalse,
      );
    });

    test('无字节统计且仍有明显缓冲余量时不重连', () {
      final detector = StallDetector(threshold: const Duration(seconds: 10));

      detector.update(
        position: const Duration(seconds: 5),
        bufferedPosition: const Duration(seconds: 20),
        bytesDownloaded: null,
        isActivelyPlaying: true,
        now: Duration.zero,
      );
      expect(
        detector.update(
          position: const Duration(seconds: 5),
          bufferedPosition: const Duration(seconds: 20),
          bytesDownloaded: null,
          isActivelyPlaying: true,
          now: const Duration(seconds: 12),
        ),
        isFalse,
      );
    });

    test('所有可用进展信号停止到阈值后只触发一次', () {
      final detector = StallDetector(threshold: const Duration(seconds: 10));

      detector.update(
        position: const Duration(seconds: 5),
        bufferedPosition: const Duration(seconds: 5),
        bytesDownloaded: null,
        isActivelyPlaying: true,
        now: Duration.zero,
      );
      expect(
        detector.update(
          position: const Duration(seconds: 5),
          bufferedPosition: const Duration(seconds: 5),
          bytesDownloaded: null,
          isActivelyPlaying: true,
          now: const Duration(seconds: 10),
        ),
        isTrue,
      );
      expect(
        detector.update(
          position: const Duration(seconds: 5),
          bufferedPosition: const Duration(seconds: 5),
          bytesDownloaded: null,
          isActivelyPlaying: true,
          now: const Duration(seconds: 20),
        ),
        isFalse,
      );
    });
  });
}
