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 get frames => const Stream.empty(); @override Future 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 = []; var reply = (status: 200, body: '{"path":"/home/demo/new.txt"}'); Completer? 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, headers = const {}, }) 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(); 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( 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( 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, headers = const {}, }) 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); }); }