import 'package:flutter/material.dart'; import 'package:flutter_test/flutter_test.dart'; import 'package:pipelines_app/features/graph/painting/graph_edge_layer.dart'; import 'package:pipelines_app/features/graph/routing/route_models.dart'; import '../routing/routing_fixtures.dart'; import 'edge_layer_fixtures.dart'; void main() { const color = Colors.black87; final straight = edgeRoute( points: const [RoutePoint(10, 20), RoutePoint(90, 20)], ); final wrapping = edgeRoute( points: const [ RoutePoint(10, 20), RoutePoint(40, 20), RoutePoint(40, 60), RoutePoint(80, 60), RoutePoint(80, 50), ], ); group('path', () { test('keeps every orthogonal vertex of the supplied route', () { final painter = GraphEdgePainter( routes: [wrapping], color: color, strokeWidth: 2, ); final path = painter.linePath(wrapping); final metric = path.computeMetrics().single; expect(metric.length, closeTo(30 + 40 + 40 + 10, 0.001)); expect( metric.getTangentForOffset(0)!.position, routeOffset(wrapping.points.first), ); expect( metric.getTangentForOffset(metric.length)!.position, routeOffset(wrapping.points.last), ); expect(path.getBounds(), const Rect.fromLTRB(10, 20, 80, 60)); }); test('a straight route is the exact port-to-port segment', () { final painter = GraphEdgePainter( routes: [straight], color: color, strokeWidth: 1, ); final metric = painter.linePath(straight).computeMetrics().single; expect(metric.length, 80); expect(metric.getTangentForOffset(40)!.vector, const Offset(1, 0)); }); test('zero- and one-point routes produce an empty path', () { final painter = GraphEdgePainter( routes: const [], color: color, strokeWidth: 1, ); final empty = edgeRoute(points: const []); final single = edgeRoute(points: const [RoutePoint(5, 5)]); for (final route in [empty, single]) { final path = painter.linePath(route); expect(path.getBounds().isEmpty, isTrue); expect(path.computeMetrics().isEmpty, isTrue); } }); }); group('arrow', () { test('points into a forward target port', () { final painter = GraphEdgePainter( routes: [straight], color: color, strokeWidth: 2, ); final arrow = painter.arrowPath(straight); final tip = routeOffset(straight.points.last); expect( arrow.computeMetrics().first.getTangentForOffset(0)!.position, tip, ); // 12 long and 8 wide for a 2px stroke, pointing right. expect(arrow.getBounds(), const Rect.fromLTRB(78, 16, 90, 24)); expect(arrow.contains(tip.translate(-5, 0)), isTrue); expect(arrow.contains(tip.translate(2, 0)), isFalse); expect(arrow.contains(tip.translate(-14, 0)), isFalse); }); test('follows self-loop arrival instead of assuming a rightward edge', () { final routing = routeFor(selfLoopGraphFixture()); final loop = routing.routes.singleWhere( (route) => route.kind == RouteKind.selfLoop, ); final painter = GraphEdgePainter( routes: routing.routes, color: color, strokeWidth: 2, ); final direction = arrival(loop); final tip = routeOffset(loop.points.last); final arrow = painter.arrowPath(loop); expect(loop.targetPort.side, RoutePortSide.right); expect(direction.dx, -1); expect(arrow.getBounds().left, tip.dx); expect(arrow.contains(tip.translate(5, 0)), isTrue); expect(arrow.contains(tip.translate(-2, 0)), isFalse); }); test('a backward route keeps its leftward arrival into a right port', () { final back = edgeRoute( kind: RouteKind.back, targetSide: RoutePortSide.right, points: const [ RoutePoint(120, 20), RoutePoint(140, 20), RoutePoint(140, 60), RoutePoint(80, 60), ], ); final painter = GraphEdgePainter( routes: [back], color: color, strokeWidth: 2, ); final arrow = painter.arrowPath(back); final tip = routeOffset(back.points.last); expect(back.kind, RouteKind.back); expect(arrow.getBounds().left, tip.dx); expect(arrow.contains(tip.translate(5, 0)), isTrue); expect(arrow.contains(tip.translate(-2, 0)), isFalse); }); test('keeps the routed arrival direction for every cycle edge', () { final routing = routeFor(cycleGraphFixture()); final painter = GraphEdgePainter( routes: routing.routes, color: color, strokeWidth: 2, ); expect(routing.routes, isNotEmpty); for (final route in routing.routes) { final direction = arrival(route); final tip = routeOffset(route.points.last); final arrow = painter.arrowPath(route); expect( arrow.computeMetrics().first.getTangentForOffset(0)!.position, tip, ); expect(arrow.contains(tip - direction * 5), isTrue); expect(arrow.contains(tip + direction * 2), isFalse); } }); test('a routed forward edge ends exactly at its target port', () { final routing = routeFor(chainGraphFixture()); final painter = GraphEdgePainter( routes: routing.routes, color: color, strokeWidth: 2, ); expect(routing.routes, isNotEmpty); for (final route in routing.routes) { expect(route.points.last, route.targetPort.point); final arrow = painter.arrowPath(route); expect( arrow.computeMetrics().first.getTangentForOffset(0)!.position, routeOffset(route.targetPort.point), ); } }); test('anchors the tip at the target port, not the last route point', () { final route = EdgeRoute( id: 7, source: 'a', outcome: 'success', target: 'b', label: 'success', fallback: false, kind: RouteKind.forward, straight: true, sourcePort: const RoutePort( side: RoutePortSide.right, point: RoutePoint(10, 20), ), targetPort: const RoutePort( side: RoutePortSide.left, point: RoutePoint(85, 20), ), points: const [RoutePoint(10, 20), RoutePoint(80, 20)], ); final arrow = GraphEdgePainter( routes: [route], color: color, strokeWidth: 2, ).arrowPath(route); expect( arrow.computeMetrics().first.getTangentForOffset(0)!.position, const Offset(85, 20), ); }); test('scales with the caller stroke width', () { final small = GraphEdgePainter( routes: [straight], color: color, strokeWidth: 1, ).arrowPath(straight); final large = GraphEdgePainter( routes: [straight], color: color, strokeWidth: 4, ).arrowPath(straight); expect(small.getBounds().width, 6); expect(small.getBounds().height, 4); expect(large.getBounds().width, 24); expect(large.getBounds().height, 16); }); test('falls back to the port inward normal for a degenerate tail', () { final route = edgeRoute( points: const [RoutePoint(50, 50), RoutePoint(50, 50)], targetSide: RoutePortSide.top, ); final arrow = GraphEdgePainter( routes: [route], color: color, strokeWidth: 2, ).arrowPath(route); final tip = routeOffset(route.points.last); expect(arrow.getBounds(), const Rect.fromLTRB(46, 38, 54, 50)); expect(arrow.contains(tip.translate(0, -5)), isTrue); }); }); }