import 'dart:async'; import 'package:flutter_test/flutter_test.dart'; import 'package:ikar/core/media/send_media_separately.dart'; void main() { test( 'waits for each send, preserves order and uses caption only once', () async { final first = Completer(); final calls = <(int, String)>[]; final items = [1, 2, 3]; final sending = sendMediaSeparately( items, 'Synthetic caption', canSend: () => true, send: (item, caption) async { calls.add((item, caption)); if (item == 1) await first.future; }, ); expect(calls, [(1, 'Synthetic caption')]); items.clear(); first.complete(); await sending; expect(calls, [(1, 'Synthetic caption'), (2, ''), (3, '')]); }, ); test('stops after chat is closed', () async { var mounted = true; final calls = []; await sendMediaSeparately( [1, 2], '', canSend: () => mounted, send: (item, _) async { calls.add(item); mounted = false; }, ); expect(calls, [1]); }); test('propagates failure without sending remaining media', () async { final calls = []; await expectLater( sendMediaSeparately( [1, 2], '', canSend: () => true, send: (item, _) async { calls.add(item); throw StateError('Synthetic upload failure'); }, ), throwsStateError, ); expect(calls, [1]); }); }