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

void main() {
  group('isMpvNetworkError', () {
    test('命中 tcp:/tls: 前缀', () {
      expect(
        isMpvNetworkError('tcp: ffurl_read returned 0xffffff99'),
        isTrue,
      );
      expect(isMpvNetworkError('tls: error during handshake'), isTrue);
    });

    test('命中 HTTP open 失败', () {
      expect(
        isMpvNetworkError(
          'Failed to open https://cdn.example/v.mkv.',
        ),
        isTrue,
      );
    });

    test('命中连接类关键词', () {
      expect(isMpvNetworkError('connection reset by peer'), isTrue);
      expect(isMpvNetworkError('Connection refused'), isTrue);
      expect(isMpvNetworkError('connection timed out'), isTrue);
      expect(isMpvNetworkError('Network is unreachable'), isTrue);
      expect(isMpvNetworkError('Host not found'), isTrue);
      expect(isMpvNetworkError('name resolution failed'), isTrue);
    });

    test('命中 TLS 与网络超时', () {
      expect(isMpvNetworkError('TLS handshake failed'), isTrue);
      expect(isMpvNetworkError('connection timed out'), isTrue);
    });

    test('不命中非网络 timeout 文本', () {
      expect(isMpvNetworkError('decoder timeout waiting for frame'), isFalse);
      expect(isMpvNetworkError('cplayer timeout while waiting'), isFalse);
    });

    test('命中 HTTP 错误码', () {
      expect(isMpvNetworkError('http error 403'), isTrue);
      expect(isMpvNetworkError('http error 502'), isTrue);
    });

    test('不命中解码器错误', () {
      expect(isMpvNetworkError('Error while decoding frame'), isFalse);
      expect(isMpvNetworkError('Failed to open decoder'), isFalse);
    });

    test('空串不命中', () {
      expect(isMpvNetworkError(''), isFalse);
    });
  });
}
