Files
pipelines/app/test/features/chat/files/files_panel_test.dart
T
alan d56a76c9a7 Keep Files breadcrumbs aligned to the current folder
Pin the horizontally scrolling breadcrumb to its trailing edge so deep paths keep the current folder visible. Preserve tappable navigation, middle-ellipsize long labels, and cover the behavior with focused widget tests and the rendered fixture.
2026-11-12 23:08:03 +00:00

467 lines
15 KiB
Dart

import 'dart:convert';
import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:pipelines_app/features/chat/files/files_preview.dart';
import 'package:pipelines_app/features/artifacts/domain/artifact_kind.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/artifacts/actions/artifact_file_bridge.dart';
import 'package:pipelines_app/features/artifacts/actions/artifact_transfer.dart';
import 'package:pipelines_app/features/artifacts/ui/artifact_viewer_fetch.dart';
import 'package:pipelines_app/features/artifacts/ui/artifact_viewer_page.dart';
import 'package:pipelines_app/features/chat/files/files_controller.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_thumbnail.dart';
import 'package:pipelines_app/features/chat/files/files_urls.dart';
import 'package:pipelines_app/features/chat/files/files_viewer.dart';
class Gateway extends PipelinesClient {
@override
Uri get endpoint => Uri.parse('ws://example.test/connect');
bool fail = false;
final requestedPaths = <String>[];
@override
Stream<JsonMap> get frames => const Stream.empty();
@override
Future<JsonMap> call(String type, [JsonMap fields = const {}]) async {
if (fail) throw StateError('internal transport secret');
final request = fields['list_directory'];
final path = request is Map && request['path'] is String
? request['path'] as String
: '/home/demo';
requestedPaths.add(path);
return {
'directory_listing': {
'path': path,
'home': '/home',
'entries': [
for (final name in [
'notes.txt',
'picture.png',
'archive.zip',
'report.pdf',
])
{'name': name, 'path': '/home/demo/$name', 'size': 1234},
],
},
};
}
}
void main() {
late FileUrlResolver original;
setUp(() {
original = resolveFileUrls;
resolveFileUrls = (_, path) => fileUrls('ws://example.test/connect', path);
});
tearDown(() => resolveFileUrls = original);
Future<FilesController> pump(
WidgetTester tester, {
Gateway? gateway,
FilesViewer? viewer,
String initialPath = '/home/demo',
}) async {
final controller = FilesController(
gateway: gateway ?? Gateway(),
initialPath: initialPath,
);
addTearDown(controller.dispose);
await tester.pumpWidget(
MaterialApp(
home: Scaffold(
body: FilesPanel(
controller: controller,
viewer: viewer ?? const FilesViewer(),
),
),
),
);
await tester.pumpAndSettle();
return controller;
}
testWidgets('the panel filters entries through its Material field', (
tester,
) async {
await pump(tester);
final field = find.byType(TextField);
expect(field, findsOneWidget);
expect(
tester.widget<TextField>(field).decoration?.hintText,
'Filter files',
);
await tester.enterText(field, 'notes');
await tester.pumpAndSettle();
expect(find.text('notes.txt'), findsOneWidget);
expect(find.text('picture.png'), findsNothing);
expect(find.widgetWithText(TextButton, '~'), findsOneWidget);
expect(find.widgetWithText(TextButton, 'demo'), findsOneWidget);
});
testWidgets(
'deep breadcrumbs keep the leaf fully visible at the trailing edge',
(tester) async {
final gateway = Gateway();
await pump(
tester,
gateway: gateway,
initialPath:
'/home/alpha/beta/gamma/delta/epsilon/zeta/eta/theta/iota/omega',
);
final scroll = tester.widget<SingleChildScrollView>(
find.byKey(const Key('files-breadcrumb-scroll')),
);
expect(find.text('omega'), findsOneWidget);
expect(
scroll.controller!.position.pixels,
scroll.controller!.position.maxScrollExtent,
);
final viewport = tester.getRect(
find.byKey(const Key('files-breadcrumb-scroll')),
);
final leaf = tester.getRect(find.text('omega'));
final leafButton = tester.getRect(
find.widgetWithText(TextButton, 'omega'),
);
expect(leaf.right, lessThanOrEqualTo(viewport.right));
expect(leafButton.right, closeTo(viewport.right, 1));
scroll.controller!.jumpTo(0);
await tester.pump();
await tester.tap(find.widgetWithText(TextButton, 'alpha'));
await tester.pumpAndSettle();
expect(gateway.requestedPaths.last, '/home/alpha');
},
);
testWidgets('navigating deeper pins the new breadcrumb leaf', (tester) async {
final gateway = Gateway();
final controller = await pump(
tester,
gateway: gateway,
initialPath: '/home/alpha/beta',
);
await controller.open('/home/alpha/beta/gamma/delta/leaf');
await tester.pumpAndSettle();
final scroll = tester.widget<SingleChildScrollView>(
find.byKey(const Key('files-breadcrumb-scroll')),
);
expect(find.text('leaf'), findsOneWidget);
expect(
scroll.controller!.position.pixels,
scroll.controller!.position.maxScrollExtent,
);
expect(gateway.requestedPaths.last, '/home/alpha/beta/gamma/delta/leaf');
});
testWidgets('long breadcrumb labels use middle ellipsis', (tester) async {
await pump(
tester,
initialPath:
'/home/a-folder-name-that-is-long-enough-to-ellipsize-in-the-middle',
);
expect(find.text('a-folder-name-that…ize-in-the-middle'), findsOneWidget);
expect(
find.text('a-folder-name-that-is-long-enough-to-ellipsize-in-the-middle'),
findsNothing,
);
});
testWidgets('file sorting uses a sort icon and preserves direction', (
tester,
) async {
final controller = await pump(tester);
expect(find.byTooltip('Sort'), findsOneWidget);
expect(find.byIcon(Icons.sort), findsOneWidget);
expect(controller.sort.key, FilesSortKey.name);
expect(controller.sort.ascending, isTrue);
await tester.tap(find.byTooltip('Sort'));
await tester.pumpAndSettle();
await tester.tap(find.text('size'));
await tester.pumpAndSettle();
expect(controller.sort.key, FilesSortKey.size);
expect(controller.sort.ascending, isTrue);
await tester.tap(find.byTooltip('Sort'));
await tester.pumpAndSettle();
await tester.tap(find.text('size ↑'));
await tester.pumpAndSettle();
expect(controller.sort.key, FilesSortKey.size);
expect(controller.sort.ascending, isFalse);
});
testWidgets('text tap opens shared viewer and fetches inline URL', (
tester,
) async {
Uri? fetched;
await pump(
tester,
viewer: FilesViewer(
fetch: (url) async {
fetched = url;
return ArtifactFetchResult(
status: 200,
bytes: utf8.encode('A text file preview'),
);
},
),
);
await tester.tap(find.text('notes.txt'));
await tester.pumpAndSettle();
expect(find.byType(ArtifactViewerPage), findsOneWidget);
expect(find.text('A text file preview'), findsOneWidget);
expect(fetched?.queryParameters['path'], '/home/demo/notes.txt');
expect(fetched?.queryParameters.containsKey('download'), isFalse);
});
testWidgets('image tap opens shared image viewer', (tester) async {
await pump(
tester,
viewer: FilesViewer(
fetch: (_) async => ArtifactFetchResult(
status: 200,
bytes: base64Decode(
'iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAYAAAAfFcSJAAAADUlEQVR42mP8z8BQDwAEhQGAhKmMIQAAAABJRU5ErkJggg==',
),
),
),
);
await tester.tap(find.text('picture.png'));
await tester.pumpAndSettle();
expect(find.byType(ArtifactViewerPage), findsOneWidget);
expect(find.byType(Image), findsWidgets);
});
testWidgets('image opened from Files owns pinch and pan until reset', (
tester,
) async {
await pump(
tester,
viewer: FilesViewer(
fetch: (_) async => ArtifactFetchResult(
status: 200,
bytes: base64Decode(
'iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAYAAAAfFcSJAAAADUlEQVR42mP8z8BQDwAEhQGAhKmMIQAAAABJRU5ErkJggg==',
),
),
),
);
await tester.tap(find.text('picture.png'));
await tester.pumpAndSettle();
final viewport = tester.getRect(
find.byKey(const Key('artifact-image-zoom')),
);
final center = viewport.center;
final one = await tester.startGesture(
center + const Offset(-24, 0),
pointer: 1,
);
final two = await tester.startGesture(
center + const Offset(24, 0),
pointer: 2,
);
await one.moveTo(center + const Offset(-96, 0));
await two.moveTo(center + const Offset(96, 0));
await tester.pump();
await one.up();
await two.up();
await tester.pumpAndSettle();
expect(find.byKey(const Key('artifact-image-reset-zoom')), findsOneWidget);
await tester.tap(find.byKey(const Key('artifact-image-reset-zoom')));
await tester.pumpAndSettle();
expect(find.byKey(const Key('artifact-image-reset-zoom')), findsNothing);
});
testWidgets('unknown file has metadata and a working download', (
tester,
) async {
String? saved;
await pump(
tester,
viewer: FilesViewer(
fetch: (_) async =>
ArtifactFetchResult(status: 200, bytes: utf8.encode('bytes')),
bridge: ArtifactFileBridge(
download: (file) async {
saved = file.name;
return const ArtifactTransferResult(
ArtifactTransferState.saved,
'Saved file.',
);
},
),
),
);
await tester.tap(find.text('archive.zip'));
await tester.pumpAndSettle();
expect(
find.text('This file type cannot be previewed here.'),
findsOneWidget,
);
await tester.tap(find.text('Download'));
await tester.pumpAndSettle();
expect(saved, 'archive.zip');
expect(find.text('Saved file.'), findsOneWidget);
});
for (final grid in [false, true]) {
testWidgets(
'${grid ? 'grid' : 'list'} PDF menu downloads without opening',
(tester) async {
String? saved;
var opened = false;
final controller = await pump(
tester,
viewer: FilesViewer(
fetch: (_) async => ArtifactFetchResult(
status: 200,
bytes: utf8.encode('pdf bytes'),
),
openExternal: (_) async {
opened = true;
return true;
},
bridge: ArtifactFileBridge(
download: (file) async {
saved = file.name;
return const ArtifactTransferResult(
ArtifactTransferState.saved,
'Saved',
);
},
),
),
);
if (grid) {
controller.setView(FilesViewMode.grid);
await tester.pumpAndSettle();
}
await tester.tap(find.byTooltip('File actions').last);
await tester.pumpAndSettle();
expect(find.text('Open'), findsOneWidget);
await tester.tap(find.text('Download'));
await tester.pumpAndSettle();
expect(saved, 'report.pdf');
expect(opened, isFalse);
expect(tester.takeException(), isNull);
},
);
}
testWidgets('thumbnail error falls back to icon and decode stays bounded', (
tester,
) async {
final controller = await pump(tester);
controller.setView(FilesViewMode.grid);
await tester.pumpAndSettle();
expect(find.byType(FileThumbnail), findsNWidgets(4));
expect(find.byIcon(Icons.image_outlined), findsOneWidget);
expect(filesThumbnailDecodeSize(10), 224);
expect(tester.takeException(), isNull);
});
testWidgets('PDF external-open failures keep download available', (
tester,
) async {
await pump(
tester,
viewer: FilesViewer(
openExternal: (_) async => throw StateError('no handler'),
),
);
await tester.tap(find.text('report.pdf'));
await tester.pumpAndSettle();
expect(find.text('Download'), findsOneWidget);
expect(tester.takeException(), isNull);
});
testWidgets('download thrown host errors become feedback', (tester) async {
await pump(
tester,
viewer: FilesViewer(
fetch: (_) async =>
ArtifactFetchResult(status: 200, bytes: utf8.encode('bytes')),
bridge: ArtifactFileBridge(
download: (_) async => throw StateError('host failure'),
),
),
);
await tester.tap(find.byTooltip('File actions').last);
await tester.pumpAndSettle();
await tester.tap(find.text('Download'));
await tester.pumpAndSettle();
expect(
find.text('Could not download the file. Try again.'),
findsOneWidget,
);
expect(tester.takeException(), isNull);
});
test('file-specific HTML previews preserve artifact export exclusions', () {
expect(
filePreviewFormat('text/html', 'session.html'),
ArtifactPreviewFormat.html,
);
expect(
artifactPreviewFormat('text/html', 'session.html'),
ArtifactPreviewFormat.none,
);
expect(
filePreviewFormat('image/svg+xml', 'icon.svg'),
ArtifactPreviewFormat.html,
);
expect(fileContentType('code.dart'), 'text/plain');
expect(fileSizeLabel(1024), '1.0 KiB');
expect(fileSizeLabel(1024 * 1024), '1.0 MiB');
});
testWidgets('folder errors hide technical detail', (tester) async {
await pump(tester, gateway: Gateway()..fail = true);
expect(find.text("Couldn't load this folder."), findsOneWidget);
expect(find.textContaining('internal transport'), findsNothing);
});
testWidgets('arrows step through the folder\'s previewable files and '
'Escape closes the viewer', (tester) async {
final fetched = <String>[];
await pump(
tester,
viewer: FilesViewer(
fetch: (url) async {
fetched.add(url.queryParameters['path'] ?? '');
return ArtifactFetchResult(
status: 200,
bytes: url.queryParameters['path']!.endsWith('.png')
? base64Decode(
'iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAYAAAAfFcSJAAAADUlEQVR42mP8z8BQDwAEhQGAhKmMIQAAAABJRU5ErkJggg==',
)
: utf8.encode('A text file preview'),
);
},
),
);
Finder shown(String name) => find.descendant(
of: find.byType(ArtifactViewerPage),
matching: find.text(name),
);
await tester.tap(find.text('notes.txt'));
await tester.pumpAndSettle();
expect(find.byType(ArtifactViewerPage), findsOneWidget);
expect(shown('notes.txt'), findsWidgets);
// The zip and PDF are not previewable, so the next file is the image.
await tester.sendKeyEvent(LogicalKeyboardKey.arrowRight);
await tester.pumpAndSettle();
expect(shown('picture.png'), findsWidgets);
expect(fetched, contains('/home/demo/picture.png'));
await tester.sendKeyEvent(LogicalKeyboardKey.arrowLeft);
await tester.pumpAndSettle();
expect(shown('notes.txt'), findsWidgets);
await tester.sendKeyEvent(LogicalKeyboardKey.escape);
await tester.pumpAndSettle();
expect(find.byType(ArtifactViewerPage), findsNothing);
expect(find.text('notes.txt'), findsOneWidget);
});
}