import 'package:comfyui_api_sdk/comfyui_api_sdk.dart';
import 'package:test/test.dart';

void main() {
  group('QueueInfo', () {
    test('fromJson creates instance with correct values', () {
      final json = {
        'queue_running': 1,
        'queue': [
          {'prompt_id': '123', 'number': 1}
        ],
        'queue_pending': {
          '456': {'prompt_id': '456', 'number': 2}
        }
      };

      final queueInfo = QueueInfo.fromJson(json);

      expect(queueInfo.queueRunning, equals(1));
      expect(queueInfo.queue.length, equals(1));
      expect(queueInfo.queue[0]['prompt_id'], equals('123'));
      expect(queueInfo.queuePending['456']['prompt_id'], equals('456'));
    });

    test('fromJson handles missing or empty values', () {
      final json = {'queue_running': 0};

      final queueInfo = QueueInfo.fromJson(json);

      expect(queueInfo.queueRunning, equals(0));
      expect(queueInfo.queue, isEmpty);
      expect(queueInfo.queuePending, isEmpty);
    });
  });

  group('PromptExecutionStatus', () {
    test('fromJson creates instance with correct values', () {
      final json = {
        'prompt_id': 'abc123',
        'number': 5,
        'status': 'processing',
        'error': null
      };

      final status = PromptExecutionStatus.fromJson(json);

      expect(status.promptId, equals('abc123'));
      expect(status.number, equals(5));
      expect(status.status, equals('processing'));
      expect(status.error, isNull);
    });

    test('fromJson handles error information', () {
      final json = {
        'prompt_id': 'abc123',
        'number': 5,
        'status': 'error',
        'error': 'Something went wrong'
      };

      final status = PromptExecutionStatus.fromJson(json);

      expect(status.status, equals('error'));
      expect(status.error, equals('Something went wrong'));
    });
  });

  group('HistoryItem', () {
    test('fromJson creates instance with correct values', () {
      final json = {
        'prompt_id': 'abc123',
        'prompt': {
          '1': {'class_type': 'TestNode'}
        },
        'outputs': {
          '2': {
            'images': {'filename': 'test.png'}
          }
        }
      };

      final item = HistoryItem.fromJson(json);

      expect(item.promptId, equals('abc123'));
      expect(item.prompt['1']['class_type'], equals('TestNode'));
      expect(item.outputs?['2']['images']['filename'], equals('test.png'));
    });

    test('fromJson handles missing outputs', () {
      final json = {
        'prompt_id': 'abc123',
        'prompt': {
          '1': {'class_type': 'TestNode'}
        }
      };

      final item = HistoryItem.fromJson(json);

      expect(item.promptId, equals('abc123'));
      expect(item.outputs, isNull);
    });
  });

  group('ProgressUpdate', () {
    test('fromJson creates instance with correct values', () {
      final json = {
        'type': 'execution_start',
        'data': {'prompt_id': 'abc123', 'node': 5}
      };

      final update = ProgressUpdate.fromJson(json);

      expect(update.type, equals('execution_start'));
      expect(update.data['prompt_id'], equals('abc123'));
      expect(update.data['node'], equals(5));
    });

    test('fromJson handles empty data', () {
      final json = {'type': 'status', 'data': {}};

      final update = ProgressUpdate.fromJson(json);

      expect(update.type, equals('status'));
      expect(update.data, isEmpty);
    });
  });
}
