import 'package:flutter_test/flutter_test.dart';
import 'package:openim/company/rtc/rtc_token_mapper.dart';

void main() {
  const fallback = 'ws://192.168.200.11:17880';

  test('解析账号服务 token，缺 liveURL 时回落到配置地址', () {
    final cred = RtcTokenMapper.parse(
      {
        'code': 0,
        'msg': '',
        'data': {'token': 'lk-token'},
      },
      fallbackLiveUrl: fallback,
    );
    expect(cred.token, 'lk-token');
    expect(cred.liveURL, fallback);
  });

  test('兼容官方 serverUrl 字段', () {
    final cred = RtcTokenMapper.parse(
      {
        'code': 0,
        'data': {'token': 't', 'serverUrl': 'ws://live.example:7880'},
      },
      fallbackLiveUrl: fallback,
    );
    expect(cred.liveURL, 'ws://live.example:7880');
  });

  test('优先使用 liveURL', () {
    final cred = RtcTokenMapper.parse(
      {
        'data': {
          'token': 't',
          'liveURL': 'ws://a:1',
          'serverUrl': 'ws://b:2',
        },
      },
      fallbackLiveUrl: fallback,
    );
    expect(cred.liveURL, 'ws://a:1');
  });

  test('业务失败码抛出服务端文案', () {
    expect(
      () => RtcTokenMapper.parse(
        {'code': 1, 'msg': '语音通话服务未配置，请联系管理员'},
        fallbackLiveUrl: fallback,
      ),
      throwsA(isA<FormatException>().having(
        (e) => e.message,
        'message',
        '语音通话服务未配置，请联系管理员',
      )),
    );
  });

  test('缺 token 失败', () {
    expect(
      () => RtcTokenMapper.parse(
        {
          'code': 0,
          'data': {'token': ''},
        },
        fallbackLiveUrl: fallback,
      ),
      throwsA(isA<FormatException>()),
    );
  });
}
