import 'dart:async'; import 'dart:io'; import 'package:flutter_test/flutter_test.dart'; import 'package:ikar/core/share/shared_file_uploads.dart'; void main() { test('closing the share route keeps the file for every recipient', () async { final directory = Directory.systemTemp.createTempSync('share_uploads_'); addTearDown(() => directory.deleteSync(recursive: true)); final photo = File('${directory.path}/photo.jpg') ..writeAsStringSync('photo'); final uploads = SharedFileUploads(); final releaseBatch = uploads.retain([photo.path]); final first = Completer(); final second = Completer(); final firstUpload = uploads.use([photo.path], () => first.future); var cleaned = false; final cleanup = uploads.waitUntilIdle([photo.path]).then((_) { photo.deleteSync(); cleaned = true; }); // Another recipient is queued after the route has already closed. final secondUpload = uploads.use([photo.path], () => second.future); releaseBatch(); first.complete(); await firstUpload; expect(cleaned, isFalse); expect(photo.readAsStringSync(), 'photo'); second.complete(); await secondUpload; await cleanup; expect(cleaned, isTrue); expect(photo.existsSync(), isFalse); }); test( 'cleanup of one share does not wait for or remove another share', () async { final uploads = SharedFileUploads(); final releaseFirst = uploads.retain(['first']); final releaseSecond = uploads.retain(['second']); var firstCleaned = false; var secondCleaned = false; final first = uploads .waitUntilIdle(['first']) .then((_) => firstCleaned = true); final second = uploads .waitUntilIdle(['second']) .then((_) => secondCleaned = true); releaseFirst(); await first; expect(firstCleaned, isTrue); expect(secondCleaned, isFalse); releaseSecond(); await second; }, ); test('failed upload releases its file and preserves the error', () async { final uploads = SharedFileUploads(); final failed = uploads.use([ 'video', ], () => throw StateError('offline')); await expectLater(failed, throwsStateError); await uploads.waitUntilIdle(['video']).timeout(const Duration(seconds: 1)); }); test( 'cancelled share and repeated release finish without an upload', () async { final uploads = SharedFileUploads(); final release = uploads.retain(['document', 'document']); release(); release(); await uploads.waitUntilIdle(['document', 'missing']); }, ); }