diff --git a/app/lib/application/pipelines_app.dart b/app/lib/application/pipelines_app.dart index f3d79a9c..462ba921 100644 --- a/app/lib/application/pipelines_app.dart +++ b/app/lib/application/pipelines_app.dart @@ -212,6 +212,7 @@ class _PipelinesAppState extends State { onRunStarted: _runStarted, initialPipelineId: route.pipelineId, initialVersion: route.version, + initialStageKey: route.stageKey, ), AppDestination.runs: (_) => RunsDestination( session: _session, diff --git a/app/lib/application/pipelines_destination.dart b/app/lib/application/pipelines_destination.dart index 6e653a14..e7c492e1 100644 --- a/app/lib/application/pipelines_destination.dart +++ b/app/lib/application/pipelines_destination.dart @@ -21,6 +21,7 @@ class PipelinesDestination extends StatelessWidget { this.onRunStarted, this.initialPipelineId, this.initialVersion = 0, + this.initialStageKey, }); final AppSession session; @@ -38,6 +39,9 @@ class PipelinesDestination extends StatelessWidget { final int? initialPipelineId; final int initialVersion; + /// The definition stage a deep link names, or null for no stage request. + final String? initialStageKey; + @override Widget build(BuildContext context) => AnimatedBuilder( animation: session, @@ -75,6 +79,7 @@ class PipelinesDestination extends StatelessWidget { onRunStarted: onRunStarted, initialPipelineId: initialPipelineId, initialVersion: initialVersion, + initialStageKey: initialStageKey, // A reconnect or an accepted template create both advance this counter; // either change re-reads through the mounted controller. syncToken: session.connectionEpoch + refreshToken, diff --git a/app/lib/features/pipelines/screen.dart b/app/lib/features/pipelines/screen.dart index d4b55191..c7de8828 100644 --- a/app/lib/features/pipelines/screen.dart +++ b/app/lib/features/pipelines/screen.dart @@ -2,6 +2,7 @@ import 'package:flutter/cupertino.dart'; import 'package:flutter/material.dart'; import '../../core/api_gateway.dart'; +import 'pipeline_models.dart'; import 'pipeline_repository.dart'; import 'pipelines_controller.dart'; import 'widgets/pipeline_controls.dart'; @@ -29,6 +30,7 @@ class PipelinesScreen extends StatefulWidget { this.onRunStarted, this.initialPipelineId, this.initialVersion = 0, + this.initialStageKey, }); final ApiGateway gateway; @@ -50,6 +52,13 @@ class PipelinesScreen extends StatefulWidget { /// The pinned version for [initialPipelineId]; 0 means latest. final int initialVersion; + /// The definition stage the route names for [initialPipelineId], or null. + /// + /// The detail opens its Graph tab with this stage selected and its definition + /// pane; a key the pinned version does not declare opens the Graph tab with + /// nothing selected. Null keeps the ordinary default tab. + final String? initialStageKey; + @override State createState() => _PipelinesScreenState(); } @@ -57,6 +66,16 @@ class PipelinesScreen extends StatefulWidget { class _PipelinesScreenState extends State { late PipelinesController _controller; + /// Counts route stage requests so a re-read detail cannot replay an old one. + int _stageRequest = 0; + + /// Whether the route's stage request has been handed to its named pin. + /// + /// The screen, not the detail panel, owns this because the narrow layout + /// destroys the panel when it returns to the list: a deep link is a one-shot + /// intent, so coming back to the same pipeline later keeps the ordinary tab. + bool _stageRequestDone = false; + @override void initState() { super.initState(); @@ -81,12 +100,15 @@ class _PipelinesScreenState extends State { // selected pipeline and requested version survive the reconnect. _controller.load(); } else if (oldWidget.initialPipelineId != widget.initialPipelineId || - oldWidget.initialVersion != widget.initialVersion) { + oldWidget.initialVersion != widget.initialVersion || + oldWidget.initialStageKey != widget.initialStageKey) { + _stageRequest++; + _stageRequestDone = false; final id = widget.initialPipelineId; - if (id != null) { - _controller.openPipeline(id, version: widget.initialVersion); - } else { + if (id == null) { _controller.clearSelection(); + } else if (!_isRoutePin(_controller.detail)) { + _controller.openPipeline(id, version: widget.initialVersion); } } } @@ -97,11 +119,47 @@ class _PipelinesScreenState extends State { super.dispose(); } + /// Whether [detail] is the pipeline and pin the route names. + /// + /// The shown detail must have been *requested* for the route's version, not + /// merely have resolved to it: a latest route (`initialVersion == 0`) never + /// matches a detail still displayed from a pinned request, so a route change + /// from a pin to latest re-reads latest. The web compares its requested + /// version the same way. A pinned route additionally requires the reply's own + /// concrete version. + bool _isRoutePin(PipelineDetail? detail) { + final id = widget.initialPipelineId; + if (detail == null || id == null || detail.id != id) return false; + final version = widget.initialVersion; + if (_controller.selectedVersion != version) return false; + return version <= 0 || detail.resolvedVersion == version; + } + + /// The route's stage request while it is still pending, or null once spent. + /// + /// The request names one stage of one pinned pipeline. It is handed to the + /// detail only until that pin's reply has been shown: the screen consumes the + /// request as soon as the first reply for the current selection arrives, so a + /// spent deep link cannot replay its pane when the operator later returns to + /// the same pipeline. The panel still scopes the request to its named + /// pipeline and version, which is what keeps it off an unrelated detail. + String? _takeStageKey() { + final key = widget.initialStageKey; + final target = widget.initialPipelineId; + if (key == null || key.isEmpty || target == null) return null; + if (_stageRequestDone) return null; + final detail = _controller.detail; + if (detail == null) return key; + _stageRequestDone = true; + return _isRoutePin(detail) ? key : null; + } + @override Widget build(BuildContext context) { return AnimatedBuilder( animation: _controller, builder: (context, _) { + final stageKey = _takeStageKey(); final body = LayoutBuilder( builder: (context, constraints) { final wide = constraints.maxWidth >= kPipelinesWideBreakpoint; @@ -112,6 +170,10 @@ class _PipelinesScreenState extends State { gateway: widget.gateway, showBack: true, onRunStarted: widget.onRunStarted, + stageKey: stageKey, + stagePipelineId: widget.initialPipelineId, + stageVersion: widget.initialVersion, + stageRequest: _stageRequest, ) : PipelineListPanel(controller: _controller); } @@ -130,6 +192,10 @@ class _PipelinesScreenState extends State { gateway: widget.gateway, showBack: false, onRunStarted: widget.onRunStarted, + stageKey: stageKey, + stagePipelineId: widget.initialPipelineId, + stageVersion: widget.initialVersion, + stageRequest: _stageRequest, ), ), ], diff --git a/app/lib/features/pipelines/widgets/pipeline_detail_panel.dart b/app/lib/features/pipelines/widgets/pipeline_detail_panel.dart index 5eef6f7f..1516bb23 100644 --- a/app/lib/features/pipelines/widgets/pipeline_detail_panel.dart +++ b/app/lib/features/pipelines/widgets/pipeline_detail_panel.dart @@ -3,6 +3,7 @@ import 'dart:math' as math; import 'package:flutter/material.dart'; import '../../../core/api_gateway.dart'; +import '../../graph/domain/graph_definition.dart'; import '../../graph/widgets/stage_definition_view.dart'; import '../pipeline_models.dart'; import '../pipeline_query.dart'; @@ -30,7 +31,14 @@ class PipelineDetailPanel extends StatefulWidget { required this.gateway, required this.showBack, this.onRunStarted, - }); + this.stageKey, + this.stagePipelineId, + this.stageVersion = 0, + this.stageRequest = 0, + }) : assert( + stageKey == null || stagePipelineId != null, + 'a routed stage names the pipeline it belongs to', + ); final PipelinesController controller; final ApiGateway gateway; @@ -39,6 +47,26 @@ class PipelineDetailPanel extends StatefulWidget { /// A run started from this detail's Run action; receives the server's id. final ValueChanged? onRunStarted; + /// The stage the route names for this detail, or null when it names none. + final String? stageKey; + + /// The pipeline [stageKey] belongs to; the request never applies to another. + final int? stagePipelineId; + + /// The version [stageKey] was named for; 0 asks for latest. + /// + /// The request applies only to a detail requested for this version, so a + /// later version switch on this screen cannot reopen it against a different + /// definition, and a latest request never inherits a pinned reply. + final int stageVersion; + + /// Identifies one route stage request; 0 is the initial mount. + /// + /// A different pipeline, version or key advances it, so one request opens one + /// definition pane even though the pinned detail may be re-read in place. A + /// later version switch from this screen is not a new route request. + final int stageRequest; + @override State createState() => _PipelineDetailPanelState(); } @@ -55,6 +83,9 @@ class _PipelineDetailPanelState extends State { /// version is a different definition, so its mark is cleared. String? _graphPin; + /// The route stage request already applied, or -1 before the first detail. + int _appliedStageRequest = -1; + PipelinesController get _controller => widget.controller; @override @@ -62,6 +93,7 @@ class _PipelineDetailPanelState extends State { final detail = _controller.detail; if (detail == null) return _withoutDetail(context); _retainGraphSelection(detail); + _applyStageRequest(context, detail); return LayoutBuilder( builder: (context, constraints) { final headerLimit = _headerLimit(constraints); @@ -149,6 +181,62 @@ class _PipelineDetailPanelState extends State { _graphSelection = null; } + /// Applies the route's stage request to the pinned [detail]. + /// + /// The request belongs to the pipeline and version the URL names, so a detail + /// for anything else is left alone: opening another pipeline must not inherit + /// a deep link the operator has navigated away from. Over the named pin, the + /// Graph tab opens for the requested key. A key the pinned definition + /// declares selects that node and opens its landed definition pane for the + /// pinned version; a key it does not declare opens the Graph tab with nothing + /// selected and no pane, so no stage is reconstructed. A reply without a + /// graph has nothing to declare, so it follows the same no-selection path. + void _applyStageRequest(BuildContext context, PipelineDetail detail) { + final stageKey = widget.stageKey; + if (stageKey == null || stageKey.isEmpty) return; + if (_appliedStageRequest == widget.stageRequest) return; + if (!_namesShownPin(detail)) return; + _appliedStageRequest = widget.stageRequest; + _tab = PipelineDetailTab.graph; + final graph = detail.graph; + final declared = + graph != null && graphNodes(graph).any((node) => node.key == stageKey); + _graphSelection = declared ? stageKey : null; + if (graph == null || !declared) return; + // A page cannot be pushed during build: open it after this frame, and only + // while this exact pinned reply is still the one shown. + WidgetsBinding.instance.addPostFrameCallback((_) { + if (!mounted || _appliedStageRequest != widget.stageRequest) return; + final shown = _controller.detail; + if (shown == null || + shown.id != detail.id || + shown.resolvedVersion != detail.resolvedVersion) { + return; + } + openStageDefinition( + context, + graph: graph, + stageKey: stageKey, + pipelineId: detail.id, + pipelineName: detail.name, + version: detail.resolvedVersion, + ); + }); + } + + /// Whether [detail] is the pipeline and pin this route stage request names. + /// + /// The shown detail must be the reply to the route's own request, so a + /// request for latest (`stageVersion == 0`) never applies to a detail still + /// displayed from a pinned request. A pinned request also requires the + /// reply's concrete version. + bool _namesShownPin(PipelineDetail detail) { + if (detail.id != widget.stagePipelineId) return false; + final version = widget.stageVersion; + if (_controller.selectedVersion != version) return false; + return version <= 0 || detail.resolvedVersion == version; + } + /// Selects a node and opens the landed definition page for the pinned graph. void _openGraphStage( BuildContext context, diff --git a/app/test/application/pipeline_stage_deep_link_test.dart b/app/test/application/pipeline_stage_deep_link_test.dart new file mode 100644 index 00000000..afd07e4f --- /dev/null +++ b/app/test/application/pipeline_stage_deep_link_test.dart @@ -0,0 +1,94 @@ +import 'dart:convert'; + +import 'package:flutter/material.dart'; +import 'package:flutter_test/flutter_test.dart'; +import 'package:pipelines_app/application/pipelines_app.dart'; +import 'package:pipelines_app/core/transport/pipelines_client.dart'; +import 'package:pipelines_app/features/graph/widgets/stage_definition_view.dart'; + +import '../core/transport/pipelines_client_test.dart' show FakeChannel; +import '../features/pipelines/fixtures.dart'; +import 'pipelines_app_test.dart' show webServer; +import 'pipelines_replies.dart'; + +/// Answers every outstanding `get_pipeline` request. +/// +/// A deep-linked mount asks twice (the route's initial open, then the list +/// read's in-place reload), and the transport serializes requests, so a single +/// reply would leave the second read unanswered. +Future _replyEveryDetail( + WidgetTester tester, + FakeChannel channel, + PipelineFixture fixture, +) async { + final answered = {}; + for (var round = 0; round < 8; round++) { + await tester.pump(const Duration(milliseconds: 10)); + final pending = channel.outgoing + .map((frame) => jsonDecode(frame as String) as Map) + .where((frame) => frame['type'] == 'get_pipeline') + .where((frame) => !answered.contains(frame['id'])) + .toList(growable: false); + if (pending.isEmpty) continue; + for (final request in pending) { + answered.add(request['id']); + final detail = fixture.detailFor( + request['pipeline_id'] as int, + request['version'] as int, + )!; + channel.incoming.add( + jsonEncode({'id': request['id'], 'type': 'pipeline', 'detail': detail}), + ); + } + await tester.pump(); + } +} + +/// The application route carries a stage key all the way to the landed pane. +void main() { + testWidgets('the stage route opens the pinned definition through the app', ( + tester, + ) async { + tester.view.physicalSize = const Size(1100, 900); + tester.view.devicePixelRatio = 1; + addTearDown(tester.view.resetPhysicalSize); + addTearDown(tester.view.resetDevicePixelRatio); + + final fixture = PipelineFixture(); + final channel = FakeChannel()..readyCompleter.complete(); + final client = PipelinesClient(channelFactory: (_) => channel); + final provider = PlatformRouteInformationProvider( + initialRouteInformation: RouteInformation( + uri: Uri.parse('/pipelines/1/1/stage/review'), + ), + ); + addTearDown(provider.dispose); + + await tester.pumpWidget( + PipelinesApp( + server: webServer(), + client: client, + cupertino: false, + routeInformationProvider: provider, + ), + ); + await tester.pump(); + await replyPipelines(tester, channel, fixture); + await tester.pump(); + await _replyEveryDetail(tester, channel, fixture); + await tester.pumpAndSettle(); + + // The route names pipeline 1's v1 `review` stage; the app's wiring must + // reach the same authored definition the screen opens for that pin. + final pane = tester.widget( + find.byType(StageDefinitionView), + ); + expect(pane.stageKey, 'review'); + expect(pane.pipelineId, 1); + expect(pane.pipelineName, 'Alpha review'); + expect(pane.version, 1); + expect(pane.graph, reviewGraphV1()); + expect(find.text('review definition'), findsOneWidget); + expect(find.text('Alpha review · v1'), findsOneWidget); + }); +} diff --git a/app/test/features/pipelines/pipeline_deep_link_test.dart b/app/test/features/pipelines/pipeline_deep_link_test.dart index 188585f9..3b0ec326 100644 --- a/app/test/features/pipelines/pipeline_deep_link_test.dart +++ b/app/test/features/pipelines/pipeline_deep_link_test.dart @@ -1,10 +1,106 @@ +import 'dart:async'; + 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/graph/widgets/definition_graph_view.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_tabs.dart'; import 'fixtures.dart'; import 'test_helpers.dart'; +Future _pumpPipelines( + WidgetTester tester, { + required FakeGateway gateway, + int? pipelineId, + int version = 0, + String? stageKey, + Size size = const Size(1100, 900), +}) async { + tester.view.physicalSize = size; + tester.view.devicePixelRatio = 1; + addTearDown(tester.view.reset); + await tester.pumpWidget( + MaterialApp( + home: PipelinesScreen( + gateway: gateway, + initialPipelineId: pipelineId, + initialVersion: version, + initialStageKey: stageKey, + ), + ), + ); + await tester.pumpAndSettle(); +} + +Map _alpha(int version) => pipelineDetail( + id: 1, + name: 'Alpha review', + version: version, + latestVersion: 2, + stages: 2, + graph: version == 1 ? reviewGraphV1() : reviewGraph(), + versions: [versionSummary(1), versionSummary(2)], +); + +Map _beta() => pipelineDetail( + id: 2, + name: 'Beta deploy', + version: 1, + latestVersion: 1, + graph: reviewGraph(), + versions: [versionSummary(1)], +); + +JsonMap _detailReply(JsonMap fields) { + if (fields['pipeline_id'] != 1) return pipelineDetailReply(_beta()); + final version = fields['version'] as int? ?? 0; + return pipelineDetailReply(_alpha(version > 0 ? version : 2)); +} + +/// Two pipelines that both declare `review`, so a pane on Beta can only come +/// from a leaked route request rather than from the graph itself. +FakeGateway _twoReviewPipelines() => FakeGateway( + (type, fields) => switch (type) { + 'list_pipelines' => pipelineListReply([ + pipelineSummary(id: 1, name: 'Alpha review', version: 2), + pipelineSummary(id: 2, name: 'Beta deploy'), + ]), + 'list_runs' => runListReply(const >[]), + 'get_pipeline' => _detailReply(fields), + _ => errorReply('unexpected $type'), + }, +); + +/// Alpha whose latest pin is v5, so "latest" and "v1" are visibly different +/// definitions. A reply can therefore prove which version was requested. +FakeGateway _versionedAlpha() => FakeGateway( + (type, fields) => switch (type) { + 'list_pipelines' => pipelineListReply([ + pipelineSummary(id: 1, name: 'Alpha review', version: 5), + ]), + 'list_runs' => runListReply(const >[]), + 'get_pipeline' => pipelineDetailReply(_alphaReplyFor(fields)), + _ => errorReply('unexpected $type'), + }, +); + +Map _alphaReplyFor(JsonMap fields) { + final requested = fields['version'] as int? ?? 0; + final version = requested > 0 ? requested : 5; + return pipelineDetail( + id: 1, + name: 'Alpha review', + version: version, + latestVersion: 5, + stages: version == 1 ? 1 : 2, + graph: version == 1 ? reviewGraphV1() : reviewGraph(), + versions: [versionSummary(1), versionSummary(5)], + ); +} + /// A deep link opens the pipeline and version it names, not the list. void main() { testWidgets('an initial pipeline route opens that pinned detail', ( @@ -12,33 +108,376 @@ void main() { ) async { final fixture = PipelineFixture(); final gateway = gatewayFor(fixture); - tester.view.physicalSize = const Size(1100, 900); - tester.view.devicePixelRatio = 1; - addTearDown(tester.view.resetPhysicalSize); - addTearDown(tester.view.resetDevicePixelRatio); - await tester.pumpWidget( - MaterialApp( - home: PipelinesScreen( - gateway: gateway, - initialPipelineId: 1, - initialVersion: 2, - ), - ), - ); - await tester.pumpAndSettle(); + await _pumpPipelines(tester, gateway: gateway, pipelineId: 1, version: 2); // The detail is open (not the "Select a pipeline." placeholder), and the // detail request names the route's pinned version. expect(find.text('Select a pipeline.'), findsNothing); expect(find.text('Alpha review'), findsWidgets); expect( - gateway.callsOf('get_pipeline').where( - (call) => - '${call.fields['pipeline_id']}' == '1' && - '${call.fields['version']}' == '2', - ), + gateway + .callsOf('get_pipeline') + .where( + (call) => + '${call.fields['pipeline_id']}' == '1' && + '${call.fields['version']}' == '2', + ), isNotEmpty, ); + // No stage key: the detail keeps its ordinary default tab. + expect( + tester + .widget(find.byType(PipelineDetailTabs)) + .selected, + PipelineDetailTab.summary, + ); + expect(find.byType(DefinitionGraphView), findsNothing); + expect(find.byType(StageDefinitionView), findsNothing); + }); + + testWidgets('a routed stage opens its authored definition for the pin', ( + tester, + ) async { + final gateway = gatewayFor(PipelineFixture()); + + await _pumpPipelines( + tester, + gateway: gateway, + pipelineId: 1, + version: 1, + stageKey: 'review', + ); + + final pane = tester.widget( + find.byType(StageDefinitionView), + ); + expect(pane.stageKey, 'review'); + expect(pane.pipelineId, 1); + expect(pane.pipelineName, 'Alpha review'); + expect(pane.version, 1); + expect(pane.graph, reviewGraphV1()); + // The pane is the landed definition view: the authored prompt for v1, its + // pinned identity, and no invented run evidence. + expect(find.text('review definition'), findsOneWidget); + expect(find.text('Alpha review · v1'), findsOneWidget); + expect(find.text('Review the change.'), findsWidgets); + expect( + gateway + .callsOf('get_pipeline') + .where( + (call) => + '${call.fields['pipeline_id']}' == '1' && + '${call.fields['version']}' == '1', + ), + isNotEmpty, + ); + + // Back lands on the Graph tab with the same pinned node selected. + await tester.pageBack(); + await tester.pumpAndSettle(); + expect(find.byType(StageDefinitionView), findsNothing); + expect( + tester + .widget(find.byType(PipelineDetailTabs)) + .selected, + PipelineDetailTab.graph, + ); + expect( + tester + .widget(find.byType(DefinitionGraphView)) + .selectedStage, + 'review', + ); + }); + + testWidgets('an unknown routed stage opens the graph with no selection', ( + tester, + ) async { + final gateway = gatewayFor(PipelineFixture()); + + await _pumpPipelines( + tester, + gateway: gateway, + pipelineId: 1, + version: 2, + stageKey: 'not_a_stage', + ); + + expect( + tester + .widget(find.byType(PipelineDetailTabs)) + .selected, + PipelineDetailTab.graph, + ); + expect(find.byType(DefinitionGraphView), findsOneWidget); + expect( + tester + .widget(find.byType(DefinitionGraphView)) + .selectedStage, + isNull, + ); + expect(find.byType(StageDefinitionView), findsNothing); + expect(find.text(unknownStageDefinition), findsNothing); + }); + + testWidgets('another pipeline never inherits the routed stage', ( + tester, + ) async { + // Narrow: Back to the list destroys the detail, then the operator opens a + // pipeline the URL never named. + await _pumpPipelines( + tester, + gateway: _twoReviewPipelines(), + pipelineId: 1, + version: 1, + stageKey: 'review', + size: const Size(390, 844), + ); + expect( + tester + .widget(find.byType(StageDefinitionView)) + .pipelineId, + 1, + ); + + await tester.pageBack(); + await tester.pumpAndSettle(); + await tester.tap(find.byTooltip('Back to pipelines')); + await tester.pumpAndSettle(); + expect(find.byKey(const Key('pipeline-list')), findsOneWidget); + + await tester.tap(find.byKey(const Key('pipeline-row-2'))); + await tester.pumpAndSettle(); + + expect( + tester + .widget(find.byType(PipelineDetailTabs)) + .selected, + PipelineDetailTab.summary, + reason: 'the request names pipeline 1, so pipeline 2 keeps its own tab', + ); + expect(find.byType(DefinitionGraphView), findsNothing); + expect(find.byType(StageDefinitionView), findsNothing); + }); + + testWidgets('a spent routed stage does not replay on its named pipeline', ( + tester, + ) async { + // Latest (`version: 0`) matches the row's own version, so only consuming + // the request stops a second pane; the pin alone would still match. + await _pumpPipelines( + tester, + gateway: _twoReviewPipelines(), + pipelineId: 1, + stageKey: 'review', + size: const Size(390, 844), + ); + expect( + tester + .widget(find.byType(StageDefinitionView)) + .pipelineId, + 1, + ); + + await tester.pageBack(); + await tester.pumpAndSettle(); + await tester.tap(find.byTooltip('Back to pipelines')); + await tester.pumpAndSettle(); + await tester.tap(find.byKey(const Key('pipeline-row-1'))); + await tester.pumpAndSettle(); + + expect( + tester + .widget(find.byType(PipelineDetailTabs)) + .selected, + PipelineDetailTab.summary, + ); + expect(find.byType(DefinitionGraphView), findsNothing); + expect(find.byType(StageDefinitionView), findsNothing); + }); + + testWidgets('a late deep-link reply cannot open a pane over another choice', ( + tester, + ) async { + // Wide: pipeline 1's routed detail is still in flight when the operator + // opens pipeline 2. Its late reply must not apply the abandoned request. + final alphaReply = Completer(); + final gateway = FakeGateway( + (type, fields) => switch (type) { + 'list_pipelines' => pipelineListReply([ + pipelineSummary(id: 1, name: 'Alpha review', version: 2), + pipelineSummary(id: 2, name: 'Beta deploy'), + ]), + 'list_runs' => runListReply(const >[]), + 'get_pipeline' => + fields['pipeline_id'] == 1 + ? alphaReply.future + : pipelineDetailReply(_beta()), + _ => errorReply('unexpected $type'), + }, + ); + tester.view.physicalSize = const Size(1100, 900); + tester.view.devicePixelRatio = 1; + addTearDown(tester.view.reset); + await tester.pumpWidget( + MaterialApp( + home: PipelinesScreen( + gateway: gateway, + initialPipelineId: 1, + initialVersion: 1, + initialStageKey: 'review', + ), + ), + ); + await tester.pump(); + await tester.pump(); + expect(find.byKey(const Key('pipeline-row-2')), findsOneWidget); + + await tester.tap(find.byKey(const Key('pipeline-row-2'))); + await tester.pump(); + await tester.pump(); + expect(find.text('Beta deploy'), findsWidgets); + + alphaReply.complete(pipelineDetailReply(_alpha(1))); + await tester.pumpAndSettle(); + + expect( + tester + .widget(find.byType(PipelineDetailTabs)) + .selected, + PipelineDetailTab.summary, + ); + expect(find.byType(DefinitionGraphView), findsNothing); + expect(find.byType(StageDefinitionView), findsNothing); + }); + + testWidgets('a new stage key on the shown pin swaps the pane in place', ( + tester, + ) async { + final gateway = _twoReviewPipelines(); + await _pumpPipelines( + tester, + gateway: gateway, + pipelineId: 1, + stageKey: 'review', + ); + expect( + tester + .widget(find.byType(StageDefinitionView)) + .stageKey, + 'review', + ); + final reads = gateway.callsOf('get_pipeline').length; + expect(reads, greaterThan(0)); + + await tester.pageBack(); + await tester.pumpAndSettle(); + + // The app pushes another stage URL for the pin already on screen. + await tester.pumpWidget( + MaterialApp( + home: PipelinesScreen( + gateway: gateway, + initialPipelineId: 1, + initialStageKey: 'publish', + ), + ), + ); + await tester.pumpAndSettle(); + + final pane = tester.widget( + find.byType(StageDefinitionView), + ); + expect(pane.stageKey, 'publish'); + expect(pane.pipelineId, 1); + // The same pin is already shown, so the new request must not re-read it. + expect(gateway.callsOf('get_pipeline'), hasLength(reads)); + }); + + testWidgets('a same-pipeline route change to latest re-reads latest', ( + tester, + ) async { + // A latest route is not "whatever version is on screen": the pinned detail + // was requested for v1, so moving the route to latest must ask again. + final gateway = _versionedAlpha(); + await _pumpPipelines(tester, gateway: gateway, pipelineId: 1, version: 1); + expect(find.text('id 1 · v1 · 1 stage'), findsOneWidget); + + await tester.pumpWidget( + MaterialApp( + home: PipelinesScreen(gateway: gateway, initialPipelineId: 1), + ), + ); + await tester.pumpAndSettle(); + + expect( + gateway.callsOf('get_pipeline').last.fields['version'], + 0, + reason: 'the latest route re-reads latest instead of keeping the v1 pin', + ); + expect(find.text('id 1 · v5 · 2 stages'), findsWidgets); + }); + + testWidgets('a routed stage follows a route change from a pin to latest', ( + tester, + ) async { + final gateway = _versionedAlpha(); + await _pumpPipelines( + tester, + gateway: gateway, + pipelineId: 1, + version: 1, + stageKey: 'review', + ); + final pinned = tester.widget( + find.byType(StageDefinitionView), + ); + expect(pinned.version, 1); + expect(pinned.graph, reviewGraphV1()); + + await tester.pageBack(); + await tester.pumpAndSettle(); + + // The same pipeline and stage, now at latest. + await tester.pumpWidget( + MaterialApp( + home: PipelinesScreen( + gateway: gateway, + initialPipelineId: 1, + initialStageKey: 'review', + ), + ), + ); + await tester.pumpAndSettle(); + + expect(gateway.callsOf('get_pipeline').last.fields['version'], 0); + final pane = tester.widget( + find.byType(StageDefinitionView), + ); + expect(pane.stageKey, 'review'); + expect(pane.pipelineId, 1); + expect( + pane.version, + 5, + reason: 'a latest stage URL cannot open the stale v1 definition', + ); + expect(pane.graph, reviewGraph()); + + // Back lands on the latest definition's graph with the node selected. + await tester.pageBack(); + await tester.pumpAndSettle(); + expect( + tester + .widget(find.byType(PipelineDetailTabs)) + .selected, + PipelineDetailTab.graph, + ); + expect( + tester + .widget(find.byType(DefinitionGraphView)) + .selectedStage, + 'review', + ); }); } diff --git a/app/test/features/pipelines/pipeline_detail_stage_request_test.dart b/app/test/features/pipelines/pipeline_detail_stage_request_test.dart new file mode 100644 index 00000000..82a86805 --- /dev/null +++ b/app/test/features/pipelines/pipeline_detail_stage_request_test.dart @@ -0,0 +1,168 @@ +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/graph/widgets/stage_definition_view.dart'; +import 'package:pipelines_app/features/pipelines/pipeline_repository.dart'; +import 'package:pipelines_app/features/pipelines/pipelines_controller.dart'; +import 'package:pipelines_app/features/pipelines/widgets/pipeline_detail_panel.dart'; +import 'package:pipelines_app/features/pipelines/widgets/pipeline_tabs.dart'; + +import 'fixtures.dart'; + +/// A route stage request belongs to the pipeline and version it names. +void main() { + testWidgets('a stage request applies only to the pin it names', ( + tester, + ) async { + tester.view.physicalSize = const Size(1100, 900); + tester.view.devicePixelRatio = 1; + addTearDown(tester.view.reset); + + // Both fixtures declare `review`: the pipeline alone cannot explain a pane. + final gateway = FakeGateway( + (type, fields) => switch (type) { + 'get_pipeline' => pipelineDetailReply( + fields['pipeline_id'] == 1 + ? pipelineDetail( + id: 1, + name: 'Alpha review', + version: fields['version'] as int, + latestVersion: 2, + stages: 2, + graph: (fields['version'] as int) == 1 + ? reviewGraphV1() + : reviewGraph(), + versions: [versionSummary(1), versionSummary(2)], + ) + : pipelineDetail( + id: 2, + name: 'Beta deploy', + version: 1, + latestVersion: 1, + graph: reviewGraph(), + versions: [versionSummary(1)], + ), + ), + _ => errorReply('unexpected $type'), + }, + ); + final controller = PipelinesController(PipelinesRepository(gateway)); + addTearDown(controller.dispose); + + PipelineDetailTab shownTab() => tester + .widget(find.byType(PipelineDetailTabs)) + .selected; + + // The request names pipeline 1 v1, but the detail shows pipeline 2. + await controller.openPipeline(2); + await tester.pumpWidget( + MaterialApp( + home: Scaffold( + body: AnimatedBuilder( + animation: controller, + builder: (context, _) => PipelineDetailPanel( + controller: controller, + gateway: gateway, + showBack: true, + stageKey: 'review', + stagePipelineId: 1, + stageVersion: 1, + ), + ), + ), + ), + ); + await tester.pumpAndSettle(); + expect(shownTab(), PipelineDetailTab.summary); + expect(find.byType(StageDefinitionView), findsNothing); + + // The named pipeline at another version is a different definition too. + await controller.openPipeline(1, version: 2); + await tester.pumpAndSettle(); + expect(shownTab(), PipelineDetailTab.summary); + expect(find.byType(StageDefinitionView), findsNothing); + + // The exact pin the request names is where it applies, once. + await controller.openPipeline(1, version: 1); + await tester.pumpAndSettle(); + final pane = tester.widget( + find.byType(StageDefinitionView), + ); + expect(pane.stageKey, 'review'); + expect(pane.pipelineId, 1); + expect(pane.version, 1); + expect(find.text('review definition'), findsOneWidget); + }); + + testWidgets('a latest stage request waits for a latest reply', ( + tester, + ) async { + tester.view.physicalSize = const Size(1100, 900); + tester.view.devicePixelRatio = 1; + addTearDown(tester.view.reset); + + final gateway = FakeGateway( + (type, fields) => switch (type) { + 'get_pipeline' => pipelineDetailReply(_versionedDetail(fields)), + _ => errorReply('unexpected $type'), + }, + ); + final controller = PipelinesController(PipelinesRepository(gateway)); + addTearDown(controller.dispose); + + PipelineDetailTab shownTab() => tester + .widget(find.byType(PipelineDetailTabs)) + .selected; + + // The route asks for latest, but the panel currently shows a detail it + // requested for v1: latest is a different request, not "any version". + await controller.openPipeline(1, version: 1); + await tester.pumpWidget( + MaterialApp( + home: Scaffold( + body: AnimatedBuilder( + animation: controller, + builder: (context, _) => PipelineDetailPanel( + controller: controller, + gateway: gateway, + showBack: true, + stageKey: 'review', + stagePipelineId: 1, + stageVersion: 0, + ), + ), + ), + ), + ); + await tester.pumpAndSettle(); + expect(shownTab(), PipelineDetailTab.summary); + expect(find.byType(StageDefinitionView), findsNothing); + + // Once the latest reply is shown, the request applies to that pin. + await controller.openPipeline(1); + await tester.pumpAndSettle(); + final pane = tester.widget( + find.byType(StageDefinitionView), + ); + expect(pane.stageKey, 'review'); + expect(pane.pipelineId, 1); + expect(pane.version, 5); + expect(pane.graph, reviewGraph()); + }); +} + +/// Alpha whose latest is v5 and whose v1 definition differs, so the pane's +/// version proves which pin the request applied to. +Map _versionedDetail(JsonMap fields) { + final requested = fields['version'] as int? ?? 0; + final version = requested > 0 ? requested : 5; + return pipelineDetail( + id: 1, + name: 'Alpha review', + version: version, + latestVersion: 5, + stages: version == 1 ? 1 : 2, + graph: version == 1 ? reviewGraphV1() : reviewGraph(), + versions: [versionSummary(1), versionSummary(5)], + ); +}