import 'package:flutter/foundation.dart';
import 'package:flutter_test/flutter_test.dart';
import 'package:rasadyar_core/core.dart';

void main() {
  group('DioFormData', () {
    late DioFormData formData;

    setUp(() {
      formData = DioFormData();
    });

    test('addField should add a field to FormData', () {
      formData.addField('userName', 'mojtaba');
      expect(formData.raw.fields.length, 1);
      expect(formData.raw.fields.first.key, 'userName');
      expect(formData.raw.fields.first.value, 'mojtaba');
    });

    test('addFile should add a file to FormData', () async {
      final bytes = Uint8List.fromList([1, 2, 3, 4]);
      formData.addFile('fileField', bytes, 'test.txt');

      expect(formData.raw.files.length, 1);

      final fileEntry = formData.raw.files.first;
      expect(fileEntry.key, 'fileField');

      final multipart = fileEntry.value;
      expect(multipart.filename, 'test.txt');

      final uploadedBytes = await multipart.finalize().toBytes();
      expect(uploadedBytes, bytes);

    });

    test('raw getter should return the internal FormData instance', () {
      final tmp = formData.raw;
      expect(tmp, isA<FormData>());
    });


  });
}

extension on Stream<List<int>> {
  /// Helper to collect stream into a single Uint8List for comparison
  Future<Uint8List> toBytes() async {
    final chunks = <int>[];
    await for (final chunk in this) {
      chunks.addAll(chunk);
    }
    return Uint8List.fromList(chunks);
  }
}
