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

void main() {
  group('isFvpNetworkError', () {
    test('命中 tcp:/tls: 前缀', () {
      expect(isFvpNetworkError('tcp: connection lost'), isTrue);
      expect(isFvpNetworkError('tls: certificate verify failed'), isTrue);
    });

    test('命中 TLS 层关键字（Windows 直连的主要场景）', () {
      expect(isFvpNetworkError('TLS handshake failed'), isTrue);
      expect(isFvpNetworkError('SSL_read: SSL_ERROR_SSL, WANT_WRITE'), isTrue);
      expect(
        isFvpNetworkError('reader: tls: handshake error (err=-1)'),
        isTrue,
      );
    });

    test('命中连接层关键字', () {
      expect(isFvpNetworkError('Connection refused'), isTrue);
      expect(isFvpNetworkError('connection reset by peer'), isTrue);
      expect(isFvpNetworkError('connection timed out'), isTrue);
      expect(isFvpNetworkError('Network is unreachable'), isTrue);
      expect(isFvpNetworkError('Host not found'), isTrue);
      expect(isFvpNetworkError('name resolution failed'), isTrue);
      expect(isFvpNetworkError('No route to host'), isTrue);
    });

    test('命中 HTTP 状态码文本', () {
      expect(isFvpNetworkError('HTTP error 403 Forbidden'), isTrue);
      expect(isFvpNetworkError('HTTP error 502 Bad Gateway'), isTrue);
      expect(isFvpNetworkError('Server returned 401 Unauthorized'), isTrue);
      expect(isFvpNetworkError('Server returned 503'), isTrue);
    });

    test('命中 ffmpeg 协议缺失（TLS 后端被剥离时的典型 detail）', () {
      expect(isFvpNetworkError('Protocol not found'), isTrue);
    });

    test('不命中解码器与非网络错误', () {
      expect(isFvpNetworkError('Failed to allocate decoder'), isFalse);
      expect(isFvpNetworkError('Error while decoding frame'), isFalse);
      expect(isFvpNetworkError('unsupported pixel format'), isFalse);
      expect(isFvpNetworkError('decoder timeout waiting for frame'), isFalse);
    });

    test('空串与无关文本不命中', () {
      expect(isFvpNetworkError(''), isFalse);
      expect(isFvpNetworkError('reader.buffering'), isFalse);
      expect(isFvpNetworkError('render.video 1st_frame'), isFalse);
    });
  });

  group('fvpNetworkErrorPayload', () {
    test('只转换 IO 类别中的已识别网络错误', () {
      expect(
        fvpNetworkErrorPayload(
          category: 'reader.error',
          errorCode: -1,
          detail: 'Connection reset by peer',
        ),
        'reader: Connection reset by peer (err=-1)',
      );
    });

    test('忽略 demuxer 解码错误和非负事件', () {
      expect(
        fvpNetworkErrorPayload(
          category: 'demuxer.error',
          errorCode: -1,
          detail: 'Invalid data found when processing input',
        ),
        isNull,
      );
      expect(
        fvpNetworkErrorPayload(
          category: 'reader.error',
          errorCode: 0,
          detail: 'Connection reset by peer',
        ),
        isNull,
      );
    });

    test('忽略非 IO 类别，即使 detail 含网络词', () {
      expect(
        fvpNetworkErrorPayload(
          category: 'decoder.video',
          errorCode: -1,
          detail: 'TLS handshake failed',
        ),
        isNull,
      );
    });
  });
}
