import 'package:flutter_test/flutter_test.dart';
import 'package:recording_tool/features/recording/model/model_clipboard.dart';

void main() {
  group('ClipboardRecordingModel', () {
    const clipboardJson = {
      'title': '王东方 丨李想 空中格斗赛',
      'startTimestamp': 1717334400,
      'endTimestamp': 1717334400,
      'address': '广州市番禺区·粤港澳大湾区青年人才双创小镇',
    };

    test('parses mini program clipboard JSON', () {
      final model = ClipboardRecordingModel.fromJson(clipboardJson);

      expect(model.title, '王东方 丨李想 空中格斗赛');
      expect(model.startTimestamp, 1717334400);
      expect(model.endTimestamp, 1717334400);
      expect(model.address, '广州市番禺区·粤港澳大湾区青年人才双创小镇');
      expect(model.filename, isNull);
      expect(model.toJson(), clipboardJson);
    });

    test('parses JSON without optional timestamps', () {
      final json = {
        'title': '郑昌梦 丨黄伟依 空中格斗赛 小学组',
        'address': '广东省汕头市番禺区青蓝街 111 号',
        'filename': '郑昌梦_黄伟依_6月3日测试-1_空中格斗赛',
      };
      final model = ClipboardRecordingModel.fromJson(json);

      expect(model.title, json['title']);
      expect(model.address, json['address']);
      expect(model.filename, json['filename']);
      expect(model.startTimestamp, isNull);
      expect(model.endTimestamp, isNull);
    });

    test('parses optional filename from mini program JSON', () {
      final json = {
        ...clipboardJson,
        'filename': '选手名称_选手ID_赛事名称_赛项',
      };
      final model = ClipboardRecordingModel.fromJson(json);

      expect(model.filename, '选手名称_选手ID_赛事名称_赛项');
      expect(model.toJson(), json);
    });

    test('throws FormatException when required field is missing', () {
      final json = Map<String, dynamic>.from(clipboardJson)..remove('title');

      expect(
        () => ClipboardRecordingModel.fromJson(json),
        throwsA(isA<FormatException>()),
      );
    });

    test('throws FormatException when optional int field has wrong type', () {
      final json = {...clipboardJson, 'startTimestamp': '1717334400'};

      expect(
        () => ClipboardRecordingModel.fromJson(json),
        throwsA(isA<FormatException>()),
      );
    });

    test('throws FormatException when required address is missing', () {
      final json = Map<String, dynamic>.from(clipboardJson)..remove('address');

      expect(
        () => ClipboardRecordingModel.fromJson(json),
        throwsA(isA<FormatException>()),
      );
    });
  });
}
