import 'package:flutter/gestures.dart'; 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/data/directory_listing.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_models.dart'; import 'package:pipelines_app/features/chat/files/files_panel.dart'; import 'package:pipelines_app/features/chat/files/files_urls.dart'; import 'package:pipelines_app/features/chat/files/files_viewer.dart'; const home = '/home'; const shown = '/home/demo'; JsonMap _entry(String parent, String name, {bool dir = false}) => { 'name': name, 'path': '$parent/$name', 'is_dir': dir, if (!dir) 'size': 12, }; /// Lists a two-level tree, records every request, and answers each mutation /// with the path the server would report unless a refusal is queued for it. class Gateway extends PipelinesClient { @override Uri get endpoint => Uri.parse('ws://example.test/connect'); final calls = <(String type, JsonMap fields)>[]; final refusals = {}; final tree = >{ shown: [ _entry(shown, 'docs', dir: true), _entry(shown, 'notes.txt'), _entry(shown, 'other.txt'), ], '$shown/docs': const [], }; @override Stream get frames => const Stream.empty(); Iterable bodies(String type) => calls.where((c) => c.$1 == type).map((c) => c.$2[type] as JsonMap); int get listings => calls.where((c) => c.$1 == 'list_directory').length; @override Future call(String type, [JsonMap fields = const {}]) async { calls.add((type, fields)); if (type == 'list_directory') { final path = (fields['list_directory'] as Map?)?['path'] as String? ?? home; final entries = tree[path]; if (entries == null) throw ProtocolError('$path does not exist'); return { 'directory_listing': { 'path': path, 'home': home, 'parent': path == home ? null : path.substring(0, path.lastIndexOf('/')), 'entries': entries, }, }; } final refusal = refusals.remove(type); if (refusal != null) throw ProtocolError(refusal); final body = fields[type] as JsonMap; final path = body['path'] as String; final parent = path.substring(0, path.lastIndexOf('/')); final name = path.substring(path.lastIndexOf('/') + 1); final result = switch (type) { 'rename_path' => '$parent/${body['name']}', 'move_path' => '${body['into']}/$name', _ => path, }; return { 'path_mutated': {'path': result}, }; } } void main() { late FileUrlResolver originalUrls; setUp(() { originalUrls = resolveFileUrls; resolveFileUrls = (_, path) => fileUrls('ws://example.test/connect', path); }); tearDown(() => resolveFileUrls = originalUrls); Future<({FilesController controller, Gateway gateway})> pump( WidgetTester tester, ) async { final gateway = Gateway(); final controller = FilesController(gateway: gateway, initialPath: shown); addTearDown(controller.dispose); await tester.pumpWidget( MaterialApp( home: Scaffold( body: FilesPanel( controller: controller, viewer: const FilesViewer(), drop: const FilesDrop.unsupported('no drops in tests'), ), ), ), ); await tester.pumpAndSettle(); expect(gateway.listings, 1); return (controller: controller, gateway: gateway); } Future pickMenu(WidgetTester tester, String menu, String item) async { await tester.tap(find.byTooltip(menu).first); await tester.pumpAndSettle(); await tester.tap(find.text(item)); await tester.pumpAndSettle(); } /// Presses and holds [name], carries it over [folder] and releases. Future dragOnto( WidgetTester tester, String name, String folder, { void Function()? whileOver, }) async { final gesture = await tester.startGesture( tester.getCenter(find.text(name)), ); await tester.pump(kLongPressTimeout + const Duration(milliseconds: 50)); await gesture.moveTo(tester.getCenter(find.text(folder))); await tester.pump(); whileOver?.call(); await gesture.up(); await tester.pumpAndSettle(); } testWidgets('Rename… prefills the name, refuses a bad one and sends ' 'rename_path, then reloads', (tester) async { final mounted = await pump(tester); await pickMenu(tester, 'File actions', 'Rename…'); final field = find.byKey(const Key('files-rename-field')); expect(tester.widget(field).controller!.text, 'notes.txt'); final save = find.widgetWithText(FilledButton, 'Save'); expect( tester.widget(save).onPressed, isNull, reason: 'an unchanged name is not sent', ); await tester.enterText(field, 'bad/name'); await tester.pump(); expect(find.text(r'A name cannot contain / or \.'), findsOneWidget); expect(tester.widget(save).onPressed, isNull); await tester.enterText(field, 'renamed.txt'); await tester.pump(); await tester.tap(save); await tester.pumpAndSettle(); expect(find.byKey(const Key('files-rename-field')), findsNothing); expect(mounted.gateway.bodies('rename_path').toList(), [ {'path': '$shown/notes.txt', 'name': 'renamed.txt'}, ]); expect(mounted.gateway.listings, 2); expect(mounted.controller.mutationError, isNull); }); testWidgets('Cancelling Rename… sends nothing', (tester) async { final mounted = await pump(tester); await pickMenu(tester, 'Folder actions', 'Rename…'); expect(find.text('Rename folder'), findsOneWidget); await tester.tap(find.text('Cancel')); await tester.pumpAndSettle(); expect(mounted.gateway.bodies('rename_path'), isEmpty); expect(mounted.gateway.listings, 1); }); testWidgets('Move… browses from the shown folder and sends move_path for ' 'the folder chosen with Move here', (tester) async { final mounted = await pump(tester); await pickMenu(tester, 'File actions', 'Move…'); expect(find.text('Move “notes.txt”'), findsOneWidget); final moveHere = find.byKey(const Key('folder-picker-start')); expect( tester.widget(moveHere).onPressed, isNull, reason: 'the entry is already in the shown folder', ); await tester.tap(find.byKey(const ValueKey('folder-entry-$shown/docs'))); await tester.pumpAndSettle(); expect(find.text('Move to ~/demo/docs'), findsOneWidget); await tester.tap(moveHere); await tester.pumpAndSettle(); expect(mounted.gateway.bodies('move_path').toList(), [ {'path': '$shown/notes.txt', 'into': '$shown/docs'}, ]); // The panel's first listing, the picker's two, then the panel's reload. expect(mounted.gateway.listings, 4); expect(mounted.gateway.calls.last.$2, { 'list_directory': {'path': shown}, }); }); testWidgets('Move… keeps a folder out of its own subtree', (tester) async { await pump(tester); await pickMenu(tester, 'Folder actions', 'Move…'); expect(find.text('Move “docs”'), findsOneWidget); final row = find.byKey(const ValueKey('folder-entry-$shown/docs')); expect(tester.widget(row).enabled, isFalse); await tester.tap(row); await tester.pumpAndSettle(); expect(find.text('Move “docs”'), findsOneWidget, reason: 'not entered'); }); testWidgets('Delete… confirms before sending delete_path', (tester) async { final mounted = await pump(tester); await pickMenu(tester, 'File actions', 'Delete…'); expect(find.text('Delete “notes.txt”?'), findsOneWidget); await tester.tap(find.text('Cancel')); await tester.pumpAndSettle(); expect(mounted.gateway.bodies('delete_path'), isEmpty); await pickMenu(tester, 'File actions', 'Delete…'); await tester.tap(find.widgetWithText(FilledButton, 'Delete')); await tester.pumpAndSettle(); expect(mounted.gateway.bodies('delete_path').toList(), [ {'path': '$shown/notes.txt'}, ]); expect(mounted.gateway.listings, 2); }); testWidgets('A folder with contents asks again, then deletes recursively', ( tester, ) async { final mounted = await pump(tester); mounted.gateway.refusals['delete_path'] = '$shown/docs is not empty'; await pickMenu(tester, 'Folder actions', 'Delete…'); await tester.tap(find.widgetWithText(FilledButton, 'Delete')); await tester.pumpAndSettle(); expect(find.text('Delete “docs” and its contents?'), findsOneWidget); expect( mounted.controller.mutationError, isNull, reason: 'the not-empty refusal is a question, not an error', ); await tester.tap(find.text('Delete all')); await tester.pumpAndSettle(); expect(mounted.gateway.bodies('delete_path').toList(), [ {'path': '$shown/docs'}, {'path': '$shown/docs', 'recursive': true}, ]); expect(mounted.gateway.listings, 3); }); testWidgets('Holding an entry and dropping it on a folder row moves it', ( tester, ) async { final mounted = await pump(tester); await dragOnto( tester, 'notes.txt', 'docs', whileOver: () { final row = find.ancestor( of: find.text('docs'), matching: find.byType(ListTile), ); expect(tester.widget(row).selected, isTrue); }, ); expect(mounted.gateway.bodies('move_path').toList(), [ {'path': '$shown/notes.txt', 'into': '$shown/docs'}, ]); expect(mounted.gateway.listings, 2); }); testWidgets( 'A refused move is shown in the server\'s words until dismissed', (tester) async { final mounted = await pump(tester); const refusal = '$shown/docs/notes.txt already exists'; mounted.gateway.refusals['move_path'] = refusal; await dragOnto(tester, 'notes.txt', 'docs'); expect(find.text(refusal), findsOneWidget); expect(mounted.gateway.listings, 2, reason: 'reloaded all the same'); await tester.tap(find.text('Dismiss')); await tester.pumpAndSettle(); expect(find.text(refusal), findsNothing); }, ); test('canDropEntryOn refuses files, itself and its own subtree', () { const docs = DirectoryEntry( name: 'docs', path: '$shown/docs', isDir: true, isGitRepo: false, hidden: false, ); const inner = DirectoryEntry( name: 'inner', path: '$shown/docs/inner', isDir: true, isGitRepo: false, hidden: false, ); const file = DirectoryEntry( name: 'notes.txt', path: '$shown/notes.txt', isDir: false, isGitRepo: false, hidden: false, ); expect(canDropEntryOn(file, docs), isTrue); expect(canDropEntryOn(docs, file), isFalse); expect(canDropEntryOn(docs, docs), isFalse); expect(canDropEntryOn(docs, inner), isFalse); expect(canDropEntryOn(inner, docs), isTrue); }); }