Merge branch 'worktree-agent-ab6edc0d99947642f'
This commit is contained in:
@@ -0,0 +1,261 @@
|
||||
import 'dart:async';
|
||||
import 'dart:typed_data';
|
||||
|
||||
import 'package:flutter/material.dart';
|
||||
import 'package:flutter_test/flutter_test.dart';
|
||||
import 'package:pipelines_app/core/api_gateway.dart';
|
||||
import 'package:pipelines_app/core/transport/pipelines_client.dart';
|
||||
import 'package:pipelines_app/features/chat/domain/chat_attachment_picker.dart';
|
||||
import 'package:pipelines_app/features/chat/files/files_controller.dart';
|
||||
import 'package:pipelines_app/features/chat/files/files_drop.dart';
|
||||
import 'package:pipelines_app/features/chat/files/files_panel.dart';
|
||||
import 'package:pipelines_app/features/chat/files/files_upload.dart';
|
||||
import 'package:pipelines_app/features/chat/files/files_urls.dart';
|
||||
import 'package:pipelines_app/features/chat/files/files_viewer.dart';
|
||||
|
||||
/// Lists `/home/demo` and counts the listings so a refresh after an upload is
|
||||
/// observable.
|
||||
class Gateway extends PipelinesClient {
|
||||
@override
|
||||
Uri get endpoint => Uri.parse('ws://example.test/connect');
|
||||
int listings = 0;
|
||||
@override
|
||||
Stream<JsonMap> get frames => const Stream.empty();
|
||||
@override
|
||||
Future<JsonMap> call(String type, [JsonMap fields = const {}]) async {
|
||||
listings += 1;
|
||||
return {
|
||||
'directory_listing': {
|
||||
'path': '/home/demo',
|
||||
'home': '/home',
|
||||
'entries': [
|
||||
{'name': 'notes.txt', 'path': '/home/demo/notes.txt', 'size': 12},
|
||||
],
|
||||
},
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
/// One recorded `POST /files`.
|
||||
typedef Posted = ({Uri uri, String name, String mime, int size});
|
||||
|
||||
ChatPickedFile picked(String name, [String mime = 'text/plain']) =>
|
||||
ChatPickedFile(name: name, mime: mime, bytes: Uint8List.fromList([1, 2]));
|
||||
|
||||
void main() {
|
||||
late FileUrlResolver originalUrls;
|
||||
late FilesUploadPost originalPost;
|
||||
final posted = <Posted>[];
|
||||
var reply = (status: 200, body: '{"path":"/home/demo/new.txt"}');
|
||||
Completer<void>? gate;
|
||||
setUp(() {
|
||||
originalUrls = resolveFileUrls;
|
||||
originalPost = postFileUpload;
|
||||
resolveFileUrls = (_, path) => fileUrls('ws://example.test/connect', path);
|
||||
posted.clear();
|
||||
reply = (status: 200, body: '{"path":"/home/demo/new.txt"}');
|
||||
gate = null;
|
||||
postFileUpload =
|
||||
(uri, {required name, required mime, required bytes}) async {
|
||||
posted.add((uri: uri, name: name, mime: mime, size: bytes.length));
|
||||
if (gate != null) await gate!.future;
|
||||
return reply;
|
||||
};
|
||||
});
|
||||
tearDown(() {
|
||||
resolveFileUrls = originalUrls;
|
||||
postFileUpload = originalPost;
|
||||
});
|
||||
|
||||
Future<({FilesController controller, Gateway gateway})> pump(
|
||||
WidgetTester tester, {
|
||||
ChatAttachmentPicker? picker,
|
||||
FilesDrop? drop,
|
||||
}) async {
|
||||
final gateway = Gateway();
|
||||
final controller = FilesController(
|
||||
gateway: gateway,
|
||||
initialPath: '/home/demo',
|
||||
);
|
||||
addTearDown(controller.dispose);
|
||||
await tester.pumpWidget(
|
||||
MaterialApp(
|
||||
home: Scaffold(
|
||||
body: FilesPanel(
|
||||
controller: controller,
|
||||
viewer: const FilesViewer(),
|
||||
picker:
|
||||
picker ??
|
||||
ChatAttachmentPicker(select: () async => [picked('new.txt')]),
|
||||
drop: drop ?? const FilesDrop.unsupported('no drops in tests'),
|
||||
),
|
||||
),
|
||||
),
|
||||
);
|
||||
await tester.pumpAndSettle();
|
||||
return (controller: controller, gateway: gateway);
|
||||
}
|
||||
|
||||
testWidgets('Upload button picks files, posts them to the shown folder and '
|
||||
'refreshes', (tester) async {
|
||||
gate = Completer<void>();
|
||||
final mounted = await pump(tester);
|
||||
expect(mounted.gateway.listings, 1);
|
||||
await tester.tap(find.byTooltip('Upload files'));
|
||||
await tester.pump();
|
||||
expect(find.text('Uploading…'), findsOneWidget);
|
||||
expect(find.byType(LinearProgressIndicator), findsOneWidget);
|
||||
expect(
|
||||
tester
|
||||
.widget<IconButton>(
|
||||
find.ancestor(
|
||||
of: find.byIcon(Icons.upload_file),
|
||||
matching: find.byType(IconButton),
|
||||
),
|
||||
)
|
||||
.onPressed,
|
||||
isNull,
|
||||
reason: 'a second batch waits for the first',
|
||||
);
|
||||
gate!.complete();
|
||||
await tester.pumpAndSettle();
|
||||
expect(posted, hasLength(1));
|
||||
expect(posted.single.uri.path, '/files');
|
||||
expect(posted.single.uri.queryParameters['path'], '/home/demo');
|
||||
expect(posted.single.name, 'new.txt');
|
||||
expect(posted.single.mime, 'text/plain');
|
||||
expect(posted.single.size, 2);
|
||||
expect(mounted.gateway.listings, 2, reason: 'the folder is reloaded');
|
||||
expect(find.text('Uploading…'), findsNothing);
|
||||
expect(mounted.controller.uploadError, isNull);
|
||||
});
|
||||
|
||||
testWidgets('a cancelled picker uploads nothing', (tester) async {
|
||||
final mounted = await pump(
|
||||
tester,
|
||||
picker: ChatAttachmentPicker(select: () async => null),
|
||||
);
|
||||
await tester.tap(find.byTooltip('Upload files'));
|
||||
await tester.pumpAndSettle();
|
||||
expect(posted, isEmpty);
|
||||
expect(mounted.gateway.listings, 1);
|
||||
});
|
||||
|
||||
testWidgets('an unsupported picker disables Upload with its reason', (
|
||||
tester,
|
||||
) async {
|
||||
await pump(
|
||||
tester,
|
||||
picker: const ChatAttachmentPicker.unsupported('No picker here.'),
|
||||
);
|
||||
final button = tester.widget<IconButton>(
|
||||
find.ancestor(
|
||||
of: find.byIcon(Icons.upload_file),
|
||||
matching: find.byType(IconButton),
|
||||
),
|
||||
);
|
||||
expect(button.onPressed, isNull);
|
||||
expect(find.byTooltip('Upload files: No picker here.'), findsOneWidget);
|
||||
});
|
||||
|
||||
testWidgets('server refusals are shown until dismissed', (tester) async {
|
||||
final mounted = await pump(tester);
|
||||
for (final (status, body, message) in [
|
||||
(409, 'exists', '"new.txt" already exists in this folder.'),
|
||||
(
|
||||
401,
|
||||
'Sign in required',
|
||||
'Sign in to upload files (Settings → Sign in).',
|
||||
),
|
||||
(413, 'too big', '"new.txt" is too large to upload (100 MiB limit).'),
|
||||
]) {
|
||||
reply = (status: status, body: body);
|
||||
await tester.tap(find.byTooltip('Upload files'));
|
||||
await tester.pumpAndSettle();
|
||||
expect(find.byType(MaterialBanner), findsOneWidget);
|
||||
expect(find.text(message), findsOneWidget);
|
||||
expect(mounted.controller.uploadError, message);
|
||||
await tester.tap(find.text('Dismiss'));
|
||||
await tester.pumpAndSettle();
|
||||
expect(find.byType(MaterialBanner), findsNothing);
|
||||
}
|
||||
expect(posted, hasLength(3));
|
||||
});
|
||||
|
||||
testWidgets('a batch keeps going after one refusal and lists both', (
|
||||
tester,
|
||||
) async {
|
||||
var calls = 0;
|
||||
postFileUpload =
|
||||
(uri, {required name, required mime, required bytes}) async {
|
||||
calls += 1;
|
||||
return name == 'kept.txt'
|
||||
? (status: 409, body: 'exists')
|
||||
: (status: 200, body: '{"path":"/home/demo/$name"}');
|
||||
};
|
||||
final mounted = await pump(
|
||||
tester,
|
||||
picker: ChatAttachmentPicker(
|
||||
select: () async => [picked('kept.txt'), picked('other.txt')],
|
||||
),
|
||||
);
|
||||
await tester.tap(find.byTooltip('Upload files'));
|
||||
await tester.pumpAndSettle();
|
||||
expect(calls, 2);
|
||||
expect(
|
||||
find.text('"kept.txt" already exists in this folder.'),
|
||||
findsOneWidget,
|
||||
);
|
||||
expect(mounted.gateway.listings, 2);
|
||||
});
|
||||
|
||||
testWidgets('a file drag over the panel highlights the folder and a drop '
|
||||
'uploads there', (tester) async {
|
||||
FilesDropListener? listener;
|
||||
var unsubscribed = 0;
|
||||
final drop = FilesDrop(
|
||||
subscribe: (registered) {
|
||||
listener = registered;
|
||||
return () => unsubscribed += 1;
|
||||
},
|
||||
);
|
||||
final mounted = await pump(tester, drop: drop);
|
||||
expect(listener, isNotNull);
|
||||
final inside = tester.getCenter(find.byType(FilesPanel));
|
||||
expect(listener!.accepts(inside), isTrue);
|
||||
expect(listener!.accepts(const Offset(-5, -5)), isFalse);
|
||||
expect(find.text('Drop to upload to ~/demo'), findsNothing);
|
||||
listener!.onHover(inside);
|
||||
await tester.pump();
|
||||
expect(find.text('Drop to upload to ~/demo'), findsOneWidget);
|
||||
listener!.onLeave();
|
||||
await tester.pump();
|
||||
expect(find.text('Drop to upload to ~/demo'), findsNothing);
|
||||
listener!.onHover(inside);
|
||||
await tester.pump();
|
||||
listener!.onDrop([picked('dropped.png', 'image/png')]);
|
||||
await tester.pumpAndSettle();
|
||||
expect(find.text('Drop to upload to ~/demo'), findsNothing);
|
||||
expect(posted, hasLength(1));
|
||||
expect(posted.single.name, 'dropped.png');
|
||||
expect(posted.single.mime, 'image/png');
|
||||
expect(posted.single.uri.queryParameters['path'], '/home/demo');
|
||||
expect(mounted.gateway.listings, 2);
|
||||
listener!.onError(StateError('is a directory'));
|
||||
await tester.pump();
|
||||
expect(
|
||||
find.textContaining('A dropped item could not be read'),
|
||||
findsOneWidget,
|
||||
);
|
||||
await tester.pumpWidget(const SizedBox());
|
||||
expect(unsubscribed, 1);
|
||||
});
|
||||
|
||||
testWidgets('an unsupported drop host keeps the panel and the button', (
|
||||
tester,
|
||||
) async {
|
||||
await pump(tester, drop: const FilesDrop.unsupported('no drops'));
|
||||
expect(find.text('notes.txt'), findsOneWidget);
|
||||
expect(find.byTooltip('Upload files'), findsOneWidget);
|
||||
});
|
||||
}
|
||||
@@ -0,0 +1,138 @@
|
||||
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.',
|
||||
),
|
||||
),
|
||||
);
|
||||
},
|
||||
);
|
||||
}
|
||||
Reference in New Issue
Block a user