import 'package:flutter_test/flutter_test.dart'; import 'package:pipelines_app/features/graph/domain/graph_protocol.dart'; import 'package:pipelines_app/features/graph/domain/run_graph_state.dart'; import 'graph_fixtures.dart'; void main() { group('linear run evidence', () { test('visits and routes follow the recorded visits', () { final state = runGraphState({ 'executing': false, 'steps': [ step(sequence: 1, stage: 'plan', outcome: 'success', target: 'build'), step(sequence: 2, stage: 'build', outcome: 'success', target: 'ship'), step( sequence: 3, stage: 'ship', outcome: 'success', target: '#success', ), ], }); expect(state.visitedStages, [ 'plan', 'build', 'ship', successEnd, ]); expect(state.takenRoutes, [ 'plan\u0000success\u0000build', 'build\u0000success\u0000ship', 'ship\u0000success\u0000#success', ]); expect(state.currentStage, 'ship'); expect(state.executing, isFalse); expect(state.currentStageStartedAt, isNull); expect(state.activeStage, isNull); }); }); group('diamond run evidence', () { test('repeated visits keep one identity and loop routes stay distinct', () { final state = runGraphState({ 'executing': false, 'steps': [ step(sequence: 1, stage: 'a', outcome: 'success', target: 'b'), step(sequence: 2, stage: 'b', outcome: 'success', target: 'd'), step(sequence: 3, stage: 'd', outcome: 'rejected', target: 'b'), step(sequence: 4, stage: 'b', outcome: 'success', target: 'd'), step(sequence: 5, stage: 'd', outcome: 'success', target: '#success'), ], }); expect(state.visitedStages, ['a', 'b', 'd', successEnd]); expect(state.visitedStages.where((key) => key == 'b'), hasLength(1)); expect(state.takenRoutes, [ 'a\u0000success\u0000b', 'b\u0000success\u0000d', 'd\u0000rejected\u0000b', 'd\u0000success\u0000#success', ]); }); test('different outcomes to the same target stay separate routes', () { final state = runGraphState({ 'executing': false, 'steps': [ step(sequence: 1, stage: 'a', outcome: 'success', target: 'b'), step(sequence: 2, stage: 'a', outcome: 'approved', target: 'b'), step(sequence: 3, stage: 'a', outcome: 'success', target: 'b'), ], }); expect(state.visitedStages, ['a']); expect(state.takenRoutes, [ 'a\u0000success\u0000b', 'a\u0000approved\u0000b', ]); }); }); group('self edges', () { test('a self edge is evidence, not a second visit or recursion', () { final state = runGraphState({ 'executing': false, 'steps': [ step(sequence: 1, stage: 'a', outcome: 'retry', target: 'a'), step(sequence: 2, stage: 'a', outcome: 'success', target: 'b'), ], }); expect(state.visitedStages, ['a']); expect(state.takenRoutes, [ 'a\u0000retry\u0000a', 'a\u0000success\u0000b', ]); }); }); group('terminals', () { test('a bare # resolves to failure on error and success otherwise', () { final failed = runGraphState({ 'steps': [ step(sequence: 1, stage: 'impl', outcome: 'error', target: '#'), ], }); expect(failed.visitedStages, ['impl', failureEnd]); expect(failed.visitedStages, isNot(contains(successEnd))); expect(failed.isRouteTaken('impl', 'error', '#'), isTrue); final succeeded = runGraphState({ 'steps': [ step(sequence: 1, stage: 'impl', outcome: 'done', target: '#'), ], }); expect(succeeded.visitedStages, ['impl', successEnd]); expect(succeeded.visitedStages, isNot(contains(failureEnd))); }); test('a non-terminal target is not visited until a step lands on it', () { final state = runGraphState({ 'steps': [ step( sequence: 1, stage: 'impl', outcome: 'success', target: 'review', ), ], }); expect(state.visitedStages, ['impl']); expect(state.isStageVisited('review'), isFalse); expect(state.isRouteTaken('impl', 'success', 'review'), isTrue); }); test('blank outcome or target records no route and no terminal visit', () { final state = runGraphState({ 'steps': [ step(sequence: 1, stage: 'impl', outcome: 'success', target: ''), step(sequence: 2, stage: 'impl', outcome: '', target: '#success'), ], }); expect(state.visitedStages, ['impl']); expect(state.takenRoutes, isEmpty); }); }); group('missing evidence', () { test('absent run or steps fabricates no visits', () { for (final run in [ null, {}, {'steps': null}, {'steps': []}, ]) { final state = runGraphState(run); expect(state.visitedStages, isEmpty); expect(state.takenRoutes, isEmpty); expect(state.currentStage, isNull); expect(state.executing, isFalse); expect(state.currentStageStartedAt, isNull); } }); test('malformed step entries are ignored, not invented', () { final state = runGraphState({ 'steps': ['nope', 42, null], }); expect(state.visitedStages, isEmpty); expect(state.takenRoutes, isEmpty); }); }); }