import 'package:flutter/gestures.dart'; import 'package:flutter/material.dart'; import 'package:flutter_test/flutter_test.dart'; import 'package:pipelines_app/features/graph/painting/edge_hit_test.dart'; import 'package:pipelines_app/features/graph/routing/route_models.dart'; import 'package:pipelines_app/features/graph/widgets/definition_graph_view.dart'; import 'package:pipelines_app/features/graph/widgets/graph_edge_hover.dart'; import 'package:pipelines_app/features/graph/widgets/run_graph_view.dart'; import '../domain/graph_fixtures.dart'; Widget _host(Widget graph) => MaterialApp( home: Scaffold( body: Align( alignment: Alignment.topLeft, child: SizedBox(width: 900, height: 600, child: graph), ), ), ); Future _settle(WidgetTester tester) async { await tester.pump(); await tester.pump(); await tester.pump(); } GraphEdgeHover _hover(WidgetTester tester) => tester.widget(find.byType(GraphEdgeHover).first); EdgeRoute _route(WidgetTester tester, String source, String outcome) => _hover(tester).routing.routes.firstWhere( (route) => route.source == source && route.outcome == outcome, ); /// A global point on [route] that hits only it: sampled along its segments, /// clear of nodes, labels and other routes. Offset _onRoute(WidgetTester tester, EdgeRoute route) { final hover = _hover(tester); final box = tester.renderObject(find.byType(GraphEdgeHover).first); for (var index = 0; index + 1 < route.points.length; index += 1) { final a = Offset(route.points[index].x, route.points[index].y); final b = Offset(route.points[index + 1].x, route.points[index + 1].y); for (final t in const [0.5, 0.3, 0.7, 0.2, 0.8]) { final point = Offset.lerp(a, b, t)!; final others = hover.routing.routes.where((r) => r.id != route.id); final clear = others.every((r) => distanceToRoute(r, point) > 10); final hit = hitTestEdge( routes: hover.routing.routes, labels: const [], nodes: hover.layout.nodes, point: point, tolerance: 1, ); final inLabel = hover.routing.labels.any( (l) => l.edgeId != route.id && Rect.fromLTWH( l.x, l.y, l.width, l.height, ).inflate(4).contains(point), ); if (clear && !inLabel && hit == route.id) return box.localToGlobal(point); } } throw StateError('no clear point on route ${route.id}'); } Finder _highlight(int id) => find.byKey(ValueKey('graph-edge-highlight:$id')); void main() { test('hit testing follows bends, prefers nodes and labels', () { final loop = EdgeRoute( id: 1, source: 'a', outcome: 'retry', target: 'a', label: 'retry', fallback: false, kind: RouteKind.values.first, straight: false, sourcePort: const RoutePort( side: RoutePortSide.top, point: RoutePoint(10, 0), ), targetPort: const RoutePort( side: RoutePortSide.top, point: RoutePoint(40, 0), ), points: const [ RoutePoint(10, 0), RoutePoint(10, -30), RoutePoint(40, -30), RoutePoint(40, 0), ], ); int? hit(Offset point, {double tolerance = 4}) => hitTestEdge( routes: [loop], labels: const [ EdgeLabelBox(edgeId: 1, x: 100, y: 100, width: 30, height: 12), ], nodes: const [], point: point, tolerance: tolerance, ); expect(distanceToRoute(loop, const Offset(25, -27)), 3); expect(hit(const Offset(25, -27)), 1, reason: 'the loop top is hit'); expect(hit(const Offset(13, -15)), 1, reason: 'the loop side is hit'); expect(hit(const Offset(25, -15)), isNull, reason: 'inside the loop'); expect(hit(const Offset(110, 105)), 1, reason: 'the label is the edge'); }); testWidgets('hovering a definition edge highlights it with a tooltip', ( tester, ) async { await tester.pumpWidget( _host(DefinitionGraphView(graph: linearGraph(), onSelected: (_) {})), ); await _settle(tester); final route = _route(tester, 'plan', 'success'); final mouse = await tester.createGesture(kind: PointerDeviceKind.mouse); await mouse.addPointer(location: Offset.zero); addTearDown(mouse.removePointer); await mouse.moveTo(_onRoute(tester, route)); await tester.pump(); expect(_highlight(route.id), findsOneWidget); expect(find.byKey(const Key('graph-edge-tooltip')), findsOneWidget); expect(find.text('plan → build\nOutcome: success'), findsOneWidget); // Over a node the edge is let go: nodes keep their own pointer. final node = _hover(tester).layout.nodeFor('build')!; final box = tester.renderObject(find.byType(GraphEdgeHover)); await mouse.moveTo( box.localToGlobal( Offset(node.x + node.width / 2, node.y + node.height / 2), ), ); await tester.pump(); expect(_highlight(route.id), findsNothing); expect(find.byKey(const Key('graph-edge-tooltip')), findsNothing); }); testWidgets('tapping a run edge pins it and counts how often it was taken', ( tester, ) async { final run = { 'steps': [ step(sequence: 1, stage: 'a', outcome: 'retry', target: 'a'), step(sequence: 2, stage: 'a', outcome: 'retry', target: 'a'), step(sequence: 3, stage: 'a', outcome: 'success', target: 'b'), step(sequence: 4, stage: 'b', status: 'running'), ], 'current_stage': 'b', 'executing': true, }; await tester.pumpWidget( _host( RunGraphView( graph: selfLoopGraph(), run: run, onStageSelected: (_) {}, showHeader: false, ), ), ); await _settle(tester); final loop = _route(tester, 'a', 'retry'); await tester.tapAt(_onRoute(tester, loop)); await tester.pump(); expect(_highlight(loop.id), findsOneWidget); expect(find.text('a → a\nOutcome: retry\nTaken 2 times'), findsOneWidget); final untaken = _route(tester, 'b', 'error'); await tester.tapAt(_onRoute(tester, untaken)); await tester.pump(); expect(_highlight(loop.id), findsNothing); expect(_highlight(untaken.id), findsOneWidget); expect(find.text('b → Failure\nOutcome: error\nNot taken'), findsOneWidget); // Tapping it again lets it go. await tester.tapAt(_onRoute(tester, untaken)); await tester.pump(); expect(_highlight(untaken.id), findsNothing); expect(find.byKey(const Key('graph-edge-tooltip')), findsNothing); }); testWidgets('edge labels name their edge for screen readers', (tester) async { final handle = tester.ensureSemantics(); await tester.pumpWidget( _host(DefinitionGraphView(graph: linearGraph(), onSelected: (_) {})), ); await _settle(tester); expect( find.bySemanticsLabel('build → ship\nOutcome: success'), findsOneWidget, ); handle.dispose(); }); }