Files
pipelines/app/test/features/chat/files/files_panel_mutations_test.dart
T
alan 4733d4b615 Rename, move and delete entries from the chat Files panel
Every row and tile menu gains Rename…, Move… and Delete… (folders now
have a menu too). Rename prefills the name with the file's stem selected
and applies the server's name rules inline. Move reuses the chat folder
picker through a FolderPickerPurpose: titled for the entry, confirmed
with Move here, never offering the entry's current folder and keeping a
moved folder's own subtree out of reach. Delete confirms, and when the
server reports a folder with contents asks once more before deleting
recursively. Holding an entry and dropping it on a folder row or tile
moves it there; the target highlights and refuses itself and ancestors.

FilesController performs each request through files_mutations.dart,
shows Renaming…/Moving…/Deleting… progress, reloads the shown folder
afterwards and keeps a refusal in the server's words in the panel's
notice until dismissed.
2026-11-09 01:46:28 +00:00

317 lines
11 KiB
Dart

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 = <String, String>{};
final tree = <String, List<JsonMap>>{
shown: [
_entry(shown, 'docs', dir: true),
_entry(shown, 'notes.txt'),
_entry(shown, 'other.txt'),
],
'$shown/docs': const [],
};
@override
Stream<JsonMap> get frames => const Stream.empty();
Iterable<JsonMap> 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<JsonMap> 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<void> 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<void> 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<TextField>(field).controller!.text, 'notes.txt');
final save = find.widgetWithText(FilledButton, 'Save');
expect(
tester.widget<FilledButton>(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<FilledButton>(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<FilledButton>(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<ListTile>(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<ListTile>(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);
});
}