import 'package:flutter_test/flutter_test.dart';
import 'package:smplayer/core/infra/player_engine/player_engine.dart';

void main() {
  group('streamOpenErrorHttpStatus', () {
    test('从回填的 error 串解析状态码', () {
      expect(
        streamOpenErrorHttpStatus('Failed to open http://h/v.mp4. (http 403)'),
        403,
      );
      expect(streamOpenErrorHttpStatus('boom (http 502)'), 502);
    });

    test('无状态码返回 null', () {
      expect(streamOpenErrorHttpStatus('Failed to open http://h/v.mp4.'), null);
      expect(streamOpenErrorHttpStatus('connection reset by peer'), null);
      expect(streamOpenErrorHttpStatus(''), null);
    });
  });

  group('isTerminalStreamOpenError', () {
    test('客户端 4xx 视为确定性失败（跳过重连）', () {
      expect(isTerminalStreamOpenError('open failed (http 403)'), isTrue);
      expect(isTerminalStreamOpenError('open failed (http 404)'), isTrue);
      expect(isTerminalStreamOpenError('open failed (http 410)'), isTrue);
      expect(isTerminalStreamOpenError('open failed (http 401)'), isTrue);
    });

    test('瞬时类 408/429 仍允许重连', () {
      expect(isTerminalStreamOpenError('open failed (http 408)'), isFalse);
      expect(isTerminalStreamOpenError('open failed (http 429)'), isFalse);
    });

    test('5xx 与无状态码不视为确定性失败', () {
      expect(isTerminalStreamOpenError('open failed (http 502)'), isFalse);
      expect(isTerminalStreamOpenError('open failed (http 503)'), isFalse);
      expect(isTerminalStreamOpenError('Failed to open http://h/v.mp4.'), isFalse);
      expect(isTerminalStreamOpenError('connection reset by peer'), isFalse);
    });
  });
}
