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: { 'error': '#', 'rejected': 'a', 'success': 'b', 'approved': 'b', 'retry': 'a', }, ), ), ['success', 'approved', 'rejected', 'retry', 'error'], ); expect(stageOutcomes({}), isEmpty); expect(stageOutcomes(null), isEmpty); }); test('edgeLabel marks only an implicit fallback error route', () { expect( edgeLabel({'fallback_error': true}, 'error'), 'error*', ); expect( edgeLabel({'fallback_error': false}, 'error'), 'error', ); expect( edgeLabel({'fallback_error': true}, 'success'), 'success', ); expect(edgeLabel({}, '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), [1, 2, 3, 4, 5]); expect(edges.map((edge) => edge.source), [ 'plan', 'plan', 'build', 'ship', 'ship', ]); expect(edges.map((edge) => edge.outcome), [ 'success', 'error', 'success', 'success', 'error', ]); expect(edges.map((edge) => edge.target), [ 'build', '#failure', 'ship', '#success', '#failure', ]); expect(edges.map((edge) => edge.label), [ 'success', 'error*', 'success', 'success', 'error', ]); expect(edges.where((edge) => edge.fallback).map((edge) => edge.id), [ 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), [ 'plan', 'build', 'ship', failureEnd, successEnd, ]); expect(nodes.map((node) => node.stageIndex), [0, 1, 2, null, null]); expect(nodes.map((node) => node.terminal), [ 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}'), [ '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}'), [ '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), [ '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 = { 'stages': [ stage('a', edges: {'retry': 'a'}), stage('a', edges: {'success': '#success'}), ], }; expect(graphNodes(graph).map((node) => node.key), [ 'a', failureEnd, successEnd, ]); // Both declarations are still definition data, with unique edge ids. expect(graphEdges(graph).map((edge) => edge.id), [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), [1, 2, 3, 4]); expect(edges.map((edge) => edge.outcome), [ '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 = { 'stages': [ stage('a', edges: {'retry': 'ghost'}), ], }; expect(graphNodes(graph).map((node) => node.key), ['a']); expect(graphEdges(graph).single.knownTarget, isFalse); }); test('a graph with no stages has no nodes or edges', () { expect(graphNodes({}), isEmpty); expect(graphEdges({'stages': null}), isEmpty); expect(graphNodes(null), isEmpty); }); }); test( 'definition nodes ignore run evidence even when the graph map carries it', () { final graph = { 'stages': [ stage('a', edges: {'success': 'b'}), stage('b'), ], 'steps': [ step(sequence: 1, stage: 'a', outcome: 'success', target: 'b'), ], 'visited_stages': ['a', 'b'], }; final nodes = graphNodes(graph); expect(nodes.map((node) => node.key), ['a', 'b']); expect(nodes.every((node) => !node.terminal), isTrue); }, ); }