Files
pipelines/app/test/features/chat/files/files_upload_test.dart
T
alan b8fb4d350c Upload files into the shown folder from the chat Files panel
The header gains an Upload button that uses the host file picker (disabled
with its reason where no picker exists), and the panel body becomes a drop
target: on web, document drag events are observed through a dart:js_interop
seam and a file drag over the panel shows a highlight naming the folder.
Each file is posted to POST /files?path=<folder> through an injectable
transport; refusals (exists, sign in, too large) stay visible until dismissed
and the folder reloads afterwards. The list/grid builders move to
files_entries.dart to keep the panel small.
2026-11-09 00:09:01 +00:00

139 lines
4.1 KiB
Dart

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}) 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}) 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<String> message(int status, String body) async {
postFileUpload =
(uri, {required name, required mime, required bytes}) 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}) =>
throw StateError('socket closed');
await expectLater(
uploadFileInto('ws://example.test/connect', '/home/demo', file),
throwsA(
isA<FilesUploadException>().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<FilesUploadException>()),
);
await expectLater(
uploadFileInto('', '/home/demo', file),
throwsA(
isA<FilesUploadException>().having(
(e) => e.message,
'message',
'No server is configured to upload files to.',
),
),
);
},
);
}