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 = 'application/zip', 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.zip'), _recorded('notes.md', contentType: 'text/markdown'), ], ), step( stage: 'review', sequence: 3, visit: 2, artifacts: [_recorded('result.zip', url: '/artifacts/1/2/result.zip')], ), ]; Future _pumpGallery( WidgetTester tester, _Platform platform, List steps, { int? focusIndex, String configuredEndpoint = '', bool executing = false, Future Function(Uri)? openExternal, // Tall enough that every visit's grid is built. Size surface = const Size(800, 1600), double textScale = 1, }) async { tester.view.physicalSize = surface; tester.view.devicePixelRatio = 1; addTearDown(tester.view.reset); final page = ArtifactGalleryPage( steps: steps, focusIndex: focusIndex, executing: executing, configuredEndpoint: configuredEndpoint, openExternal: openExternal ?? (_) async => true, ); await tester.pumpWidget( MediaQuery( data: MediaQueryData( size: surface, textScaler: TextScaler.linear(textScale), ), child: 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.zip'), 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.zip').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.zip'), _recorded('notes.md')], skipped: 2, truncated: 3, ), ]); expect( find.text('Artifacts · 2 recorded · partial capture'), findsOneWidget, ); await tester.tap(find.text('result.zip')); 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.zip', url: '/artifacts/1/1/result.zip'), ], ), ], configuredEndpoint: 'ws://127.0.0.1:8081/connect', openExternal: (uri) async { opened.add(uri); return true; }, ); await tester.tap(find.text('result.zip')); 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.zip'), ]); 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.zip', url: '//evil.test/result.zip')], ), ], configuredEndpoint: 'ws://127.0.0.1:8081/connect'); await tester.tap(find.text('result.zip')); 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.zip', 'url': '', 'content_type': 'application/zip', 'size_bytes': 10, }, ], ), ]); await tester.tap(find.text('result.zip')); 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.zip', url: '/artifacts/1/1/result.zip'), ], ), ], configuredEndpoint: 'ws://127.0.0.1:8081/connect', openExternal: (uri) async { opened.add(uri); return true; }, ); await tester.tap(find.text('result.zip')); 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.zip']); 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); }); testWidgets('the run header names the visit each capture note belongs to', ( tester, ) async { await _pumpGallery(tester, _Platform.material, [ step( stage: 'review', visit: 1, artifacts: [_recorded('first.zip')], skipped: 2, ), step( stage: 'review', sequence: 2, visit: 2, artifacts: [_recorded('second.zip')], truncated: 3, ), ]); expect( find.text('review · visit 1 · 2 files excluded from artifact capture.'), findsOneWidget, ); expect( find.text( 'review · visit 2 · 3 files omitted by artifact capture limits.', ), findsOneWidget, ); expect(find.text('2 files excluded from artifact capture.'), findsNothing); }); testWidgets('a focused visit header states its own notes without a prefix', ( tester, ) async { await _pumpGallery(tester, _Platform.material, [ step( stage: 'review', visit: 1, artifacts: [_recorded('result.zip')], skipped: 2, truncated: 3, ), step( stage: 'review', sequence: 2, visit: 2, artifacts: [_recorded('later.zip')], ), ], focusIndex: 0); expect( find.text('Artifacts · 1 recorded · partial capture'), findsOneWidget, ); expect( find.text('2 files excluded from artifact capture.'), findsOneWidget, ); expect( find.text('3 files omitted by artifact capture limits.'), findsOneWidget, ); expect(find.text('later.zip'), findsNothing); }); testWidgets('a complete capture adds no header notes', (tester) async { await _pumpGallery(tester, _Platform.material, [ step(stage: 'review', visit: 1, artifacts: [_recorded('result.zip')]), ]); expect(find.text('Artifact · 1 recorded'), findsOneWidget); expect(find.textContaining('files excluded'), findsNothing); expect(find.textContaining('files omitted'), findsNothing); }); for (final platform in _Platform.values) { testWidgets( '${platform.name}: a long run header scrolls with the gallery rows', (tester) async { // Ten partial visits at a phone surface: the header is taller than the // free space a fixed sibling could get without taking the list's. await _pumpGallery(tester, platform, [ for (var visit = 1; visit <= 10; visit += 1) step( stage: 'review', sequence: visit, visit: visit, artifacts: [_recorded('file$visit.zip')], skipped: 1, ), ], surface: const Size(360, 640)); expect(tester.takeException(), isNull); final list = find.byType(CustomScrollView); expect(tester.getSize(list).height, greaterThan(0)); // The header belongs to that scroll view, so it scrolls away instead of // shrinking the list. expect( find.descendant( of: list, matching: find.text('Artifacts · 10 recorded · partial capture'), ), findsOneWidget, ); await tester.scrollUntilVisible(find.text('file10.zip'), 200); expect(find.text('file10.zip'), findsOneWidget); }, ); testWidgets( '${platform.name}: focused notes at 2x text keep the artifact list', (tester) async { await _pumpGallery( tester, platform, [ step( stage: 'review', visit: 1, artifacts: [ _recorded('result.zip', url: '/artifacts/1/1/result.zip'), ], skipped: 12, truncated: 34, ), ], focusIndex: 0, surface: const Size(320, 568), textScale: 2, ); expect(tester.takeException(), isNull); expect( tester.getSize(find.byType(CustomScrollView)).height, greaterThan(0), ); expect( find.text('12 files excluded from artifact capture.'), findsOneWidget, ); expect( find.text('34 files omitted by artifact capture limits.'), findsOneWidget, ); await tester.scrollUntilVisible(find.text('result.zip'), 200); expect(find.text('result.zip'), findsOneWidget); }, ); testWidgets( '${platform.name}: sheet actions keep Close, Copy, Open order', (tester) async { await _pumpGallery(tester, platform, [ step( stage: 'review', visit: 1, artifacts: [ _recorded('result.zip', url: '/artifacts/1/1/result.zip'), ], ), ], configuredEndpoint: 'ws://127.0.0.1:8081/connect'); await tester.tap(find.text('result.zip')); await tester.pumpAndSettle(); double orderOf(String label) { final offset = tester.getTopLeft(_inDialog(platform, label)); return platform == _Platform.material ? offset.dx : offset.dy; } expect(orderOf('Close'), lessThan(orderOf('Copy link'))); expect(orderOf('Copy link'), lessThan(orderOf('Open'))); }, ); } }