merge(app): land measured graph edge routing and label geometry
This commit is contained in:
@@ -0,0 +1,191 @@
|
||||
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 = <String>{
|
||||
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 = <EdgeLabelBox>[
|
||||
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(), <String>{'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(), <String>{
|
||||
'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>[
|
||||
RouteSkipReason.unknownTarget,
|
||||
RouteSkipReason.unknownTarget,
|
||||
]);
|
||||
expect(skipped.map((item) => item.id), <int>[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), <int>[3, 4]);
|
||||
expect(routing.routeFor(1), isNull);
|
||||
expect(routing.routeFor(2), isNull);
|
||||
expect(layoutBox.nodeFor('ghost'), isNull);
|
||||
expectValidRouting(routing, layoutBox, extents: labels);
|
||||
});
|
||||
}
|
||||
@@ -0,0 +1,161 @@
|
||||
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/layout/layout_models.dart';
|
||||
import 'package:pipelines_app/features/graph/routing/graph_edge_routing.dart';
|
||||
import 'package:pipelines_app/features/graph/routing/route_models.dart';
|
||||
|
||||
import 'routing_assertions.dart';
|
||||
import 'routing_fixtures.dart';
|
||||
import '../layout/layout_fixtures.dart' as layout;
|
||||
|
||||
void main() {
|
||||
test(
|
||||
'wide measured labels move outside the drawing instead of covering it',
|
||||
() {
|
||||
final graph = chainGraphFixture();
|
||||
final layoutBox = layoutFor(graph);
|
||||
final edges = graphEdges(graph);
|
||||
final labels = fixedLabels(edges, width: 400, height: 40);
|
||||
final routing = routeFor(graph, labels: labels);
|
||||
|
||||
expect(routing.labels.length, edges.length);
|
||||
for (final label in routing.labels) {
|
||||
expect(label.y, greaterThanOrEqualTo(layoutBox.height));
|
||||
expect(label.right, lessThanOrEqualTo(routing.width));
|
||||
expect(label.bottom, lessThanOrEqualTo(routing.height));
|
||||
}
|
||||
expect(routing.height, greaterThan(layoutBox.height));
|
||||
expectValidRouting(routing, layoutBox, extents: labels);
|
||||
},
|
||||
);
|
||||
|
||||
test('tight gaps and expanded extents keep the same guarantees', () {
|
||||
for (final gap in <double>[8, 24, 80]) {
|
||||
for (final scale in <double>[1, 2.6]) {
|
||||
final extents = layout.scaledExtents(
|
||||
layout.heterogeneousExtents(),
|
||||
scale,
|
||||
);
|
||||
final layoutBox = layoutFor(
|
||||
diamondGraphFixture(),
|
||||
extents: extents,
|
||||
config: routingLayoutConfig(columnGap: gap, rowGap: 16),
|
||||
);
|
||||
final edges = graphEdges(diamondGraphFixture());
|
||||
final labels = labelsFor(edges);
|
||||
final routing = routeFor(
|
||||
diamondGraphFixture(),
|
||||
extents: extents,
|
||||
labels: labels,
|
||||
layoutConfig: routingLayoutConfig(columnGap: gap, rowGap: 16),
|
||||
);
|
||||
expectValidRouting(routing, layoutBox, extents: labels);
|
||||
}
|
||||
}
|
||||
});
|
||||
|
||||
test('a three-stage cycle keeps every same-rank corridor distinct', () {
|
||||
final graph = cycleThreeGraph();
|
||||
final layoutBox = layoutFor(
|
||||
graph,
|
||||
config: routingLayoutConfig(columnGap: 8),
|
||||
);
|
||||
final edges = graphEdges(graph);
|
||||
final labels = labelsFor(edges);
|
||||
final routing = routeFor(
|
||||
graph,
|
||||
labels: labels,
|
||||
layoutConfig: routingLayoutConfig(columnGap: 8),
|
||||
);
|
||||
|
||||
expect(routing.routes.length, 3);
|
||||
expect(
|
||||
routing.routes.every((route) => route.kind == RouteKind.sameRank),
|
||||
isTrue,
|
||||
);
|
||||
expectValidRouting(routing, layoutBox, extents: labels);
|
||||
});
|
||||
|
||||
test('a caller-supplied backward rank routes around the target column', () {
|
||||
final layoutBox = GraphLayout(
|
||||
nodes: <GraphNodeBox>[
|
||||
const GraphNodeBox(
|
||||
key: 'a',
|
||||
terminal: false,
|
||||
stageIndex: 0,
|
||||
rank: 1,
|
||||
x: 208,
|
||||
y: 8,
|
||||
width: 120,
|
||||
height: 60,
|
||||
),
|
||||
const GraphNodeBox(
|
||||
key: 'b',
|
||||
terminal: false,
|
||||
stageIndex: 1,
|
||||
rank: 0,
|
||||
x: 8,
|
||||
y: 8,
|
||||
width: 120,
|
||||
height: 60,
|
||||
),
|
||||
],
|
||||
ranks: <String, int>{'a': 1, 'b': 0},
|
||||
rankCount: 2,
|
||||
width: 336,
|
||||
height: 76,
|
||||
);
|
||||
final edge = GraphEdge(
|
||||
id: 1,
|
||||
source: 'a',
|
||||
outcome: 'success',
|
||||
target: 'b',
|
||||
label: 'success',
|
||||
fallback: false,
|
||||
knownTarget: true,
|
||||
);
|
||||
final labels = <int, LabelExtent>{1: LabelExtent(width: 52, height: 18)};
|
||||
final routing = routeGraphEdges(
|
||||
layout: layoutBox,
|
||||
edges: <GraphEdge>[edge],
|
||||
labelExtents: labels,
|
||||
config: routingConfig(),
|
||||
);
|
||||
|
||||
final route = routing.routes.single;
|
||||
expect(route.kind, RouteKind.back);
|
||||
expect(route.sourcePort.side, RoutePortSide.right);
|
||||
expect(route.targetPort.side, RoutePortSide.right);
|
||||
expect(route.straight, isFalse);
|
||||
expect(route.points.length, greaterThan(4));
|
||||
expect(routing.width, greaterThan(layoutBox.width));
|
||||
expectValidRouting(routing, layoutBox, extents: labels);
|
||||
});
|
||||
|
||||
test('a self loop and forward routes share one valid layout', () {
|
||||
final graph = selfLoopGraphFixture();
|
||||
final layoutBox = layoutFor(graph);
|
||||
final labels = labelsFor(graphEdges(graph));
|
||||
final routing = routeFor(graph, labels: labels);
|
||||
|
||||
expect(routing.routes.length, 4);
|
||||
expect(routing.routes.map((route) => route.kind).toSet(), <RouteKind>{
|
||||
RouteKind.forward,
|
||||
RouteKind.selfLoop,
|
||||
});
|
||||
final loop = routing.routes.singleWhere(
|
||||
(route) => route.kind == RouteKind.selfLoop,
|
||||
);
|
||||
expect(loop.source, 'a');
|
||||
expect(loop.target, 'a');
|
||||
expect(loop.sourcePort.point.y, isNot(loop.targetPort.point.y));
|
||||
expect(
|
||||
routing.routes
|
||||
.where((route) => route.target == successEnd)
|
||||
.every((route) => route.targetPort.side == RoutePortSide.left),
|
||||
isTrue,
|
||||
);
|
||||
expectValidRouting(routing, layoutBox, extents: labels);
|
||||
});
|
||||
}
|
||||
@@ -0,0 +1,222 @@
|
||||
import 'package:flutter_test/flutter_test.dart';
|
||||
import 'package:pipelines_app/features/graph/domain/graph_definition.dart';
|
||||
import 'package:pipelines_app/features/graph/routing/graph_edge_routing.dart';
|
||||
import 'package:pipelines_app/features/graph/routing/route_models.dart';
|
||||
|
||||
import 'routing_fixtures.dart';
|
||||
|
||||
GraphEdge _edge({
|
||||
int id = 1,
|
||||
String source = 'a',
|
||||
String target = 'b',
|
||||
String outcome = 'success',
|
||||
bool knownTarget = true,
|
||||
}) {
|
||||
return GraphEdge(
|
||||
id: id,
|
||||
source: source,
|
||||
outcome: outcome,
|
||||
target: target,
|
||||
label: outcome,
|
||||
fallback: false,
|
||||
knownTarget: knownTarget,
|
||||
);
|
||||
}
|
||||
|
||||
void main() {
|
||||
test('an empty graph has empty routes and the layout bounds', () {
|
||||
final layoutBox = layoutFor(<String, dynamic>{'stages': <dynamic>[]});
|
||||
final routing = routeGraphEdges(
|
||||
layout: layoutBox,
|
||||
edges: const <GraphEdge>[],
|
||||
labelExtents: const <int, LabelExtent>{},
|
||||
config: routingConfig(),
|
||||
);
|
||||
|
||||
expect(routing.isEmpty, isTrue);
|
||||
expect(routing.routes, isEmpty);
|
||||
expect(routing.labels, isEmpty);
|
||||
expect(routing.skipped, isEmpty);
|
||||
expect(routing.width, layoutBox.width + routingConfig().boundsPadding);
|
||||
expect(routing.height, layoutBox.height + routingConfig().boundsPadding);
|
||||
});
|
||||
|
||||
test('unknown endpoints become diagnostics, never invented nodes', () {
|
||||
final layoutBox = layoutFor(chainGraphFixture());
|
||||
final edges = <GraphEdge>[
|
||||
_edge(id: 1, source: 'missing', target: 's0'),
|
||||
_edge(id: 2, source: 's0', target: 'ghost'),
|
||||
_edge(id: 3, source: 's0', target: '', knownTarget: false),
|
||||
];
|
||||
final routing = routeGraphEdges(
|
||||
layout: layoutBox,
|
||||
edges: edges,
|
||||
labelExtents: const <int, LabelExtent>{},
|
||||
config: routingConfig(),
|
||||
);
|
||||
|
||||
expect(routing.routes, isEmpty);
|
||||
expect(routing.skipped.map((item) => item.reason), <RouteSkipReason>[
|
||||
RouteSkipReason.unknownSource,
|
||||
RouteSkipReason.unknownTarget,
|
||||
RouteSkipReason.unknownTarget,
|
||||
]);
|
||||
expect(routing.skipped.map((item) => item.id), <int>[1, 2, 3]);
|
||||
expect(layoutBox.nodeFor('missing'), isNull);
|
||||
expect(layoutBox.nodeFor('ghost'), isNull);
|
||||
});
|
||||
|
||||
test('routed edges require measured label extents', () {
|
||||
final graph = diamondGraphFixture();
|
||||
final layoutBox = layoutFor(graph);
|
||||
final edges = graphEdges(graph);
|
||||
final labels = labelsFor(edges)..remove(edges.first.id);
|
||||
|
||||
expect(
|
||||
() => routeGraphEdges(
|
||||
layout: layoutBox,
|
||||
edges: edges,
|
||||
labelExtents: labels,
|
||||
config: routingConfig(),
|
||||
),
|
||||
throwsArgumentError,
|
||||
);
|
||||
});
|
||||
|
||||
test('extra label extents are ignored and duplicate ids are rejected', () {
|
||||
final graph = diamondGraphFixture();
|
||||
final layoutBox = layoutFor(graph);
|
||||
final edges = graphEdges(graph);
|
||||
final labels = labelsFor(edges)..[999] = LabelExtent(width: 10, height: 10);
|
||||
final routing = routeGraphEdges(
|
||||
layout: layoutBox,
|
||||
edges: edges,
|
||||
labelExtents: labels,
|
||||
config: routingConfig(),
|
||||
);
|
||||
expect(routing.routes.length, edges.length);
|
||||
expect(routing.labelFor(999), isNull);
|
||||
|
||||
expect(
|
||||
() => routeGraphEdges(
|
||||
layout: layoutBox,
|
||||
edges: <GraphEdge>[edges.first, edges.first],
|
||||
labelExtents: labelsFor(edges),
|
||||
config: routingConfig(),
|
||||
),
|
||||
throwsArgumentError,
|
||||
);
|
||||
});
|
||||
|
||||
test('invalid configuration and label extents are rejected', () {
|
||||
expect(
|
||||
() => RoutingConfig(
|
||||
nodeClearance: -1,
|
||||
laneSpacing: 10,
|
||||
corridorSpacing: 10,
|
||||
portSpacing: 8,
|
||||
labelGap: 2,
|
||||
),
|
||||
throwsArgumentError,
|
||||
);
|
||||
expect(
|
||||
() => RoutingConfig(
|
||||
nodeClearance: 2,
|
||||
laneSpacing: 0,
|
||||
corridorSpacing: 10,
|
||||
portSpacing: 8,
|
||||
labelGap: 2,
|
||||
),
|
||||
throwsArgumentError,
|
||||
);
|
||||
expect(
|
||||
() => RoutingConfig(
|
||||
nodeClearance: 2,
|
||||
laneSpacing: 10,
|
||||
corridorSpacing: double.nan,
|
||||
portSpacing: 8,
|
||||
labelGap: 2,
|
||||
),
|
||||
throwsArgumentError,
|
||||
);
|
||||
expect(
|
||||
() => LabelExtent(width: double.infinity, height: 10),
|
||||
throwsArgumentError,
|
||||
);
|
||||
expect(() => LabelExtent(width: 10, height: -1), throwsArgumentError);
|
||||
});
|
||||
|
||||
test('the same input produces equal immutable results', () {
|
||||
final graph = diamondGraphFixture();
|
||||
final layoutBox = layoutFor(graph);
|
||||
final edges = graphEdges(graph);
|
||||
final labels = labelsFor(edges);
|
||||
final config = routingConfig();
|
||||
|
||||
final first = routeGraphEdges(
|
||||
layout: layoutBox,
|
||||
edges: edges,
|
||||
labelExtents: labels,
|
||||
config: config,
|
||||
);
|
||||
final second = routeGraphEdges(
|
||||
layout: layoutBox,
|
||||
edges: edges,
|
||||
labelExtents: labels,
|
||||
config: config,
|
||||
);
|
||||
|
||||
expect(first.routes, second.routes);
|
||||
expect(first.labels, second.labels);
|
||||
expect(first.skipped, second.skipped);
|
||||
expect(first.width, second.width);
|
||||
expect(first.height, second.height);
|
||||
expect(() => first.routes.add(first.routes.first), throwsUnsupportedError);
|
||||
expect(
|
||||
() => first.routes.first.points.add(const RoutePoint(0, 0)),
|
||||
throwsUnsupportedError,
|
||||
);
|
||||
expect(() => first.labels.add(first.labels.first), throwsUnsupportedError);
|
||||
expect(
|
||||
() => first.skipped.add(
|
||||
const RouteDiagnostic(
|
||||
id: 1,
|
||||
source: 'a',
|
||||
outcome: 'success',
|
||||
target: 'b',
|
||||
reason: RouteSkipReason.unknownTarget,
|
||||
message: '',
|
||||
),
|
||||
),
|
||||
throwsUnsupportedError,
|
||||
);
|
||||
});
|
||||
|
||||
test('edges are routed in definition id order regardless of input order', () {
|
||||
final graph = diamondGraphFixture();
|
||||
final layoutBox = layoutFor(graph);
|
||||
final edges = graphEdges(graph);
|
||||
final labels = labelsFor(edges);
|
||||
final forward = routeGraphEdges(
|
||||
layout: layoutBox,
|
||||
edges: edges,
|
||||
labelExtents: labels,
|
||||
config: routingConfig(),
|
||||
);
|
||||
final reversed = routeGraphEdges(
|
||||
layout: layoutBox,
|
||||
edges: edges.reversed.toList(),
|
||||
labelExtents: labels,
|
||||
config: routingConfig(),
|
||||
);
|
||||
|
||||
expect(reversed.routes, forward.routes);
|
||||
expect(reversed.labels, forward.labels);
|
||||
expect(reversed.width, forward.width);
|
||||
expect(reversed.height, forward.height);
|
||||
expect(
|
||||
forward.routes.map((route) => route.id),
|
||||
edges.map((edge) => edge.id),
|
||||
);
|
||||
});
|
||||
}
|
||||
@@ -0,0 +1,215 @@
|
||||
import 'dart:math' as math;
|
||||
|
||||
import 'package:flutter_test/flutter_test.dart';
|
||||
import 'package:pipelines_app/features/graph/layout/layout_models.dart';
|
||||
import 'package:pipelines_app/features/graph/routing/route_models.dart';
|
||||
|
||||
const double _epsilon = 1e-9;
|
||||
|
||||
/// Checks the routing guarantees every fixture must satisfy.
|
||||
void expectValidRouting(
|
||||
GraphRouting routing,
|
||||
GraphLayout layout, {
|
||||
Map<int, LabelExtent>? extents,
|
||||
}) {
|
||||
expect(routing.width.isFinite, isTrue);
|
||||
expect(routing.height.isFinite, isTrue);
|
||||
expect(routing.width, greaterThan(0));
|
||||
expect(routing.height, greaterThan(0));
|
||||
|
||||
for (final route in routing.routes) {
|
||||
_expectRoute(route, layout, routing.width, routing.height);
|
||||
_expectNoNodeInterior(route, layout);
|
||||
}
|
||||
expectDistinctCorridors(routing.routes);
|
||||
|
||||
final byId = <int, EdgeLabelBox>{
|
||||
for (final label in routing.labels) label.edgeId: label,
|
||||
};
|
||||
expect(byId.length, routing.labels.length, reason: 'one label per edge');
|
||||
for (final route in routing.routes) {
|
||||
final label = byId[route.id];
|
||||
expect(label, isNotNull, reason: 'edge ${route.id} has a label');
|
||||
final extent = extents?[route.id];
|
||||
if (extent != null) {
|
||||
expect(label!.width, extent.width);
|
||||
expect(label.height, extent.height);
|
||||
}
|
||||
expect(label!.x, greaterThanOrEqualTo(-_epsilon));
|
||||
expect(label.y, greaterThanOrEqualTo(-_epsilon));
|
||||
expect(label.right, lessThanOrEqualTo(routing.width + _epsilon));
|
||||
expect(label.bottom, lessThanOrEqualTo(routing.height + _epsilon));
|
||||
for (final node in layout.nodes) {
|
||||
expect(
|
||||
_overlaps(
|
||||
label.x,
|
||||
label.y,
|
||||
label.width,
|
||||
label.height,
|
||||
node.x,
|
||||
node.y,
|
||||
node.width,
|
||||
node.height,
|
||||
),
|
||||
isFalse,
|
||||
reason: 'label for edge ${route.id} overlaps node ${node.key}',
|
||||
);
|
||||
}
|
||||
}
|
||||
for (var first = 0; first < routing.labels.length; first += 1) {
|
||||
for (var second = first + 1; second < routing.labels.length; second += 1) {
|
||||
final a = routing.labels[first];
|
||||
final b = routing.labels[second];
|
||||
expect(
|
||||
_overlaps(a.x, a.y, a.width, a.height, b.x, b.y, b.width, b.height),
|
||||
isFalse,
|
||||
reason: 'labels for edges ${a.edgeId} and ${b.edgeId} overlap',
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
for (final skipped in routing.skipped) {
|
||||
expect(routing.routeFor(skipped.id), isNull);
|
||||
expect(routing.labelFor(skipped.id), isNull);
|
||||
}
|
||||
}
|
||||
|
||||
void expectDistinctCorridors(List<EdgeRoute> routes) {
|
||||
for (var first = 0; first < routes.length; first += 1) {
|
||||
for (var second = first + 1; second < routes.length; second += 1) {
|
||||
final a = routes[first];
|
||||
final b = routes[second];
|
||||
for (var ai = 1; ai < a.points.length; ai += 1) {
|
||||
final a0 = a.points[ai - 1];
|
||||
final a1 = a.points[ai];
|
||||
for (var bi = 1; bi < b.points.length; bi += 1) {
|
||||
final b0 = b.points[bi - 1];
|
||||
final b1 = b.points[bi];
|
||||
if (a0.x == a1.x && b0.x == b1.x && a0.x == b0.x) {
|
||||
final aLo = math.min(a0.y, a1.y);
|
||||
final aHi = math.max(a0.y, a1.y);
|
||||
final bLo = math.min(b0.y, b1.y);
|
||||
final bHi = math.max(b0.y, b1.y);
|
||||
expect(
|
||||
aLo < bHi - _epsilon && bLo < aHi - _epsilon,
|
||||
isFalse,
|
||||
reason: 'edges ${a.id} and ${b.id} share a vertical corridor',
|
||||
);
|
||||
}
|
||||
if (a0.y == a1.y && b0.y == b1.y && a0.y == b0.y) {
|
||||
final aLo = math.min(a0.x, a1.x);
|
||||
final aHi = math.max(a0.x, a1.x);
|
||||
final bLo = math.min(b0.x, b1.x);
|
||||
final bHi = math.max(b0.x, b1.x);
|
||||
expect(
|
||||
aLo < bHi - _epsilon && bLo < aHi - _epsilon,
|
||||
isFalse,
|
||||
reason: 'edges ${a.id} and ${b.id} share a horizontal run',
|
||||
);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void _expectRoute(
|
||||
EdgeRoute route,
|
||||
GraphLayout layout,
|
||||
double width,
|
||||
double height,
|
||||
) {
|
||||
expect(route.points.length, greaterThanOrEqualTo(2));
|
||||
expect(route.points.first, route.sourcePort.point);
|
||||
expect(route.points.last, route.targetPort.point);
|
||||
for (var index = 0; index < route.points.length; index += 1) {
|
||||
final point = route.points[index];
|
||||
expect(point.x.isFinite, isTrue);
|
||||
expect(point.y.isFinite, isTrue);
|
||||
expect(point.x, greaterThanOrEqualTo(-_epsilon));
|
||||
expect(point.y, greaterThanOrEqualTo(-_epsilon));
|
||||
expect(point.x, lessThanOrEqualTo(width + _epsilon));
|
||||
expect(point.y, lessThanOrEqualTo(height + _epsilon));
|
||||
if (index > 0) {
|
||||
final previous = route.points[index - 1];
|
||||
expect(
|
||||
previous.x == point.x || previous.y == point.y,
|
||||
isTrue,
|
||||
reason: 'edge ${route.id} segment ${index - 1} is not orthogonal',
|
||||
);
|
||||
expect(previous, isNot(point), reason: 'zero-length segment');
|
||||
}
|
||||
}
|
||||
_expectPort(route.sourcePort, layout.nodeFor(route.source)!);
|
||||
_expectPort(route.targetPort, layout.nodeFor(route.target)!);
|
||||
}
|
||||
|
||||
void _expectPort(RoutePort port, GraphNodeBox box) {
|
||||
switch (port.side) {
|
||||
case RoutePortSide.right:
|
||||
expect(port.point.x, closeTo(box.right, _epsilon));
|
||||
expect(port.point.y, greaterThanOrEqualTo(box.y - _epsilon));
|
||||
expect(port.point.y, lessThanOrEqualTo(box.bottom + _epsilon));
|
||||
case RoutePortSide.left:
|
||||
expect(port.point.x, closeTo(box.x, _epsilon));
|
||||
expect(port.point.y, greaterThanOrEqualTo(box.y - _epsilon));
|
||||
expect(port.point.y, lessThanOrEqualTo(box.bottom + _epsilon));
|
||||
case RoutePortSide.top:
|
||||
expect(port.point.y, closeTo(box.y, _epsilon));
|
||||
expect(port.point.x, greaterThanOrEqualTo(box.x - _epsilon));
|
||||
expect(port.point.x, lessThanOrEqualTo(box.right + _epsilon));
|
||||
case RoutePortSide.bottom:
|
||||
expect(port.point.y, closeTo(box.bottom, _epsilon));
|
||||
expect(port.point.x, greaterThanOrEqualTo(box.x - _epsilon));
|
||||
expect(port.point.x, lessThanOrEqualTo(box.right + _epsilon));
|
||||
}
|
||||
}
|
||||
|
||||
void _expectNoNodeInterior(EdgeRoute route, GraphLayout layout) {
|
||||
for (var index = 1; index < route.points.length; index += 1) {
|
||||
final a = route.points[index - 1];
|
||||
final b = route.points[index];
|
||||
for (final node in layout.nodes) {
|
||||
if (a.y == b.y) {
|
||||
final left = math.min(a.x, b.x);
|
||||
final right = math.max(a.x, b.x);
|
||||
final crosses =
|
||||
a.y > node.y + _epsilon &&
|
||||
a.y < node.bottom - _epsilon &&
|
||||
right > node.x + _epsilon &&
|
||||
left < node.right - _epsilon;
|
||||
expect(
|
||||
crosses,
|
||||
isFalse,
|
||||
reason: 'edge ${route.id} crosses node ${node.key} horizontally',
|
||||
);
|
||||
} else {
|
||||
final top = math.min(a.y, b.y);
|
||||
final bottom = math.max(a.y, b.y);
|
||||
final crosses =
|
||||
a.x > node.x + _epsilon &&
|
||||
a.x < node.right - _epsilon &&
|
||||
bottom > node.y + _epsilon &&
|
||||
top < node.bottom - _epsilon;
|
||||
expect(
|
||||
crosses,
|
||||
isFalse,
|
||||
reason: 'edge ${route.id} crosses node ${node.key} vertically',
|
||||
);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
bool _overlaps(
|
||||
double ax,
|
||||
double ay,
|
||||
double aw,
|
||||
double ah,
|
||||
double bx,
|
||||
double by,
|
||||
double bw,
|
||||
double bh,
|
||||
) {
|
||||
return ax < bx + bw && ax + aw > bx && ay < by + bh && ay + ah > by;
|
||||
}
|
||||
@@ -0,0 +1,143 @@
|
||||
import 'package:pipelines_app/features/graph/domain/graph_definition.dart';
|
||||
import 'package:pipelines_app/features/graph/layout/graph_node_layout.dart';
|
||||
import 'package:pipelines_app/features/graph/layout/layout_models.dart';
|
||||
import 'package:pipelines_app/features/graph/routing/graph_edge_routing.dart';
|
||||
import 'package:pipelines_app/features/graph/routing/route_models.dart';
|
||||
|
||||
import '../layout/layout_fixtures.dart' as layout;
|
||||
import '../domain/graph_fixtures.dart' as domain;
|
||||
|
||||
/// Definitions, measured extents and configs shared by the routing tests.
|
||||
|
||||
GraphLayoutConfig routingLayoutConfig({
|
||||
double columnGap = 80,
|
||||
double rowGap = 16,
|
||||
double padding = 8,
|
||||
}) {
|
||||
return GraphLayoutConfig(
|
||||
columnGap: columnGap,
|
||||
rowGap: rowGap,
|
||||
padding: padding,
|
||||
);
|
||||
}
|
||||
|
||||
RoutingConfig routingConfig({
|
||||
double nodeClearance = 8,
|
||||
double laneSpacing = 28,
|
||||
double corridorSpacing = 20,
|
||||
double portSpacing = 12,
|
||||
double labelGap = 4,
|
||||
double boundsPadding = 4,
|
||||
}) {
|
||||
return RoutingConfig(
|
||||
nodeClearance: nodeClearance,
|
||||
laneSpacing: laneSpacing,
|
||||
corridorSpacing: corridorSpacing,
|
||||
portSpacing: portSpacing,
|
||||
labelGap: labelGap,
|
||||
boundsPadding: boundsPadding,
|
||||
);
|
||||
}
|
||||
|
||||
GraphLayout layoutFor(
|
||||
Map<String, dynamic> graph, {
|
||||
Map<String, NodeExtent>? extents,
|
||||
GraphLayoutConfig? config,
|
||||
}) {
|
||||
final nodes = graphNodes(graph);
|
||||
return layoutGraphNodes(
|
||||
nodes: nodes,
|
||||
edges: graphEdges(graph),
|
||||
extents: extents ?? layout.uniformExtents(nodes),
|
||||
config: config ?? routingLayoutConfig(),
|
||||
);
|
||||
}
|
||||
|
||||
/// Measured label boxes sized from their text, as a renderer would supply them.
|
||||
Map<int, LabelExtent> labelsFor(
|
||||
Iterable<GraphEdge> edges, {
|
||||
double charWidth = 7,
|
||||
double padding = 12,
|
||||
double height = 18,
|
||||
}) {
|
||||
return <int, LabelExtent>{
|
||||
for (final edge in edges)
|
||||
edge.id: LabelExtent(
|
||||
width: edge.label.length * charWidth + padding,
|
||||
height: height,
|
||||
),
|
||||
};
|
||||
}
|
||||
|
||||
/// One box per edge at an explicit size, used for wide-label checks.
|
||||
Map<int, LabelExtent> fixedLabels(
|
||||
Iterable<GraphEdge> edges, {
|
||||
double width = 52,
|
||||
double height = 18,
|
||||
}) {
|
||||
return <int, LabelExtent>{
|
||||
for (final edge in edges)
|
||||
edge.id: LabelExtent(width: width, height: height),
|
||||
};
|
||||
}
|
||||
|
||||
GraphRouting routeFor(
|
||||
Map<String, dynamic> graph, {
|
||||
Map<String, NodeExtent>? extents,
|
||||
Map<int, LabelExtent>? labels,
|
||||
GraphLayoutConfig? layoutConfig,
|
||||
RoutingConfig? config,
|
||||
}) {
|
||||
final layout = layoutFor(graph, extents: extents, config: layoutConfig);
|
||||
final edges = graphEdges(graph);
|
||||
return routeGraphEdges(
|
||||
layout: layout,
|
||||
edges: edges,
|
||||
labelExtents: labels ?? labelsFor(edges),
|
||||
config: config ?? routingConfig(),
|
||||
);
|
||||
}
|
||||
|
||||
/// Three outcomes from `a` to the same target `b`, then `b` to Success.
|
||||
Map<String, dynamic> parallelOutcomesGraph() => <String, dynamic>{
|
||||
'stages': <dynamic>[
|
||||
domain.stage(
|
||||
'a',
|
||||
edges: <String, String?>{
|
||||
'success': 'b',
|
||||
'approved': 'b',
|
||||
'rejected': 'b',
|
||||
},
|
||||
),
|
||||
domain.stage('b', edges: <String, String?>{'success': '#success'}),
|
||||
],
|
||||
};
|
||||
|
||||
/// A single stage that retries itself and has no terminal pair.
|
||||
Map<String, dynamic> selfLoopOnlyGraph() => <String, dynamic>{
|
||||
'stages': <dynamic>[
|
||||
domain.stage('a', edges: <String, String?>{'retry': 'a'}),
|
||||
],
|
||||
};
|
||||
|
||||
Map<String, dynamic> chainGraphFixture() => layout.chainGraph(4);
|
||||
|
||||
Map<String, dynamic> diamondGraphFixture() => layout.diamondGraphFixture();
|
||||
|
||||
Map<String, dynamic> cycleGraphFixture() => layout.cycleGraph();
|
||||
|
||||
Map<String, dynamic> selfLoopGraphFixture() => layout.selfLoopGraphFixture();
|
||||
|
||||
Map<String, dynamic> disconnectedGraphFixture() => layout.disconnectedGraph();
|
||||
|
||||
Map<String, dynamic> incompleteGraphFixture() =>
|
||||
layout.incompleteGraphFixture();
|
||||
|
||||
/// Three stages in one strongly connected component with no terminals.
|
||||
Map<String, dynamic> cycleThreeGraph() => <String, dynamic>{
|
||||
'stages': <dynamic>[
|
||||
domain.stage('a', edges: <String, String?>{'success': 'b'}),
|
||||
domain.stage('b', edges: <String, String?>{'success': 'c'}),
|
||||
domain.stage('c', edges: <String, String?>{'success': 'a'}),
|
||||
],
|
||||
};
|
||||
Reference in New Issue
Block a user