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 graph, { Map? 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), [ 'plan', 'build', 'ship', failureEnd, successEnd, ]); expect(layout.ranks, { '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); }); }