Files
pipelines/app/test/features/graph/domain/graph_definition_test.dart
T
alan 2c105074aa feat(app): add graph domain model for runs and definitions
Pure-Dart graph foundation mirroring the current web client:
terminal targets and labels, definition edges and nodes with web
outcome ordering, recorded run evidence (visited stages, taken
routes, current stage and live start), and sequence-ordered stage
visit selection with clamping.

No layout, widgets, screens or coordinates; fixtures cover linear,
diamond, self-loop, repeated visits, mixed outcomes, terminals and
missing/malformed evidence.
2026-10-21 15:31:43 +01:00

230 lines
6.8 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/domain/graph_protocol.dart';
import 'graph_fixtures.dart';
void main() {
group('outcome and edge order', () {
test('success first, other outcomes alphabetical, error last', () {
expect(
stageOutcomes(
stage(
'a',
edges: <String, String?>{
'error': '#',
'rejected': 'a',
'success': 'b',
'approved': 'b',
'retry': 'a',
},
),
),
<String>['success', 'approved', 'rejected', 'retry', 'error'],
);
expect(stageOutcomes(<String, dynamic>{}), isEmpty);
expect(stageOutcomes(null), isEmpty);
});
test('edgeLabel marks only an implicit fallback error route', () {
expect(
edgeLabel(<String, dynamic>{'fallback_error': true}, 'error'),
'error*',
);
expect(
edgeLabel(<String, dynamic>{'fallback_error': false}, 'error'),
'error',
);
expect(
edgeLabel(<String, dynamic>{'fallback_error': true}, 'success'),
'success',
);
expect(edgeLabel(<String, dynamic>{}, 'rejected'), 'rejected');
});
});
group('linear definition', () {
test('edges keep stage and outcome order with contiguous ids', () {
final edges = graphEdges(linearGraph());
expect(edges.map((edge) => edge.id), <int>[1, 2, 3, 4, 5]);
expect(edges.map((edge) => edge.source), <String>[
'plan',
'plan',
'build',
'ship',
'ship',
]);
expect(edges.map((edge) => edge.outcome), <String>[
'success',
'error',
'success',
'success',
'error',
]);
expect(edges.map((edge) => edge.target), <String>[
'build',
'#failure',
'ship',
'#success',
'#failure',
]);
expect(edges.map((edge) => edge.label), <String>[
'success',
'error*',
'success',
'success',
'error',
]);
expect(edges.where((edge) => edge.fallback).map((edge) => edge.id), <int>[
2,
]);
expect(edges.every((edge) => edge.knownTarget), isTrue);
});
test('nodes are declaration order plus failure then success terminals', () {
final nodes = graphNodes(linearGraph());
expect(nodes.map((node) => node.key), <String>[
'plan',
'build',
'ship',
failureEnd,
successEnd,
]);
expect(nodes.map((node) => node.stageIndex), <int?>[0, 1, 2, null, null]);
expect(nodes.map((node) => node.terminal), <bool>[
false,
false,
false,
true,
true,
]);
expect(nodes[3].label, 'Failure');
expect(nodes[3].shortLabel, 'FAILURE');
expect(nodes[4].label, 'Success');
expect(nodes[4].shortLabel, 'SUCCESS');
});
});
group('diamond definition', () {
test('keeps every converging edge distinct in stable order', () {
final edges = graphEdges(diamondGraph());
expect(
edges.map((edge) => '${edge.source}/${edge.outcome}->${edge.target}'),
<String>[
'a/success->b',
'a/approved->c',
'a/error->#failure',
'b/success->d',
'c/success->d',
'd/success->#success',
],
);
expect(edges.every((edge) => edge.knownTarget), isTrue);
});
});
group('self loops', () {
test('a self edge is one edge and never duplicates node identity', () {
final graph = selfLoopGraph();
final edges = graphEdges(graph);
expect(
edges.map((edge) => '${edge.source}/${edge.outcome}->${edge.target}'),
<String>[
'a/success->b',
'a/retry->a',
'b/success->#success',
'b/error->#failure',
],
);
expect(edges[1].source, edges[1].target);
expect(edges[1].knownTarget, isTrue);
final nodes = graphNodes(graph);
expect(nodes.map((node) => node.key), <String>[
'a',
'b',
failureEnd,
successEnd,
]);
expect(nodes.where((node) => node.key == 'a'), hasLength(1));
});
test(
'a stage declared twice keeps one node and one set of edges per key',
() {
final graph = <String, dynamic>{
'stages': <dynamic>[
stage('a', edges: <String, String?>{'retry': 'a'}),
stage('a', edges: <String, String?>{'success': '#success'}),
],
};
expect(graphNodes(graph).map((node) => node.key), <String>[
'a',
failureEnd,
successEnd,
]);
// Both declarations are still definition data, with unique edge ids.
expect(graphEdges(graph).map((edge) => edge.id), <int>[1, 2]);
},
);
});
group('missing and unknown targets', () {
test(
'an absent edge declares nothing; empty and unknown targets stay honest',
() {
final edges = graphEdges(incompleteGraph());
expect(edges.map((edge) => edge.id), <int>[1, 2, 3, 4]);
expect(edges.map((edge) => edge.outcome), <String>[
'blocked',
'retry',
'error',
'success',
]);
expect(edges[0].target, '');
expect(edges[0].knownTarget, isFalse);
expect(edges[1].target, 'ghost');
expect(edges[1].knownTarget, isFalse);
expect(edges[2].target, failureEnd);
expect(edges[2].fallback, isTrue);
expect(edges[3].target, successEnd);
},
);
test('an unknown target does not become a node or a terminal', () {
final graph = <String, dynamic>{
'stages': <dynamic>[
stage('a', edges: <String, String?>{'retry': 'ghost'}),
],
};
expect(graphNodes(graph).map((node) => node.key), <String>['a']);
expect(graphEdges(graph).single.knownTarget, isFalse);
});
test('a graph with no stages has no nodes or edges', () {
expect(graphNodes(<String, dynamic>{}), isEmpty);
expect(graphEdges(<String, dynamic>{'stages': null}), isEmpty);
expect(graphNodes(null), isEmpty);
});
});
test(
'definition nodes ignore run evidence even when the graph map carries it',
() {
final graph = <String, dynamic>{
'stages': <dynamic>[
stage('a', edges: <String, String?>{'success': 'b'}),
stage('b'),
],
'steps': <dynamic>[
step(sequence: 1, stage: 'a', outcome: 'success', target: 'b'),
],
'visited_stages': <String>['a', 'b'],
};
final nodes = graphNodes(graph);
expect(nodes.map((node) => node.key), <String>['a', 'b']);
expect(nodes.every((node) => !node.terminal), isTrue);
},
);
}