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; @override Stream get frames => const Stream.empty(); @override Future call(String type, [JsonMap fields = const {}]) async { if (fail) throw StateError('internal transport secret'); return { 'directory_listing': { 'path': '/home/demo', '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 pump( WidgetTester tester, { Gateway? gateway, FilesViewer? viewer, }) async { final controller = FilesController( gateway: gateway ?? Gateway(), initialPath: '/home/demo', ); addTearDown(controller.dispose); await tester.pumpWidget( MaterialApp( home: Scaffold( body: FilesPanel( controller: controller, viewer: viewer ?? const FilesViewer(), ), ), ), ); await tester.pumpAndSettle(); return controller; } 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 = []; 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); }); }