import 'package:flutter/material.dart'; import 'package:flutter_test/flutter_test.dart'; import 'run_detail_finders.dart'; import 'package:pipelines_app/features/artifacts/ui/artifact_tile.dart'; import 'package:pipelines_app/features/artifacts/ui/artifact_viewer_page.dart'; import 'package:pipelines_app/core/api_gateway.dart'; import 'package:pipelines_app/features/runs/data/runs_repository.dart'; import 'package:pipelines_app/features/runs/ui/run_detail_view.dart'; import 'package:pipelines_app/features/runs/ui/run_search_selection.dart'; import 'package:pipelines_app/features/runs/ui/runs_controller.dart'; import 'run_detail_graph_support.dart'; /// plan visits 5 and 1 around one build visit 2: the server's sequence is not /// the step's row index, so resolution must read the recorded sequence. JsonMap searchSelectionRun() => { 'id': 77, 'title': 'Search selection run', 'name': 'release', 'status': 'succeeded', 'input': {'prompt': 'ship it'}, 'output': {'result': 'ok'}, 'steps': [ { 'stage': 'plan', 'sequence': 5, 'status': 'completed', 'outcome': 'success', }, { 'stage': 'build', 'sequence': 2, 'status': 'completed', 'outcome': 'success', }, { 'stage': 'plan', 'sequence': 1, 'status': 'completed', 'outcome': 'success', }, ], }; /// Pumps one mounted detail under a mutable search scope, returning the notifier /// that applies a later request to the same detail state. Future> pumpLiveSelection( WidgetTester tester, RunDetailTestPlatform platform, RunSearchSelection selection, { VoidCallback? onConsumed, JsonMap? run, }) async { tester.view.physicalSize = const Size(900, 1000); tester.view.devicePixelRatio = 1; addTearDown(tester.view.reset); final gateway = RunDetailFakeGateway(run ?? searchSelectionRun()); final controller = RunsController(RunsRepository(gateway)); addTearDown(controller.dispose); await controller.select(77); final pending = ValueNotifier(selection); addTearDown(pending.dispose); final page = MediaQuery( data: MediaQueryData.fromView(tester.view), child: ValueListenableBuilder( valueListenable: pending, builder: (context, current, _) => RunSearchSelectionScope( selection: current, onConsumed: onConsumed ?? () {}, child: RunDetailView( controller: controller, gateway: gateway, onBack: () {}, ), ), ), ); await tester.pumpWidget(const SizedBox.shrink()); await tester.pumpWidget(MaterialApp(home: Scaffold(body: page))); await tester.pumpAndSettle(); return pending; } Future pumpSelection( WidgetTester tester, RunDetailTestPlatform platform, RunSearchSelection selection, { VoidCallback? onConsumed, }) async { await pumpLiveSelection(tester, platform, selection, onConsumed: onConsumed); } /// The browser's `searchFieldTab` mapping, independent of whether a detail is /// mounted: every text hit names a Summary, expanding only a current section. void _expectSummarySection(RunSearchTarget target, RunSummarySection? section) { expect(target, isA()); expect((target as RunSummaryTarget).section, section); } const _origin = 'http://127.0.0.1:8096'; void main() { for (final platform in const [RunDetailTestPlatform.material]) { testWidgets('${platform.name} artifact hit opens its recorded visit', ( tester, ) async { await pumpSelection( tester, platform, const RunSearchSelection(runId: 77, sequence: 2), ); expect(find.byKey(const Key('run-visit-pane')), findsOneWidget); expectVisitHeading(tester, 'build', 1); }); } /// The search fixture with one recorded artifact on build (sequence 2). /// /// The fake gateway configures no server, so the recorded URLs are absolute: /// a relative one would not resolve and the gallery would show its facts /// dialog instead of the viewer. The app-level search wiring test covers the /// server-relative URLs a real server records. JsonMap runWithArtifact() { final run = searchSelectionRun(); final steps = [ for (final step in run['steps'] as List) Map.from(step as Map), ]; steps[1]['artifacts'] = [ { 'name': 'release-report.txt', 'content_type': 'text/plain', 'size_bytes': 12, 'url': '$_origin/artifacts/77/2/release-report.txt', }, ]; return {...run, 'steps': steps}; } for (final platform in const [RunDetailTestPlatform.material]) { testWidgets('${platform.name} artifact hit opens that artifact over the ' 'Artifacts tab, and the timeline keeps working afterwards', ( tester, ) async { final pending = await pumpLiveSelection( tester, platform, const RunSearchSelection( runId: 77, sequence: 2, artifactUrl: '$_origin/artifacts/77/2/release-report.txt', ), run: runWithArtifact(), ); expect(find.byType(ArtifactViewerPage), findsOneWidget); expect(find.text('release-report.txt'), findsWidgets); // Back from the viewer lands on the run's Artifacts tab, the artifact // shown there selected, exactly as after tapping it in the gallery. Navigator.of(tester.element(find.byType(ArtifactViewerPage))).pop(); await tester.pumpAndSettle(); expect(find.byType(ArtifactViewerPage), findsNothing); expect(find.byKey(const Key('run-visit-pane')), findsNothing); expect(find.byKey(const ValueKey('run-detail-artifacts-77')), findsOne); final tile = tester.widget(find.byType(ArtifactTile)); expect(tile.artifact.url, '$_origin/artifacts/77/2/release-report.txt'); expect(tile.selected, isTrue); // Selecting another visit afterwards still opens it. pending.value = const RunSearchSelection(runId: 77, sequence: 5); await tester.pumpAndSettle(); expectVisitHeading(tester, 'plan', 2); }); } testWidgets('a missing artifact opens the Artifacts tab with a notice', ( tester, ) async { await pumpLiveSelection( tester, RunDetailTestPlatform.material, const RunSearchSelection( runId: 77, sequence: 2, artifactUrl: '$_origin/artifacts/77/2/gone.txt', ), run: runWithArtifact(), ); expect(find.byType(ArtifactViewerPage), findsNothing); expect(find.byKey(const ValueKey('run-detail-artifacts-77')), findsOne); expect(find.text('That artifact is no longer available.'), findsOneWidget); }); testWidgets('an artifact far down the gallery is scrolled into view', ( tester, ) async { final run = runWithArtifact(); final steps = [ for (final step in run['steps'] as List) Map.from(step as Map), ]; // Both plan visits list many files ahead of the build visit's, which // pushes the requested build artifact well below the fold. for (final index in [0, 2]) { final sequence = steps[index]['sequence']; steps[index]['artifacts'] = [ for (var i = 0; i < 30; i++) { 'name': 'filler-$i.bin', 'content_type': 'application/octet-stream', 'size_bytes': 1, 'url': '$_origin/artifacts/77/$sequence/filler-$i.bin', }, ]; } (steps[1]['artifacts'] as List).add({ 'name': 'late-notes.txt', 'content_type': 'text/plain', 'size_bytes': 3, 'url': '$_origin/artifacts/77/2/late-notes.txt', }); await pumpLiveSelection( tester, RunDetailTestPlatform.material, const RunSearchSelection( runId: 77, sequence: 2, artifactUrl: '$_origin/artifacts/77/2/late-notes.txt', ), run: {...run, 'steps': steps}, ); expect(find.byType(ArtifactViewerPage), findsOneWidget); Navigator.of(tester.element(find.byType(ArtifactViewerPage))).pop(); await tester.pumpAndSettle(); final late = find.byWidgetPredicate( (widget) => widget is ArtifactTile && widget.artifact.url == '$_origin/artifacts/77/2/late-notes.txt', ); expect(late, findsOneWidget); expect(tester.widget(late).selected, isTrue); final rect = tester.getRect(late); expect(rect.top, greaterThanOrEqualTo(0)); expect(rect.bottom, lessThanOrEqualTo(1000)); }); testWidgets('an unknown artifact sequence opens the plain run', ( tester, ) async { await pumpSelection( tester, RunDetailTestPlatform.material, const RunSearchSelection(runId: 77, sequence: 99), ); expect(find.byKey(const Key('run-visit-pane')), findsNothing); expect( find.byKey(const Key('run-detail-tab-body-summary')), findsOneWidget, ); }); for (final platform in RunDetailTestPlatform.values) { testWidgets('${platform.name} input field expands the input section', ( tester, ) async { await pumpSelection( tester, platform, const RunSearchSelection(runId: 77, sequence: 5, field: 'input'), ); expect( find.byKey(const Key('run-detail-tab-body-summary')), findsOneWidget, ); expect(find.text('ship it'), findsOneWidget); // Output keeps its default: open, as every Summary opens. expect(find.text('ok'), findsOneWidget); }); testWidgets( '${platform.name} output field expands only the output section', (tester) async { await pumpSelection( tester, platform, const RunSearchSelection(runId: 77, sequence: 5, field: 'output'), ); expect( find.byKey(const Key('run-detail-tab-body-summary')), findsOneWidget, ); expect(find.text('ok'), findsOneWidget); expect(find.text('ship it'), findsNothing); }, ); } testWidgets('a retired field opens the plain Summary', (tester) async { await pumpSelection( tester, RunDetailTestPlatform.material, const RunSearchSelection(runId: 77, sequence: 5, field: 'evidence'), ); expect( find.byKey(const Key('run-detail-tab-body-summary')), findsOneWidget, ); // The plain Summary shows its default sections: Output open, Input closed. expect(find.text('ship it'), findsNothing); expect(find.text('ok'), findsOneWidget); }); for (final platform in RunDetailTestPlatform.values) { testWidgets( '${platform.name} retired field replaces an open visit with Summary', (tester) async { final pending = await pumpLiveSelection( tester, platform, const RunSearchSelection(runId: 77, sequence: 2), ); expect(find.byKey(const Key('run-visit-pane')), findsOneWidget); pending.value = const RunSearchSelection( runId: 77, sequence: 5, field: 'evidence', ); await tester.pumpAndSettle(); expect(find.byKey(const Key('run-visit-pane')), findsNothing); expect( find.byKey(const Key('run-detail-tab-body-summary')), findsOneWidget, ); // The Summary's default sections: Output open, Input closed. expect(find.text('ship it'), findsNothing); expect(find.text('ok'), findsOneWidget); }, ); } testWidgets('the search request is consumed once it has been applied', ( tester, ) async { var consumed = 0; await pumpSelection( tester, RunDetailTestPlatform.material, const RunSearchSelection(runId: 77, sequence: 2), onConsumed: () => consumed++, ); expect(find.byKey(const Key('run-visit-pane')), findsOneWidget); expect(consumed, 1); }); group('resolveRunSearchSelection', () { test('input and output name their Summary section', () { _expectSummarySection( resolveRunSearchSelection( const RunSearchSelection(runId: 77, field: 'input'), searchSelectionRun(), ), RunSummarySection.input, ); _expectSummarySection( resolveRunSearchSelection( const RunSearchSelection(runId: 77, field: 'output'), searchSelectionRun(), ), RunSummarySection.output, ); }); test('a retired or unknown field still names the plain Summary', () { for (final field in ['evidence', 'transcript', 'unknown']) { _expectSummarySection( resolveRunSearchSelection( RunSearchSelection(runId: 77, field: field), searchSelectionRun(), ), null, ); } }); test('a recorded sequence names its own detail row', () { final target = resolveRunSearchSelection( const RunSearchSelection(runId: 77, sequence: 2), searchSelectionRun(), ); expect(target, isA()); expect((target as RunVisitTarget).stageKey, 'build'); expect(target.timelineIndex, 1); }); test('an artifact selection names its recorded URL', () { final target = resolveRunSearchSelection( const RunSearchSelection( runId: 77, sequence: 2, artifactUrl: '$_origin/artifacts/77/2/release-report.txt', ), searchSelectionRun(), ); expect(target, isA()); final artifact = target as RunArtifactTarget; expect(artifact.url, '$_origin/artifacts/77/2/release-report.txt'); }); test('an unrecorded sequence falls back to the plain Summary', () { _expectSummarySection( resolveRunSearchSelection( const RunSearchSelection(runId: 77, sequence: 99), searchSelectionRun(), ), null, ); }); }); }