import 'package:flutter/cupertino.dart'; import 'package:flutter/material.dart'; import 'package:flutter/services.dart'; import 'package:flutter_test/flutter_test.dart'; import 'package:pipelines_app/core/api_gateway.dart'; import 'package:pipelines_app/features/artifacts/domain/artifact_groups.dart'; import 'package:pipelines_app/features/artifacts/ui/artifact_gallery_page.dart'; import 'package:pipelines_app/features/artifacts/ui/artifact_visit_list.dart'; import '../domain/artifact_fixtures.dart'; enum _Platform { material, cupertino } JsonMap _recorded( String name, { String url = '', String contentType = 'text/csv', int sizeBytes = 2048, }) => artifact( name, url: url.isEmpty ? null : url, contentType: contentType, sizeBytes: sizeBytes, ); /// Two visits of `review` record the same filename; the second visit also /// records `notes.md`. `build` records one file. List _steps() => [ step( stage: 'build', sequence: 1, visit: 1, artifacts: [_recorded('build.log', contentType: 'text/plain')], ), step( stage: 'review', sequence: 2, visit: 1, artifacts: [ _recorded('result.csv'), _recorded('notes.md', contentType: 'text/markdown'), ], ), step( stage: 'review', sequence: 3, visit: 2, artifacts: [_recorded('result.csv', url: '/artifacts/1/2/result.csv')], ), ]; Future _pumpGallery( WidgetTester tester, _Platform platform, List steps, { String configuredEndpoint = '', bool executing = false, Future Function(Uri)? openExternal, }) async { final page = ArtifactGalleryPage( steps: steps, executing: executing, configuredEndpoint: configuredEndpoint, openExternal: openExternal ?? (_) async => true, ); await tester.pumpWidget( platform == _Platform.material ? MaterialApp(home: page) : CupertinoApp(home: page), ); await tester.pumpAndSettle(); } Finder _inDialog(_Platform platform, String text) => find.descendant( of: find.byType( platform == _Platform.material ? AlertDialog : CupertinoAlertDialog, ), matching: find.text(text), ); void main() { for (final platform in _Platform.values) { testWidgets('${platform.name}: lists recorded groups and rows', ( tester, ) async { await _pumpGallery(tester, platform, _steps()); expect(find.text('Artifacts · 4 recorded'), findsOneWidget); expect(find.text('build · visit 1'), findsOneWidget); expect(find.text('review · visit 1'), findsOneWidget); expect(find.text('review · visit 2'), findsOneWidget); expect(find.text('result.csv'), findsNWidgets(2)); expect(find.text('notes.md'), findsOneWidget); expect(find.text('text/markdown · 2.0\u00a0KiB'), findsOneWidget); }); testWidgets('${platform.name}: duplicate filename stays visit distinct', ( tester, ) async { final steps = _steps(); await _pumpGallery(tester, platform, steps); await tester.tap(find.text('result.csv').at(1)); await tester.pumpAndSettle(); final groups = artifactGroups(steps); final visitTwo = groups.firstWhere( (group) => group.stageKey == 'review' && group.visit == 2, ); final list = tester.widget( find.byType(ArtifactVisitList), ); expect( list.selectedIdentity, artifactVisitIdentity(visitTwo, visitTwo.artifacts.single), ); expect(_inDialog(platform, 'review · visit 2'), findsOneWidget); expect(_inDialog(platform, 'review · visit 1'), findsNothing); expect(_inDialog(platform, 'Artifacts · 1 recorded'), findsOneWidget); }); } testWidgets('capture notes and partial label come from the visit step', ( tester, ) async { await _pumpGallery(tester, _Platform.material, [ step( stage: 'review', visit: 1, artifacts: [_recorded('result.csv'), _recorded('notes.md')], skipped: 2, truncated: 3, ), ]); expect( find.text('Artifacts · 2 recorded · partial capture'), findsOneWidget, ); await tester.tap(find.text('result.csv')); await tester.pumpAndSettle(); expect( _inDialog(_Platform.material, 'Artifacts · 2 recorded · partial capture'), findsOneWidget, ); expect( _inDialog(_Platform.material, '2 files excluded from artifact capture.'), findsOneWidget, ); expect( _inDialog( _Platform.material, '3 files omitted by artifact capture limits.', ), findsOneWidget, ); }); testWidgets('an unavailable capture never claims nothing was recorded', ( tester, ) async { await _pumpGallery(tester, _Platform.material, [ step(stage: 'review', visit: 1, includeArtifacts: false), ]); expect(find.text('Artifacts · capture unavailable'), findsOneWidget); expect(find.text('No artifacts recorded for this run.'), findsNothing); }); testWidgets('a recorded empty capture says nothing was recorded', ( tester, ) async { await _pumpGallery(tester, _Platform.material, [ step(stage: 'review', visit: 1, artifacts: const []), ]); expect(find.text('No artifacts recorded for this run.'), findsOneWidget); expect(find.byType(ArtifactVisitList), findsNothing); }); testWidgets('a page with no steps says so explicitly', (tester) async { await _pumpGallery(tester, _Platform.material, const []); expect(find.text('No steps recorded for this run.'), findsOneWidget); }); testWidgets('Open uses the resolved recorded url only', (tester) async { final opened = []; await _pumpGallery( tester, _Platform.material, [ step( stage: 'review', visit: 1, artifacts: [ _recorded('result.csv', url: '/artifacts/1/1/result.csv'), ], ), ], configuredEndpoint: 'ws://127.0.0.1:8081/connect', openExternal: (uri) async { opened.add(uri); return true; }, ); await tester.tap(find.text('result.csv')); await tester.pumpAndSettle(); await tester.tap(_inDialog(_Platform.material, 'Open')); await tester.pumpAndSettle(); expect(opened, [ Uri.parse('http://127.0.0.1:8081/artifacts/1/1/result.csv'), ]); expect(find.text('Link opened externally.'), findsOneWidget); expect(find.byType(AlertDialog), findsNothing); }); testWidgets('an unsafe recorded url offers no open or copy action', ( tester, ) async { await _pumpGallery(tester, _Platform.material, [ step( stage: 'review', visit: 1, artifacts: [_recorded('result.csv', url: '//evil.test/result.csv')], ), ], configuredEndpoint: 'ws://127.0.0.1:8081/connect'); await tester.tap(find.text('result.csv')); await tester.pumpAndSettle(); expect( _inDialog( _Platform.material, 'The recorded artifact URL cannot be opened safely.', ), findsOneWidget, ); expect(_inDialog(_Platform.material, 'Open'), findsNothing); expect(_inDialog(_Platform.material, 'Copy link'), findsNothing); }); testWidgets('a record with no url is explicit and offers no action', ( tester, ) async { await _pumpGallery(tester, _Platform.material, [ step( stage: 'review', visit: 1, artifacts: [ { 'name': 'result.csv', 'url': '', 'content_type': 'text/csv', 'size_bytes': 10, }, ], ), ]); await tester.tap(find.text('result.csv')); await tester.pumpAndSettle(); expect( _inDialog(_Platform.material, 'No artifact URL recorded.'), findsOneWidget, ); expect(_inDialog(_Platform.material, 'Open'), findsNothing); }); testWidgets('Copy link copies the resolved url, separate from Open', ( tester, ) async { final copied = []; tester.binding.defaultBinaryMessenger.setMockMethodCallHandler( SystemChannels.platform, (call) async { if (call.method == 'Clipboard.setData') { copied.add((call.arguments as Map)['text'] as String); } return null; }, ); addTearDown( () => tester.binding.defaultBinaryMessenger.setMockMethodCallHandler( SystemChannels.platform, null, ), ); final opened = []; await _pumpGallery( tester, _Platform.material, [ step( stage: 'review', visit: 1, artifacts: [ _recorded('result.csv', url: '/artifacts/1/1/result.csv'), ], ), ], configuredEndpoint: 'ws://127.0.0.1:8081/connect', openExternal: (uri) async { opened.add(uri); return true; }, ); await tester.tap(find.text('result.csv')); await tester.pumpAndSettle(); await tester.tap(_inDialog(_Platform.material, 'Copy link')); await tester.pumpAndSettle(); expect(copied, ['http://127.0.0.1:8081/artifacts/1/1/result.csv']); expect(opened, isEmpty); expect(find.text('Link copied.'), findsOneWidget); }); testWidgets('an executing run says capture pending', (tester) async { await _pumpGallery(tester, _Platform.material, [ step( stage: 'review', visit: 1, includeArtifacts: false, status: 'running', ), ], executing: true); expect(find.text('Artifacts · capture pending'), findsOneWidget); }); }