import 'package:flutter/material.dart'; import 'package:flutter_test/flutter_test.dart'; import 'package:pipelines_app/features/graph/widgets/definition_graph_view.dart'; import 'package:pipelines_app/features/graph/widgets/graph_stage_node.dart'; import 'package:pipelines_app/features/graph/widgets/stage_definition_view.dart'; import 'package:pipelines_app/features/pipelines/screen.dart'; import 'package:pipelines_app/features/pipelines/widgets/pipeline_graph_tab.dart'; import 'fixtures.dart'; import 'test_helpers.dart'; Finder _node(String key) => find.byWidgetPredicate( (widget) => widget is GraphStageNode && widget.node.key == key, ); /// Taps a node, panning the stock canvas first when the node starts offscreen. Future _tapNode(WidgetTester tester, String key) async { final viewport = tester.getRect(find.byType(InteractiveViewer)); final node = tester.getRect(_node(key)); if (!viewport.contains(node.center)) { await tester.drag( find.byType(InteractiveViewer), viewport.center - node.center, ); await tester.pumpAndSettle(); } await tester.tap(_node(key)); await tester.pump(); } Future _pumpScreen( WidgetTester tester, Size size, { double scale = 1, FakeGateway? gateway, int syncToken = 0, }) async { tester.view.physicalSize = size; tester.view.devicePixelRatio = 1; addTearDown(tester.view.reset); await tester.pumpWidget( MaterialApp( builder: (context, child) => MediaQuery( data: MediaQuery.of( context, ).copyWith(textScaler: TextScaler.linear(scale)), child: child!, ), home: PipelinesScreen( gateway: gateway ?? gatewayFor(PipelineFixture()), syncToken: syncToken, ), ), ); await tester.pumpAndSettle(); } Future _openGraphTab(WidgetTester tester, int pipelineId) async { await tester.tap(find.byKey(Key('pipeline-row-$pipelineId'))); await tester.pumpAndSettle(); // The detail header scrolls when content-driven title/tab height needs it // (the header gained the native Run action), so reach the tab like an // operator would instead of requiring it to fit without scrolling. await tester.ensureVisible(find.text('Graph')); await tester.pumpAndSettle(); await tester.tap(find.text('Graph')); await tester.pump(); await tester.pump(); } void main() { test('detail keeps the pinned graph verbatim without touching source', () { final graph = reviewGraph(); final detail = PipelineDetail.fromJson( pipelineDetail( id: 9, name: 'Pinned review', version: 4, latestVersion: 5, source: 'exact: authored source', stages: 2, versions: [versionSummary(4, stages: 2)], graph: graph, shape: graph, ), ); expect(detail.graph, graph); expect(detail.shape, graph); expect(detail.graph!['settings'], {'schedules': []}); expect((detail.graph!['stages'] as List).first['future_field'], { 'kept': true, }); expect(detail.source, 'exact: authored source'); expect(detail.versions.single.version, 4); expect(detail.resolvedVersion, 4); }); test('a reply without a graph keeps null instead of inventing one', () { final detail = PipelineDetail.fromJson( pipelineDetail(id: 2, name: 'Beta deploy', version: 1, latestVersion: 1), ); expect(detail.graph, isNull); expect(detail.shape, isNull); }); // The note must never take the canvas away: a short window (390x600), a short // wide split pane (1100x400) and a large accessibility text scale (3x) all // used to overflow the tab and lay the canvas out at zero height. for (final (size, scale) in <(Size, double)>[ (const Size(390, 844), 2), (const Size(390, 600), 2), (const Size(1100, 400), 2), (const Size(1100, 900), 3), ]) { testWidgets('graph tab keeps a usable canvas at ' '${size.width.toInt()}x${size.height.toInt()} @${scale}x', ( tester, ) async { await _pumpScreen(tester, size, scale: scale); await _openGraphTab(tester, 1); expect(tester.takeException(), isNull); expect(find.text(pipelineGraphNote), findsOneWidget); expect(find.byType(DefinitionGraphView), findsOneWidget); final tab = tester.getSize(find.byType(PipelineGraphTab)); final note = tester.getSize(find.byKey(const Key('pipeline-graph-note'))); final canvas = tester.getSize(find.byType(DefinitionGraphView)); expect(canvas.height, greaterThan(0)); expect( note.height, lessThanOrEqualTo(tab.height * 0.5 + 1), reason: 'the guidance note may never take more than half the tab', ); expect( canvas.height, greaterThanOrEqualTo(tab.height * 0.5 - 1), reason: 'the canvas keeps at least half of the tab slot', ); expect(canvas.height, lessThan(tab.height)); }); } testWidgets('an empty definition keeps its message in a short slot', ( tester, ) async { final detail = PipelineDetail.fromJson( pipelineDetail( id: 4, name: 'Empty definition', version: 1, latestVersion: 1, graph: {'name': 'Empty definition', 'stages': []}, ), ); await tester.pumpWidget( MaterialApp( home: Scaffold( body: Center( child: SizedBox( width: 390, height: 200, child: PipelineGraphTab( detail: detail, selectedStage: null, onSelected: (_) {}, ), ), ), ), ), ); await tester.pumpAndSettle(); expect(find.text(definitionGraphEmpty), findsOneWidget); expect(find.byType(GraphStageNode), findsNothing); expect(tester.takeException(), isNull); }); testWidgets('selecting a node opens its exact pinned definition', ( tester, ) async { await _pumpScreen(tester, const Size(1100, 900)); await _openGraphTab(tester, 1); await _tapNode(tester, 'review'); await tester.pumpAndSettle(); final stagePane = tester.widget( find.byType(StageDefinitionView), ); expect(stagePane.stageKey, 'review'); expect(stagePane.pipelineId, 1); expect(stagePane.pipelineName, 'Alpha review'); expect(stagePane.version, 2); expect(stagePane.graph, reviewGraph()); expect(find.text('review definition'), findsOneWidget); expect(find.text('Alpha review ยท v2'), findsOneWidget); await tester.pageBack(); await tester.pumpAndSettle(); expect(find.byType(StageDefinitionView), findsNothing); expect(tester.widget(_node('review')).selected, isTrue); // The selection belongs to the panel, so an in-place tab round trip keeps // the same node marked (the web keeps its graph selection across tabs). await tester.tap(find.text('Summary')); await tester.pumpAndSettle(); await tester.tap(find.text('Graph')); await tester.pump(); await tester.pump(); expect(tester.widget(_node('review')).selected, isTrue); // Terminals are definition targets too: they open the same pane with the // pinned identity, not a visit. await _tapNode(tester, '#success'); await tester.pumpAndSettle(); final terminalPane = tester.widget( find.byType(StageDefinitionView), ); expect(terminalPane.stageKey, '#success'); expect(terminalPane.pipelineId, 1); expect(terminalPane.version, 2); expect(find.text('Success definition'), findsOneWidget); await tester.pageBack(); await tester.pumpAndSettle(); expect(tester.takeException(), isNull); // The selection belongs to the pinned definition: changing version must not // carry a same-named node's mark into the other version. await tester.tap(find.text('Summary')); await tester.pumpAndSettle(); await tester.ensureVisible(find.byKey(const Key('pipeline-version-1'))); await tester.pumpAndSettle(); await tester.tap(find.byKey(const Key('pipeline-version-1'))); await tester.pumpAndSettle(); expect(find.text('Viewing v1 (latest v2).'), findsOneWidget); await tester.tap(find.text('Graph')); await tester.pump(); await tester.pump(); expect(tester.widget(_node('review')).selected, isFalse); expect( tester .widget(find.byType(DefinitionGraphView)) .graph, reviewGraphV1(), ); }); testWidgets('a reconnect that replaces the detail clears the old mark', ( tester, ) async { var latest = 2; final fixture = PipelineFixture(); final gateway = FakeGateway( (type, fields) => switch (type) { 'list_pipelines' => pipelineListReply(fixture.summaries), 'list_runs' => runListReply(fixture.runs), 'get_pipeline' => pipelineDetailReply( fixture.detailFor(fields['pipeline_id'] as int, latest)!, ), _ => errorReply('unexpected $type'), }, ); await _pumpScreen(tester, const Size(1100, 900), gateway: gateway); await _openGraphTab(tester, 1); await _tapNode(tester, 'review'); await tester.pumpAndSettle(); await tester.pageBack(); await tester.pumpAndSettle(); expect(tester.widget(_node('review')).selected, isTrue); // A reconnect reloads the selected detail in place (it is not nulled), so // this is the path the old tab-state guard existed for. The newer pin must // not inherit the previous version's mark. latest = 3; await _pumpScreen( tester, const Size(1100, 900), gateway: gateway, syncToken: 1, ); expect(find.byType(GraphStageNode), findsWidgets); expect(tester.widget(_node('review')).selected, isFalse); expect(tester.takeException(), isNull); }); testWidgets('a missing definition graph is stated, never drawn', ( tester, ) async { await _pumpScreen(tester, const Size(1100, 900)); await _openGraphTab(tester, 2); expect(find.text(pipelineGraphMissing), findsOneWidget); expect(find.byType(DefinitionGraphView), findsNothing); expect(find.byType(GraphStageNode), findsNothing); }); }