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.
This commit is contained in:
@@ -0,0 +1,229 @@
|
||||
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);
|
||||
},
|
||||
);
|
||||
}
|
||||
@@ -0,0 +1,96 @@
|
||||
import 'package:pipelines_app/core/api_gateway.dart';
|
||||
|
||||
/// Small, server-shaped fixtures shared by the graph domain tests.
|
||||
|
||||
JsonMap step({
|
||||
int sequence = 0,
|
||||
String stage = '',
|
||||
String status = 'completed',
|
||||
String outcome = '',
|
||||
String target = '',
|
||||
String startedAt = '',
|
||||
Map<String, dynamic>? control,
|
||||
}) {
|
||||
return <String, dynamic>{
|
||||
'sequence': sequence,
|
||||
'stage': stage,
|
||||
'status': status,
|
||||
'outcome': outcome,
|
||||
'target': target,
|
||||
if (startedAt.isNotEmpty) 'started_at': startedAt,
|
||||
'control': ?control,
|
||||
};
|
||||
}
|
||||
|
||||
JsonMap stage(
|
||||
String key, {
|
||||
Map<String, String?> edges = const {},
|
||||
bool fallbackError = false,
|
||||
String type = 'model',
|
||||
}) {
|
||||
return <String, dynamic>{
|
||||
'key': key,
|
||||
'type': type,
|
||||
'edges': edges,
|
||||
if (fallbackError) 'fallback_error': true,
|
||||
};
|
||||
}
|
||||
|
||||
/// plan -> build -> ship, with an implicit failure fallback on plan and an
|
||||
/// explicit terminal pair on ship.
|
||||
JsonMap linearGraph() => <String, dynamic>{
|
||||
'stages': <dynamic>[
|
||||
stage(
|
||||
'plan',
|
||||
edges: <String, String?>{'success': 'build', 'error': '#'},
|
||||
fallbackError: true,
|
||||
),
|
||||
stage('build', edges: <String, String?>{'success': 'ship'}),
|
||||
stage(
|
||||
'ship',
|
||||
edges: <String, String?>{'success': '#success', 'error': '#failure'},
|
||||
),
|
||||
],
|
||||
};
|
||||
|
||||
/// a fans out to b/c and both converge on d.
|
||||
JsonMap diamondGraph() => <String, dynamic>{
|
||||
'stages': <dynamic>[
|
||||
stage(
|
||||
'a',
|
||||
edges: <String, String?>{'success': 'b', 'approved': 'c', 'error': '#'},
|
||||
fallbackError: true,
|
||||
),
|
||||
stage('b', edges: <String, String?>{'success': 'd'}),
|
||||
stage('c', edges: <String, String?>{'success': 'd'}),
|
||||
stage('d', edges: <String, String?>{'success': '#success'}),
|
||||
],
|
||||
};
|
||||
|
||||
/// a retries itself and can still succeed into b; terminal pair on b.
|
||||
JsonMap selfLoopGraph() => <String, dynamic>{
|
||||
'stages': <dynamic>[
|
||||
stage('a', edges: <String, String?>{'success': 'b', 'retry': 'a'}),
|
||||
stage(
|
||||
'b',
|
||||
edges: <String, String?>{'success': '#success', 'error': '#failure'},
|
||||
),
|
||||
],
|
||||
};
|
||||
|
||||
/// A definition with an absent edge, an empty target and an unknown target.
|
||||
JsonMap incompleteGraph() => <String, dynamic>{
|
||||
'stages': <dynamic>[
|
||||
stage(
|
||||
'a',
|
||||
edges: <String, String?>{
|
||||
'success': null,
|
||||
'blocked': '',
|
||||
'retry': 'ghost',
|
||||
'error': '#',
|
||||
},
|
||||
fallbackError: true,
|
||||
),
|
||||
stage('b', edges: <String, String?>{'success': '#success'}),
|
||||
],
|
||||
};
|
||||
@@ -0,0 +1,56 @@
|
||||
import 'package:flutter_test/flutter_test.dart';
|
||||
import 'package:pipelines_app/features/graph/domain/graph_protocol.dart';
|
||||
|
||||
void main() {
|
||||
group('terminal target normalization', () {
|
||||
test('a bare # resolves by outcome exactly like the web protocol', () {
|
||||
expect(resolveTarget('error', '#'), failureEnd);
|
||||
expect(resolveTarget('success', '#'), successEnd);
|
||||
expect(resolveTarget('approved', '#'), successEnd);
|
||||
expect(resolveTarget('', '#'), successEnd);
|
||||
});
|
||||
|
||||
test('explicit, empty and unknown targets are preserved verbatim', () {
|
||||
expect(resolveTarget('success', successEnd), successEnd);
|
||||
expect(resolveTarget('error', failureEnd), failureEnd);
|
||||
expect(resolveTarget('success', 'review'), 'review');
|
||||
expect(resolveTarget('blocked', ''), '');
|
||||
expect(resolveTarget('retry', 'ghost'), 'ghost');
|
||||
expect(resolveTarget('success', ' #success'), ' #success');
|
||||
});
|
||||
|
||||
test(
|
||||
'a missing target stays missing instead of fabricating a terminal',
|
||||
() {
|
||||
expect(resolveTarget('success', null), isNull);
|
||||
expect(resolveTarget(null, null), isNull);
|
||||
},
|
||||
);
|
||||
|
||||
test('only the explicit terminal keys are terminals', () {
|
||||
expect(isTerminalTarget(successEnd), isTrue);
|
||||
expect(isTerminalTarget(failureEnd), isTrue);
|
||||
expect(isTerminalTarget('#'), isFalse);
|
||||
expect(isTerminalTarget('review'), isFalse);
|
||||
expect(isTerminalTarget(''), isFalse);
|
||||
expect(isTerminalTarget(null), isFalse);
|
||||
});
|
||||
});
|
||||
|
||||
group('terminal labels', () {
|
||||
test('TUI labels are SUCCESS and FAILURE', () {
|
||||
expect(nodeLabel(successEnd), 'SUCCESS');
|
||||
expect(nodeLabel(failureEnd), 'FAILURE');
|
||||
expect(nodeLabel('implementer'), 'implementer');
|
||||
expect(nodeLabel(''), '');
|
||||
expect(nodeLabel(null), '');
|
||||
});
|
||||
|
||||
test('display labels title-case the terminals and keep stage keys', () {
|
||||
expect(nodeDisplayLabel(successEnd), 'Success');
|
||||
expect(nodeDisplayLabel(failureEnd), 'Failure');
|
||||
expect(nodeDisplayLabel('implementer'), 'implementer');
|
||||
expect(nodeDisplayLabel(null), '');
|
||||
});
|
||||
});
|
||||
}
|
||||
@@ -0,0 +1,91 @@
|
||||
import 'package:flutter_test/flutter_test.dart';
|
||||
import 'package:pipelines_app/features/graph/domain/run_graph_state.dart';
|
||||
|
||||
import 'graph_fixtures.dart';
|
||||
|
||||
void main() {
|
||||
group('current stage fallback', () {
|
||||
test('the run current stage wins over the last recorded visit', () {
|
||||
final state = runGraphState(<String, dynamic>{
|
||||
'current_stage': 'committer',
|
||||
'steps': <dynamic>[step(sequence: 1, stage: 'review')],
|
||||
});
|
||||
expect(state.currentStage, 'committer');
|
||||
});
|
||||
|
||||
test('a blank current stage falls back to the last visit, else null', () {
|
||||
final fallback = runGraphState(<String, dynamic>{
|
||||
'current_stage': ' ',
|
||||
'steps': <dynamic>[
|
||||
step(sequence: 1, stage: 'a'),
|
||||
step(sequence: 2, stage: 'b'),
|
||||
],
|
||||
});
|
||||
expect(fallback.currentStage, 'b');
|
||||
|
||||
final blankLast = runGraphState(<String, dynamic>{
|
||||
'steps': <dynamic>[step(sequence: 1)],
|
||||
});
|
||||
expect(blankLast.currentStage, isNull);
|
||||
});
|
||||
});
|
||||
|
||||
group('active stage and start', () {
|
||||
test('a live run exposes its running visit start', () {
|
||||
final state = runGraphState(<String, dynamic>{
|
||||
'executing': true,
|
||||
'current_stage': 'review',
|
||||
'steps': <dynamic>[
|
||||
step(
|
||||
sequence: 1,
|
||||
stage: 'review',
|
||||
status: 'running',
|
||||
startedAt: '2026-01-01T00:00:00Z',
|
||||
),
|
||||
],
|
||||
});
|
||||
expect(state.executing, isTrue);
|
||||
expect(state.activeStage, 'review');
|
||||
expect(state.currentStageStartedAt, '2026-01-01T00:00:00Z');
|
||||
});
|
||||
|
||||
test('a finished run has no active stage or start', () {
|
||||
final state = runGraphState(<String, dynamic>{
|
||||
'executing': false,
|
||||
'current_stage': 'review',
|
||||
'steps': <dynamic>[
|
||||
step(
|
||||
sequence: 1,
|
||||
stage: 'review',
|
||||
status: 'running',
|
||||
startedAt: '2026-01-01T00:00:00Z',
|
||||
),
|
||||
],
|
||||
});
|
||||
expect(state.activeStage, isNull);
|
||||
expect(state.currentStageStartedAt, isNull);
|
||||
});
|
||||
|
||||
test('only an exact running status with a non-blank start is used', () {
|
||||
final state = runGraphState(<String, dynamic>{
|
||||
'executing': true,
|
||||
'current_stage': 'review',
|
||||
'steps': <dynamic>[
|
||||
step(
|
||||
sequence: 1,
|
||||
stage: 'review',
|
||||
status: 'Running',
|
||||
startedAt: '2026-01-01T00:00:00Z',
|
||||
),
|
||||
step(
|
||||
sequence: 2,
|
||||
stage: 'review',
|
||||
status: 'running',
|
||||
startedAt: ' ',
|
||||
),
|
||||
],
|
||||
});
|
||||
expect(state.currentStageStartedAt, isNull);
|
||||
});
|
||||
});
|
||||
}
|
||||
@@ -0,0 +1,170 @@
|
||||
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(<String, dynamic>{
|
||||
'executing': false,
|
||||
'steps': <dynamic>[
|
||||
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, <String>[
|
||||
'plan',
|
||||
'build',
|
||||
'ship',
|
||||
successEnd,
|
||||
]);
|
||||
expect(state.takenRoutes, <String>[
|
||||
'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(<String, dynamic>{
|
||||
'executing': false,
|
||||
'steps': <dynamic>[
|
||||
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, <String>['a', 'b', 'd', successEnd]);
|
||||
expect(state.visitedStages.where((key) => key == 'b'), hasLength(1));
|
||||
expect(state.takenRoutes, <String>[
|
||||
'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(<String, dynamic>{
|
||||
'executing': false,
|
||||
'steps': <dynamic>[
|
||||
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, <String>['a']);
|
||||
expect(state.takenRoutes, <String>[
|
||||
'a\u0000success\u0000b',
|
||||
'a\u0000approved\u0000b',
|
||||
]);
|
||||
});
|
||||
});
|
||||
|
||||
group('self edges', () {
|
||||
test('a self edge is evidence, not a second visit or recursion', () {
|
||||
final state = runGraphState(<String, dynamic>{
|
||||
'executing': false,
|
||||
'steps': <dynamic>[
|
||||
step(sequence: 1, stage: 'a', outcome: 'retry', target: 'a'),
|
||||
step(sequence: 2, stage: 'a', outcome: 'success', target: 'b'),
|
||||
],
|
||||
});
|
||||
expect(state.visitedStages, <String>['a']);
|
||||
expect(state.takenRoutes, <String>[
|
||||
'a\u0000retry\u0000a',
|
||||
'a\u0000success\u0000b',
|
||||
]);
|
||||
});
|
||||
});
|
||||
|
||||
group('terminals', () {
|
||||
test('a bare # resolves to failure on error and success otherwise', () {
|
||||
final failed = runGraphState(<String, dynamic>{
|
||||
'steps': <dynamic>[
|
||||
step(sequence: 1, stage: 'impl', outcome: 'error', target: '#'),
|
||||
],
|
||||
});
|
||||
expect(failed.visitedStages, <String>['impl', failureEnd]);
|
||||
expect(failed.visitedStages, isNot(contains(successEnd)));
|
||||
expect(failed.isRouteTaken('impl', 'error', '#'), isTrue);
|
||||
|
||||
final succeeded = runGraphState(<String, dynamic>{
|
||||
'steps': <dynamic>[
|
||||
step(sequence: 1, stage: 'impl', outcome: 'done', target: '#'),
|
||||
],
|
||||
});
|
||||
expect(succeeded.visitedStages, <String>['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(<String, dynamic>{
|
||||
'steps': <dynamic>[
|
||||
step(
|
||||
sequence: 1,
|
||||
stage: 'impl',
|
||||
outcome: 'success',
|
||||
target: 'review',
|
||||
),
|
||||
],
|
||||
});
|
||||
expect(state.visitedStages, <String>['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(<String, dynamic>{
|
||||
'steps': <dynamic>[
|
||||
step(sequence: 1, stage: 'impl', outcome: 'success', target: ''),
|
||||
step(sequence: 2, stage: 'impl', outcome: '', target: '#success'),
|
||||
],
|
||||
});
|
||||
expect(state.visitedStages, <String>['impl']);
|
||||
expect(state.takenRoutes, isEmpty);
|
||||
});
|
||||
});
|
||||
|
||||
group('missing evidence', () {
|
||||
test('absent run or steps fabricates no visits', () {
|
||||
for (final run in <dynamic>[
|
||||
null,
|
||||
<String, dynamic>{},
|
||||
<String, dynamic>{'steps': null},
|
||||
<String, dynamic>{'steps': <dynamic>[]},
|
||||
]) {
|
||||
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(<String, dynamic>{
|
||||
'steps': <dynamic>['nope', 42, null],
|
||||
});
|
||||
expect(state.visitedStages, isEmpty);
|
||||
expect(state.takenRoutes, isEmpty);
|
||||
});
|
||||
});
|
||||
}
|
||||
@@ -0,0 +1,150 @@
|
||||
import 'package:flutter_test/flutter_test.dart';
|
||||
import 'package:pipelines_app/features/graph/domain/stage_visits.dart';
|
||||
|
||||
import 'graph_fixtures.dart';
|
||||
|
||||
void main() {
|
||||
group('ordered stage visits', () {
|
||||
test('repeated visits are ordered by sequence, not input order', () {
|
||||
final steps = <dynamic>[
|
||||
step(
|
||||
sequence: 5,
|
||||
stage: 'review',
|
||||
outcome: 'rejected',
|
||||
target: 'implementer',
|
||||
control: <String, dynamic>{'visit': 2},
|
||||
),
|
||||
step(sequence: 3, stage: 'implementer'),
|
||||
step(
|
||||
sequence: 2,
|
||||
stage: 'review',
|
||||
outcome: 'success',
|
||||
target: 'done',
|
||||
control: <String, dynamic>{'visit': 1},
|
||||
),
|
||||
].cast<Map<String, dynamic>>();
|
||||
|
||||
final byStage = visitsByStage(steps);
|
||||
expect(byStage.keys, <String>['review', 'implementer']);
|
||||
expect(byStage['review']!.map((visit) => visit['sequence']), <int>[2, 5]);
|
||||
expect(byStage['review']!.map((visit) => visit['outcome']), <String>[
|
||||
'success',
|
||||
'rejected',
|
||||
]);
|
||||
expect(displayVisit(byStage['review']![0], 0), 1);
|
||||
expect(displayVisit(byStage['review']![1], 1), 2);
|
||||
});
|
||||
|
||||
test('equal sequences keep their recorded order', () {
|
||||
final steps = <dynamic>[
|
||||
step(sequence: 1, stage: 'a', outcome: 'first'),
|
||||
step(sequence: 1, stage: 'a', outcome: 'second'),
|
||||
].cast<Map<String, dynamic>>();
|
||||
expect(
|
||||
visitsByStage(steps)['a']!.map((visit) => visit['outcome']),
|
||||
<String>['first', 'second'],
|
||||
);
|
||||
});
|
||||
|
||||
test('a missing control visit falls back to the position', () {
|
||||
final steps = <dynamic>[
|
||||
step(sequence: 1, stage: 'a'),
|
||||
step(sequence: 2, stage: 'a'),
|
||||
].cast<Map<String, dynamic>>();
|
||||
final visits = visitsByStage(steps)['a']!;
|
||||
expect(displayVisit(visits[0], 0), 1);
|
||||
expect(displayVisit(visits[1], 1), 2);
|
||||
});
|
||||
|
||||
test(
|
||||
'a non-positive or malformed control visit falls back to position',
|
||||
() {
|
||||
expect(
|
||||
displayVisit(step(control: <String, dynamic>{'visit': 0}), 2),
|
||||
3,
|
||||
);
|
||||
expect(
|
||||
displayVisit(step(control: <String, dynamic>{'visit': -1}), 2),
|
||||
3,
|
||||
);
|
||||
expect(
|
||||
displayVisit(step(control: <String, dynamic>{'visit': '3'}), 0),
|
||||
3,
|
||||
);
|
||||
expect(displayVisit(step(), 4), 5);
|
||||
},
|
||||
);
|
||||
});
|
||||
|
||||
group('clamping', () {
|
||||
test('keeps a valid index and handles an empty list', () {
|
||||
expect(clampVisitIndex(0, 3), 0);
|
||||
expect(clampVisitIndex(2, 3), 2);
|
||||
expect(clampVisitIndex(99, 3), 2);
|
||||
expect(clampVisitIndex(-4, 3), 0);
|
||||
expect(clampVisitIndex(5, 0), 0);
|
||||
});
|
||||
});
|
||||
|
||||
group('selection', () {
|
||||
List<Map<String, dynamic>> repeatedVisits() => <dynamic>[
|
||||
step(sequence: 5, stage: 'review', outcome: 'rejected'),
|
||||
step(sequence: 2, stage: 'review', outcome: 'success'),
|
||||
].cast<Map<String, dynamic>>();
|
||||
|
||||
test('no requested visit selects the stage most recent recorded visit', () {
|
||||
final selection = selectStageVisit(repeatedVisits(), 'review');
|
||||
expect(selection.hasVisit, isTrue);
|
||||
expect(selection.index, 1);
|
||||
expect(selection.step!['sequence'], 5);
|
||||
expect(selection.visitNumber, 2);
|
||||
});
|
||||
|
||||
test('an explicit visit selects that recorded visit', () {
|
||||
final selection = selectStageVisit(
|
||||
repeatedVisits(),
|
||||
'review',
|
||||
visitIndex: 0,
|
||||
);
|
||||
expect(selection.step!['sequence'], 2);
|
||||
expect(selection.visitNumber, 1);
|
||||
});
|
||||
|
||||
test('out-of-range indexes clamp to the nearest recorded visit', () {
|
||||
final late = selectStageVisit(repeatedVisits(), 'review', visitIndex: 99);
|
||||
expect(late.index, 1);
|
||||
expect(late.step!['sequence'], 5);
|
||||
expect(late.correctedRequest(99), isTrue);
|
||||
|
||||
final early = selectStageVisit(
|
||||
repeatedVisits(),
|
||||
'review',
|
||||
visitIndex: -4,
|
||||
);
|
||||
expect(early.index, 0);
|
||||
expect(early.step!['sequence'], 2);
|
||||
expect(early.correctedRequest(-4), isTrue);
|
||||
expect(early.correctedRequest(null), isFalse);
|
||||
});
|
||||
|
||||
test('a stage with no recorded visit returns no fabricated step', () {
|
||||
final selection = selectStageVisit(repeatedVisits(), 'never-ran');
|
||||
expect(selection.hasVisit, isFalse);
|
||||
expect(selection.step, isNull);
|
||||
expect(selection.index, 0);
|
||||
expect(selection.visitNumber, 0);
|
||||
|
||||
final empty = selectStageVisit(const <Map<String, dynamic>>[], 'review');
|
||||
expect(empty.hasVisit, isFalse);
|
||||
expect(empty.step, isNull);
|
||||
});
|
||||
|
||||
test('an absent capture for one stage does not hide another stage', () {
|
||||
final steps = <dynamic>[
|
||||
step(sequence: 1, stage: 'a', outcome: 'success', target: 'b'),
|
||||
].cast<Map<String, dynamic>>();
|
||||
expect(selectStageVisit(steps, 'a').hasVisit, isTrue);
|
||||
expect(selectStageVisit(steps, 'b').hasVisit, isFalse);
|
||||
});
|
||||
});
|
||||
}
|
||||
Reference in New Issue
Block a user