import 'dart:typed_data'; import 'package:flutter_test/flutter_test.dart'; import 'package:pipelines_app/features/chat/domain/chat_attachment_picker.dart'; import 'package:pipelines_app/features/chat/files/files_upload.dart'; void main() { late FilesUploadPost original; setUp(() => original = postFileUpload); tearDown(() => postFileUpload = original); final file = ChatPickedFile( name: 'notes.txt', mime: 'text/plain', bytes: Uint8List.fromList([1, 2, 3]), ); test( 'posts to /files with the shown folder and returns the stored path', () async { Uri? posted; String? postedName; String? postedMime; postFileUpload = ( uri, { required name, required mime, required bytes, headers = const {}, }) async { posted = uri; postedName = name; postedMime = mime; return ( status: 200, body: '{"name":"notes.txt","path":"/home/demo/notes.txt","size":3}', ); }; final path = await uploadFileInto( 'ws://example.test/connect', '/home/demo', file, ); expect(path, '/home/demo/notes.txt'); expect(posted?.path, '/files'); expect(posted?.queryParameters, {'path': '/home/demo'}); expect(posted?.scheme, 'http'); expect(postedName, 'notes.txt'); expect(postedMime, 'text/plain'); }, ); test('a relay endpoint posts through its peer prefix', () async { Uri? posted; postFileUpload = ( uri, { required name, required mime, required bytes, headers = const {}, }) async { posted = uri; return (status: 200, body: '{"path":"/home/x/notes.txt"}'); }; await uploadFileInto( 'wss://web.test/peers/laptop/connect', '/home/x', file, ); expect(posted?.path, '/peers/laptop/files'); }); test('server refusals become plain messages', () async { Future message(int status, String body) async { postFileUpload = ( uri, { required name, required mime, required bytes, headers = const {}, }) async => (status: status, body: body); try { await uploadFileInto('ws://example.test/connect', '/home/demo', file); } on FilesUploadException catch (error) { return error.message; } return 'no error'; } expect( await message(409, 'a file named notes.txt already exists'), '"notes.txt" already exists in this folder.', ); expect( await message(401, 'Sign in required'), 'Sign in to upload files (Settings → Sign in).', ); expect( await message(413, 'file exceeds the 100 MiB limit'), '"notes.txt" is too large to upload (100 MiB limit).', ); expect( await message(403, 'folder access denied'), '"notes.txt" could not be uploaded: folder access denied', ); expect( await message(500, ''), '"notes.txt" could not be uploaded (HTTP 500).', ); expect( await message(200, 'not json'), '"notes.txt" upload returned no stored path.', ); }); test( 'transport failures and blank names are reported, not swallowed', () async { postFileUpload = ( uri, { required name, required mime, required bytes, headers = const {}, }) => throw StateError('socket closed'); await expectLater( uploadFileInto('ws://example.test/connect', '/home/demo', file), throwsA( isA().having( (e) => e.message, 'message', '"notes.txt" could not be uploaded: Bad state: socket closed', ), ), ); await expectLater( uploadFileInto( 'ws://example.test/connect', '/home/demo', ChatPickedFile(name: ' ', mime: '', bytes: Uint8List(0)), ), throwsA(isA()), ); await expectLater( uploadFileInto('', '/home/demo', file), throwsA( isA().having( (e) => e.message, 'message', 'No server is configured to upload files to.', ), ), ); }, ); }