feat(app): add deterministic content-sized graph node layout
Pure-Dart SCC ranking and column placement over the landed graph identities. Every extent and gap is caller supplied; missing measurements are rejected rather than defaulted. Focused tests cover linear, diamond, cycle, self-loop, disconnected, terminal, expanded-text and invalid-input cases.
This commit is contained in:
@@ -0,0 +1,96 @@
|
||||
import 'package:flutter_test/flutter_test.dart';
|
||||
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 'layout_fixtures.dart';
|
||||
|
||||
/// Invalid and boundary input for the pure layout engine.
|
||||
void main() {
|
||||
test('an empty definition yields empty padded bounds', () {
|
||||
final layout = layoutGraphNodes(
|
||||
nodes: const <GraphNode>[],
|
||||
edges: const <GraphEdge>[],
|
||||
extents: const <String, NodeExtent>{},
|
||||
config: layoutConfig(columnGap: 30, rowGap: 10, padding: 6),
|
||||
);
|
||||
|
||||
expect(layout.isEmpty, isTrue);
|
||||
expect(layout.rankCount, 0);
|
||||
expect(layout.width, 12);
|
||||
expect(layout.height, 12);
|
||||
expect(layout.ranks, isEmpty);
|
||||
});
|
||||
|
||||
test('missing measurements are reported rather than invented', () {
|
||||
final graph = linearGraphFixture();
|
||||
final nodes = graphNodes(graph);
|
||||
final extents = uniformExtents(nodes)..remove('build');
|
||||
|
||||
expect(
|
||||
() => layoutGraphNodes(
|
||||
nodes: nodes,
|
||||
edges: graphEdges(graph),
|
||||
extents: extents,
|
||||
config: layoutConfig(),
|
||||
),
|
||||
throwsArgumentError,
|
||||
);
|
||||
});
|
||||
|
||||
test('extra measurements are ignored because they cannot become nodes', () {
|
||||
final graph = linearGraphFixture();
|
||||
final nodes = graphNodes(graph);
|
||||
final extents = uniformExtents(nodes)
|
||||
..['ghost'] = NodeExtent(width: 20, height: 20);
|
||||
|
||||
final layout = layoutGraphNodes(
|
||||
nodes: nodes,
|
||||
edges: graphEdges(graph),
|
||||
extents: extents,
|
||||
config: layoutConfig(),
|
||||
);
|
||||
|
||||
expect(layout.nodeFor('ghost'), isNull);
|
||||
expect(layout.nodes.length, nodes.length);
|
||||
});
|
||||
|
||||
test('duplicate or empty node keys are rejected', () {
|
||||
const stageNode = GraphNode(key: 'a', terminal: false, stageIndex: 0);
|
||||
expect(
|
||||
() => layoutGraphNodes(
|
||||
nodes: const <GraphNode>[stageNode, stageNode],
|
||||
edges: const <GraphEdge>[],
|
||||
extents: <String, NodeExtent>{'a': NodeExtent(width: 10, height: 10)},
|
||||
config: layoutConfig(),
|
||||
),
|
||||
throwsArgumentError,
|
||||
);
|
||||
expect(
|
||||
() => layoutGraphNodes(
|
||||
nodes: const <GraphNode>[
|
||||
GraphNode(key: '', terminal: false, stageIndex: 0),
|
||||
],
|
||||
edges: const <GraphEdge>[],
|
||||
extents: const <String, NodeExtent>{},
|
||||
config: layoutConfig(),
|
||||
),
|
||||
throwsArgumentError,
|
||||
);
|
||||
});
|
||||
|
||||
test('invalid gaps and extents are rejected', () {
|
||||
expect(
|
||||
() => GraphLayoutConfig(columnGap: -1, rowGap: 8),
|
||||
throwsArgumentError,
|
||||
);
|
||||
expect(
|
||||
() => GraphLayoutConfig(columnGap: 8, rowGap: double.nan),
|
||||
throwsArgumentError,
|
||||
);
|
||||
expect(
|
||||
() => NodeExtent(width: double.infinity, height: 10),
|
||||
throwsArgumentError,
|
||||
);
|
||||
});
|
||||
}
|
||||
@@ -0,0 +1,228 @@
|
||||
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/graph_node_layout.dart';
|
||||
import 'package:pipelines_app/features/graph/layout/layout_models.dart';
|
||||
|
||||
import 'layout_fixtures.dart';
|
||||
|
||||
GraphLayout _layout(
|
||||
Map<String, dynamic> graph, {
|
||||
Map<String, NodeExtent>? extents,
|
||||
GraphLayoutConfig? config,
|
||||
}) {
|
||||
final nodes = graphNodes(graph);
|
||||
return layoutGraphNodes(
|
||||
nodes: nodes,
|
||||
edges: graphEdges(graph),
|
||||
extents: extents ?? uniformExtents(nodes),
|
||||
config: config ?? layoutConfig(),
|
||||
);
|
||||
}
|
||||
|
||||
void _expectWithinBounds(GraphLayout layout) {
|
||||
for (final box in layout.nodes) {
|
||||
expect(box.x, greaterThanOrEqualTo(0), reason: '${box.key} left');
|
||||
expect(box.y, greaterThanOrEqualTo(0), reason: '${box.key} top');
|
||||
expect(
|
||||
box.right,
|
||||
lessThanOrEqualTo(layout.width),
|
||||
reason: '${box.key} right inside width',
|
||||
);
|
||||
expect(
|
||||
box.bottom,
|
||||
lessThanOrEqualTo(layout.height),
|
||||
reason: '${box.key} bottom inside height',
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
void _expectSeparated(GraphLayout layout) {
|
||||
for (var first = 0; first < layout.nodes.length; first += 1) {
|
||||
for (var second = first + 1; second < layout.nodes.length; second += 1) {
|
||||
final a = layout.nodes[first];
|
||||
final b = layout.nodes[second];
|
||||
final separated =
|
||||
a.right <= b.x ||
|
||||
b.right <= a.x ||
|
||||
a.bottom <= b.y ||
|
||||
b.bottom <= a.y;
|
||||
expect(separated, isTrue, reason: '${a.key} overlaps ${b.key}');
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void main() {
|
||||
test('linear nodes keep declaration order and terminal identity', () {
|
||||
final layout = _layout(linearGraphFixture());
|
||||
|
||||
expect(layout.rankCount, 4);
|
||||
expect(layout.nodes.map((box) => box.key), <String>[
|
||||
'plan',
|
||||
'build',
|
||||
'ship',
|
||||
failureEnd,
|
||||
successEnd,
|
||||
]);
|
||||
expect(layout.ranks, <String, int>{
|
||||
'plan': 0,
|
||||
'build': 1,
|
||||
'ship': 2,
|
||||
failureEnd: 3,
|
||||
successEnd: 3,
|
||||
});
|
||||
expect(layout.nodeFor('plan')!.stageIndex, 0);
|
||||
expect(layout.nodeFor('build')!.stageIndex, 1);
|
||||
expect(layout.nodeFor('ship')!.stageIndex, 2);
|
||||
expect(layout.nodeFor(failureEnd)!.terminal, isTrue);
|
||||
expect(layout.nodeFor(failureEnd)!.stageIndex, isNull);
|
||||
expect(layout.nodeFor(successEnd)!.terminal, isTrue);
|
||||
expect(
|
||||
layout.nodeFor(successEnd)!.y - layout.nodeFor(failureEnd)!.bottom,
|
||||
layoutConfig().rowGap,
|
||||
);
|
||||
expect(layout.nodeFor(failureEnd)!.x, layout.nodeFor(successEnd)!.x);
|
||||
expect(layout.nodeFor('plan')!.x, layoutConfig().padding);
|
||||
expect(layout.nodeFor('plan')!.y, layoutConfig().padding);
|
||||
expect(
|
||||
layout.nodeFor('build')!.x - layout.nodeFor('plan')!.right,
|
||||
layoutConfig().columnGap,
|
||||
);
|
||||
expect(layout.width, 560);
|
||||
expect(layout.height, 140);
|
||||
_expectWithinBounds(layout);
|
||||
_expectSeparated(layout);
|
||||
});
|
||||
|
||||
test('diamond ranks place siblings in one column', () {
|
||||
final layout = _layout(diamondGraphFixture());
|
||||
|
||||
final b = layout.nodeFor('b')!;
|
||||
final c = layout.nodeFor('c')!;
|
||||
expect(b.rank, 1);
|
||||
expect(c.rank, 1);
|
||||
expect(b.x, c.x);
|
||||
expect(c.y - b.bottom, layoutConfig().rowGap);
|
||||
expect(layout.nodeFor('d')!.rank, 2);
|
||||
expect(layout.nodeFor('d')!.x, greaterThan(b.right));
|
||||
_expectWithinBounds(layout);
|
||||
_expectSeparated(layout);
|
||||
});
|
||||
|
||||
test('cycles share a column and terminals still follow them', () {
|
||||
final layout = _layout(cycleGraph());
|
||||
|
||||
final a = layout.nodeFor('a')!;
|
||||
final b = layout.nodeFor('b')!;
|
||||
expect(a.rank, 0);
|
||||
expect(b.rank, 0);
|
||||
expect(b.y - a.bottom, layoutConfig().rowGap);
|
||||
expect(layout.nodeFor(failureEnd)!.rank, 1);
|
||||
expect(layout.nodeFor(successEnd)!.rank, 1);
|
||||
_expectWithinBounds(layout);
|
||||
_expectSeparated(layout);
|
||||
});
|
||||
|
||||
test('self loops and disconnected stages stay finite and separate', () {
|
||||
final selfLoop = _layout(selfLoopGraphFixture());
|
||||
expect(selfLoop.nodeFor('a')!.rank, 0);
|
||||
expect(selfLoop.nodeFor('b')!.rank, 1);
|
||||
|
||||
final disconnected = _layout(disconnectedGraph());
|
||||
expect(disconnected.nodeFor('a')!.rank, 0);
|
||||
expect(disconnected.nodeFor('b')!.rank, 0);
|
||||
expect(disconnected.nodeFor('c')!.rank, 0);
|
||||
expect(
|
||||
disconnected.nodeFor('b')!.y,
|
||||
greaterThan(disconnected.nodeFor('a')!.bottom),
|
||||
);
|
||||
_expectWithinBounds(selfLoop);
|
||||
_expectSeparated(selfLoop);
|
||||
_expectWithinBounds(disconnected);
|
||||
_expectSeparated(disconnected);
|
||||
});
|
||||
|
||||
test('unknown edge targets add no node and no column', () {
|
||||
final graph = incompleteGraphFixture();
|
||||
final layout = _layout(graph);
|
||||
|
||||
expect(
|
||||
layout.nodes.map((box) => box.key),
|
||||
graphNodes(graph).map((node) => node.key),
|
||||
);
|
||||
expect(layout.nodeFor('ghost'), isNull);
|
||||
expect(layout.ranks.containsKey('ghost'), isFalse);
|
||||
_expectWithinBounds(layout);
|
||||
_expectSeparated(layout);
|
||||
});
|
||||
|
||||
test('heterogeneous measured widths stay inside their columns', () {
|
||||
final layout = _layout(
|
||||
diamondGraphFixture(),
|
||||
extents: heterogeneousExtents(),
|
||||
);
|
||||
|
||||
for (final entry in heterogeneousExtents().entries) {
|
||||
final box = layout.nodeFor(entry.key)!;
|
||||
expect(box.width, entry.value.width, reason: '${entry.key} width');
|
||||
expect(box.height, entry.value.height, reason: '${entry.key} height');
|
||||
}
|
||||
final b = layout.nodeFor('b')!;
|
||||
final c = layout.nodeFor('c')!;
|
||||
expect(b.rank, c.rank);
|
||||
expect(b.x + b.width / 2, c.x + c.width / 2);
|
||||
expect(c.y - b.bottom, layoutConfig().rowGap);
|
||||
expect(layout.width, greaterThan(0));
|
||||
expect(layout.width, 770);
|
||||
expect(layout.height, 236);
|
||||
_expectWithinBounds(layout);
|
||||
_expectSeparated(layout);
|
||||
});
|
||||
|
||||
test('expanded text extents grow the canvas without overlap or clipping', () {
|
||||
final base = _layout(
|
||||
diamondGraphFixture(),
|
||||
extents: heterogeneousExtents(),
|
||||
);
|
||||
final expandedExtents = scaledExtents(heterogeneousExtents(), 2.6);
|
||||
final expanded = _layout(diamondGraphFixture(), extents: expandedExtents);
|
||||
|
||||
expect(expanded.width, greaterThan(base.width));
|
||||
expect(expanded.height, greaterThan(base.height));
|
||||
for (final entry in expandedExtents.entries) {
|
||||
final box = expanded.nodeFor(entry.key)!;
|
||||
expect(box.width, entry.value.width, reason: '${entry.key} width');
|
||||
expect(box.height, entry.value.height, reason: '${entry.key} height');
|
||||
}
|
||||
_expectWithinBounds(expanded);
|
||||
_expectSeparated(expanded);
|
||||
});
|
||||
|
||||
test('the same input produces the same immutable layout', () {
|
||||
final graph = diamondGraphFixture();
|
||||
final nodes = graphNodes(graph);
|
||||
final edges = graphEdges(graph);
|
||||
final extents = uniformExtents(nodes, width: 133, height: 77);
|
||||
|
||||
final first = layoutGraphNodes(
|
||||
nodes: nodes,
|
||||
edges: edges,
|
||||
extents: extents,
|
||||
config: layoutConfig(),
|
||||
);
|
||||
final second = layoutGraphNodes(
|
||||
nodes: nodes,
|
||||
edges: edges,
|
||||
extents: extents,
|
||||
config: layoutConfig(),
|
||||
);
|
||||
|
||||
expect(second.nodes, first.nodes);
|
||||
expect(second.ranks, first.ranks);
|
||||
expect(second.width, first.width);
|
||||
expect(second.height, first.height);
|
||||
expect(second.rankCount, first.rankCount);
|
||||
expect(() => second.nodes.add(first.nodes.first), throwsUnsupportedError);
|
||||
expect(() => second.ranks['a'] = 99, throwsUnsupportedError);
|
||||
});
|
||||
}
|
||||
@@ -0,0 +1,116 @@
|
||||
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/graph_ranks.dart';
|
||||
|
||||
import 'layout_fixtures.dart';
|
||||
|
||||
GraphRanks _ranksFor(Map<String, dynamic> graph) {
|
||||
return assignGraphRanks(graphNodes(graph), graphEdges(graph));
|
||||
}
|
||||
|
||||
void main() {
|
||||
test('linear definitions rank one stage per column', () {
|
||||
final ranks = _ranksFor(linearGraphFixture());
|
||||
|
||||
expect(ranks.rankOf, <String, int>{
|
||||
'plan': 0,
|
||||
'build': 1,
|
||||
'ship': 2,
|
||||
failureEnd: 3,
|
||||
successEnd: 3,
|
||||
});
|
||||
expect(ranks.rankCount, 4);
|
||||
expect(ranks.components, <List<String>>[
|
||||
<String>['plan'],
|
||||
<String>['build'],
|
||||
<String>['ship'],
|
||||
<String>[failureEnd],
|
||||
<String>[successEnd],
|
||||
]);
|
||||
});
|
||||
|
||||
test('diamond fan-out shares a rank and converges after it', () {
|
||||
final ranks = _ranksFor(diamondGraphFixture());
|
||||
|
||||
expect(ranks.rankOf['a'], 0);
|
||||
expect(ranks.rankOf['b'], ranks.rankOf['c']);
|
||||
expect(ranks.rankOf['b'], 1);
|
||||
expect(ranks.rankOf['d'], 2);
|
||||
expect(ranks.rankOf[failureEnd], 1);
|
||||
expect(ranks.rankOf[successEnd], 3);
|
||||
expect(ranks.rankCount, 4);
|
||||
});
|
||||
|
||||
test('a cycle collapses to one shared rank instead of recursing', () {
|
||||
final ranks = _ranksFor(cycleGraph());
|
||||
|
||||
expect(ranks.rankOf['a'], 0);
|
||||
expect(ranks.rankOf['b'], 0);
|
||||
expect(ranks.rankOf[failureEnd], 1);
|
||||
expect(ranks.rankOf[successEnd], 1);
|
||||
expect(ranks.rankCount, 2);
|
||||
expect(ranks.components, contains(equals(<String>['a', 'b'])));
|
||||
});
|
||||
|
||||
test('a self edge stays inside its own component and adds no rank', () {
|
||||
final ranks = _ranksFor(selfLoopGraphFixture());
|
||||
|
||||
expect(ranks.rankOf, <String, int>{
|
||||
'a': 0,
|
||||
'b': 1,
|
||||
failureEnd: 2,
|
||||
successEnd: 2,
|
||||
});
|
||||
expect(ranks.components, contains(equals(<String>['a'])));
|
||||
expect(ranks.components, contains(equals(<String>['b'])));
|
||||
});
|
||||
|
||||
test('disconnected stages stay at rank zero', () {
|
||||
final ranks = _ranksFor(disconnectedGraph());
|
||||
|
||||
expect(ranks.rankOf['a'], 0);
|
||||
expect(ranks.rankOf['b'], 0);
|
||||
expect(ranks.rankOf['c'], 0);
|
||||
expect(ranks.rankOf[failureEnd], 1);
|
||||
expect(ranks.rankOf[successEnd], 1);
|
||||
expect(ranks.rankCount, 2);
|
||||
});
|
||||
|
||||
test('unknown and empty edge targets never become ranks or nodes', () {
|
||||
final graph = incompleteGraphFixture();
|
||||
final nodes = graphNodes(graph);
|
||||
final ranks = assignGraphRanks(nodes, graphEdges(graph));
|
||||
|
||||
expect(nodes.map((node) => node.key), isNot(contains('ghost')));
|
||||
expect(ranks.rankOf.containsKey('ghost'), isFalse);
|
||||
expect(ranks.rankOf.containsKey(''), isFalse);
|
||||
expect(ranks.rankOf['a'], 0);
|
||||
expect(ranks.rankOf['b'], 0);
|
||||
expect(ranks.rankOf[failureEnd], 1);
|
||||
expect(ranks.rankOf[successEnd], 1);
|
||||
});
|
||||
|
||||
test('ranks are stable across repeated assignments', () {
|
||||
final graph = diamondGraphFixture();
|
||||
final nodes = graphNodes(graph);
|
||||
final edges = graphEdges(graph);
|
||||
|
||||
final first = assignGraphRanks(nodes, edges);
|
||||
final second = assignGraphRanks(nodes, edges);
|
||||
|
||||
expect(second.rankOf, first.rankOf);
|
||||
expect(second.rankCount, first.rankCount);
|
||||
expect(second.components, first.components);
|
||||
});
|
||||
|
||||
test('a long chain is walked iteratively without overflow', () {
|
||||
const length = 2000;
|
||||
final ranks = _ranksFor(chainGraph(length));
|
||||
|
||||
expect(ranks.rankCount, length + 1);
|
||||
expect(ranks.rankOf['s0'], 0);
|
||||
expect(ranks.rankOf['s${length - 1}'], length - 1);
|
||||
expect(ranks.rankOf[successEnd], length);
|
||||
});
|
||||
}
|
||||
@@ -0,0 +1,102 @@
|
||||
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 '../domain/graph_fixtures.dart' as domain;
|
||||
|
||||
/// Small definitions and measured extents shared by the layout tests.
|
||||
|
||||
GraphLayoutConfig layoutConfig({
|
||||
double columnGap = 24,
|
||||
double rowGap = 12,
|
||||
double padding = 4,
|
||||
}) {
|
||||
return GraphLayoutConfig(
|
||||
columnGap: columnGap,
|
||||
rowGap: rowGap,
|
||||
padding: padding,
|
||||
);
|
||||
}
|
||||
|
||||
/// One measured size for every node, optionally scaled to simulate text growth.
|
||||
Map<String, NodeExtent> uniformExtents(
|
||||
Iterable<GraphNode> nodes, {
|
||||
double width = 120,
|
||||
double height = 60,
|
||||
double scale = 1,
|
||||
}) {
|
||||
return <String, NodeExtent>{
|
||||
for (final node in nodes)
|
||||
node.key: NodeExtent(width: width * scale, height: height * scale),
|
||||
};
|
||||
}
|
||||
|
||||
Map<String, NodeExtent> scaledExtents(
|
||||
Map<String, NodeExtent> base,
|
||||
double scale,
|
||||
) {
|
||||
return <String, NodeExtent>{
|
||||
for (final entry in base.entries)
|
||||
entry.key: NodeExtent(
|
||||
width: entry.value.width * scale,
|
||||
height: entry.value.height * scale,
|
||||
),
|
||||
};
|
||||
}
|
||||
|
||||
/// Deliberately uneven measured boxes for the diamond definition.
|
||||
Map<String, NodeExtent> heterogeneousExtents() {
|
||||
return <String, NodeExtent>{
|
||||
'a': NodeExtent(width: 210, height: 84),
|
||||
'b': NodeExtent(width: 92, height: 44),
|
||||
'c': NodeExtent(width: 148, height: 126),
|
||||
'd': NodeExtent(width: 104, height: 58),
|
||||
failureEnd: NodeExtent(width: 72, height: 34),
|
||||
successEnd: NodeExtent(width: 228, height: 96),
|
||||
};
|
||||
}
|
||||
|
||||
/// Two stages in a cycle; both terminals are reached from inside the cycle.
|
||||
Map<String, dynamic> cycleGraph() => <String, dynamic>{
|
||||
'stages': <dynamic>[
|
||||
domain.stage(
|
||||
'a',
|
||||
edges: <String, String?>{'success': 'b', 'error': '#'},
|
||||
fallbackError: true,
|
||||
),
|
||||
domain.stage(
|
||||
'b',
|
||||
edges: <String, String?>{'success': 'a', 'error': '#success'},
|
||||
),
|
||||
],
|
||||
};
|
||||
|
||||
/// Two independent routes and a stage with no edges at all.
|
||||
Map<String, dynamic> disconnectedGraph() => <String, dynamic>{
|
||||
'stages': <dynamic>[
|
||||
domain.stage('a', edges: <String, String?>{'success': '#success'}),
|
||||
domain.stage('b', edges: <String, String?>{'success': '#failure'}),
|
||||
domain.stage('c'),
|
||||
],
|
||||
};
|
||||
|
||||
Map<String, dynamic> linearGraphFixture() => domain.linearGraph();
|
||||
|
||||
Map<String, dynamic> diamondGraphFixture() => domain.diamondGraph();
|
||||
|
||||
Map<String, dynamic> selfLoopGraphFixture() => domain.selfLoopGraph();
|
||||
|
||||
Map<String, dynamic> incompleteGraphFixture() => domain.incompleteGraph();
|
||||
|
||||
/// A long chain, used to prove the iterative rank walk cannot overflow.
|
||||
Map<String, dynamic> chainGraph(int length) => <String, dynamic>{
|
||||
'stages': <dynamic>[
|
||||
for (var index = 0; index < length; index += 1)
|
||||
domain.stage(
|
||||
's$index',
|
||||
edges: <String, String?>{
|
||||
'success': index + 1 < length ? 's${index + 1}' : '#success',
|
||||
},
|
||||
),
|
||||
],
|
||||
};
|
||||
Reference in New Issue
Block a user