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/layout/linear_label_placement.dart'; import 'package:pipelines_app/features/graph/widgets/definition_graph_canvas.dart'; import 'package:pipelines_app/features/graph/widgets/definition_graph_view.dart'; import 'package:pipelines_app/features/graph/widgets/run_graph_canvas.dart'; import 'package:pipelines_app/features/graph/widgets/run_graph_view.dart'; import '../domain/graph_fixtures.dart'; const String _longKey = 'implementer-with-an-unusually-long-stage-label'; /// The browser probe's definition (tool/graph_canvas_probe.dart): a long /// wrapping stage, a fallback error, a retry self-loop and blocked/error /// routes to the terminals. JsonMap _probeGraph() => { 'stages': [ { 'key': 'plan', 'type': 'model', 'edges': {'success': 'build', 'error': '#'}, 'fallback_error': true, }, { 'key': 'build', 'type': 'model', 'edges': {'success': _longKey, 'retry': 'build'}, }, { 'key': _longKey, 'type': 'model', 'edges': {'success': 'audit', 'blocked': '#failure'}, }, { 'key': 'audit', 'type': 'model', 'edges': {'success': '#success', 'error': '#failure'}, }, ], }; /// A run that took plan -> build, retried build, then went on to the long /// stage, so taken labels carry order badges (`1`, `2`, `3`). JsonMap _probeRun() => { 'id': 7, 'steps': [ step(sequence: 1, stage: 'plan', outcome: 'success', target: 'build'), step(sequence: 2, stage: 'build', outcome: 'retry', target: 'build'), step(sequence: 3, stage: 'build', outcome: 'success', target: _longKey), step( sequence: 4, stage: _longKey, status: 'running', startedAt: '2026-10-21T10:00:00Z', ), ], 'current_stage': _longKey, 'executing': true, }; Widget _host(Widget child, double scale, double width) => MaterialApp( home: Scaffold( body: Builder( builder: (context) => MediaQuery( data: MediaQuery.of( context, ).copyWith(textScaler: TextScaler.linear(scale)), child: SizedBox(width: width, height: 700, child: child), ), ), ), ); /// Asserts no placed label comes within the clearance of any placed node or /// other label, using the on-screen rectangles of the canvas children. void _expectLabelsClear(WidgetTester tester, Finder canvas) { final scale = tester .widget(find.byType(InteractiveViewer)) .transformationController! .value .getMaxScaleOnAxis(); final placed = tester.widgetList( find.descendant(of: canvas, matching: find.byType(Positioned)), ); final nodes = []; final labels = []; for (final positioned in placed) { final rect = tester.getRect(find.byWidget(positioned.child).first); // Nodes are placed with their measured box, labels by origin only; the // edge layer fills the whole canvas and is neither. if (positioned.width != null) { nodes.add(rect); } else if (positioned.right == null && positioned.bottom == null) { labels.add(rect); } } expect(nodes, isNotEmpty); expect(labels, isNotEmpty); // Slightly under the graph-space clearance, for float rounding. final margin = (edgeLabelClearance - 0.01) * scale; for (final label in labels) { final grown = label.inflate(margin); for (final node in nodes) { expect( grown.overlaps(node), isFalse, reason: 'label $label overlaps node $node', ); } for (final other in labels) { if (other == label) continue; expect( grown.overlaps(other), isFalse, reason: 'label $label overlaps label $other', ); } } } void main() { for (final scale in const [1.0, 2.0]) { for (final width in const [560.0, 1200.0]) { testWidgets('definition labels clear nodes at ${scale}x, $width wide', ( tester, ) async { await tester.pumpWidget( _host( DefinitionGraphView(graph: _probeGraph(), onSelected: (_) {}), scale, width, ), ); await tester.pump(); await tester.pump(); _expectLabelsClear(tester, find.byType(DefinitionGraphCanvas)); expect(tester.takeException(), isNull); }); testWidgets('run labels with order badges clear nodes at ${scale}x, ' '$width wide', (tester) async { await tester.pumpWidget( _host( RunGraphView( graph: _probeGraph(), run: _probeRun(), onStageSelected: (_) {}, now: () => DateTime.utc(2026, 10, 21, 10, 2), ), scale, width, ), ); await tester.pump(); await tester.pump(); expect(find.byType(RunRouteLabel), findsWidgets); _expectLabelsClear(tester, find.byType(RunGraphCanvas)); expect(tester.takeException(), isNull); }); } } }