Files
pipelines/app/test/features/graph/widgets/graph_label_clearance_test.dart
T
alan fb1935a62d Keep graph edge labels clear of nodes and other labels
The linear layout used by the definition and run graphs placed each
outcome label at the middle of its widest run with no collision check,
and the run graph measured only the label text while rendering an order
badge in front of it, so taken labels ran into the next node.

Labels now try positions along their own route (horizontal runs above
and below, sliding along, then vertical runs) and take the first whose
box stays 4px clear of every node and earlier label, preferring spots
off other wires. If a label finds no spot, the layout widens its column
gaps and lanes and runs again; a last-resort shelf below the drawing
never overlaps. The run graph measures the exact badge+label widget it
renders, keyed by edge id.
2026-10-31 16:36:48 +00:00

165 lines
5.2 KiB
Dart

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() => <String, dynamic>{
'stages': <dynamic>[
<String, dynamic>{
'key': 'plan',
'type': 'model',
'edges': <String, String?>{'success': 'build', 'error': '#'},
'fallback_error': true,
},
<String, dynamic>{
'key': 'build',
'type': 'model',
'edges': <String, String?>{'success': _longKey, 'retry': 'build'},
},
<String, dynamic>{
'key': _longKey,
'type': 'model',
'edges': <String, String?>{'success': 'audit', 'blocked': '#failure'},
},
<String, dynamic>{
'key': 'audit',
'type': 'model',
'edges': <String, String?>{'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() => <String, dynamic>{
'id': 7,
'steps': <dynamic>[
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<InteractiveViewer>(find.byType(InteractiveViewer))
.transformationController!
.value
.getMaxScaleOnAxis();
final placed = tester.widgetList<Positioned>(
find.descendant(of: canvas, matching: find.byType(Positioned)),
);
final nodes = <Rect>[];
final labels = <Rect>[];
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);
});
}
}
}