import 'package:flutter_test/flutter_test.dart'; import 'package:pipelines_app/features/graph/domain/graph_definition.dart'; import 'package:pipelines_app/features/graph/domain/graph_protocol.dart'; import 'package:pipelines_app/features/graph/routing/route_models.dart'; import '../layout/layout_fixtures.dart' as layout; import 'routing_assertions.dart'; import 'routing_fixtures.dart'; void main() { test('chain routes keep identities and use straight forward runs', () { final graph = chainGraphFixture(); final layoutBox = layoutFor(graph); final edges = graphEdges(graph); final labels = labelsFor(edges); final routing = routeFor(graph, labels: labels); expect(routing.skipped, isEmpty); expect( routing.routes.map((route) => route.id), edges.map((edge) => edge.id), ); for (var index = 0; index < edges.length; index += 1) { final edge = edges[index]; final route = routing.routes[index]; expect(route.source, edge.source); expect(route.outcome, edge.outcome); expect(route.target, edge.target); expect(route.label, edge.label); expect(route.fallback, edge.fallback); expect(route.kind, RouteKind.forward); expect(route.straight, isTrue); expect(route.points.length, 2); expect(route.sourcePort.side, RoutePortSide.right); expect(route.targetPort.side, RoutePortSide.left); } expectValidRouting(routing, layoutBox, extents: labels); }); test('diamond fan-out and fan-in avoid every node interior', () { final graph = diamondGraphFixture(); final layoutBox = layoutFor(graph); final labels = labelsFor(graphEdges(graph)); final routing = routeFor(graph, labels: labels); expect(routing.skipped, isEmpty); expect(routing.routes.length, graphEdges(graph).length); final fromA = routing.routes.where((route) => route.source == 'a'); expect(fromA.length, 3); expect( fromA.where((route) => route.straight).length, greaterThanOrEqualTo(1), ); final longForward = fromA.singleWhere( (route) => route.target == failureEnd, ); expect(longForward.kind, RouteKind.forward); expect(longForward.straight, isFalse); expect(longForward.points.length, greaterThan(4)); expectValidRouting(routing, layoutBox, extents: labels); }); test('parallel outcomes to one target get separate corridors and labels', () { final graph = parallelOutcomesGraph(); final layoutBox = layoutFor(graph); final labels = labelsFor(graphEdges(graph)); final routing = routeFor(graph, labels: labels); final parallel = routing.routes .where((route) => route.source == 'a' && route.target == 'b') .toList(); expect(parallel.length, 3); final ports = { for (final route in parallel) '${route.sourcePort.point.y}/${route.targetPort.point.y}', }; expect(ports.length, 3, reason: 'each outcome has its own port pair'); final labelBoxes = [ for (final route in parallel) routing.labelFor(route.id)!, ]; expect(labelBoxes.map((box) => box.y).toSet().length, greaterThan(1)); expectValidRouting(routing, layoutBox, extents: labels); }); test('a same-rank cycle uses distinct lanes around both members', () { final graph = cycleGraphFixture(); final layoutBox = layoutFor(graph); final labels = labelsFor(graphEdges(graph)); final routing = routeFor(graph, labels: labels); final sameRank = routing.routes .where((route) => route.kind == RouteKind.sameRank) .toList(); expect(sameRank.length, 2); expect(sameRank.map((route) => route.target).toSet(), {'a', 'b'}); for (final route in sameRank) { expect(route.straight, isFalse); expect(route.sourcePort.side, RoutePortSide.right); expect(route.targetPort.side, RoutePortSide.right); expect( route.points.map((point) => point.y).toSet().length, greaterThan(2), ); } expectValidRouting(routing, layoutBox, extents: labels); }); test('a self loop expands bounds and keeps both ports on its border', () { final graph = selfLoopOnlyGraph(); final layoutBox = layoutFor(graph); final labels = labelsFor(graphEdges(graph)); final routing = routeFor(graph, labels: labels); final route = routing.routes.single; expect(route.kind, RouteKind.selfLoop); expect(route.straight, isFalse); expect(route.points.length, greaterThanOrEqualTo(4)); expect(route.sourcePort.side, RoutePortSide.right); expect(route.targetPort.side, RoutePortSide.right); expect(route.sourcePort.point.y, isNot(route.targetPort.point.y)); expect(routing.width, greaterThan(layoutBox.width)); expect(routing.height, greaterThan(layoutBox.height)); expectValidRouting(routing, layoutBox, extents: labels); }); test('disconnected stages route only their declared edges', () { final graph = disconnectedGraphFixture(); final layoutBox = layoutFor(graph); final labels = labelsFor(graphEdges(graph)); final routing = routeFor(graph, labels: labels); expect(routing.skipped, isEmpty); expect(routing.routes.length, 2); expect(routing.routes.map((route) => route.source).toSet(), { 'a', 'b', }); expect(layoutBox.nodeFor('c'), isNotNull); expect(routing.routes.any((route) => route.source == 'c'), isFalse); expectValidRouting(routing, layoutBox, extents: labels); }); test('heterogeneous and expanded text sizes stay valid and grow bounds', () { final graph = diamondGraphFixture(); final baseLayout = layoutFor(graph, extents: layout.heterogeneousExtents()); final expandedExtents = layout.scaledExtents( layout.heterogeneousExtents(), 2.6, ); final expandedLayout = layoutFor(graph, extents: expandedExtents); final edges = graphEdges(graph); final labels = fixedLabels(edges); final base = routeFor( graph, extents: layout.heterogeneousExtents(), labels: labels, ); final expanded = routeFor(graph, extents: expandedExtents, labels: labels); expectValidRouting(base, baseLayout, extents: labels); expectValidRouting(expanded, expandedLayout, extents: labels); expect(expanded.width, greaterThan(base.width)); expect(expanded.height, greaterThan(base.height)); }); test('unknown and missing targets are skipped with their identity', () { final graph = incompleteGraphFixture(); final layoutBox = layoutFor(graph); final edges = graphEdges(graph); final labels = labelsFor(edges); final routing = routeFor(graph, labels: labels); expect(routing.skipped.length, 2); final skipped = routing.skipped.toList(); expect(skipped.map((item) => item.reason), [ RouteSkipReason.unknownTarget, RouteSkipReason.unknownTarget, ]); expect(skipped.map((item) => item.id), [1, 2]); expect(skipped[0].source, 'a'); expect(skipped[0].outcome, 'blocked'); expect(skipped[0].target, ''); expect(skipped[1].target, 'ghost'); expect(routing.routes.map((route) => route.id), [3, 4]); expect(routing.routeFor(1), isNull); expect(routing.routeFor(2), isNull); expect(layoutBox.nodeFor('ghost'), isNull); expectValidRouting(routing, layoutBox, extents: labels); }); }