Files
pipelines/app/test/features/graph/routing/graph_routing_validation_test.dart
T
alan b381290f34 feat(app): add deterministic orthogonal graph edge routing
Pure-Dart P2c slice: interval-aware ports and corridors, label-aware lane spacing, measured label placement with fallback shelf, skipped diagnostics and routing bounds. 20 focused tests plus existing suite and web build pass.
2026-10-21 16:51:38 +01:00

223 lines
6.3 KiB
Dart

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),
);
});
}