import 'dart:typed_data'; import 'package:flutter/cupertino.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/features/artifacts/actions/artifact_file.dart'; import 'package:pipelines_app/features/artifacts/actions/artifact_file_actions.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/domain/artifact_groups.dart'; import 'package:pipelines_app/features/artifacts/domain/artifact_record.dart'; import 'package:pipelines_app/features/artifacts/domain/artifact_url.dart'; import 'package:pipelines_app/features/artifacts/ui/artifact_gallery_page.dart'; import 'package:pipelines_app/features/artifacts/ui/artifact_visit_list.dart'; import 'package:pipelines_app/features/artifacts/ui/artifact_viewer_fetch.dart'; import 'package:pipelines_app/features/artifacts/ui/artifact_viewer_page.dart'; import '../domain/artifact_fixtures.dart'; import '../ui/artifact_visit_list_fixture.dart'; const _endpoint = 'ws://127.0.0.1:8081/connect'; final Uint8List _bytes = Uint8List.fromList([7, 8, 9]); final ArtifactRecord _record = ArtifactRecord.fromJson({ 'name': 'report.zip', 'url': '/artifacts/1/1/report.zip', 'content_type': 'application/zip', 'size_bytes': 3, }); ArtifactFileBridge _bridge({ List? saved, List? shared, bool downloadSupported = true, bool shareSupported = true, }) => ArtifactFileBridge( downloadSupported: downloadSupported, shareSupported: shareSupported, downloadUnsupportedMessage: 'No download on this host.', shareUnsupportedMessage: 'No share on this host.', download: (file) async { saved?.add(file); return ArtifactTransferResult( ArtifactTransferState.saved, 'Downloaded ${file.name}.', ); }, share: (file) async { shared?.add(file); return const ArtifactTransferResult( ArtifactTransferState.shared, 'File shared.', ); }, ); Future _fetch(Uri uri) async => ArtifactFetchResult(status: 200, bytes: _bytes); Widget _row({ required bool cupertino, ArtifactFileBridge? bridge, ArtifactRecord? artifact, ArtifactFetch? fetch, }) { final actions = ArtifactFileActions( artifact: artifact ?? _record, resolved: resolveArtifactUrl(_endpoint, _record.url)!, fetch: fetch ?? _fetch, bridge: bridge ?? _bridge(), ); return cupertino ? CupertinoApp( home: CupertinoPageScaffold( child: SafeArea( child: Padding(padding: const EdgeInsets.all(8), child: actions), ), ), ) : MaterialApp( home: Scaffold( body: Padding(padding: const EdgeInsets.all(8), child: actions), ), ); } TextButton _materialButton(WidgetTester tester, String label) => tester.widget( find .ancestor(of: find.text(label), matching: find.byType(TextButton)) .first, ); CupertinoButton _cupertinoButton(WidgetTester tester, String label) => tester.widget( find .ancestor( of: find.text(label), matching: find.byType(CupertinoButton), ) .first, ); void main() { testWidgets('Download saves the exact named bytes and reports it', ( tester, ) async { final saved = []; await tester.pumpWidget( _row(cupertino: false, bridge: _bridge(saved: saved)), ); await tester.tap(find.text('Download')); await tester.pumpAndSettle(); expect(saved, hasLength(1)); expect(saved.single.name, 'report.zip'); expect(saved.single.mimeType, 'application/zip'); expect(saved.single.bytes, _bytes); expect(find.text('Downloaded report.zip.'), findsOneWidget); }); testWidgets('Share starts one file transfer, not a link', (tester) async { final shared = []; await tester.pumpWidget( _row(cupertino: false, bridge: _bridge(shared: shared)), ); await tester.tap(find.text('Share')); await tester.pumpAndSettle(); expect(shared.single.name, 'report.zip'); expect(shared.single.byteLength, 3); expect(find.text('File shared.'), findsOneWidget); }); testWidgets('unsupported share is explained but download stays enabled', ( tester, ) async { final saved = []; await tester.pumpWidget( _row( cupertino: false, bridge: _bridge(saved: saved, shareSupported: false), ), ); expect(find.text('No share on this host.'), findsOneWidget); expect(_materialButton(tester, 'Share').onPressed, isNull); expect(_materialButton(tester, 'Download').onPressed, isNotNull); await tester.tap(find.text('Download')); await tester.pumpAndSettle(); expect(saved, hasLength(1)); }); testWidgets('a slow fetch shows progress and cancel, then reports cancel', ( tester, ) async { await tester.pumpWidget( _row( cupertino: false, bridge: _bridge(), fetch: (_) => Future.delayed( const Duration(seconds: 30), () => ArtifactFetchResult(status: 200, bytes: _bytes), ), ), ); await tester.tap(find.text('Download')); await tester.pump(); expect(find.text('Preparing file…'), findsOneWidget); expect(find.byType(CircularProgressIndicator), findsOneWidget); expect(_materialButton(tester, 'Download').onPressed, isNull); expect(find.text('Cancel'), findsOneWidget); await tester.tap(find.text('Cancel')); await tester.pump(); expect(find.text('Download canceled.'), findsOneWidget); expect(find.text('Cancel'), findsNothing); await tester.pump(const Duration(seconds: 31)); }); testWidgets('both hosts keep the file actions whole at 320/2x dark', ( tester, ) async { tester.view.physicalSize = const Size(640, 1200); tester.view.devicePixelRatio = 2; tester.platformDispatcher.textScaleFactorTestValue = 2; tester.platformDispatcher.platformBrightnessTestValue = Brightness.dark; addTearDown(tester.view.resetPhysicalSize); addTearDown(tester.view.resetDevicePixelRatio); addTearDown(tester.platformDispatcher.clearTextScaleFactorTestValue); addTearDown(tester.platformDispatcher.clearPlatformBrightnessTestValue); for (final cupertino in [false, true]) { await tester.pumpWidget(_row(cupertino: cupertino)); await tester.pumpAndSettle(); expect(tester.takeException(), isNull); expect(find.text('Download'), findsOneWidget); expect(find.text('Share'), findsOneWidget); if (cupertino) { expect(_cupertinoButton(tester, 'Download').onPressed, isNotNull); expect(find.byType(TextButton), findsNothing); } else { expect(_materialButton(tester, 'Download').onPressed, isNotNull); expect(find.byType(CupertinoButton), findsNothing); } } }); testWidgets('the gallery selection offers both file actions in its dialog', ( tester, ) async { final saved = []; await tester.pumpWidget( MaterialApp( home: ArtifactGalleryPage( steps: [ step( stage: 'review', visit: 1, artifacts: [ artifact( 'report.zip', contentType: 'application/zip', sizeBytes: 3, ), ], ), ], configuredEndpoint: _endpoint, fetch: _fetch, fileBridge: _bridge(saved: saved), ), ), ); await tester.pumpAndSettle(); await tester.tap(find.text('report.zip')); await tester.pumpAndSettle(); expect(find.byType(AlertDialog), findsOneWidget); expect(find.text('Download'), findsOneWidget); expect(find.text('Share'), findsOneWidget); expect(find.text('Open'), findsOneWidget); expect(find.text('Copy link'), findsOneWidget); await tester.tap(find.text('Download')); await tester.pumpAndSettle(); expect(saved.single.bytes, _bytes); expect(find.text('Downloaded report.zip.'), findsOneWidget); expect(find.byType(AlertDialog), findsOneWidget); }); testWidgets('a host without file capabilities explains and disables both', ( tester, ) async { await tester.pumpWidget( MaterialApp( home: Scaffold( body: Padding( padding: const EdgeInsets.all(8), child: ArtifactFileActions( artifact: _record, resolved: resolveArtifactUrl(_endpoint, _record.url)!, fetch: _fetch, bridge: const ArtifactFileBridge( downloadSupported: false, shareSupported: false, downloadUnsupportedMessage: 'Downloading files is not supported on this platform yet.', shareUnsupportedMessage: 'File sharing is not supported on this platform yet.', ), ), ), ), ), ); expect(find.text('Download'), findsOneWidget); expect(find.text('Share'), findsOneWidget); expect(_materialButton(tester, 'Download').onPressed, isNull); expect(_materialButton(tester, 'Share').onPressed, isNull); expect( find.text('Downloading files is not supported on this platform yet.'), findsOneWidget, ); expect( find.text('File sharing is not supported on this platform yet.'), findsOneWidget, ); }); testWidgets('gallery selection keeps both actions at 320/2x dark', ( tester, ) async { tester.view.physicalSize = const Size(640, 1200); tester.view.devicePixelRatio = 2; tester.platformDispatcher.textScaleFactorTestValue = 2; tester.platformDispatcher.platformBrightnessTestValue = Brightness.dark; addTearDown(tester.view.resetPhysicalSize); addTearDown(tester.view.resetDevicePixelRatio); addTearDown(tester.platformDispatcher.clearTextScaleFactorTestValue); addTearDown(tester.platformDispatcher.clearPlatformBrightnessTestValue); final steps = [ step( stage: 'review', visit: 1, artifacts: [ artifact('report.zip', contentType: 'application/zip', sizeBytes: 3), ], ), ]; for (final cupertino in [false, true]) { await tester.pumpWidget( cupertino ? CupertinoApp( theme: const CupertinoThemeData(brightness: Brightness.dark), home: ArtifactGalleryPage( steps: steps, configuredEndpoint: _endpoint, fetch: _fetch, fileBridge: _bridge(), ), ) : MaterialApp( theme: ThemeData(brightness: Brightness.dark), home: ArtifactGalleryPage( steps: steps, configuredEndpoint: _endpoint, fetch: _fetch, fileBridge: _bridge(), ), ), ); await tester.pumpAndSettle(); await tester.tap(find.text('report.zip')); await tester.pumpAndSettle(); expect(tester.takeException(), isNull); expect(find.text('Download'), findsOneWidget); expect(find.text('Share'), findsOneWidget); expect( cupertino ? find.byType(CupertinoAlertDialog) : find.byType(AlertDialog), findsOneWidget, ); await tester.tap(find.text('Close')); await tester.pumpAndSettle(); } }); testWidgets( 'the viewer offers file actions only for a same-origin artifact', (tester) async { final local = recordedArtifact( 'clip.mp4', url: '/artifacts/1/1/clip.mp4', contentType: 'video/mp4', ); final external = recordedArtifact( 'elsewhere.png', url: 'https://elsewhere.test/elsewhere.png', contentType: 'image/png', ); final localGroup = visitGroup(artifacts: [local]); await tester.pumpWidget( MaterialApp( home: Scaffold( body: ArtifactViewerPage( key: const ValueKey('local'), groups: [localGroup], selectedIdentity: artifactVisitIdentity(localGroup, local), configuredEndpoint: _endpoint, fetch: _fetch, fileBridge: _bridge(), openExternal: (_) async => true, ), ), ), ); await tester.pumpAndSettle(); expect(find.byKey(const Key('artifact-download')), findsOneWidget); expect(find.byKey(const Key('artifact-share')), findsOneWidget); final externalGroup = visitGroup(artifacts: [external]); await tester.pumpWidget( MaterialApp( home: Scaffold( body: ArtifactViewerPage( key: const ValueKey('external'), groups: [externalGroup], selectedIdentity: artifactVisitIdentity(externalGroup, external), configuredEndpoint: _endpoint, fetch: _fetch, fileBridge: _bridge(), openExternal: (_) async => true, ), ), ), ); await tester.pumpAndSettle(); expect(find.byKey(const Key('artifact-download')), findsNothing); expect(find.byKey(const Key('artifact-share')), findsNothing); expect(find.byKey(const Key('artifact-open')), findsOneWidget); }, ); }