Merge branch 'main' into flutter/x1-search
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);
|
||||
});
|
||||
});
|
||||
}
|
||||
@@ -0,0 +1,96 @@
|
||||
import 'package:flutter_test/flutter_test.dart';
|
||||
import 'package:pipelines_app/features/graph/domain/graph_definition.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';
|
||||
|
||||
/// Invalid and boundary input for the pure layout engine.
|
||||
void main() {
|
||||
test('an empty definition yields empty padded bounds', () {
|
||||
final layout = layoutGraphNodes(
|
||||
nodes: const <GraphNode>[],
|
||||
edges: const <GraphEdge>[],
|
||||
extents: const <String, NodeExtent>{},
|
||||
config: layoutConfig(columnGap: 30, rowGap: 10, padding: 6),
|
||||
);
|
||||
|
||||
expect(layout.isEmpty, isTrue);
|
||||
expect(layout.rankCount, 0);
|
||||
expect(layout.width, 12);
|
||||
expect(layout.height, 12);
|
||||
expect(layout.ranks, isEmpty);
|
||||
});
|
||||
|
||||
test('missing measurements are reported rather than invented', () {
|
||||
final graph = linearGraphFixture();
|
||||
final nodes = graphNodes(graph);
|
||||
final extents = uniformExtents(nodes)..remove('build');
|
||||
|
||||
expect(
|
||||
() => layoutGraphNodes(
|
||||
nodes: nodes,
|
||||
edges: graphEdges(graph),
|
||||
extents: extents,
|
||||
config: layoutConfig(),
|
||||
),
|
||||
throwsArgumentError,
|
||||
);
|
||||
});
|
||||
|
||||
test('extra measurements are ignored because they cannot become nodes', () {
|
||||
final graph = linearGraphFixture();
|
||||
final nodes = graphNodes(graph);
|
||||
final extents = uniformExtents(nodes)
|
||||
..['ghost'] = NodeExtent(width: 20, height: 20);
|
||||
|
||||
final layout = layoutGraphNodes(
|
||||
nodes: nodes,
|
||||
edges: graphEdges(graph),
|
||||
extents: extents,
|
||||
config: layoutConfig(),
|
||||
);
|
||||
|
||||
expect(layout.nodeFor('ghost'), isNull);
|
||||
expect(layout.nodes.length, nodes.length);
|
||||
});
|
||||
|
||||
test('duplicate or empty node keys are rejected', () {
|
||||
const stageNode = GraphNode(key: 'a', terminal: false, stageIndex: 0);
|
||||
expect(
|
||||
() => layoutGraphNodes(
|
||||
nodes: const <GraphNode>[stageNode, stageNode],
|
||||
edges: const <GraphEdge>[],
|
||||
extents: <String, NodeExtent>{'a': NodeExtent(width: 10, height: 10)},
|
||||
config: layoutConfig(),
|
||||
),
|
||||
throwsArgumentError,
|
||||
);
|
||||
expect(
|
||||
() => layoutGraphNodes(
|
||||
nodes: const <GraphNode>[
|
||||
GraphNode(key: '', terminal: false, stageIndex: 0),
|
||||
],
|
||||
edges: const <GraphEdge>[],
|
||||
extents: const <String, NodeExtent>{},
|
||||
config: layoutConfig(),
|
||||
),
|
||||
throwsArgumentError,
|
||||
);
|
||||
});
|
||||
|
||||
test('invalid gaps and extents are rejected', () {
|
||||
expect(
|
||||
() => GraphLayoutConfig(columnGap: -1, rowGap: 8),
|
||||
throwsArgumentError,
|
||||
);
|
||||
expect(
|
||||
() => GraphLayoutConfig(columnGap: 8, rowGap: double.nan),
|
||||
throwsArgumentError,
|
||||
);
|
||||
expect(
|
||||
() => NodeExtent(width: double.infinity, height: 10),
|
||||
throwsArgumentError,
|
||||
);
|
||||
});
|
||||
}
|
||||
@@ -0,0 +1,228 @@
|
||||
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<String, dynamic> graph, {
|
||||
Map<String, NodeExtent>? 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), <String>[
|
||||
'plan',
|
||||
'build',
|
||||
'ship',
|
||||
failureEnd,
|
||||
successEnd,
|
||||
]);
|
||||
expect(layout.ranks, <String, int>{
|
||||
'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);
|
||||
});
|
||||
}
|
||||
@@ -0,0 +1,116 @@
|
||||
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_ranks.dart';
|
||||
|
||||
import 'layout_fixtures.dart';
|
||||
|
||||
GraphRanks _ranksFor(Map<String, dynamic> graph) {
|
||||
return assignGraphRanks(graphNodes(graph), graphEdges(graph));
|
||||
}
|
||||
|
||||
void main() {
|
||||
test('linear definitions rank one stage per column', () {
|
||||
final ranks = _ranksFor(linearGraphFixture());
|
||||
|
||||
expect(ranks.rankOf, <String, int>{
|
||||
'plan': 0,
|
||||
'build': 1,
|
||||
'ship': 2,
|
||||
failureEnd: 3,
|
||||
successEnd: 3,
|
||||
});
|
||||
expect(ranks.rankCount, 4);
|
||||
expect(ranks.components, <List<String>>[
|
||||
<String>['plan'],
|
||||
<String>['build'],
|
||||
<String>['ship'],
|
||||
<String>[failureEnd],
|
||||
<String>[successEnd],
|
||||
]);
|
||||
});
|
||||
|
||||
test('diamond fan-out shares a rank and converges after it', () {
|
||||
final ranks = _ranksFor(diamondGraphFixture());
|
||||
|
||||
expect(ranks.rankOf['a'], 0);
|
||||
expect(ranks.rankOf['b'], ranks.rankOf['c']);
|
||||
expect(ranks.rankOf['b'], 1);
|
||||
expect(ranks.rankOf['d'], 2);
|
||||
expect(ranks.rankOf[failureEnd], 1);
|
||||
expect(ranks.rankOf[successEnd], 3);
|
||||
expect(ranks.rankCount, 4);
|
||||
});
|
||||
|
||||
test('a cycle collapses to one shared rank instead of recursing', () {
|
||||
final ranks = _ranksFor(cycleGraph());
|
||||
|
||||
expect(ranks.rankOf['a'], 0);
|
||||
expect(ranks.rankOf['b'], 0);
|
||||
expect(ranks.rankOf[failureEnd], 1);
|
||||
expect(ranks.rankOf[successEnd], 1);
|
||||
expect(ranks.rankCount, 2);
|
||||
expect(ranks.components, contains(equals(<String>['a', 'b'])));
|
||||
});
|
||||
|
||||
test('a self edge stays inside its own component and adds no rank', () {
|
||||
final ranks = _ranksFor(selfLoopGraphFixture());
|
||||
|
||||
expect(ranks.rankOf, <String, int>{
|
||||
'a': 0,
|
||||
'b': 1,
|
||||
failureEnd: 2,
|
||||
successEnd: 2,
|
||||
});
|
||||
expect(ranks.components, contains(equals(<String>['a'])));
|
||||
expect(ranks.components, contains(equals(<String>['b'])));
|
||||
});
|
||||
|
||||
test('disconnected stages stay at rank zero', () {
|
||||
final ranks = _ranksFor(disconnectedGraph());
|
||||
|
||||
expect(ranks.rankOf['a'], 0);
|
||||
expect(ranks.rankOf['b'], 0);
|
||||
expect(ranks.rankOf['c'], 0);
|
||||
expect(ranks.rankOf[failureEnd], 1);
|
||||
expect(ranks.rankOf[successEnd], 1);
|
||||
expect(ranks.rankCount, 2);
|
||||
});
|
||||
|
||||
test('unknown and empty edge targets never become ranks or nodes', () {
|
||||
final graph = incompleteGraphFixture();
|
||||
final nodes = graphNodes(graph);
|
||||
final ranks = assignGraphRanks(nodes, graphEdges(graph));
|
||||
|
||||
expect(nodes.map((node) => node.key), isNot(contains('ghost')));
|
||||
expect(ranks.rankOf.containsKey('ghost'), isFalse);
|
||||
expect(ranks.rankOf.containsKey(''), isFalse);
|
||||
expect(ranks.rankOf['a'], 0);
|
||||
expect(ranks.rankOf['b'], 0);
|
||||
expect(ranks.rankOf[failureEnd], 1);
|
||||
expect(ranks.rankOf[successEnd], 1);
|
||||
});
|
||||
|
||||
test('ranks are stable across repeated assignments', () {
|
||||
final graph = diamondGraphFixture();
|
||||
final nodes = graphNodes(graph);
|
||||
final edges = graphEdges(graph);
|
||||
|
||||
final first = assignGraphRanks(nodes, edges);
|
||||
final second = assignGraphRanks(nodes, edges);
|
||||
|
||||
expect(second.rankOf, first.rankOf);
|
||||
expect(second.rankCount, first.rankCount);
|
||||
expect(second.components, first.components);
|
||||
});
|
||||
|
||||
test('a long chain is walked iteratively without overflow', () {
|
||||
const length = 2000;
|
||||
final ranks = _ranksFor(chainGraph(length));
|
||||
|
||||
expect(ranks.rankCount, length + 1);
|
||||
expect(ranks.rankOf['s0'], 0);
|
||||
expect(ranks.rankOf['s${length - 1}'], length - 1);
|
||||
expect(ranks.rankOf[successEnd], length);
|
||||
});
|
||||
}
|
||||
@@ -0,0 +1,102 @@
|
||||
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/layout_models.dart';
|
||||
|
||||
import '../domain/graph_fixtures.dart' as domain;
|
||||
|
||||
/// Small definitions and measured extents shared by the layout tests.
|
||||
|
||||
GraphLayoutConfig layoutConfig({
|
||||
double columnGap = 24,
|
||||
double rowGap = 12,
|
||||
double padding = 4,
|
||||
}) {
|
||||
return GraphLayoutConfig(
|
||||
columnGap: columnGap,
|
||||
rowGap: rowGap,
|
||||
padding: padding,
|
||||
);
|
||||
}
|
||||
|
||||
/// One measured size for every node, optionally scaled to simulate text growth.
|
||||
Map<String, NodeExtent> uniformExtents(
|
||||
Iterable<GraphNode> nodes, {
|
||||
double width = 120,
|
||||
double height = 60,
|
||||
double scale = 1,
|
||||
}) {
|
||||
return <String, NodeExtent>{
|
||||
for (final node in nodes)
|
||||
node.key: NodeExtent(width: width * scale, height: height * scale),
|
||||
};
|
||||
}
|
||||
|
||||
Map<String, NodeExtent> scaledExtents(
|
||||
Map<String, NodeExtent> base,
|
||||
double scale,
|
||||
) {
|
||||
return <String, NodeExtent>{
|
||||
for (final entry in base.entries)
|
||||
entry.key: NodeExtent(
|
||||
width: entry.value.width * scale,
|
||||
height: entry.value.height * scale,
|
||||
),
|
||||
};
|
||||
}
|
||||
|
||||
/// Deliberately uneven measured boxes for the diamond definition.
|
||||
Map<String, NodeExtent> heterogeneousExtents() {
|
||||
return <String, NodeExtent>{
|
||||
'a': NodeExtent(width: 210, height: 84),
|
||||
'b': NodeExtent(width: 92, height: 44),
|
||||
'c': NodeExtent(width: 148, height: 126),
|
||||
'd': NodeExtent(width: 104, height: 58),
|
||||
failureEnd: NodeExtent(width: 72, height: 34),
|
||||
successEnd: NodeExtent(width: 228, height: 96),
|
||||
};
|
||||
}
|
||||
|
||||
/// Two stages in a cycle; both terminals are reached from inside the cycle.
|
||||
Map<String, dynamic> cycleGraph() => <String, dynamic>{
|
||||
'stages': <dynamic>[
|
||||
domain.stage(
|
||||
'a',
|
||||
edges: <String, String?>{'success': 'b', 'error': '#'},
|
||||
fallbackError: true,
|
||||
),
|
||||
domain.stage(
|
||||
'b',
|
||||
edges: <String, String?>{'success': 'a', 'error': '#success'},
|
||||
),
|
||||
],
|
||||
};
|
||||
|
||||
/// Two independent routes and a stage with no edges at all.
|
||||
Map<String, dynamic> disconnectedGraph() => <String, dynamic>{
|
||||
'stages': <dynamic>[
|
||||
domain.stage('a', edges: <String, String?>{'success': '#success'}),
|
||||
domain.stage('b', edges: <String, String?>{'success': '#failure'}),
|
||||
domain.stage('c'),
|
||||
],
|
||||
};
|
||||
|
||||
Map<String, dynamic> linearGraphFixture() => domain.linearGraph();
|
||||
|
||||
Map<String, dynamic> diamondGraphFixture() => domain.diamondGraph();
|
||||
|
||||
Map<String, dynamic> selfLoopGraphFixture() => domain.selfLoopGraph();
|
||||
|
||||
Map<String, dynamic> incompleteGraphFixture() => domain.incompleteGraph();
|
||||
|
||||
/// A long chain, used to prove the iterative rank walk cannot overflow.
|
||||
Map<String, dynamic> chainGraph(int length) => <String, dynamic>{
|
||||
'stages': <dynamic>[
|
||||
for (var index = 0; index < length; index += 1)
|
||||
domain.stage(
|
||||
's$index',
|
||||
edges: <String, String?>{
|
||||
'success': index + 1 < length ? 's${index + 1}' : '#success',
|
||||
},
|
||||
),
|
||||
],
|
||||
};
|
||||
@@ -0,0 +1,192 @@
|
||||
import 'package:flutter/cupertino.dart';
|
||||
import 'package:flutter/material.dart';
|
||||
import 'package:flutter_test/flutter_test.dart';
|
||||
import 'package:pipelines_app/core/api_gateway.dart';
|
||||
import 'package:pipelines_app/features/runs/actions/cancel_eligibility.dart';
|
||||
import 'package:pipelines_app/features/runs/actions/cancel_wording.dart';
|
||||
|
||||
import 'cancel_test_support.dart';
|
||||
|
||||
void main() {
|
||||
test('cancel eligibility matches the web canCancel exactly', () {
|
||||
final cases = <(String, JsonMap, bool)>[
|
||||
('executing run', executingRun(), true),
|
||||
(
|
||||
'waiting while a session export is pending',
|
||||
executingRun(executing: false, status: 'waiting', exportPending: 2),
|
||||
true,
|
||||
),
|
||||
(
|
||||
'waiting with no pending export',
|
||||
executingRun(executing: false, status: 'waiting'),
|
||||
false,
|
||||
),
|
||||
(
|
||||
'status says running but the run is not executing',
|
||||
executingRun(executing: false),
|
||||
false,
|
||||
),
|
||||
(
|
||||
'cancellation already requested',
|
||||
executingRun(cancelRequested: true),
|
||||
false,
|
||||
),
|
||||
('cleanup unconfirmed', executingRun(cleanupUnconfirmed: true), false),
|
||||
(
|
||||
'completed run',
|
||||
executingRun(executing: false, status: 'succeeded'),
|
||||
false,
|
||||
),
|
||||
('failed run', executingRun(executing: false, status: 'failed'), false),
|
||||
];
|
||||
for (final (label, run, expected) in cases) {
|
||||
expect(canCancelRun(run), expected, reason: label);
|
||||
}
|
||||
expect(canCancelRun(null), isFalse);
|
||||
});
|
||||
|
||||
testWidgets(
|
||||
'material cancel asks first, Keep sends nothing, then sends the envelope',
|
||||
(tester) async {
|
||||
final accepted = acceptedCancelRun();
|
||||
final gateway = FakeCancelGateway(
|
||||
(type, fields) async => {'run_detail': accepted},
|
||||
);
|
||||
final changed = <JsonMap>[];
|
||||
await tester.pumpWidget(
|
||||
cancelApp(
|
||||
CancelHost(
|
||||
gateway: gateway,
|
||||
initial: executingRun(),
|
||||
changed: changed,
|
||||
),
|
||||
cupertino: false,
|
||||
),
|
||||
);
|
||||
|
||||
expect(find.byKey(cancelActionKey), findsOneWidget);
|
||||
expect(find.byType(FilledButton), findsOneWidget);
|
||||
expect(gateway.calls, isEmpty);
|
||||
|
||||
await tester.tap(find.byKey(cancelActionKey));
|
||||
await tester.pumpAndSettle();
|
||||
expect(find.byType(AlertDialog), findsOneWidget);
|
||||
expect(find.text(cancelDialogTitle(7)), findsOneWidget);
|
||||
expect(find.text(cancelDialogMessage), findsOneWidget);
|
||||
expect(find.text(cancelKeepLabel), findsOneWidget);
|
||||
expect(gateway.calls, isEmpty);
|
||||
|
||||
await tester.tap(find.byKey(cancelKeepKey));
|
||||
await tester.pumpAndSettle();
|
||||
expect(find.byType(AlertDialog), findsNothing);
|
||||
expect(find.byKey(cancelActionKey), findsOneWidget);
|
||||
expect(gateway.calls, isEmpty);
|
||||
|
||||
await tester.tap(find.byKey(cancelActionKey));
|
||||
await tester.pumpAndSettle();
|
||||
await tester.tap(find.byKey(cancelConfirmKey));
|
||||
await tester.pumpAndSettle();
|
||||
|
||||
expect(gateway.calls.map((call) => call.type), ['cancel_run']);
|
||||
expect(gateway.calls.single.fields, {'run_id': 7});
|
||||
expect(changed.single, equals(accepted));
|
||||
expect(find.byType(AlertDialog), findsNothing);
|
||||
expect(find.byKey(cancelActionKey), findsNothing);
|
||||
},
|
||||
);
|
||||
|
||||
testWidgets(
|
||||
'cupertino cancel uses a native prompt and sends only on confirm',
|
||||
(tester) async {
|
||||
final accepted = acceptedCancelRun();
|
||||
final gateway = FakeCancelGateway(
|
||||
(type, fields) async => {'run_detail': accepted},
|
||||
);
|
||||
final changed = <JsonMap>[];
|
||||
await tester.pumpWidget(
|
||||
cancelApp(
|
||||
CancelHost(
|
||||
gateway: gateway,
|
||||
initial: executingRun(),
|
||||
changed: changed,
|
||||
),
|
||||
cupertino: true,
|
||||
),
|
||||
);
|
||||
|
||||
expect(find.byKey(cancelActionKey), findsOneWidget);
|
||||
expect(find.byType(CupertinoButton), findsOneWidget);
|
||||
expect(find.byType(FilledButton), findsNothing);
|
||||
|
||||
await tester.tap(find.byKey(cancelActionKey));
|
||||
await tester.pumpAndSettle();
|
||||
expect(find.byType(CupertinoAlertDialog), findsOneWidget);
|
||||
expect(find.byType(AlertDialog), findsNothing);
|
||||
expect(find.text(cancelDialogTitle(7)), findsOneWidget);
|
||||
expect(find.text(cancelDialogMessage), findsOneWidget);
|
||||
expect(gateway.calls, isEmpty);
|
||||
|
||||
await tester.tap(find.byKey(cancelKeepKey));
|
||||
await tester.pumpAndSettle();
|
||||
expect(gateway.calls, isEmpty);
|
||||
expect(find.byType(CupertinoAlertDialog), findsNothing);
|
||||
|
||||
await tester.tap(find.byKey(cancelActionKey));
|
||||
await tester.pumpAndSettle();
|
||||
await tester.tap(find.byKey(cancelConfirmKey));
|
||||
await tester.pumpAndSettle();
|
||||
|
||||
expect(gateway.calls.map((call) => call.type), ['cancel_run']);
|
||||
expect(gateway.calls.single.fields, {'run_id': 7});
|
||||
expect(changed.single, equals(accepted));
|
||||
expect(find.byType(CupertinoAlertDialog), findsNothing);
|
||||
expect(find.byKey(cancelActionKey), findsNothing);
|
||||
},
|
||||
);
|
||||
|
||||
testWidgets('the action is absent for runs the web does not cancel', (
|
||||
tester,
|
||||
) async {
|
||||
final gateway = FakeCancelGateway(
|
||||
(type, fields) async => {'run_detail': executingRun()},
|
||||
);
|
||||
final changed = <JsonMap>[];
|
||||
final cases = <(String, JsonMap, bool)>[
|
||||
('executing', executingRun(), true),
|
||||
(
|
||||
'export wait',
|
||||
executingRun(executing: false, status: 'waiting', exportPending: 1),
|
||||
true,
|
||||
),
|
||||
(
|
||||
'no export wait',
|
||||
executingRun(executing: false, status: 'waiting'),
|
||||
false,
|
||||
),
|
||||
('stale running status', executingRun(executing: false), false),
|
||||
('already stopping', executingRun(cancelRequested: true), false),
|
||||
('cleanup unconfirmed', executingRun(cleanupUnconfirmed: true), false),
|
||||
('finished', executingRun(executing: false, status: 'succeeded'), false),
|
||||
];
|
||||
for (final (label, run, visible) in cases) {
|
||||
await tester.pumpWidget(
|
||||
cancelApp(
|
||||
CancelHost(
|
||||
key: ValueKey(label),
|
||||
gateway: gateway,
|
||||
initial: run,
|
||||
changed: changed,
|
||||
),
|
||||
cupertino: false,
|
||||
),
|
||||
);
|
||||
expect(
|
||||
find.byKey(cancelActionKey),
|
||||
visible ? findsOneWidget : findsNothing,
|
||||
reason: label,
|
||||
);
|
||||
}
|
||||
expect(gateway.calls, isEmpty);
|
||||
expect(changed, isEmpty);
|
||||
});
|
||||
}
|
||||
@@ -0,0 +1,271 @@
|
||||
import 'dart:async';
|
||||
|
||||
import 'package:flutter/cupertino.dart';
|
||||
import 'package:flutter/material.dart';
|
||||
import 'package:flutter_test/flutter_test.dart';
|
||||
import 'package:pipelines_app/core/api_gateway.dart';
|
||||
import 'package:pipelines_app/core/transport/connection_state.dart';
|
||||
import 'package:pipelines_app/features/runs/actions/cancel_wording.dart';
|
||||
|
||||
import 'cancel_test_support.dart';
|
||||
|
||||
Future<void> _pump(
|
||||
WidgetTester tester,
|
||||
FakeCancelGateway gateway,
|
||||
List<JsonMap> changed, {
|
||||
bool cupertino = false,
|
||||
double textScale = 1,
|
||||
JsonMap? run,
|
||||
}) async {
|
||||
await tester.pumpWidget(
|
||||
cancelApp(
|
||||
CancelHost(
|
||||
gateway: gateway,
|
||||
initial: run ?? executingRun(),
|
||||
changed: changed,
|
||||
),
|
||||
cupertino: cupertino,
|
||||
textScale: textScale,
|
||||
),
|
||||
);
|
||||
}
|
||||
|
||||
Future<void> _openAndConfirm(WidgetTester tester) async {
|
||||
await tester.tap(find.byKey(cancelActionKey));
|
||||
await tester.pumpAndSettle();
|
||||
await tester.tap(find.byKey(cancelConfirmKey));
|
||||
}
|
||||
|
||||
void main() {
|
||||
testWidgets('a refusal stays visible and delivers the resynced run', (
|
||||
tester,
|
||||
) async {
|
||||
const refusal = 'run 7 already completed; nothing to cancel';
|
||||
final refreshed = executingRun(executing: false, status: 'succeeded');
|
||||
final gateway = FakeCancelGateway((type, fields) async {
|
||||
if (type == 'cancel_run') throw const ProtocolError(refusal);
|
||||
expect(type, 'get_run');
|
||||
return {'run_detail': refreshed};
|
||||
});
|
||||
final changed = <JsonMap>[];
|
||||
await _pump(tester, gateway, changed);
|
||||
|
||||
await _openAndConfirm(tester);
|
||||
await tester.pumpAndSettle();
|
||||
|
||||
expect(gateway.calls.map((call) => call.type), ['cancel_run', 'get_run']);
|
||||
expect(gateway.calls.first.fields, {'run_id': 7});
|
||||
expect(gateway.calls.last.fields, {'run_id': 7});
|
||||
expect(find.text(refusal), findsOneWidget);
|
||||
expect(find.byKey(cancelErrorKey), findsOneWidget);
|
||||
expect(find.byType(AlertDialog), findsOneWidget);
|
||||
expect(changed.single, equals(refreshed));
|
||||
expect(find.byKey(cancelActionKey), findsNothing);
|
||||
});
|
||||
|
||||
testWidgets('a failed resync never hides the refusal or invents a run', (
|
||||
tester,
|
||||
) async {
|
||||
const refusal = 'server refused to stop the run';
|
||||
final gateway = FakeCancelGateway((type, fields) async {
|
||||
if (type == 'cancel_run') throw const ProtocolError(refusal);
|
||||
throw const NotConnectedError();
|
||||
});
|
||||
final changed = <JsonMap>[];
|
||||
await _pump(tester, gateway, changed);
|
||||
|
||||
await _openAndConfirm(tester);
|
||||
await tester.pumpAndSettle();
|
||||
|
||||
expect(gateway.calls.map((call) => call.type), ['cancel_run', 'get_run']);
|
||||
expect(find.text(refusal), findsOneWidget);
|
||||
expect(find.byType(AlertDialog), findsOneWidget);
|
||||
expect(changed, isEmpty);
|
||||
expect(find.byKey(cancelActionKey), findsOneWidget);
|
||||
});
|
||||
|
||||
testWidgets('a refusal and Keep do not wait for the resync', (tester) async {
|
||||
const refusal = 'run 7 is already stopping';
|
||||
final resync = Completer<JsonMap>();
|
||||
final gateway = FakeCancelGateway((type, fields) {
|
||||
if (type == 'cancel_run') throw const ProtocolError(refusal);
|
||||
return resync.future;
|
||||
});
|
||||
final changed = <JsonMap>[];
|
||||
await _pump(tester, gateway, changed);
|
||||
|
||||
await _openAndConfirm(tester);
|
||||
await tester.pump();
|
||||
|
||||
expect(find.text(refusal), findsOneWidget);
|
||||
expect(
|
||||
tester.widget<TextButton>(find.byKey(cancelKeepKey)).onPressed,
|
||||
isNotNull,
|
||||
);
|
||||
expect(
|
||||
tester.widget<FilledButton>(find.byKey(cancelConfirmKey)).onPressed,
|
||||
isNull,
|
||||
);
|
||||
expect(changed, isEmpty);
|
||||
|
||||
await tester.tap(find.byKey(cancelKeepKey));
|
||||
await tester.pumpAndSettle();
|
||||
expect(find.byType(AlertDialog), findsNothing);
|
||||
|
||||
resync.complete({
|
||||
'run_detail': executingRun(executing: false, status: 'succeeded'),
|
||||
});
|
||||
await tester.pump();
|
||||
|
||||
expect(tester.takeException(), isNull);
|
||||
expect(changed.single['status'], 'succeeded');
|
||||
});
|
||||
|
||||
testWidgets('a late resync after disposal is ignored safely', (tester) async {
|
||||
final resync = Completer<JsonMap>();
|
||||
final gateway = FakeCancelGateway((type, fields) {
|
||||
if (type == 'cancel_run') throw const ProtocolError('already stopped');
|
||||
return resync.future;
|
||||
});
|
||||
final changed = <JsonMap>[];
|
||||
await _pump(tester, gateway, changed);
|
||||
|
||||
await _openAndConfirm(tester);
|
||||
await tester.pump();
|
||||
expect(gateway.calls.map((call) => call.type), ['cancel_run', 'get_run']);
|
||||
|
||||
await tester.pumpWidget(const SizedBox());
|
||||
resync.complete({'run_detail': acceptedCancelRun()});
|
||||
await tester.pump();
|
||||
|
||||
expect(tester.takeException(), isNull);
|
||||
expect(changed, isEmpty);
|
||||
});
|
||||
|
||||
testWidgets('an ineligible resync disables confirm but keeps the refusal', (
|
||||
tester,
|
||||
) async {
|
||||
const refusal = 'run 7 already completed; nothing to cancel';
|
||||
final finished = executingRun(executing: false, status: 'succeeded');
|
||||
final gateway = FakeCancelGateway((type, fields) async {
|
||||
if (type == 'cancel_run') throw const ProtocolError(refusal);
|
||||
return {'run_detail': finished};
|
||||
});
|
||||
final changed = <JsonMap>[];
|
||||
await _pump(tester, gateway, changed);
|
||||
|
||||
await _openAndConfirm(tester);
|
||||
await tester.pumpAndSettle();
|
||||
|
||||
expect(gateway.calls.map((call) => call.type), ['cancel_run', 'get_run']);
|
||||
expect(find.text(refusal), findsOneWidget);
|
||||
expect(
|
||||
tester.widget<FilledButton>(find.byKey(cancelConfirmKey)).onPressed,
|
||||
isNull,
|
||||
);
|
||||
expect(
|
||||
tester.widget<TextButton>(find.byKey(cancelKeepKey)).onPressed,
|
||||
isNotNull,
|
||||
);
|
||||
|
||||
await tester.tap(find.byKey(cancelConfirmKey));
|
||||
await tester.pump();
|
||||
expect(gateway.calls.map((call) => call.type), ['cancel_run', 'get_run']);
|
||||
|
||||
await tester.tap(find.byKey(cancelKeepKey));
|
||||
await tester.pumpAndSettle();
|
||||
expect(find.byType(AlertDialog), findsNothing);
|
||||
expect(find.byKey(cancelActionKey), findsNothing);
|
||||
expect(changed.single, equals(finished));
|
||||
});
|
||||
|
||||
testWidgets(
|
||||
'confirm cannot submit twice while the cancellation is in flight',
|
||||
(tester) async {
|
||||
final reply = Completer<JsonMap>();
|
||||
final gateway = FakeCancelGateway((type, fields) => reply.future);
|
||||
final changed = <JsonMap>[];
|
||||
await _pump(tester, gateway, changed);
|
||||
|
||||
await tester.tap(find.byKey(cancelActionKey));
|
||||
await tester.pumpAndSettle();
|
||||
final confirm = find.byKey(cancelConfirmKey);
|
||||
await tester.tap(confirm);
|
||||
await tester.tap(confirm);
|
||||
await tester.pump();
|
||||
|
||||
expect(
|
||||
gateway.calls.where((call) => call.type == 'cancel_run'),
|
||||
hasLength(1),
|
||||
);
|
||||
expect(
|
||||
tester.widget<FilledButton>(find.byKey(cancelConfirmKey)).onPressed,
|
||||
isNull,
|
||||
);
|
||||
expect(
|
||||
tester.widget<TextButton>(find.byKey(cancelKeepKey)).onPressed,
|
||||
isNull,
|
||||
);
|
||||
expect(find.byType(AlertDialog), findsOneWidget);
|
||||
|
||||
reply.complete({'run_detail': acceptedCancelRun()});
|
||||
await tester.pumpAndSettle();
|
||||
expect(find.byType(AlertDialog), findsNothing);
|
||||
expect(changed, hasLength(1));
|
||||
},
|
||||
);
|
||||
|
||||
testWidgets('a late reply after disposal is ignored safely', (tester) async {
|
||||
final reply = Completer<JsonMap>();
|
||||
final gateway = FakeCancelGateway((type, fields) => reply.future);
|
||||
final changed = <JsonMap>[];
|
||||
await _pump(tester, gateway, changed);
|
||||
|
||||
await _openAndConfirm(tester);
|
||||
await tester.pump();
|
||||
expect(gateway.calls, hasLength(1));
|
||||
|
||||
await tester.pumpWidget(const SizedBox());
|
||||
reply.complete({'run_detail': acceptedCancelRun()});
|
||||
await tester.pump();
|
||||
|
||||
expect(tester.takeException(), isNull);
|
||||
expect(changed, isEmpty);
|
||||
});
|
||||
|
||||
testWidgets('both prompts fit 2x text at a narrow width', (tester) async {
|
||||
tester.view.physicalSize = const Size(320, 480);
|
||||
tester.view.devicePixelRatio = 1;
|
||||
addTearDown(tester.view.resetPhysicalSize);
|
||||
addTearDown(tester.view.resetDevicePixelRatio);
|
||||
|
||||
for (final cupertino in [false, true]) {
|
||||
final gateway = FakeCancelGateway(
|
||||
(type, fields) async => {'run_detail': acceptedCancelRun()},
|
||||
);
|
||||
await _pump(
|
||||
tester,
|
||||
gateway,
|
||||
<JsonMap>[],
|
||||
cupertino: cupertino,
|
||||
textScale: 2,
|
||||
);
|
||||
await tester.tap(find.byKey(cancelActionKey));
|
||||
await tester.pumpAndSettle();
|
||||
|
||||
expect(tester.takeException(), isNull, reason: 'cupertino=$cupertino');
|
||||
expect(find.text(cancelDialogTitle(7)), findsOneWidget);
|
||||
expect(find.text(cancelDialogMessage), findsOneWidget);
|
||||
expect(
|
||||
cupertino
|
||||
? find.byType(CupertinoAlertDialog)
|
||||
: find.byType(AlertDialog),
|
||||
findsOneWidget,
|
||||
);
|
||||
expect(gateway.calls, isEmpty);
|
||||
|
||||
await tester.tap(find.byKey(cancelKeepKey));
|
||||
await tester.pumpAndSettle();
|
||||
}
|
||||
});
|
||||
}
|
||||
@@ -0,0 +1,101 @@
|
||||
import 'package:flutter/cupertino.dart';
|
||||
import 'package:flutter/material.dart';
|
||||
import 'package:pipelines_app/core/api_gateway.dart';
|
||||
import 'package:pipelines_app/features/runs/actions/cancel_run_action.dart';
|
||||
|
||||
typedef CancelHandler = Future<JsonMap> Function(String type, JsonMap fields);
|
||||
|
||||
/// A fake gateway that records every request and never owns a socket.
|
||||
class FakeCancelGateway implements ApiGateway {
|
||||
FakeCancelGateway(this.handler);
|
||||
|
||||
final CancelHandler handler;
|
||||
final List<({String type, JsonMap fields})> calls = [];
|
||||
|
||||
@override
|
||||
Future<JsonMap> call(String type, [JsonMap fields = const {}]) {
|
||||
final copy = Map<String, dynamic>.from(fields);
|
||||
calls.add((type: type, fields: copy));
|
||||
return handler(type, copy);
|
||||
}
|
||||
|
||||
@override
|
||||
Stream<JsonMap> get frames => const Stream.empty();
|
||||
}
|
||||
|
||||
JsonMap executingRun({
|
||||
num id = 7,
|
||||
bool executing = true,
|
||||
String status = 'running',
|
||||
int exportPending = 0,
|
||||
bool cancelRequested = false,
|
||||
bool cleanupUnconfirmed = false,
|
||||
}) => {
|
||||
'id': id,
|
||||
'title': 'Nightly build',
|
||||
'status': status,
|
||||
'executing': executing,
|
||||
'export_pending_sequence': exportPending,
|
||||
'cancel_requested': cancelRequested,
|
||||
'cleanup_unconfirmed': cleanupUnconfirmed,
|
||||
};
|
||||
|
||||
JsonMap acceptedCancelRun() =>
|
||||
executingRun(executing: false, status: 'canceled', cancelRequested: true);
|
||||
|
||||
/// Owns the run like a run-detail screen would, so the callback's returned
|
||||
/// server detail is what the action renders next.
|
||||
class CancelHost extends StatefulWidget {
|
||||
const CancelHost({
|
||||
super.key,
|
||||
required this.gateway,
|
||||
required this.initial,
|
||||
required this.changed,
|
||||
});
|
||||
|
||||
final FakeCancelGateway gateway;
|
||||
final JsonMap initial;
|
||||
final List<JsonMap> changed;
|
||||
|
||||
@override
|
||||
State<CancelHost> createState() => _CancelHostState();
|
||||
}
|
||||
|
||||
class _CancelHostState extends State<CancelHost> {
|
||||
late JsonMap _run = widget.initial;
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
return CancelRunAction(
|
||||
run: _run,
|
||||
gateway: widget.gateway,
|
||||
onRunChanged: (run) {
|
||||
widget.changed.add(run);
|
||||
setState(() => _run = run);
|
||||
},
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
Widget cancelApp(
|
||||
Widget child, {
|
||||
required bool cupertino,
|
||||
double textScale = 1,
|
||||
}) {
|
||||
Widget media(BuildContext context, Widget? app) => MediaQuery(
|
||||
data: MediaQuery.of(
|
||||
context,
|
||||
).copyWith(textScaler: TextScaler.linear(textScale)),
|
||||
child: app!,
|
||||
);
|
||||
if (cupertino) {
|
||||
return CupertinoApp(
|
||||
builder: media,
|
||||
home: CupertinoPageScaffold(child: Center(child: child)),
|
||||
);
|
||||
}
|
||||
return MaterialApp(
|
||||
builder: media,
|
||||
home: Scaffold(body: Center(child: child)),
|
||||
);
|
||||
}
|
||||
@@ -0,0 +1,265 @@
|
||||
import 'package:flutter/cupertino.dart';
|
||||
import 'package:flutter/material.dart';
|
||||
import 'package:flutter_test/flutter_test.dart';
|
||||
import 'package:pipelines_app/core/api_gateway.dart';
|
||||
import 'package:pipelines_app/features/runs/actions/decision_action.dart';
|
||||
import 'package:pipelines_app/features/runs/actions/decision_wording.dart';
|
||||
|
||||
typedef _Handler = Future<JsonMap> Function(String type, JsonMap fields);
|
||||
|
||||
class _FakeGateway implements ApiGateway {
|
||||
_FakeGateway(this.handler);
|
||||
|
||||
final _Handler handler;
|
||||
final List<({String type, JsonMap fields})> calls = [];
|
||||
|
||||
@override
|
||||
Future<JsonMap> call(String type, [JsonMap fields = const {}]) {
|
||||
final copy = Map<String, dynamic>.from(fields);
|
||||
calls.add((type: type, fields: copy));
|
||||
return handler(type, copy);
|
||||
}
|
||||
|
||||
@override
|
||||
Stream<JsonMap> get frames => const Stream.empty();
|
||||
}
|
||||
|
||||
JsonMap _waitingRun({
|
||||
String status = 'waiting',
|
||||
int exportPending = 0,
|
||||
String type = 'human',
|
||||
List<String> options = const ['go', 'skip'],
|
||||
}) => {
|
||||
'id': 7,
|
||||
'title': 'Report',
|
||||
'name': 'Build',
|
||||
'status': status,
|
||||
'current_stage': 'human',
|
||||
'export_pending_sequence': exportPending,
|
||||
'graph': {
|
||||
'stages': [
|
||||
{
|
||||
'key': 'human',
|
||||
'type': type,
|
||||
'prompt': 'Graph prompt',
|
||||
'edges': {'go': '#', 'skip': 'finish'},
|
||||
},
|
||||
],
|
||||
},
|
||||
'steps': [
|
||||
{
|
||||
'sequence': 1,
|
||||
'status': 'waiting',
|
||||
'stage': 'human',
|
||||
'output': {'prompt': 'Ship it?', 'options': options},
|
||||
},
|
||||
],
|
||||
};
|
||||
|
||||
JsonMap _decided() => {
|
||||
..._waitingRun(),
|
||||
'status': 'succeeded',
|
||||
'steps': const <JsonMap>[],
|
||||
};
|
||||
|
||||
Widget _app(Widget child, {required bool cupertino, double textScale = 1}) {
|
||||
if (cupertino) {
|
||||
return CupertinoApp(
|
||||
builder: (context, app) => MediaQuery(
|
||||
data: MediaQuery.of(
|
||||
context,
|
||||
).copyWith(textScaler: TextScaler.linear(textScale)),
|
||||
child: app!,
|
||||
),
|
||||
home: CupertinoPageScaffold(child: Center(child: child)),
|
||||
);
|
||||
}
|
||||
return MaterialApp(
|
||||
builder: (context, app) => MediaQuery(
|
||||
data: MediaQuery.of(
|
||||
context,
|
||||
).copyWith(textScaler: TextScaler.linear(textScale)),
|
||||
child: app!,
|
||||
),
|
||||
home: Scaffold(body: Center(child: child)),
|
||||
);
|
||||
}
|
||||
|
||||
Future<void> _pump(
|
||||
WidgetTester tester,
|
||||
_FakeGateway gateway,
|
||||
JsonMap run, {
|
||||
required bool cupertino,
|
||||
required List<JsonMap> changed,
|
||||
double textScale = 1,
|
||||
}) async {
|
||||
await tester.pumpWidget(
|
||||
_app(
|
||||
HumanDecisionAction(
|
||||
run: run,
|
||||
gateway: gateway,
|
||||
onRunChanged: changed.add,
|
||||
),
|
||||
cupertino: cupertino,
|
||||
textScale: textScale,
|
||||
),
|
||||
);
|
||||
}
|
||||
|
||||
Future<void> _open(WidgetTester tester) async {
|
||||
await tester.tap(find.byKey(decisionActionKey));
|
||||
await tester.pumpAndSettle();
|
||||
}
|
||||
|
||||
void main() {
|
||||
testWidgets(
|
||||
'material decision waits for option and confirm, then sends exact fields',
|
||||
(tester) async {
|
||||
final gateway = _FakeGateway(
|
||||
(type, fields) async => {
|
||||
'run_detail': type == 'decide_run' ? _decided() : _waitingRun(),
|
||||
},
|
||||
);
|
||||
final changed = <JsonMap>[];
|
||||
await _pump(
|
||||
tester,
|
||||
gateway,
|
||||
_waitingRun(),
|
||||
cupertino: false,
|
||||
changed: changed,
|
||||
);
|
||||
|
||||
expect(find.byKey(decisionActionKey), findsOneWidget);
|
||||
expect(find.byType(FilledButton), findsOneWidget);
|
||||
expect(gateway.calls, isEmpty);
|
||||
|
||||
await _open(tester);
|
||||
expect(find.byType(AlertDialog), findsOneWidget);
|
||||
expect(find.text(decisionWaitingTitle), findsOneWidget);
|
||||
expect(find.text('Ship it?'), findsOneWidget);
|
||||
expect(find.text('→ SUCCESS'), findsOneWidget);
|
||||
expect(gateway.calls, isEmpty);
|
||||
|
||||
final confirm = find.widgetWithText(FilledButton, decisionConfirmLabel);
|
||||
expect(tester.widget<FilledButton>(confirm).onPressed, isNull);
|
||||
|
||||
await tester.tap(find.text('go'));
|
||||
await tester.pump();
|
||||
expect(gateway.calls, isEmpty);
|
||||
expect(tester.widget<FilledButton>(confirm).onPressed, isNotNull);
|
||||
|
||||
await tester.enterText(find.byKey(decisionFeedbackKey), ' looks good ');
|
||||
await tester.tap(find.text(decisionConfirmLabel));
|
||||
await tester.pumpAndSettle();
|
||||
|
||||
expect(gateway.calls.map((call) => call.type), ['decide_run']);
|
||||
expect(gateway.calls.single.fields, {
|
||||
'run_id': 7,
|
||||
'stage': 'human',
|
||||
'option': 'go',
|
||||
'feedback': 'looks good',
|
||||
});
|
||||
expect(changed.single['status'], 'succeeded');
|
||||
expect(find.byType(AlertDialog), findsNothing);
|
||||
expect(find.byKey(decisionActionKey), findsNothing);
|
||||
},
|
||||
);
|
||||
|
||||
testWidgets(
|
||||
'cupertino decision uses a native dialog and omits empty feedback',
|
||||
(tester) async {
|
||||
final gateway = _FakeGateway(
|
||||
(type, fields) async => {
|
||||
'run_detail': type == 'decide_run' ? _decided() : _waitingRun(),
|
||||
},
|
||||
);
|
||||
final changed = <JsonMap>[];
|
||||
await _pump(
|
||||
tester,
|
||||
gateway,
|
||||
_waitingRun(),
|
||||
cupertino: true,
|
||||
changed: changed,
|
||||
);
|
||||
|
||||
expect(find.byKey(decisionActionKey), findsOneWidget);
|
||||
expect(find.byType(FilledButton), findsNothing);
|
||||
await _open(tester);
|
||||
expect(find.byType(CupertinoAlertDialog), findsOneWidget);
|
||||
expect(find.text(decisionWaitingTitle), findsOneWidget);
|
||||
expect(gateway.calls, isEmpty);
|
||||
|
||||
final confirm = find.widgetWithText(
|
||||
CupertinoDialogAction,
|
||||
decisionConfirmLabel,
|
||||
);
|
||||
expect(tester.widget<CupertinoDialogAction>(confirm).onPressed, isNull);
|
||||
await tester.tap(find.text('skip'));
|
||||
await tester.pump();
|
||||
expect(gateway.calls, isEmpty);
|
||||
|
||||
await tester.tap(find.text(decisionConfirmLabel));
|
||||
await tester.pumpAndSettle();
|
||||
|
||||
expect(gateway.calls.map((call) => call.type), ['decide_run']);
|
||||
expect(gateway.calls.single.fields, {
|
||||
'run_id': 7,
|
||||
'stage': 'human',
|
||||
'option': 'skip',
|
||||
});
|
||||
expect(changed.single['status'], 'succeeded');
|
||||
expect(find.byType(CupertinoAlertDialog), findsNothing);
|
||||
},
|
||||
);
|
||||
|
||||
testWidgets('Cancel dismisses the draft without sending anything', (
|
||||
tester,
|
||||
) async {
|
||||
final gateway = _FakeGateway(
|
||||
(type, fields) async => {'run_detail': _decided()},
|
||||
);
|
||||
final changed = <JsonMap>[];
|
||||
await _pump(
|
||||
tester,
|
||||
gateway,
|
||||
_waitingRun(),
|
||||
cupertino: false,
|
||||
changed: changed,
|
||||
);
|
||||
|
||||
await _open(tester);
|
||||
await tester.tap(find.text('go'));
|
||||
await tester.pump();
|
||||
await tester.enterText(find.byKey(decisionFeedbackKey), 'not sent');
|
||||
await tester.tap(find.widgetWithText(TextButton, decisionCancelLabel));
|
||||
await tester.pumpAndSettle();
|
||||
|
||||
expect(find.byType(AlertDialog), findsNothing);
|
||||
expect(gateway.calls, isEmpty);
|
||||
expect(changed, isEmpty);
|
||||
expect(find.byKey(decisionActionKey), findsOneWidget);
|
||||
});
|
||||
|
||||
testWidgets('the action is absent unless a run is genuinely waiting', (
|
||||
tester,
|
||||
) async {
|
||||
final gateway = _FakeGateway(
|
||||
(type, fields) async => {'run_detail': _waitingRun()},
|
||||
);
|
||||
final changed = <JsonMap>[];
|
||||
for (final (label, run) in [
|
||||
('not waiting', _waitingRun(status: 'running')),
|
||||
('export pending', _waitingRun(exportPending: 2)),
|
||||
('model stage', _waitingRun(type: 'model')),
|
||||
]) {
|
||||
await _pump(tester, gateway, run, cupertino: false, changed: changed);
|
||||
expect(
|
||||
find.byKey(decisionActionKey),
|
||||
findsNothing,
|
||||
reason: '$label must not offer a decision',
|
||||
);
|
||||
}
|
||||
expect(gateway.calls, isEmpty);
|
||||
expect(changed, isEmpty);
|
||||
});
|
||||
}
|
||||
@@ -0,0 +1,351 @@
|
||||
import 'dart:async';
|
||||
|
||||
import 'package:flutter/cupertino.dart';
|
||||
import 'package:flutter/material.dart';
|
||||
import 'package:flutter_test/flutter_test.dart';
|
||||
import 'package:pipelines_app/core/api_gateway.dart';
|
||||
import 'package:pipelines_app/core/transport/connection_state.dart';
|
||||
import 'package:pipelines_app/features/runs/actions/decision_action.dart';
|
||||
import 'package:pipelines_app/features/runs/actions/decision_wording.dart';
|
||||
|
||||
typedef _Handler = Future<JsonMap> Function(String type, JsonMap fields);
|
||||
|
||||
class _FakeGateway implements ApiGateway {
|
||||
_FakeGateway(this.handler);
|
||||
|
||||
final _Handler handler;
|
||||
final List<({String type, JsonMap fields})> calls = [];
|
||||
|
||||
@override
|
||||
Future<JsonMap> call(String type, [JsonMap fields = const {}]) {
|
||||
final copy = Map<String, dynamic>.from(fields);
|
||||
calls.add((type: type, fields: copy));
|
||||
return handler(type, copy);
|
||||
}
|
||||
|
||||
@override
|
||||
Stream<JsonMap> get frames => const Stream.empty();
|
||||
}
|
||||
|
||||
JsonMap _waitingRun() => {
|
||||
'id': 7,
|
||||
'title': 'Report',
|
||||
'status': 'waiting',
|
||||
'current_stage': 'human',
|
||||
'graph': {
|
||||
'stages': [
|
||||
{
|
||||
'key': 'human',
|
||||
'type': 'human',
|
||||
'prompt': 'Graph prompt',
|
||||
'edges': {'go': '#', 'skip': 'finish'},
|
||||
},
|
||||
],
|
||||
},
|
||||
'steps': [
|
||||
{
|
||||
'sequence': 1,
|
||||
'status': 'waiting',
|
||||
'stage': 'human',
|
||||
'output': {
|
||||
'prompt': 'Ship it?',
|
||||
'options': ['go', 'skip'],
|
||||
},
|
||||
},
|
||||
],
|
||||
};
|
||||
|
||||
JsonMap _decided() => {
|
||||
..._waitingRun(),
|
||||
'status': 'succeeded',
|
||||
'steps': const <JsonMap>[],
|
||||
};
|
||||
|
||||
Widget _app(Widget child, {required bool cupertino, double textScale = 1}) {
|
||||
Widget media(BuildContext context, Widget? app) => MediaQuery(
|
||||
data: MediaQuery.of(
|
||||
context,
|
||||
).copyWith(textScaler: TextScaler.linear(textScale)),
|
||||
child: app!,
|
||||
);
|
||||
if (cupertino) {
|
||||
return CupertinoApp(
|
||||
builder: media,
|
||||
home: CupertinoPageScaffold(child: Center(child: child)),
|
||||
);
|
||||
}
|
||||
return MaterialApp(
|
||||
builder: media,
|
||||
home: Scaffold(body: Center(child: child)),
|
||||
);
|
||||
}
|
||||
|
||||
Future<void> _pump(
|
||||
WidgetTester tester,
|
||||
_FakeGateway gateway,
|
||||
List<JsonMap> changed, {
|
||||
bool cupertino = false,
|
||||
double textScale = 1,
|
||||
}) async {
|
||||
await tester.pumpWidget(
|
||||
_app(
|
||||
HumanDecisionAction(
|
||||
run: _waitingRun(),
|
||||
gateway: gateway,
|
||||
onRunChanged: changed.add,
|
||||
),
|
||||
cupertino: cupertino,
|
||||
textScale: textScale,
|
||||
),
|
||||
);
|
||||
}
|
||||
|
||||
Future<void> _chooseGo(WidgetTester tester, {String feedback = ''}) async {
|
||||
await tester.tap(find.byKey(decisionActionKey));
|
||||
await tester.pumpAndSettle();
|
||||
await tester.tap(find.text('go'));
|
||||
await tester.pump();
|
||||
if (feedback.isNotEmpty) {
|
||||
await tester.enterText(find.byKey(decisionFeedbackKey), feedback);
|
||||
}
|
||||
}
|
||||
|
||||
void main() {
|
||||
testWidgets('a refusal stays visible and keeps the selected draft', (
|
||||
tester,
|
||||
) async {
|
||||
const refusal = 'option "go" is not a valid outcome; choose one of: skip';
|
||||
final gateway = _FakeGateway((type, fields) async {
|
||||
if (type == 'decide_run') throw const ProtocolError(refusal);
|
||||
expect(type, 'get_run');
|
||||
return {
|
||||
'run_detail': {
|
||||
..._waitingRun(),
|
||||
'status': 'failed',
|
||||
'steps': const <JsonMap>[],
|
||||
},
|
||||
};
|
||||
});
|
||||
final changed = <JsonMap>[];
|
||||
await _pump(tester, gateway, changed);
|
||||
await _chooseGo(tester, feedback: ' keep me ');
|
||||
|
||||
await tester.tap(find.text(decisionConfirmLabel));
|
||||
await tester.pumpAndSettle();
|
||||
|
||||
expect(gateway.calls.map((call) => call.type), ['decide_run', 'get_run']);
|
||||
expect(gateway.calls.last.fields, {'run_id': 7});
|
||||
expect(find.text(refusal), findsOneWidget);
|
||||
expect(find.byType(AlertDialog), findsOneWidget);
|
||||
expect(
|
||||
tester
|
||||
.widget<RadioGroup<String>>(find.byType(RadioGroup<String>))
|
||||
.groupValue,
|
||||
'go',
|
||||
);
|
||||
expect(
|
||||
tester
|
||||
.widget<TextField>(find.byKey(decisionFeedbackKey))
|
||||
.controller
|
||||
?.text,
|
||||
' keep me ',
|
||||
);
|
||||
expect(changed.single['status'], 'failed');
|
||||
expect(find.byKey(decisionActionKey), findsNothing);
|
||||
});
|
||||
|
||||
testWidgets('a refusal is shown before the resync keeps the draft', (
|
||||
tester,
|
||||
) async {
|
||||
const refusal = 'option "go" is no longer valid';
|
||||
final resync = Completer<JsonMap>();
|
||||
final gateway = _FakeGateway((type, fields) {
|
||||
if (type == 'decide_run') throw const ProtocolError(refusal);
|
||||
return resync.future;
|
||||
});
|
||||
final changed = <JsonMap>[];
|
||||
await _pump(tester, gateway, changed);
|
||||
await _chooseGo(tester, feedback: ' keep me ');
|
||||
|
||||
await tester.tap(find.text(decisionConfirmLabel));
|
||||
await tester.pump();
|
||||
|
||||
expect(find.text(refusal), findsOneWidget);
|
||||
expect(find.byType(AlertDialog), findsOneWidget);
|
||||
expect(
|
||||
tester
|
||||
.widget<RadioGroup<String>>(find.byType(RadioGroup<String>))
|
||||
.groupValue,
|
||||
'go',
|
||||
);
|
||||
expect(
|
||||
tester
|
||||
.widget<TextField>(find.byKey(decisionFeedbackKey))
|
||||
.controller
|
||||
?.text,
|
||||
' keep me ',
|
||||
);
|
||||
expect(
|
||||
tester
|
||||
.widget<TextButton>(
|
||||
find.widgetWithText(TextButton, decisionCancelLabel),
|
||||
)
|
||||
.onPressed,
|
||||
isNotNull,
|
||||
);
|
||||
expect(changed, isEmpty);
|
||||
|
||||
resync.complete({'run_detail': _decided()});
|
||||
await tester.pumpAndSettle();
|
||||
|
||||
expect(find.text(refusal), findsOneWidget);
|
||||
expect(
|
||||
tester
|
||||
.widget<FilledButton>(
|
||||
find.widgetWithText(FilledButton, decisionConfirmLabel),
|
||||
)
|
||||
.onPressed,
|
||||
isNull,
|
||||
);
|
||||
expect(changed.single['status'], 'succeeded');
|
||||
});
|
||||
|
||||
testWidgets('a failed resync keeps the draft and allows a retry', (
|
||||
tester,
|
||||
) async {
|
||||
const refusal = 'server refused the decision';
|
||||
final gateway = _FakeGateway((type, fields) async {
|
||||
if (type == 'decide_run') throw const ProtocolError(refusal);
|
||||
throw const NotConnectedError();
|
||||
});
|
||||
final changed = <JsonMap>[];
|
||||
await _pump(tester, gateway, changed);
|
||||
await _chooseGo(tester, feedback: ' keep me ');
|
||||
|
||||
await tester.tap(find.text(decisionConfirmLabel));
|
||||
await tester.pumpAndSettle();
|
||||
|
||||
expect(gateway.calls.map((call) => call.type), ['decide_run', 'get_run']);
|
||||
expect(find.text(refusal), findsOneWidget);
|
||||
expect(
|
||||
tester
|
||||
.widget<RadioGroup<String>>(find.byType(RadioGroup<String>))
|
||||
.groupValue,
|
||||
'go',
|
||||
);
|
||||
expect(
|
||||
tester
|
||||
.widget<FilledButton>(
|
||||
find.widgetWithText(FilledButton, decisionConfirmLabel),
|
||||
)
|
||||
.onPressed,
|
||||
isNotNull,
|
||||
);
|
||||
expect(changed, isEmpty);
|
||||
});
|
||||
|
||||
testWidgets('a late resync after disposal is ignored safely', (tester) async {
|
||||
final resync = Completer<JsonMap>();
|
||||
final gateway = _FakeGateway((type, fields) {
|
||||
if (type == 'decide_run') throw const ProtocolError('no longer waiting');
|
||||
return resync.future;
|
||||
});
|
||||
final changed = <JsonMap>[];
|
||||
await _pump(tester, gateway, changed);
|
||||
await _chooseGo(tester);
|
||||
await tester.tap(find.text(decisionConfirmLabel));
|
||||
await tester.pump();
|
||||
expect(gateway.calls.map((call) => call.type), ['decide_run', 'get_run']);
|
||||
|
||||
await tester.pumpWidget(const SizedBox());
|
||||
resync.complete({'run_detail': _decided()});
|
||||
await tester.pump();
|
||||
|
||||
expect(tester.takeException(), isNull);
|
||||
expect(changed, isEmpty);
|
||||
});
|
||||
|
||||
testWidgets('confirm cannot submit twice while the decision is in flight', (
|
||||
tester,
|
||||
) async {
|
||||
final reply = Completer<JsonMap>();
|
||||
final gateway = _FakeGateway((type, fields) => reply.future);
|
||||
final changed = <JsonMap>[];
|
||||
await _pump(tester, gateway, changed);
|
||||
await _chooseGo(tester);
|
||||
|
||||
final confirm = find.text(decisionConfirmLabel);
|
||||
await tester.tap(confirm);
|
||||
await tester.tap(confirm);
|
||||
await tester.pump();
|
||||
|
||||
expect(
|
||||
gateway.calls.where((call) => call.type == 'decide_run'),
|
||||
hasLength(1),
|
||||
);
|
||||
expect(find.text(decisionSendingLabel), findsOneWidget);
|
||||
expect(
|
||||
tester
|
||||
.widget<TextButton>(
|
||||
find.widgetWithText(TextButton, decisionCancelLabel),
|
||||
)
|
||||
.onPressed,
|
||||
isNull,
|
||||
);
|
||||
|
||||
reply.complete({'run_detail': _decided()});
|
||||
await tester.pumpAndSettle();
|
||||
expect(find.byType(AlertDialog), findsNothing);
|
||||
expect(changed, hasLength(1));
|
||||
});
|
||||
|
||||
testWidgets('a late reply after disposal is ignored safely', (tester) async {
|
||||
final reply = Completer<JsonMap>();
|
||||
final gateway = _FakeGateway((type, fields) => reply.future);
|
||||
final changed = <JsonMap>[];
|
||||
await _pump(tester, gateway, changed);
|
||||
await _chooseGo(tester);
|
||||
await tester.tap(find.text(decisionConfirmLabel));
|
||||
await tester.pump();
|
||||
expect(gateway.calls, hasLength(1));
|
||||
|
||||
await tester.pumpWidget(const SizedBox());
|
||||
reply.complete({'run_detail': _decided()});
|
||||
await tester.pump();
|
||||
|
||||
expect(tester.takeException(), isNull);
|
||||
expect(changed, isEmpty);
|
||||
});
|
||||
|
||||
testWidgets('both prompts fit 2x text at a narrow width', (tester) async {
|
||||
tester.view.physicalSize = const Size(320, 480);
|
||||
tester.view.devicePixelRatio = 1;
|
||||
addTearDown(tester.view.resetPhysicalSize);
|
||||
addTearDown(tester.view.resetDevicePixelRatio);
|
||||
|
||||
for (final cupertino in [false, true]) {
|
||||
final gateway = _FakeGateway(
|
||||
(type, fields) async => {'run_detail': _decided()},
|
||||
);
|
||||
await _pump(
|
||||
tester,
|
||||
gateway,
|
||||
<JsonMap>[],
|
||||
cupertino: cupertino,
|
||||
textScale: 2,
|
||||
);
|
||||
await tester.tap(find.byKey(decisionActionKey));
|
||||
await tester.pumpAndSettle();
|
||||
|
||||
expect(tester.takeException(), isNull, reason: 'cupertino=$cupertino');
|
||||
expect(find.text(decisionWaitingTitle), findsOneWidget);
|
||||
expect(find.text('Ship it?'), findsOneWidget);
|
||||
expect(
|
||||
cupertino
|
||||
? find.byType(CupertinoAlertDialog)
|
||||
: find.byType(AlertDialog),
|
||||
findsOneWidget,
|
||||
);
|
||||
}
|
||||
});
|
||||
}
|
||||
@@ -0,0 +1,245 @@
|
||||
import 'package:flutter/cupertino.dart';
|
||||
import 'package:flutter/material.dart';
|
||||
import 'package:flutter_test/flutter_test.dart';
|
||||
import 'package:pipelines_app/core/api_gateway.dart';
|
||||
import 'package:pipelines_app/features/runs/actions/retry_eligibility.dart';
|
||||
import 'package:pipelines_app/features/runs/actions/retry_intent.dart';
|
||||
import 'package:pipelines_app/features/runs/actions/retry_wording.dart';
|
||||
|
||||
import 'retry_test_support.dart';
|
||||
|
||||
void main() {
|
||||
test('retry eligibility matches the web canRetry exactly', () {
|
||||
final cases = <(String, JsonMap, bool)>[
|
||||
('failed and stopped', failedRun(), true),
|
||||
('failed but still executing', failedRun(executing: true), false),
|
||||
('running', failedRun(status: 'running'), false),
|
||||
('waiting', failedRun(status: 'waiting'), false),
|
||||
('succeeded', failedRun(status: 'succeeded'), false),
|
||||
('canceled', failedRun(status: 'canceled'), false),
|
||||
];
|
||||
for (final (label, run, expected) in cases) {
|
||||
expect(canRetryRun(run), expected, reason: label);
|
||||
}
|
||||
expect(canRetryRun(null), isFalse);
|
||||
expect(retryRunId(failedRun()), 7);
|
||||
expect(retryRunId({'id': '7'}), isNull);
|
||||
});
|
||||
|
||||
test('a generated retry key is bounded, run-scoped and fresh', () {
|
||||
final first = newRetryRequestKey(7);
|
||||
final second = newRetryRequestKey(7);
|
||||
|
||||
expect(first, startsWith('app-retry-7-'));
|
||||
expect(first.length, lessThanOrEqualTo(128));
|
||||
expect(second.length, lessThanOrEqualTo(128));
|
||||
expect(first, isNot(second));
|
||||
expect(
|
||||
newRetryRequestKey(7, now: DateTime.utc(2026, 1, 1), random: 36),
|
||||
'app-retry-7-${DateTime.utc(2026, 1, 1).microsecondsSinceEpoch}-10',
|
||||
);
|
||||
});
|
||||
|
||||
testWidgets(
|
||||
'material retry asks first, Keep sends nothing, then omits retry_mode',
|
||||
(tester) async {
|
||||
final accepted = retriedRun();
|
||||
final gateway = FakeRetryGateway(
|
||||
(type, fields) async => {'run_detail': accepted},
|
||||
);
|
||||
final changed = <JsonMap>[];
|
||||
await pumpRetry(tester, gateway: gateway, changed: changed);
|
||||
|
||||
expect(find.byKey(retryActionKey), findsOneWidget);
|
||||
expect(find.byType(FilledButton), findsOneWidget);
|
||||
expect(gateway.calls, isEmpty);
|
||||
|
||||
await tester.tap(find.byKey(retryActionKey));
|
||||
await tester.pumpAndSettle();
|
||||
expect(find.byType(AlertDialog), findsOneWidget);
|
||||
expect(find.text(retryDialogTitle(7)), findsOneWidget);
|
||||
expect(find.text(retryDialogMessage), findsOneWidget);
|
||||
expect(
|
||||
find.descendant(
|
||||
of: find.byType(AlertDialog),
|
||||
matching: find.text(retryConfirmLabel),
|
||||
),
|
||||
findsOneWidget,
|
||||
);
|
||||
expect(find.text(restartConfirmLabel), findsOneWidget);
|
||||
expect(gateway.calls, isEmpty);
|
||||
|
||||
await tester.tap(find.byKey(retryKeepKey));
|
||||
await tester.pumpAndSettle();
|
||||
expect(find.byType(AlertDialog), findsNothing);
|
||||
expect(find.byKey(retryActionKey), findsOneWidget);
|
||||
expect(gateway.calls, isEmpty);
|
||||
|
||||
await tester.tap(find.byKey(retryActionKey));
|
||||
await tester.pumpAndSettle();
|
||||
await tester.tap(find.byKey(retryConfirmKey));
|
||||
await tester.pumpAndSettle();
|
||||
|
||||
expect(gateway.retries, hasLength(1));
|
||||
final fields = gateway.retries.single.fields;
|
||||
expect(fields.keys, containsAll(<String>['run_id', 'request_key']));
|
||||
expect(fields.containsKey('retry_mode'), isFalse);
|
||||
expect(fields['run_id'], 7);
|
||||
expect(fields['request_key'], startsWith('app-retry-7-'));
|
||||
expect(fields['request_key'].length, lessThanOrEqualTo(128));
|
||||
expect(changed.single, equals(accepted));
|
||||
expect(find.byType(AlertDialog), findsNothing);
|
||||
expect(find.byKey(retryActionKey), findsNothing);
|
||||
},
|
||||
);
|
||||
|
||||
testWidgets(
|
||||
'material restart has its own confirmation and sends retry_mode',
|
||||
(tester) async {
|
||||
final accepted = retriedRun();
|
||||
final gateway = FakeRetryGateway(
|
||||
(type, fields) async => {'run_detail': accepted},
|
||||
);
|
||||
final changed = <JsonMap>[];
|
||||
await pumpRetry(tester, gateway: gateway, changed: changed);
|
||||
|
||||
await tester.tap(find.byKey(retryActionKey));
|
||||
await tester.pumpAndSettle();
|
||||
await tester.tap(find.byKey(retryRestartKey));
|
||||
await tester.pumpAndSettle();
|
||||
|
||||
expect(find.text(restartDialogTitle(7)), findsOneWidget);
|
||||
expect(find.text(restartDialogMessage), findsOneWidget);
|
||||
expect(find.text(retryDialogTitle(7)), findsNothing);
|
||||
expect(find.text(retryDialogMessage), findsNothing);
|
||||
expect(find.text(restartConfirmLabel), findsOneWidget);
|
||||
expect(find.byKey(retryRestartKey), findsNothing);
|
||||
expect(gateway.calls, isEmpty);
|
||||
|
||||
await tester.tap(find.byKey(retryConfirmKey));
|
||||
await tester.pumpAndSettle();
|
||||
|
||||
expect(gateway.retries, hasLength(1));
|
||||
final fields = gateway.retries.single.fields;
|
||||
expect(fields['run_id'], 7);
|
||||
expect(fields['retry_mode'], 'restart');
|
||||
expect(fields['request_key'], startsWith('app-retry-7-'));
|
||||
expect(changed.single, equals(accepted));
|
||||
expect(find.byType(AlertDialog), findsNothing);
|
||||
},
|
||||
);
|
||||
|
||||
testWidgets(
|
||||
'cupertino retry uses a native prompt and sends only on confirm',
|
||||
(tester) async {
|
||||
final accepted = retriedRun();
|
||||
final gateway = FakeRetryGateway(
|
||||
(type, fields) async => {'run_detail': accepted},
|
||||
);
|
||||
final changed = <JsonMap>[];
|
||||
await pumpRetry(
|
||||
tester,
|
||||
gateway: gateway,
|
||||
changed: changed,
|
||||
cupertino: true,
|
||||
);
|
||||
|
||||
expect(find.byKey(retryActionKey), findsOneWidget);
|
||||
expect(find.byType(CupertinoButton), findsOneWidget);
|
||||
expect(find.byType(FilledButton), findsNothing);
|
||||
|
||||
await tester.tap(find.byKey(retryActionKey));
|
||||
await tester.pumpAndSettle();
|
||||
expect(find.byType(CupertinoAlertDialog), findsOneWidget);
|
||||
expect(find.byType(AlertDialog), findsNothing);
|
||||
expect(find.text(retryDialogTitle(7)), findsOneWidget);
|
||||
expect(find.text(retryDialogMessage), findsOneWidget);
|
||||
expect(gateway.calls, isEmpty);
|
||||
|
||||
await tester.tap(find.byKey(retryKeepKey));
|
||||
await tester.pumpAndSettle();
|
||||
expect(gateway.calls, isEmpty);
|
||||
expect(find.byType(CupertinoAlertDialog), findsNothing);
|
||||
|
||||
await tester.tap(find.byKey(retryActionKey));
|
||||
await tester.pumpAndSettle();
|
||||
await tester.tap(find.byKey(retryConfirmKey));
|
||||
await tester.pumpAndSettle();
|
||||
|
||||
expect(gateway.retries, hasLength(1));
|
||||
final fields = gateway.retries.single.fields;
|
||||
expect(fields['run_id'], 7);
|
||||
expect(fields.containsKey('retry_mode'), isFalse);
|
||||
expect(fields['request_key'], startsWith('app-retry-7-'));
|
||||
expect(changed.single, equals(accepted));
|
||||
expect(find.byType(CupertinoAlertDialog), findsNothing);
|
||||
},
|
||||
);
|
||||
|
||||
testWidgets('cupertino restart switches to the restart confirmation', (
|
||||
tester,
|
||||
) async {
|
||||
final gateway = FakeRetryGateway(
|
||||
(type, fields) async => {'run_detail': retriedRun()},
|
||||
);
|
||||
await pumpRetry(
|
||||
tester,
|
||||
gateway: gateway,
|
||||
changed: <JsonMap>[],
|
||||
cupertino: true,
|
||||
);
|
||||
|
||||
await tester.tap(find.byKey(retryActionKey));
|
||||
await tester.pumpAndSettle();
|
||||
await tester.tap(find.byKey(retryRestartKey));
|
||||
await tester.pumpAndSettle();
|
||||
|
||||
expect(find.byType(CupertinoAlertDialog), findsOneWidget);
|
||||
expect(find.text(restartDialogTitle(7)), findsOneWidget);
|
||||
expect(find.text(restartDialogMessage), findsOneWidget);
|
||||
expect(gateway.calls, isEmpty);
|
||||
|
||||
await tester.tap(find.byKey(retryConfirmKey));
|
||||
await tester.pumpAndSettle();
|
||||
expect(gateway.retries.single.fields['retry_mode'], 'restart');
|
||||
});
|
||||
|
||||
testWidgets('the action is absent for runs the web does not retry', (
|
||||
tester,
|
||||
) async {
|
||||
final gateway = FakeRetryGateway(
|
||||
(type, fields) async => {'run_detail': retriedRun()},
|
||||
);
|
||||
final changed = <JsonMap>[];
|
||||
final cases = <(String, JsonMap, bool)>[
|
||||
('failed', failedRun(), true),
|
||||
('executing', failedRun(executing: true), false),
|
||||
('running', failedRun(status: 'running'), false),
|
||||
('waiting', failedRun(status: 'waiting'), false),
|
||||
('succeeded', failedRun(status: 'succeeded'), false),
|
||||
('canceled', failedRun(status: 'canceled'), false),
|
||||
];
|
||||
for (final cupertino in [false, true]) {
|
||||
for (final (label, run, visible) in cases) {
|
||||
await tester.pumpWidget(
|
||||
retryApp(
|
||||
RetryHost(
|
||||
key: ValueKey('$label-$cupertino'),
|
||||
gateway: gateway,
|
||||
initial: run,
|
||||
changed: changed,
|
||||
),
|
||||
cupertino: cupertino,
|
||||
),
|
||||
);
|
||||
expect(
|
||||
find.byKey(retryActionKey),
|
||||
visible ? findsOneWidget : findsNothing,
|
||||
reason: '$label cupertino=$cupertino',
|
||||
);
|
||||
}
|
||||
}
|
||||
expect(gateway.calls, isEmpty);
|
||||
expect(changed, isEmpty);
|
||||
});
|
||||
}
|
||||
@@ -0,0 +1,223 @@
|
||||
import 'dart:async';
|
||||
|
||||
import 'package:flutter/material.dart';
|
||||
import 'package:flutter_test/flutter_test.dart';
|
||||
import 'package:pipelines_app/core/api_gateway.dart';
|
||||
import 'package:pipelines_app/core/transport/connection_state.dart';
|
||||
import 'package:pipelines_app/features/runs/actions/retry_eligibility.dart';
|
||||
import 'package:pipelines_app/features/runs/actions/retry_wording.dart';
|
||||
|
||||
import 'retry_test_support.dart';
|
||||
|
||||
Future<void> _openAndConfirm(WidgetTester tester) async {
|
||||
await tester.tap(find.byKey(retryActionKey));
|
||||
await tester.pumpAndSettle();
|
||||
await tester.tap(find.byKey(retryConfirmKey));
|
||||
}
|
||||
|
||||
void main() {
|
||||
testWidgets('a refusal is shown at once and Keep does not wait for resync', (
|
||||
tester,
|
||||
) async {
|
||||
const refusal = 'run 7 is still executing; wait for it to stop completely';
|
||||
final resync = Completer<JsonMap>();
|
||||
final gateway = FakeRetryGateway((type, fields) {
|
||||
if (type == 'retry_run') throw const ProtocolError(refusal);
|
||||
return resync.future;
|
||||
});
|
||||
final changed = <JsonMap>[];
|
||||
await pumpRetry(tester, gateway: gateway, changed: changed);
|
||||
|
||||
await _openAndConfirm(tester);
|
||||
await tester.pump();
|
||||
|
||||
expect(gateway.calls.map((call) => call.type), ['retry_run', 'get_run']);
|
||||
expect(find.text(refusal), findsOneWidget);
|
||||
expect(find.byKey(retryErrorKey), findsOneWidget);
|
||||
expect(
|
||||
tester.widget<TextButton>(find.byKey(retryKeepKey)).onPressed,
|
||||
isNotNull,
|
||||
);
|
||||
expect(
|
||||
tester.widget<FilledButton>(find.byKey(retryConfirmKey)).onPressed,
|
||||
isNull,
|
||||
);
|
||||
expect(changed, isEmpty);
|
||||
|
||||
await tester.tap(find.byKey(retryKeepKey));
|
||||
await tester.pumpAndSettle();
|
||||
expect(find.byType(AlertDialog), findsNothing);
|
||||
|
||||
resync.complete({'run_detail': failedRun(status: 'succeeded')});
|
||||
await tester.pump();
|
||||
await tester.pump();
|
||||
|
||||
expect(tester.takeException(), isNull);
|
||||
expect(changed.single['status'], 'succeeded');
|
||||
expect(find.byKey(retryActionKey), findsNothing);
|
||||
});
|
||||
|
||||
testWidgets('server truth that is ineligible blocks a repeat action', (
|
||||
tester,
|
||||
) async {
|
||||
const refusal = 'only failed runs can be retried';
|
||||
final finished = failedRun(status: 'succeeded');
|
||||
final gateway = FakeRetryGateway((type, fields) async {
|
||||
if (type == 'retry_run') throw const ProtocolError(refusal);
|
||||
return {'run_detail': finished};
|
||||
});
|
||||
final changed = <JsonMap>[];
|
||||
await pumpRetry(tester, gateway: gateway, changed: changed);
|
||||
|
||||
await _openAndConfirm(tester);
|
||||
await tester.pumpAndSettle();
|
||||
|
||||
expect(gateway.calls.map((call) => call.type), ['retry_run', 'get_run']);
|
||||
expect(find.text(refusal), findsOneWidget);
|
||||
expect(
|
||||
tester.widget<FilledButton>(find.byKey(retryConfirmKey)).onPressed,
|
||||
isNull,
|
||||
);
|
||||
expect(
|
||||
tester.widget<TextButton>(find.byKey(retryKeepKey)).onPressed,
|
||||
isNotNull,
|
||||
);
|
||||
|
||||
await tester.tap(find.byKey(retryConfirmKey));
|
||||
await tester.pump();
|
||||
expect(gateway.calls.map((call) => call.type), ['retry_run', 'get_run']);
|
||||
|
||||
await tester.tap(find.byKey(retryKeepKey));
|
||||
await tester.pumpAndSettle();
|
||||
expect(find.byType(AlertDialog), findsNothing);
|
||||
expect(find.byKey(retryActionKey), findsNothing);
|
||||
expect(changed.single, equals(finished));
|
||||
expect(canRetryRun(changed.single), isFalse);
|
||||
});
|
||||
|
||||
testWidgets('a failed recheck keeps the same key for the uncertain request', (
|
||||
tester,
|
||||
) async {
|
||||
const refusal = 'connection dropped while retrying';
|
||||
var attempts = 0;
|
||||
final gateway = FakeRetryGateway((type, fields) async {
|
||||
if (type == 'get_run') throw const NotConnectedError();
|
||||
attempts += 1;
|
||||
if (attempts == 1) throw const ProtocolError(refusal);
|
||||
return {'run_detail': retriedRun()};
|
||||
});
|
||||
final changed = <JsonMap>[];
|
||||
await pumpRetry(tester, gateway: gateway, changed: changed);
|
||||
|
||||
await _openAndConfirm(tester);
|
||||
await tester.pumpAndSettle();
|
||||
|
||||
expect(find.text(refusal), findsOneWidget);
|
||||
expect(
|
||||
tester.widget<FilledButton>(find.byKey(retryConfirmKey)).onPressed,
|
||||
isNotNull,
|
||||
);
|
||||
expect(gateway.retries, hasLength(1));
|
||||
|
||||
await tester.tap(find.byKey(retryConfirmKey));
|
||||
await tester.pumpAndSettle();
|
||||
|
||||
expect(gateway.retries, hasLength(2));
|
||||
expect(
|
||||
gateway.retries.last.fields['request_key'],
|
||||
gateway.retries.first.fields['request_key'],
|
||||
);
|
||||
expect(
|
||||
gateway.retries.every((call) => !call.fields.containsKey('retry_mode')),
|
||||
isTrue,
|
||||
);
|
||||
expect(changed.single, equals(retriedRun()));
|
||||
expect(find.byType(AlertDialog), findsNothing);
|
||||
});
|
||||
|
||||
testWidgets('a restart retry keeps the mode and key across uncertainty', (
|
||||
tester,
|
||||
) async {
|
||||
var attempts = 0;
|
||||
final gateway = FakeRetryGateway((type, fields) async {
|
||||
if (type == 'get_run') throw const NotConnectedError();
|
||||
attempts += 1;
|
||||
if (attempts == 1) throw const ProtocolError('network lost');
|
||||
return {'run_detail': retriedRun()};
|
||||
});
|
||||
await pumpRetry(tester, gateway: gateway, changed: <JsonMap>[]);
|
||||
|
||||
await tester.tap(find.byKey(retryActionKey));
|
||||
await tester.pumpAndSettle();
|
||||
await tester.tap(find.byKey(retryRestartKey));
|
||||
await tester.pumpAndSettle();
|
||||
await tester.tap(find.byKey(retryConfirmKey));
|
||||
await tester.pumpAndSettle();
|
||||
await tester.tap(find.byKey(retryConfirmKey));
|
||||
await tester.pumpAndSettle();
|
||||
|
||||
expect(gateway.retries, hasLength(2));
|
||||
expect(gateway.retries.first.fields['retry_mode'], 'restart');
|
||||
expect(gateway.retries.last.fields['retry_mode'], 'restart');
|
||||
expect(
|
||||
gateway.retries.last.fields['request_key'],
|
||||
gateway.retries.first.fields['request_key'],
|
||||
);
|
||||
});
|
||||
|
||||
testWidgets('a deliberate reopen sends a fresh request key', (tester) async {
|
||||
final gateway = FakeRetryGateway((type, fields) async {
|
||||
if (type == 'get_run') throw const NotConnectedError();
|
||||
throw const ProtocolError('server did not answer');
|
||||
});
|
||||
await pumpRetry(tester, gateway: gateway, changed: <JsonMap>[]);
|
||||
|
||||
await _openAndConfirm(tester);
|
||||
await tester.pumpAndSettle();
|
||||
await tester.tap(find.byKey(retryKeepKey));
|
||||
await tester.pumpAndSettle();
|
||||
|
||||
await _openAndConfirm(tester);
|
||||
await tester.pumpAndSettle();
|
||||
|
||||
expect(gateway.retries, hasLength(2));
|
||||
expect(
|
||||
gateway.retries.first.fields['request_key'],
|
||||
isNot(gateway.retries.last.fields['request_key']),
|
||||
);
|
||||
});
|
||||
|
||||
testWidgets('a restart after a dismissed resume uses a fresh key', (
|
||||
tester,
|
||||
) async {
|
||||
var attempts = 0;
|
||||
final gateway = FakeRetryGateway((type, fields) async {
|
||||
if (type == 'get_run') throw const NotConnectedError();
|
||||
attempts += 1;
|
||||
if (attempts == 1) throw const ProtocolError('resume refused');
|
||||
return {'run_detail': retriedRun()};
|
||||
});
|
||||
await pumpRetry(tester, gateway: gateway, changed: <JsonMap>[]);
|
||||
|
||||
await _openAndConfirm(tester);
|
||||
await tester.pumpAndSettle();
|
||||
// A refusal leaves the prompt open; keep the mode frozen and dismiss.
|
||||
await tester.tap(find.byKey(retryKeepKey));
|
||||
await tester.pumpAndSettle();
|
||||
|
||||
await tester.tap(find.byKey(retryActionKey));
|
||||
await tester.pumpAndSettle();
|
||||
await tester.tap(find.byKey(retryRestartKey));
|
||||
await tester.pumpAndSettle();
|
||||
await tester.tap(find.byKey(retryConfirmKey));
|
||||
await tester.pumpAndSettle();
|
||||
|
||||
expect(gateway.retries, hasLength(2));
|
||||
expect(gateway.retries.first.fields.containsKey('retry_mode'), isFalse);
|
||||
expect(gateway.retries.last.fields['retry_mode'], 'restart');
|
||||
expect(
|
||||
gateway.retries.first.fields['request_key'],
|
||||
isNot(gateway.retries.last.fields['request_key']),
|
||||
);
|
||||
});
|
||||
}
|
||||
@@ -0,0 +1,133 @@
|
||||
import 'dart:async';
|
||||
|
||||
import 'package:flutter/cupertino.dart';
|
||||
import 'package:flutter/material.dart';
|
||||
import 'package:flutter_test/flutter_test.dart';
|
||||
import 'package:pipelines_app/core/api_gateway.dart';
|
||||
import 'package:pipelines_app/core/transport/connection_state.dart';
|
||||
import 'package:pipelines_app/features/runs/actions/retry_wording.dart';
|
||||
|
||||
import 'retry_test_support.dart';
|
||||
|
||||
void main() {
|
||||
testWidgets('confirm cannot submit twice while the retry is in flight', (
|
||||
tester,
|
||||
) async {
|
||||
final reply = Completer<JsonMap>();
|
||||
final gateway = FakeRetryGateway((type, fields) => reply.future);
|
||||
final changed = <JsonMap>[];
|
||||
await pumpRetry(tester, gateway: gateway, changed: changed);
|
||||
|
||||
await tester.tap(find.byKey(retryActionKey));
|
||||
await tester.pumpAndSettle();
|
||||
final confirm = find.byKey(retryConfirmKey);
|
||||
await tester.tap(confirm);
|
||||
await tester.tap(confirm);
|
||||
await tester.pump();
|
||||
|
||||
expect(gateway.retries, hasLength(1));
|
||||
expect(
|
||||
tester.widget<FilledButton>(find.byKey(retryConfirmKey)).onPressed,
|
||||
isNull,
|
||||
);
|
||||
expect(
|
||||
tester.widget<TextButton>(find.byKey(retryKeepKey)).onPressed,
|
||||
isNull,
|
||||
);
|
||||
expect(find.byType(AlertDialog), findsOneWidget);
|
||||
|
||||
reply.complete({'run_detail': retriedRun()});
|
||||
await tester.pumpAndSettle();
|
||||
expect(find.byType(AlertDialog), findsNothing);
|
||||
expect(changed, hasLength(1));
|
||||
});
|
||||
|
||||
testWidgets('a late retry reply after disposal is ignored safely', (
|
||||
tester,
|
||||
) async {
|
||||
final reply = Completer<JsonMap>();
|
||||
final gateway = FakeRetryGateway((type, fields) => reply.future);
|
||||
final changed = <JsonMap>[];
|
||||
await pumpRetry(tester, gateway: gateway, changed: changed);
|
||||
|
||||
await tester.tap(find.byKey(retryActionKey));
|
||||
await tester.pumpAndSettle();
|
||||
await tester.tap(find.byKey(retryConfirmKey));
|
||||
await tester.pump();
|
||||
expect(gateway.retries, hasLength(1));
|
||||
|
||||
await tester.pumpWidget(const SizedBox());
|
||||
reply.complete({'run_detail': retriedRun()});
|
||||
await tester.pump();
|
||||
|
||||
expect(tester.takeException(), isNull);
|
||||
expect(changed, isEmpty);
|
||||
});
|
||||
|
||||
testWidgets('a late resync after disposal is ignored safely', (tester) async {
|
||||
final resync = Completer<JsonMap>();
|
||||
final gateway = FakeRetryGateway((type, fields) {
|
||||
if (type == 'retry_run') throw const ProtocolError('refused');
|
||||
return resync.future;
|
||||
});
|
||||
final changed = <JsonMap>[];
|
||||
await pumpRetry(tester, gateway: gateway, changed: changed);
|
||||
|
||||
await tester.tap(find.byKey(retryActionKey));
|
||||
await tester.pumpAndSettle();
|
||||
await tester.tap(find.byKey(retryConfirmKey));
|
||||
await tester.pump();
|
||||
expect(gateway.calls.map((call) => call.type), ['retry_run', 'get_run']);
|
||||
|
||||
await tester.pumpWidget(const SizedBox());
|
||||
resync.complete({'run_detail': retriedRun()});
|
||||
await tester.pump();
|
||||
|
||||
expect(tester.takeException(), isNull);
|
||||
expect(changed, isEmpty);
|
||||
});
|
||||
|
||||
testWidgets('both confirmations fit 2x text at a narrow width', (
|
||||
tester,
|
||||
) async {
|
||||
tester.view.physicalSize = const Size(320, 480);
|
||||
tester.view.devicePixelRatio = 1;
|
||||
addTearDown(tester.view.resetPhysicalSize);
|
||||
addTearDown(tester.view.resetDevicePixelRatio);
|
||||
|
||||
for (final cupertino in [false, true]) {
|
||||
final gateway = FakeRetryGateway(
|
||||
(type, fields) async => {'run_detail': retriedRun()},
|
||||
);
|
||||
await pumpRetry(
|
||||
tester,
|
||||
gateway: gateway,
|
||||
changed: <JsonMap>[],
|
||||
cupertino: cupertino,
|
||||
textScale: 2,
|
||||
);
|
||||
|
||||
await tester.tap(find.byKey(retryActionKey));
|
||||
await tester.pumpAndSettle();
|
||||
expect(tester.takeException(), isNull, reason: 'cupertino=$cupertino');
|
||||
expect(find.text(retryDialogTitle(7)), findsOneWidget);
|
||||
expect(find.text(retryDialogMessage), findsOneWidget);
|
||||
expect(
|
||||
cupertino
|
||||
? find.byType(CupertinoAlertDialog)
|
||||
: find.byType(AlertDialog),
|
||||
findsOneWidget,
|
||||
);
|
||||
|
||||
await tester.tap(find.byKey(retryRestartKey));
|
||||
await tester.pumpAndSettle();
|
||||
expect(tester.takeException(), isNull, reason: 'restart=$cupertino');
|
||||
expect(find.text(restartDialogTitle(7)), findsOneWidget);
|
||||
expect(find.text(restartDialogMessage), findsOneWidget);
|
||||
|
||||
await tester.tap(find.byKey(retryKeepKey));
|
||||
await tester.pumpAndSettle();
|
||||
expect(gateway.calls, isEmpty);
|
||||
}
|
||||
});
|
||||
}
|
||||
@@ -0,0 +1,112 @@
|
||||
import 'package:flutter/cupertino.dart';
|
||||
import 'package:flutter/material.dart';
|
||||
import 'package:flutter_test/flutter_test.dart';
|
||||
import 'package:pipelines_app/core/api_gateway.dart';
|
||||
import 'package:pipelines_app/features/runs/actions/retry_action.dart';
|
||||
|
||||
typedef RetryHandler = Future<JsonMap> Function(String type, JsonMap fields);
|
||||
|
||||
/// A fake gateway that records every request and never owns a socket.
|
||||
class FakeRetryGateway implements ApiGateway {
|
||||
FakeRetryGateway(this.handler);
|
||||
|
||||
final RetryHandler handler;
|
||||
final List<({String type, JsonMap fields})> calls = [];
|
||||
|
||||
@override
|
||||
Future<JsonMap> call(String type, [JsonMap fields = const {}]) {
|
||||
final copy = Map<String, dynamic>.from(fields);
|
||||
calls.add((type: type, fields: copy));
|
||||
return handler(type, copy);
|
||||
}
|
||||
|
||||
@override
|
||||
Stream<JsonMap> get frames => const Stream.empty();
|
||||
|
||||
Iterable<({String type, JsonMap fields})> get retries =>
|
||||
calls.where((call) => call.type == 'retry_run');
|
||||
}
|
||||
|
||||
JsonMap failedRun({
|
||||
num id = 7,
|
||||
bool executing = false,
|
||||
String status = 'failed',
|
||||
String title = 'Nightly build',
|
||||
}) => {'id': id, 'title': title, 'status': status, 'executing': executing};
|
||||
|
||||
JsonMap retriedRun({num id = 8}) =>
|
||||
failedRun(id: id, status: 'running', executing: true, title: 'Retry of 7');
|
||||
|
||||
/// Owns the run like a run-detail screen would, so the callback's returned
|
||||
/// server detail is what the action renders next.
|
||||
class RetryHost extends StatefulWidget {
|
||||
const RetryHost({
|
||||
super.key,
|
||||
required this.gateway,
|
||||
required this.initial,
|
||||
required this.changed,
|
||||
});
|
||||
|
||||
final FakeRetryGateway gateway;
|
||||
final JsonMap initial;
|
||||
final List<JsonMap> changed;
|
||||
|
||||
@override
|
||||
State<RetryHost> createState() => _RetryHostState();
|
||||
}
|
||||
|
||||
class _RetryHostState extends State<RetryHost> {
|
||||
late JsonMap _run = widget.initial;
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
return RetryRunAction(
|
||||
run: _run,
|
||||
gateway: widget.gateway,
|
||||
onRunChanged: (run) {
|
||||
widget.changed.add(run);
|
||||
setState(() => _run = run);
|
||||
},
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
Widget retryApp(Widget child, {required bool cupertino, double textScale = 1}) {
|
||||
Widget media(BuildContext context, Widget? app) => MediaQuery(
|
||||
data: MediaQuery.of(
|
||||
context,
|
||||
).copyWith(textScaler: TextScaler.linear(textScale)),
|
||||
child: app!,
|
||||
);
|
||||
if (cupertino) {
|
||||
return CupertinoApp(
|
||||
builder: media,
|
||||
home: CupertinoPageScaffold(child: Center(child: child)),
|
||||
);
|
||||
}
|
||||
return MaterialApp(
|
||||
builder: media,
|
||||
home: Scaffold(body: Center(child: child)),
|
||||
);
|
||||
}
|
||||
|
||||
Future<void> pumpRetry(
|
||||
WidgetTester tester, {
|
||||
required FakeRetryGateway gateway,
|
||||
required List<JsonMap> changed,
|
||||
bool cupertino = false,
|
||||
double textScale = 1,
|
||||
JsonMap? run,
|
||||
}) async {
|
||||
await tester.pumpWidget(
|
||||
retryApp(
|
||||
RetryHost(
|
||||
gateway: gateway,
|
||||
initial: run ?? failedRun(),
|
||||
changed: changed,
|
||||
),
|
||||
cupertino: cupertino,
|
||||
textScale: textScale,
|
||||
),
|
||||
);
|
||||
}
|
||||
@@ -0,0 +1,244 @@
|
||||
import 'package:flutter/cupertino.dart';
|
||||
import 'package:flutter/material.dart';
|
||||
import 'package:flutter_test/flutter_test.dart';
|
||||
import 'package:pipelines_app/core/api_gateway.dart';
|
||||
import 'package:pipelines_app/core/transport/connection_state.dart';
|
||||
import 'package:pipelines_app/features/runs/actions/title_action.dart';
|
||||
import 'package:pipelines_app/features/runs/actions/title_wording.dart';
|
||||
|
||||
import 'title_test_support.dart';
|
||||
|
||||
String _materialFieldText(WidgetTester tester) =>
|
||||
tester.widget<TextField>(find.byKey(runTitleFieldKey)).controller!.text;
|
||||
|
||||
String _cupertinoFieldText(WidgetTester tester) => tester
|
||||
.widget<CupertinoTextField>(find.byKey(runTitleFieldKey))
|
||||
.controller!
|
||||
.text;
|
||||
|
||||
void main() {
|
||||
testWidgets(
|
||||
'material rename starts from the stored title, typing sends nothing, '
|
||||
'Cancel sends nothing and Save sends the exact text',
|
||||
(tester) async {
|
||||
final accepted = renamedRun(title: 'Release build');
|
||||
final gateway = FakeTitleGateway(
|
||||
(type, fields) async => {'run_detail': accepted},
|
||||
);
|
||||
final changed = <JsonMap>[];
|
||||
await tester.pumpWidget(
|
||||
titleApp(
|
||||
TitleHost(gateway: gateway, initial: titledRun(), changed: changed),
|
||||
cupertino: false,
|
||||
),
|
||||
);
|
||||
|
||||
expect(find.byKey(runTitleActionKey), findsOneWidget);
|
||||
expect(find.byType(OutlinedButton), findsOneWidget);
|
||||
expect(gateway.calls, isEmpty);
|
||||
|
||||
await tester.tap(find.byKey(runTitleActionKey));
|
||||
await tester.pumpAndSettle();
|
||||
expect(find.byType(AlertDialog), findsOneWidget);
|
||||
expect(find.text(runTitleDialogTitle), findsWidgets);
|
||||
expect(find.text(runTitleFieldLabel), findsOneWidget);
|
||||
expect(_materialFieldText(tester), 'Nightly build');
|
||||
|
||||
await tester.enterText(
|
||||
find.byKey(runTitleFieldKey),
|
||||
' 发布 🚀 — nightly ',
|
||||
);
|
||||
await tester.pump();
|
||||
expect(gateway.calls, isEmpty);
|
||||
|
||||
await tester.tap(find.byKey(runTitleCancelKey));
|
||||
await tester.pumpAndSettle();
|
||||
expect(find.byType(AlertDialog), findsNothing);
|
||||
expect(gateway.calls, isEmpty);
|
||||
expect(changed, isEmpty);
|
||||
expect(
|
||||
tester.widget<Text>(find.byKey(titleHostTitleKey)).data,
|
||||
'Nightly build',
|
||||
);
|
||||
|
||||
await tester.tap(find.byKey(runTitleActionKey));
|
||||
await tester.pumpAndSettle();
|
||||
expect(_materialFieldText(tester), 'Nightly build');
|
||||
await tester.enterText(
|
||||
find.byKey(runTitleFieldKey),
|
||||
' 发布 🚀 — nightly ',
|
||||
);
|
||||
await tester.tap(find.byKey(runTitleSaveKey));
|
||||
await tester.pumpAndSettle();
|
||||
|
||||
expect(gateway.calls.map((call) => call.type), ['set_run_title']);
|
||||
expect(gateway.calls.single.fields, {
|
||||
'run_id': 7,
|
||||
'title': ' 发布 🚀 — nightly ',
|
||||
});
|
||||
expect(changed.single, equals(accepted));
|
||||
expect(find.byType(AlertDialog), findsNothing);
|
||||
expect(
|
||||
tester.widget<Text>(find.byKey(titleHostTitleKey)).data,
|
||||
'Release build',
|
||||
);
|
||||
},
|
||||
);
|
||||
|
||||
testWidgets('cupertino rename is native and sends only on Save', (
|
||||
tester,
|
||||
) async {
|
||||
final accepted = renamedRun(title: 'Release build');
|
||||
final gateway = FakeTitleGateway(
|
||||
(type, fields) async => {'run_detail': accepted},
|
||||
);
|
||||
final changed = <JsonMap>[];
|
||||
await tester.pumpWidget(
|
||||
titleApp(
|
||||
TitleHost(gateway: gateway, initial: titledRun(), changed: changed),
|
||||
cupertino: true,
|
||||
),
|
||||
);
|
||||
|
||||
expect(find.byKey(runTitleActionKey), findsOneWidget);
|
||||
expect(find.byType(CupertinoButton), findsOneWidget);
|
||||
expect(find.byType(OutlinedButton), findsNothing);
|
||||
expect(gateway.calls, isEmpty);
|
||||
|
||||
await tester.tap(find.byKey(runTitleActionKey));
|
||||
await tester.pumpAndSettle();
|
||||
expect(find.byType(CupertinoAlertDialog), findsOneWidget);
|
||||
expect(find.byType(AlertDialog), findsNothing);
|
||||
expect(_cupertinoFieldText(tester), 'Nightly build');
|
||||
|
||||
await tester.enterText(find.byKey(runTitleFieldKey), 'Typed only');
|
||||
await tester.pump();
|
||||
expect(gateway.calls, isEmpty);
|
||||
await tester.tap(find.byKey(runTitleCancelKey));
|
||||
await tester.pumpAndSettle();
|
||||
expect(find.byType(CupertinoAlertDialog), findsNothing);
|
||||
expect(gateway.calls, isEmpty);
|
||||
|
||||
await tester.tap(find.byKey(runTitleActionKey));
|
||||
await tester.pumpAndSettle();
|
||||
await tester.enterText(find.byKey(runTitleFieldKey), 'Release build');
|
||||
await tester.tap(find.byKey(runTitleSaveKey));
|
||||
await tester.pumpAndSettle();
|
||||
|
||||
expect(gateway.calls.map((call) => call.type), ['set_run_title']);
|
||||
expect(gateway.calls.single.fields, {
|
||||
'run_id': 7,
|
||||
'title': 'Release build',
|
||||
});
|
||||
expect(changed.single, equals(accepted));
|
||||
expect(find.byType(CupertinoAlertDialog), findsNothing);
|
||||
});
|
||||
|
||||
testWidgets('a blank title is sent exactly and the server refusal is shown', (
|
||||
tester,
|
||||
) async {
|
||||
const refusal = 'title must not be blank';
|
||||
final gateway = FakeTitleGateway(
|
||||
(type, fields) async => throw const ProtocolError(refusal),
|
||||
);
|
||||
final changed = <JsonMap>[];
|
||||
await tester.pumpWidget(
|
||||
titleApp(
|
||||
TitleHost(gateway: gateway, initial: titledRun(), changed: changed),
|
||||
cupertino: false,
|
||||
),
|
||||
);
|
||||
|
||||
await tester.tap(find.byKey(runTitleActionKey));
|
||||
await tester.pumpAndSettle();
|
||||
await tester.enterText(find.byKey(runTitleFieldKey), '');
|
||||
await tester.tap(find.byKey(runTitleSaveKey));
|
||||
await tester.pumpAndSettle();
|
||||
|
||||
expect(gateway.calls.single.fields, {'run_id': 7, 'title': ''});
|
||||
expect(find.text(refusal), findsOneWidget);
|
||||
expect(find.byKey(runTitleErrorKey), findsOneWidget);
|
||||
expect(_materialFieldText(tester), '');
|
||||
expect(find.byType(AlertDialog), findsOneWidget);
|
||||
expect(changed, isEmpty);
|
||||
expect(
|
||||
tester.widget<Text>(find.byKey(titleHostTitleKey)).data,
|
||||
'Nightly build',
|
||||
);
|
||||
expect(
|
||||
tester.widget<FilledButton>(find.byKey(runTitleSaveKey)).onPressed,
|
||||
isNotNull,
|
||||
);
|
||||
});
|
||||
|
||||
testWidgets('the prompt is seeded from the stored title, never the prompt', (
|
||||
tester,
|
||||
) async {
|
||||
final gateway = FakeTitleGateway(
|
||||
(type, fields) async => {'run_detail': titledRun()},
|
||||
);
|
||||
final changed = <JsonMap>[];
|
||||
|
||||
await tester.pumpWidget(
|
||||
titleApp(
|
||||
TitleHost(
|
||||
gateway: gateway,
|
||||
initial: titledRun(
|
||||
title: 'Stored title',
|
||||
prompt: 'prompt fallback must not appear',
|
||||
),
|
||||
changed: changed,
|
||||
),
|
||||
cupertino: false,
|
||||
),
|
||||
);
|
||||
await tester.tap(find.byKey(runTitleActionKey));
|
||||
await tester.pumpAndSettle();
|
||||
expect(_materialFieldText(tester), 'Stored title');
|
||||
expect(find.text('prompt fallback must not appear'), findsNothing);
|
||||
await tester.tap(find.byKey(runTitleCancelKey));
|
||||
await tester.pumpAndSettle();
|
||||
|
||||
await tester.pumpWidget(
|
||||
titleApp(
|
||||
TitleHost(
|
||||
gateway: gateway,
|
||||
initial: titledRun(title: null, prompt: 'prompt is not a title'),
|
||||
changed: changed,
|
||||
),
|
||||
cupertino: false,
|
||||
),
|
||||
);
|
||||
await tester.tap(find.byKey(runTitleActionKey));
|
||||
await tester.pumpAndSettle();
|
||||
expect(_materialFieldText(tester), '');
|
||||
expect(find.text('prompt is not a title'), findsNothing);
|
||||
await tester.tap(find.byKey(runTitleCancelKey));
|
||||
await tester.pumpAndSettle();
|
||||
|
||||
expect(gateway.calls, isEmpty);
|
||||
expect(changed, isEmpty);
|
||||
});
|
||||
|
||||
testWidgets('a malformed run has no rename entry point', (tester) async {
|
||||
final gateway = FakeTitleGateway(
|
||||
(type, fields) async => {'run_detail': titledRun()},
|
||||
);
|
||||
await tester.pumpWidget(
|
||||
titleApp(
|
||||
TitleHost(
|
||||
gateway: gateway,
|
||||
initial: {'title': 'No id'},
|
||||
changed: <JsonMap>[],
|
||||
),
|
||||
cupertino: false,
|
||||
),
|
||||
);
|
||||
|
||||
expect(find.byKey(runTitleActionKey), findsNothing);
|
||||
expect(gateway.calls, isEmpty);
|
||||
expect(runTitleId({'title': 'No id'}), isNull);
|
||||
expect(storedRunTitle({'title': 'Kept'}), 'Kept');
|
||||
expect(storedRunTitle({}), '');
|
||||
});
|
||||
}
|
||||
@@ -0,0 +1,243 @@
|
||||
import 'dart:async';
|
||||
|
||||
import 'package:flutter/cupertino.dart';
|
||||
import 'package:flutter/material.dart';
|
||||
import 'package:flutter_test/flutter_test.dart';
|
||||
import 'package:pipelines_app/core/api_gateway.dart';
|
||||
import 'package:pipelines_app/core/transport/connection_state.dart';
|
||||
import 'package:pipelines_app/features/runs/actions/title_wording.dart';
|
||||
|
||||
import 'title_test_support.dart';
|
||||
|
||||
Future<void> _pump(
|
||||
WidgetTester tester,
|
||||
FakeTitleGateway gateway,
|
||||
List<JsonMap> changed, {
|
||||
bool cupertino = false,
|
||||
double textScale = 1,
|
||||
JsonMap? run,
|
||||
}) async {
|
||||
await tester.pumpWidget(
|
||||
titleApp(
|
||||
TitleHost(
|
||||
gateway: gateway,
|
||||
initial: run ?? titledRun(),
|
||||
changed: changed,
|
||||
),
|
||||
cupertino: cupertino,
|
||||
textScale: textScale,
|
||||
),
|
||||
);
|
||||
}
|
||||
|
||||
Future<void> _open(WidgetTester tester) async {
|
||||
await tester.tap(find.byKey(runTitleActionKey));
|
||||
await tester.pumpAndSettle();
|
||||
}
|
||||
|
||||
String _fieldText(WidgetTester tester, {required bool cupertino}) {
|
||||
if (cupertino) {
|
||||
return tester
|
||||
.widget<CupertinoTextField>(find.byKey(runTitleFieldKey))
|
||||
.controller!
|
||||
.text;
|
||||
}
|
||||
return tester
|
||||
.widget<TextField>(find.byKey(runTitleFieldKey))
|
||||
.controller!
|
||||
.text;
|
||||
}
|
||||
|
||||
void main() {
|
||||
testWidgets('a failed save keeps the prompt open with the draft intact', (
|
||||
tester,
|
||||
) async {
|
||||
const refusal = 'rename refused: the run is locked';
|
||||
final gateway = FakeTitleGateway(
|
||||
(type, fields) async => throw const ProtocolError(refusal),
|
||||
);
|
||||
final changed = <JsonMap>[];
|
||||
await _pump(tester, gateway, changed);
|
||||
|
||||
await _open(tester);
|
||||
await tester.enterText(find.byKey(runTitleFieldKey), ' Renamed draft ');
|
||||
await tester.tap(find.byKey(runTitleSaveKey));
|
||||
await tester.pumpAndSettle();
|
||||
|
||||
expect(gateway.calls.map((call) => call.type), ['set_run_title']);
|
||||
expect(gateway.calls.single.fields, {
|
||||
'run_id': 7,
|
||||
'title': ' Renamed draft ',
|
||||
});
|
||||
expect(find.text(refusal), findsOneWidget);
|
||||
expect(find.byKey(runTitleErrorKey), findsOneWidget);
|
||||
expect(_fieldText(tester, cupertino: false), ' Renamed draft ');
|
||||
expect(find.byType(AlertDialog), findsOneWidget);
|
||||
expect(changed, isEmpty);
|
||||
expect(
|
||||
tester.widget<Text>(find.byKey(titleHostTitleKey)).data,
|
||||
'Nightly build',
|
||||
);
|
||||
});
|
||||
|
||||
testWidgets('a transport failure keeps the server words and the draft', (
|
||||
tester,
|
||||
) async {
|
||||
final gateway = FakeTitleGateway(
|
||||
(type, fields) async => throw const NotConnectedError(),
|
||||
);
|
||||
final changed = <JsonMap>[];
|
||||
await _pump(tester, gateway, changed, cupertino: true);
|
||||
|
||||
await _open(tester);
|
||||
await tester.enterText(find.byKey(runTitleFieldKey), 'Unsent title');
|
||||
await tester.tap(find.byKey(runTitleSaveKey));
|
||||
await tester.pumpAndSettle();
|
||||
|
||||
expect(find.byKey(runTitleErrorKey), findsOneWidget);
|
||||
expect(_fieldText(tester, cupertino: true), 'Unsent title');
|
||||
expect(find.byType(CupertinoAlertDialog), findsOneWidget);
|
||||
expect(changed, isEmpty);
|
||||
});
|
||||
|
||||
testWidgets('Save cannot send twice while the rename is in flight', (
|
||||
tester,
|
||||
) async {
|
||||
final reply = Completer<JsonMap>();
|
||||
final gateway = FakeTitleGateway((type, fields) => reply.future);
|
||||
final changed = <JsonMap>[];
|
||||
await _pump(tester, gateway, changed);
|
||||
|
||||
await _open(tester);
|
||||
await tester.enterText(find.byKey(runTitleFieldKey), 'One title');
|
||||
final save = find.byKey(runTitleSaveKey);
|
||||
await tester.tap(save);
|
||||
await tester.tap(save);
|
||||
await tester.pump();
|
||||
|
||||
expect(
|
||||
gateway.calls.where((call) => call.type == 'set_run_title'),
|
||||
hasLength(1),
|
||||
);
|
||||
expect(tester.widget<FilledButton>(save).onPressed, isNull);
|
||||
expect(
|
||||
tester.widget<TextButton>(find.byKey(runTitleCancelKey)).onPressed,
|
||||
isNull,
|
||||
);
|
||||
expect(find.byType(AlertDialog), findsOneWidget);
|
||||
|
||||
reply.complete({'run_detail': renamedRun(title: 'One title')});
|
||||
await tester.pumpAndSettle();
|
||||
expect(find.byType(AlertDialog), findsNothing);
|
||||
expect(changed, hasLength(1));
|
||||
expect(changed.single['title'], 'One title');
|
||||
});
|
||||
|
||||
testWidgets('a late reply after disposal is ignored safely', (tester) async {
|
||||
final reply = Completer<JsonMap>();
|
||||
final gateway = FakeTitleGateway((type, fields) => reply.future);
|
||||
final changed = <JsonMap>[];
|
||||
await _pump(tester, gateway, changed);
|
||||
|
||||
await _open(tester);
|
||||
await tester.tap(find.byKey(runTitleSaveKey));
|
||||
await tester.pump();
|
||||
expect(gateway.calls, hasLength(1));
|
||||
|
||||
await tester.pumpWidget(const SizedBox());
|
||||
reply.complete({'run_detail': renamedRun()});
|
||||
await tester.pump();
|
||||
|
||||
expect(tester.takeException(), isNull);
|
||||
expect(changed, isEmpty);
|
||||
});
|
||||
|
||||
testWidgets('a reply for a run the widget no longer owns is not delivered', (
|
||||
tester,
|
||||
) async {
|
||||
final reply = Completer<JsonMap>();
|
||||
final gateway = FakeTitleGateway((type, fields) => reply.future);
|
||||
final changed = <JsonMap>[];
|
||||
await _pump(tester, gateway, changed);
|
||||
|
||||
await _open(tester);
|
||||
await tester.enterText(find.byKey(runTitleFieldKey), 'Run seven title');
|
||||
await tester.tap(find.byKey(runTitleSaveKey));
|
||||
await tester.pump();
|
||||
|
||||
await tester.pumpWidget(
|
||||
titleApp(
|
||||
TitleHost(
|
||||
gateway: gateway,
|
||||
initial: titledRun(id: 8, title: 'Another run'),
|
||||
changed: changed,
|
||||
),
|
||||
cupertino: false,
|
||||
),
|
||||
);
|
||||
await tester.pump();
|
||||
reply.complete({'run_detail': renamedRun(title: 'Run seven title')});
|
||||
await tester.pumpAndSettle();
|
||||
|
||||
expect(tester.takeException(), isNull);
|
||||
expect(gateway.calls.single.fields, {
|
||||
'run_id': 7,
|
||||
'title': 'Run seven title',
|
||||
});
|
||||
expect(changed, isEmpty);
|
||||
expect(
|
||||
tester.widget<Text>(find.byKey(titleHostTitleKey)).data,
|
||||
'Another run',
|
||||
);
|
||||
});
|
||||
|
||||
testWidgets('both prompts fit 2x text at a narrow width', (tester) async {
|
||||
tester.view.physicalSize = const Size(320, 480);
|
||||
tester.view.devicePixelRatio = 1;
|
||||
addTearDown(tester.view.resetPhysicalSize);
|
||||
addTearDown(tester.view.resetDevicePixelRatio);
|
||||
|
||||
const refusal = 'the title is too long for this run; shorten it';
|
||||
for (final cupertino in [false, true]) {
|
||||
final gateway = FakeTitleGateway(
|
||||
(type, fields) async => throw const ProtocolError(refusal),
|
||||
);
|
||||
await _pump(
|
||||
tester,
|
||||
gateway,
|
||||
<JsonMap>[],
|
||||
cupertino: cupertino,
|
||||
textScale: 2,
|
||||
);
|
||||
await _open(tester);
|
||||
|
||||
expect(tester.takeException(), isNull, reason: 'cupertino=$cupertino');
|
||||
expect(
|
||||
cupertino
|
||||
? find.byType(CupertinoAlertDialog)
|
||||
: find.byType(AlertDialog),
|
||||
findsOneWidget,
|
||||
);
|
||||
expect(find.text(runTitleFieldLabel), findsOneWidget);
|
||||
expect(gateway.calls, isEmpty);
|
||||
|
||||
await tester.enterText(
|
||||
find.byKey(runTitleFieldKey),
|
||||
'A deliberately long title that must scroll rather than overflow',
|
||||
);
|
||||
await tester.tap(find.byKey(runTitleSaveKey));
|
||||
await tester.pumpAndSettle();
|
||||
expect(tester.takeException(), isNull, reason: 'cupertino=$cupertino');
|
||||
expect(find.text(refusal), findsOneWidget);
|
||||
expect(
|
||||
_fieldText(tester, cupertino: cupertino),
|
||||
'A deliberately long title that must scroll rather than overflow',
|
||||
);
|
||||
|
||||
await tester.tap(find.byKey(runTitleCancelKey));
|
||||
await tester.pumpAndSettle();
|
||||
expect(find.byType(AlertDialog), findsNothing);
|
||||
expect(find.byType(CupertinoAlertDialog), findsNothing);
|
||||
}
|
||||
});
|
||||
}
|
||||
@@ -0,0 +1,110 @@
|
||||
import 'package:flutter/cupertino.dart';
|
||||
import 'package:flutter/material.dart';
|
||||
import 'package:pipelines_app/core/api_gateway.dart';
|
||||
import 'package:pipelines_app/features/runs/actions/title_action.dart';
|
||||
|
||||
typedef TitleHandler = Future<JsonMap> Function(String type, JsonMap fields);
|
||||
|
||||
/// A fake gateway that records every request and never owns a socket.
|
||||
class FakeTitleGateway implements ApiGateway {
|
||||
FakeTitleGateway(this.handler);
|
||||
|
||||
final TitleHandler handler;
|
||||
final List<({String type, JsonMap fields})> calls = [];
|
||||
|
||||
@override
|
||||
Future<JsonMap> call(String type, [JsonMap fields = const {}]) {
|
||||
final copy = Map<String, dynamic>.from(fields);
|
||||
calls.add((type: type, fields: copy));
|
||||
return handler(type, copy);
|
||||
}
|
||||
|
||||
@override
|
||||
Stream<JsonMap> get frames => const Stream.empty();
|
||||
}
|
||||
|
||||
JsonMap titledRun({
|
||||
num id = 7,
|
||||
String? title = 'Nightly build',
|
||||
String status = 'succeeded',
|
||||
String prompt = 'the submitted prompt',
|
||||
}) => {
|
||||
'id': id,
|
||||
'title': title,
|
||||
'status': status,
|
||||
'input': {'prompt': prompt},
|
||||
};
|
||||
|
||||
JsonMap renamedRun({num id = 7, String title = 'Release build'}) =>
|
||||
titledRun(id: id, title: title, status: 'succeeded');
|
||||
|
||||
/// Owns the run like a run-detail screen would, so the callback's returned
|
||||
/// server detail is what the action renders next. Re-pumping with a different
|
||||
/// initial run updates the action in place, as a refreshed detail does.
|
||||
class TitleHost extends StatefulWidget {
|
||||
const TitleHost({
|
||||
super.key,
|
||||
required this.gateway,
|
||||
required this.initial,
|
||||
required this.changed,
|
||||
});
|
||||
|
||||
final FakeTitleGateway gateway;
|
||||
final JsonMap initial;
|
||||
final List<JsonMap> changed;
|
||||
|
||||
@override
|
||||
State<TitleHost> createState() => _TitleHostState();
|
||||
}
|
||||
|
||||
class _TitleHostState extends State<TitleHost> {
|
||||
late JsonMap _run = widget.initial;
|
||||
|
||||
@override
|
||||
void didUpdateWidget(covariant TitleHost oldWidget) {
|
||||
super.didUpdateWidget(oldWidget);
|
||||
if (!identical(oldWidget.initial, widget.initial)) {
|
||||
_run = widget.initial;
|
||||
}
|
||||
}
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
return Column(
|
||||
mainAxisSize: MainAxisSize.min,
|
||||
children: [
|
||||
Text('${_run['title'] ?? ''}', key: titleHostTitleKey),
|
||||
RunTitleAction(
|
||||
run: _run,
|
||||
gateway: widget.gateway,
|
||||
onRunChanged: (run) {
|
||||
widget.changed.add(run);
|
||||
setState(() => _run = run);
|
||||
},
|
||||
),
|
||||
],
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
const titleHostTitleKey = ValueKey<String>('rename-host-title');
|
||||
|
||||
Widget titleApp(Widget child, {required bool cupertino, double textScale = 1}) {
|
||||
Widget media(BuildContext context, Widget? app) => MediaQuery(
|
||||
data: MediaQuery.of(
|
||||
context,
|
||||
).copyWith(textScaler: TextScaler.linear(textScale)),
|
||||
child: app!,
|
||||
);
|
||||
final body = Center(child: child);
|
||||
if (cupertino) {
|
||||
return CupertinoApp(
|
||||
builder: media,
|
||||
home: CupertinoPageScaffold(child: body),
|
||||
);
|
||||
}
|
||||
return MaterialApp(
|
||||
builder: media,
|
||||
home: Scaffold(body: body),
|
||||
);
|
||||
}
|
||||
@@ -0,0 +1,101 @@
|
||||
import 'package:flutter_test/flutter_test.dart';
|
||||
import 'package:pipelines_app/features/schedules/domain/saved_schedule.dart';
|
||||
|
||||
import 'schedule_test_support.dart';
|
||||
|
||||
void main() {
|
||||
test('saved schedule exposes run-configuration provenance', () {
|
||||
final schedule = SavedSchedule(savedRecord());
|
||||
|
||||
expect(schedule.id, 8);
|
||||
expect(schedule.revision, 3);
|
||||
expect(schedule.pipelineId, 4);
|
||||
expect(schedule.version, 2);
|
||||
expect(schedule.name, 'Research batteries');
|
||||
expect(schedule.enabled, isTrue);
|
||||
expect(schedule.prompt, 'Research batteries');
|
||||
expect(schedule.input['custom'], {'nested': 'kept'});
|
||||
expect(schedule.workspace, '/srv/research');
|
||||
expect(schedule.writePaths, ['reports/research.md']);
|
||||
expect(schedule.allowTools, isTrue);
|
||||
expect(schedule.model, 'openai/gpt-5');
|
||||
expect(schedule.stageModels, {'one': 'openai/gpt-5'});
|
||||
expect(schedule.notifyMode, 'off');
|
||||
expect(schedule.title, 'Battery research');
|
||||
expect(schedule.outputFormat, 'markdown');
|
||||
expect(schedule.rule.every, '24h');
|
||||
expect(schedule.rule.days, ['mon', 'wed']);
|
||||
expect(schedule.rule.timezone, 'Europe/London');
|
||||
expect(schedule.rule.isClock, isFalse);
|
||||
expect(schedule.nextDue, '2026-10-22T09:00:00Z');
|
||||
expect(schedule.createdAt, '2026-10-20T09:00:00Z');
|
||||
expect(schedule.updatedAt, '2026-10-21T09:00:00Z');
|
||||
});
|
||||
|
||||
test('unknown fields survive the typed view and pause copy', () {
|
||||
final schedule = SavedSchedule(savedRecord());
|
||||
final paused = schedule.withEnabled(false);
|
||||
|
||||
expect(paused.enabled, isFalse);
|
||||
expect(paused.id, 8);
|
||||
expect(paused.revision, 3);
|
||||
expect(paused.toJson()['future_server_field'], {
|
||||
'nested': [
|
||||
1,
|
||||
2,
|
||||
{'deep': true},
|
||||
],
|
||||
});
|
||||
expect(paused.toJson()['input'], {
|
||||
'prompt': 'Research batteries',
|
||||
'custom': {'nested': 'kept'},
|
||||
});
|
||||
expect(paused.toJson()['rule'], savedRecord()['rule']);
|
||||
|
||||
// The original snapshot is untouched and still enabled.
|
||||
expect(schedule.enabled, isTrue);
|
||||
expect(schedule.revision, 3);
|
||||
});
|
||||
|
||||
test('toJson is a deep copy, not an alias of the server reply', () {
|
||||
final source = savedRecord();
|
||||
final schedule = SavedSchedule(source);
|
||||
|
||||
final first = schedule.toJson();
|
||||
((first['future_server_field'] as Map)['nested'] as List).add('changed');
|
||||
final second = schedule.toJson();
|
||||
expect((second['future_server_field'] as Map)['nested'], [
|
||||
1,
|
||||
2,
|
||||
{'deep': true},
|
||||
]);
|
||||
|
||||
final promptInput = schedule.input;
|
||||
promptInput['custom'] = 'changed';
|
||||
expect(schedule.input['custom'], {'nested': 'kept'});
|
||||
});
|
||||
|
||||
test('missing fields stay absent instead of being fabricated', () {
|
||||
final schedule = SavedSchedule(const {});
|
||||
|
||||
expect(schedule.id, isNull);
|
||||
expect(schedule.revision, isNull);
|
||||
expect(schedule.pipelineId, isNull);
|
||||
expect(schedule.version, isNull);
|
||||
expect(schedule.enabled, isFalse);
|
||||
expect(schedule.prompt, '');
|
||||
expect(schedule.workspace, '');
|
||||
expect(schedule.rule.every, '');
|
||||
expect(schedule.rule.times, isEmpty);
|
||||
expect(schedule.writePaths, isEmpty);
|
||||
expect(schedule.stageModels, isEmpty);
|
||||
expect(schedule.toJson(), isEmpty);
|
||||
});
|
||||
|
||||
test('a fractional revision is not truncated into a false token', () {
|
||||
final schedule = SavedSchedule(const {'id': 8, 'revision': 3.5});
|
||||
|
||||
expect(schedule.id, 8);
|
||||
expect(schedule.revision, isNull);
|
||||
});
|
||||
}
|
||||
@@ -0,0 +1,83 @@
|
||||
import 'dart:convert';
|
||||
|
||||
import 'package:pipelines_app/core/api_gateway.dart';
|
||||
|
||||
class RecordedCall {
|
||||
const RecordedCall(this.type, this.fields);
|
||||
final String type;
|
||||
final JsonMap fields;
|
||||
}
|
||||
|
||||
/// Records the exact request built by the repository and replays queued
|
||||
/// replies. Both are deep-copied, so a test can prove the repository neither
|
||||
/// mutated its input nor depended on alias mutation of a reply.
|
||||
class RecordingGateway implements ApiGateway {
|
||||
final calls = <RecordedCall>[];
|
||||
final replies = <JsonMap>[];
|
||||
Object? failure;
|
||||
|
||||
@override
|
||||
Future<JsonMap> call(String type, [JsonMap fields = const {}]) async {
|
||||
calls.add(RecordedCall(type, deepCopy(fields)));
|
||||
if (failure case final error?) throw error;
|
||||
if (replies.isEmpty) throw StateError('No recorded reply for $type');
|
||||
return deepCopy(replies.removeAt(0));
|
||||
}
|
||||
|
||||
@override
|
||||
Stream<JsonMap> get frames => const Stream.empty();
|
||||
}
|
||||
|
||||
JsonMap deepCopy(JsonMap value) => jsonDecode(jsonEncode(value)) as JsonMap;
|
||||
|
||||
/// A complete server record including fields this client does not interpret
|
||||
/// yet, an unknown nested rule field and unknown input.
|
||||
JsonMap savedRecord() => {
|
||||
'id': 8,
|
||||
'revision': 3,
|
||||
'name': 'Research batteries',
|
||||
'pipeline_id': 4,
|
||||
'version': 2,
|
||||
'enabled': true,
|
||||
'input': {
|
||||
'prompt': 'Research batteries',
|
||||
'custom': {'nested': 'kept'},
|
||||
},
|
||||
'workspace': '/srv/research',
|
||||
'write_paths': ['reports/research.md'],
|
||||
'allow_tools': true,
|
||||
'model': 'openai/gpt-5',
|
||||
'stage_models': {'one': 'openai/gpt-5'},
|
||||
'notify_mode': 'off',
|
||||
'title': 'Battery research',
|
||||
'output_format': 'markdown',
|
||||
'rule': {
|
||||
'every': '24h',
|
||||
'days': ['mon', 'wed'],
|
||||
'timezone': 'Europe/London',
|
||||
'future_rule_field': 'kept',
|
||||
},
|
||||
'next_due': '2026-10-22T09:00:00Z',
|
||||
'created_at': '2026-10-20T09:00:00Z',
|
||||
'updated_at': '2026-10-21T09:00:00Z',
|
||||
'future_server_field': {
|
||||
'nested': [
|
||||
1,
|
||||
2,
|
||||
{'deep': true},
|
||||
],
|
||||
},
|
||||
};
|
||||
|
||||
/// The create form of the record: id 0 is the server's create sentinel and no
|
||||
/// revision has been issued yet.
|
||||
JsonMap createRecord() => {
|
||||
'id': 0,
|
||||
'name': 'Research batteries',
|
||||
'pipeline_id': 4,
|
||||
'version': 2,
|
||||
'enabled': true,
|
||||
'input': {'prompt': 'Research batteries'},
|
||||
'workspace': '/srv/research',
|
||||
'rule': {'every': '24h', 'timezone': 'Europe/London'},
|
||||
};
|
||||
@@ -0,0 +1,131 @@
|
||||
import 'package:flutter_test/flutter_test.dart';
|
||||
import 'package:pipelines_app/core/transport/connection_state.dart';
|
||||
import 'package:pipelines_app/features/schedules/data/schedules_repository.dart';
|
||||
|
||||
import 'schedule_test_support.dart';
|
||||
|
||||
void main() {
|
||||
test('delete sends the exact ID and expected revision', () async {
|
||||
final gateway = RecordingGateway()
|
||||
..replies.add({'type': 'schedule_deleted', 'schedule_id': 8});
|
||||
final repository = SchedulesRepository(gateway);
|
||||
|
||||
await repository.deleteSchedule(8, 3);
|
||||
|
||||
expect(gateway.calls.single.type, 'delete_schedule');
|
||||
expect(gateway.calls.single.fields, {
|
||||
'schedule_id': 8,
|
||||
'expected_revision': 3,
|
||||
});
|
||||
});
|
||||
|
||||
test('delete refuses a confirmation for anything else', () async {
|
||||
final gateway = RecordingGateway();
|
||||
final repository = SchedulesRepository(gateway);
|
||||
for (final reply in [
|
||||
<String, dynamic>{},
|
||||
{'type': 'pong'},
|
||||
{'type': 'schedule_deleted'},
|
||||
{'type': 'schedule_deleted', 'schedule_id': 9},
|
||||
]) {
|
||||
gateway.replies.add(reply);
|
||||
await expectLater(
|
||||
repository.deleteSchedule(8, 3),
|
||||
throwsA(
|
||||
isA<SchedulesRepositoryException>().having(
|
||||
(error) => error.message,
|
||||
'message',
|
||||
'The server did not confirm deletion of this schedule.',
|
||||
),
|
||||
),
|
||||
);
|
||||
}
|
||||
});
|
||||
|
||||
test('server refusals and conflicts propagate verbatim', () async {
|
||||
final conflict = const ProtocolError(
|
||||
'schedule revision conflict; reload before editing',
|
||||
);
|
||||
final recording = RecordingGateway()..failure = conflict;
|
||||
final conflicting = SchedulesRepository(recording);
|
||||
await expectLater(
|
||||
conflicting.saveSchedule(createRecord()),
|
||||
throwsA(same(conflict)),
|
||||
);
|
||||
await expectLater(
|
||||
conflicting.deleteSchedule(8, 3),
|
||||
throwsA(same(conflict)),
|
||||
);
|
||||
|
||||
final unsupported = const ProtocolError(
|
||||
'unknown message type: list_schedules',
|
||||
);
|
||||
recording.failure = unsupported;
|
||||
await expectLater(
|
||||
conflicting.listSchedules(pipelineId: 4),
|
||||
throwsA(same(unsupported)),
|
||||
);
|
||||
});
|
||||
|
||||
test('missing and empty list replies are different facts', () async {
|
||||
final gateway = RecordingGateway();
|
||||
final repository = SchedulesRepository(gateway);
|
||||
|
||||
gateway.replies.add({'type': 'schedules'});
|
||||
expect(await repository.listSchedules(), isEmpty);
|
||||
|
||||
gateway.replies.add({'type': 'schedules', 'schedules': null});
|
||||
expect(await repository.listSchedules(), isEmpty);
|
||||
|
||||
for (final reply in [
|
||||
<String, dynamic>{},
|
||||
{'type': 'pong'},
|
||||
{'type': 'schedules', 'schedules': <String, dynamic>{}},
|
||||
{
|
||||
'type': 'schedules',
|
||||
'schedules': ['not a record'],
|
||||
},
|
||||
]) {
|
||||
gateway.replies.add(reply);
|
||||
await expectLater(
|
||||
repository.listSchedules(),
|
||||
throwsA(isA<SchedulesRepositoryException>()),
|
||||
);
|
||||
}
|
||||
});
|
||||
|
||||
test('save requires a saved schedule in the reply', () async {
|
||||
final gateway = RecordingGateway();
|
||||
final repository = SchedulesRepository(gateway);
|
||||
for (final reply in [
|
||||
<String, dynamic>{},
|
||||
{'type': 'schedule'},
|
||||
{'type': 'schedule', 'schedule': null},
|
||||
{'type': 'schedule', 'schedule': 'record'},
|
||||
]) {
|
||||
gateway.replies.add(reply);
|
||||
await expectLater(
|
||||
repository.saveSchedule(createRecord()),
|
||||
throwsA(
|
||||
isA<SchedulesRepositoryException>().having(
|
||||
(error) => error.message,
|
||||
'message',
|
||||
'The server returned no saved schedule.',
|
||||
),
|
||||
),
|
||||
);
|
||||
}
|
||||
});
|
||||
|
||||
test('an empty schedule object is not turned into a fake identity', () async {
|
||||
final gateway = RecordingGateway()
|
||||
..replies.add({'type': 'schedule', 'schedule': <String, dynamic>{}});
|
||||
final repository = SchedulesRepository(gateway);
|
||||
|
||||
final saved = await repository.saveSchedule(createRecord());
|
||||
|
||||
expect(saved.id, isNull);
|
||||
expect(saved.revision, isNull);
|
||||
expect(saved.toJson(), isEmpty);
|
||||
});
|
||||
}
|
||||
@@ -0,0 +1,81 @@
|
||||
import 'package:flutter_test/flutter_test.dart';
|
||||
import 'package:pipelines_app/features/schedules/data/schedules_repository.dart';
|
||||
|
||||
import 'schedule_test_support.dart';
|
||||
|
||||
void main() {
|
||||
test(
|
||||
'list rejects non-positive and unsafe pipeline IDs before sending',
|
||||
() async {
|
||||
final gateway = RecordingGateway();
|
||||
final repository = SchedulesRepository(gateway);
|
||||
|
||||
for (final id in [0, -1, 9007199254740992]) {
|
||||
await expectLater(
|
||||
repository.listSchedules(pipelineId: id),
|
||||
throwsA(
|
||||
isA<SchedulesRepositoryException>().having(
|
||||
(error) => error.message,
|
||||
'message',
|
||||
'A positive pipeline ID is required.',
|
||||
),
|
||||
),
|
||||
);
|
||||
}
|
||||
expect(gateway.calls, isEmpty);
|
||||
},
|
||||
);
|
||||
|
||||
test(
|
||||
'delete rejects an inexact identity or revision before sending',
|
||||
() async {
|
||||
final gateway = RecordingGateway();
|
||||
final repository = SchedulesRepository(gateway);
|
||||
|
||||
for (final pair in [
|
||||
(0, 1),
|
||||
(-1, 1),
|
||||
(8, 0),
|
||||
(8, -1),
|
||||
(8, 9007199254740992),
|
||||
]) {
|
||||
await expectLater(
|
||||
repository.deleteSchedule(pair.$1, pair.$2),
|
||||
throwsA(
|
||||
isA<SchedulesRepositoryException>().having(
|
||||
(error) => error.message,
|
||||
'message',
|
||||
'An exact schedule ID and revision are required.',
|
||||
),
|
||||
),
|
||||
);
|
||||
}
|
||||
expect(gateway.calls, isEmpty);
|
||||
},
|
||||
);
|
||||
|
||||
test(
|
||||
'request keys are bounded at 128 characters like the web client',
|
||||
() async {
|
||||
final gateway = RecordingGateway();
|
||||
final repository = SchedulesRepository(gateway);
|
||||
|
||||
await expectLater(
|
||||
repository.saveSchedule(createRecord(), requestKey: 'x' * 129),
|
||||
throwsA(
|
||||
isA<SchedulesRepositoryException>().having(
|
||||
(error) => error.message,
|
||||
'message',
|
||||
'Schedule request keys must be strings of at most 128 characters.',
|
||||
),
|
||||
),
|
||||
);
|
||||
expect(gateway.calls, isEmpty);
|
||||
|
||||
final key = 'x' * 128;
|
||||
gateway.replies.add({'type': 'schedule', 'schedule': savedRecord()});
|
||||
await repository.saveSchedule(createRecord(), requestKey: key);
|
||||
expect(gateway.calls.single.fields['request_key'], key);
|
||||
},
|
||||
);
|
||||
}
|
||||
@@ -0,0 +1,132 @@
|
||||
import 'package:flutter_test/flutter_test.dart';
|
||||
import 'package:pipelines_app/features/schedules/data/schedules_repository.dart';
|
||||
import 'package:pipelines_app/features/schedules/domain/saved_schedule.dart';
|
||||
|
||||
import 'schedule_test_support.dart';
|
||||
|
||||
void main() {
|
||||
test(
|
||||
'lists with the exact web envelope and retains every record field',
|
||||
() async {
|
||||
final record = savedRecord();
|
||||
final gateway = RecordingGateway()
|
||||
..replies.add({
|
||||
'type': 'schedules',
|
||||
'schedules': [record],
|
||||
});
|
||||
final repository = SchedulesRepository(gateway);
|
||||
|
||||
final listed = await repository.listSchedules(pipelineId: 4);
|
||||
|
||||
expect(gateway.calls.single.type, 'list_schedules');
|
||||
expect(gateway.calls.single.fields, {'pipeline_id': 4});
|
||||
expect(listed.single.toJson(), record);
|
||||
expect(listed.single.pipelineId, 4);
|
||||
expect(listed.single.version, 2);
|
||||
expect(listed.single.prompt, 'Research batteries');
|
||||
expect(listed.single.workspace, '/srv/research');
|
||||
expect(listed.single.enabled, isTrue);
|
||||
expect(listed.single.rule.every, '24h');
|
||||
expect(listed.single.rule.days, ['mon', 'wed']);
|
||||
expect(listed.single.rule.timezone, 'Europe/London');
|
||||
expect(listed.single.model, 'openai/gpt-5');
|
||||
expect(listed.single.notifyMode, 'off');
|
||||
expect(listed.single.toJson()['future_server_field'], {
|
||||
'nested': [
|
||||
1,
|
||||
2,
|
||||
{'deep': true},
|
||||
],
|
||||
});
|
||||
|
||||
gateway.replies.add({'type': 'schedules'});
|
||||
await repository.listSchedules();
|
||||
expect(gateway.calls.last.type, 'list_schedules');
|
||||
expect(gateway.calls.last.fields, isEmpty);
|
||||
},
|
||||
);
|
||||
|
||||
test(
|
||||
'create sends the web sentinel record without inventing a revision',
|
||||
() async {
|
||||
final create = createRecord();
|
||||
final original = deepCopy(create);
|
||||
final gateway = RecordingGateway()
|
||||
..replies.add({'type': 'schedule', 'schedule': savedRecord()});
|
||||
final repository = SchedulesRepository(gateway);
|
||||
|
||||
final saved = await repository.saveSchedule(create);
|
||||
|
||||
expect(gateway.calls.single.type, 'save_schedule');
|
||||
expect(gateway.calls.single.fields, {'schedule': create});
|
||||
expect(gateway.calls.single.fields.containsKey('request_key'), isFalse);
|
||||
expect(
|
||||
gateway.calls.single.fields.containsKey('expected_revision'),
|
||||
isFalse,
|
||||
);
|
||||
expect(create, original, reason: 'the caller record must be unchanged');
|
||||
expect(saved.id, 8);
|
||||
expect(saved.revision, 3);
|
||||
expect(saved.toJson(), savedRecord());
|
||||
},
|
||||
);
|
||||
|
||||
test(
|
||||
'a stable create key is forwarded exactly and blanks are omitted',
|
||||
() async {
|
||||
final create = createRecord();
|
||||
final gateway = RecordingGateway();
|
||||
final repository = SchedulesRepository(gateway);
|
||||
|
||||
gateway.replies.add({'type': 'schedule', 'schedule': savedRecord()});
|
||||
await repository.saveSchedule(
|
||||
create,
|
||||
requestKey: 'schedule:stable-editor-key',
|
||||
);
|
||||
expect(gateway.calls.last.fields, {
|
||||
'schedule': create,
|
||||
'request_key': 'schedule:stable-editor-key',
|
||||
});
|
||||
|
||||
gateway.replies.add({'type': 'schedule', 'schedule': savedRecord()});
|
||||
await repository.saveSchedule(create, requestKey: '');
|
||||
expect(gateway.calls.last.fields, {'schedule': create});
|
||||
},
|
||||
);
|
||||
|
||||
test('update reuses the read revision as the CAS token', () async {
|
||||
final current = SavedSchedule(savedRecord());
|
||||
final paused = current.withEnabled(false);
|
||||
expect(paused.id, 8);
|
||||
expect(paused.revision, 3);
|
||||
expect(paused.toJson()['future_server_field'], isNotNull);
|
||||
|
||||
final gateway = RecordingGateway()
|
||||
..replies.add({
|
||||
'type': 'schedule',
|
||||
'schedule': {...savedRecord(), 'enabled': false, 'revision': 4},
|
||||
});
|
||||
final repository = SchedulesRepository(gateway);
|
||||
|
||||
final updated = await repository.saveSchedule(paused.toJson());
|
||||
|
||||
final body = gateway.calls.single.fields['schedule'] as Map;
|
||||
expect(gateway.calls.single.type, 'save_schedule');
|
||||
expect(gateway.calls.single.fields.containsKey('request_key'), isFalse);
|
||||
expect(
|
||||
gateway.calls.single.fields.containsKey('expected_revision'),
|
||||
isFalse,
|
||||
);
|
||||
expect(body['id'], 8);
|
||||
expect(body['revision'], 3);
|
||||
expect(body['enabled'], isFalse);
|
||||
expect(body['future_server_field'], isNotNull);
|
||||
expect(updated.revision, 4);
|
||||
expect(updated.enabled, isFalse);
|
||||
|
||||
// The stale draft keeps its token: a conflict must not be papered over by
|
||||
// silently adopting the reply revision.
|
||||
expect(current.revision, 3);
|
||||
expect(current.enabled, isTrue);
|
||||
});
|
||||
}
|
||||
@@ -0,0 +1,147 @@
|
||||
import 'package:flutter_test/flutter_test.dart';
|
||||
import 'package:pipelines_app/core/api_gateway.dart';
|
||||
import 'package:pipelines_app/features/templates/data/template_repository.dart';
|
||||
|
||||
import 'template_test_support.dart';
|
||||
|
||||
void main() {
|
||||
test(
|
||||
'validate sends the exact source and returns the server graph',
|
||||
() async {
|
||||
final graph = <String, dynamic>{
|
||||
'stages': [
|
||||
{'key': 'build', 'type': 'model'},
|
||||
],
|
||||
'unknown_graph_field': true,
|
||||
};
|
||||
final gateway = RecordingGateway([
|
||||
{'type': 'validated', 'graph': graph},
|
||||
]);
|
||||
|
||||
final result = await TemplateRepository(gateway).validate(source);
|
||||
|
||||
expect(gateway.calls.single.type, 'validate');
|
||||
expect(gateway.calls.single.fields, {'source': source});
|
||||
expect(gateway.calls.single.fields['source'], source);
|
||||
expect(result, graph);
|
||||
},
|
||||
);
|
||||
|
||||
test(
|
||||
'validate rejects a reply without a graph instead of faking success',
|
||||
() async {
|
||||
for (final reply in <JsonMap>[
|
||||
<String, dynamic>{'type': 'validated'},
|
||||
<String, dynamic>{'type': 'validated', 'graph': null},
|
||||
]) {
|
||||
final gateway = RecordingGateway([reply]);
|
||||
|
||||
await expectLater(
|
||||
TemplateRepository(gateway).validate(source),
|
||||
throwsA(
|
||||
isA<TemplateRepositoryException>().having(
|
||||
(error) => error.message,
|
||||
'message',
|
||||
'The server returned no graph.',
|
||||
),
|
||||
),
|
||||
);
|
||||
}
|
||||
},
|
||||
);
|
||||
|
||||
test('validate propagates a server refusal verbatim', () async {
|
||||
const refusal = ServerRefusal('invalid stage "añadir": unknown key');
|
||||
final gateway = RefusingGateway(refusal);
|
||||
|
||||
await expectLater(
|
||||
TemplateRepository(gateway).validate(source),
|
||||
throwsA(same(refusal)),
|
||||
);
|
||||
expect(gateway.calls, ['validate']);
|
||||
});
|
||||
|
||||
test('submit refuses a blank name without sending anything', () async {
|
||||
for (final name in ['', ' ', '\t\n']) {
|
||||
final gateway = RecordingGateway([
|
||||
{
|
||||
'detail': {'id': 1},
|
||||
},
|
||||
]);
|
||||
|
||||
await expectLater(
|
||||
TemplateRepository(gateway).submit(name, source),
|
||||
throwsA(
|
||||
isA<TemplateRepositoryException>().having(
|
||||
(error) => error.message,
|
||||
'message',
|
||||
'Enter a pipeline name.',
|
||||
),
|
||||
),
|
||||
);
|
||||
expect(gateway.calls, isEmpty);
|
||||
}
|
||||
});
|
||||
|
||||
test(
|
||||
'submit trims the name, preserves the source and is always create-only',
|
||||
() async {
|
||||
final detail = <String, dynamic>{
|
||||
'id': 9,
|
||||
'name': 'Release 2',
|
||||
'version': 1,
|
||||
};
|
||||
final gateway = RecordingGateway([
|
||||
{'type': 'submitted', 'detail': detail},
|
||||
]);
|
||||
|
||||
final result = await TemplateRepository(
|
||||
gateway,
|
||||
).submit(' Release 2 ', source);
|
||||
|
||||
expect(gateway.calls.single.type, 'submit');
|
||||
expect(gateway.calls.single.fields, {
|
||||
'name': 'Release 2',
|
||||
'source': source,
|
||||
'create_only': true,
|
||||
});
|
||||
expect(gateway.calls.single.fields.keys, [
|
||||
'name',
|
||||
'source',
|
||||
'create_only',
|
||||
]);
|
||||
expect(result, detail);
|
||||
},
|
||||
);
|
||||
|
||||
test(
|
||||
'submit rejects a reply without a detail instead of faking success',
|
||||
() async {
|
||||
final gateway = RecordingGateway([
|
||||
<String, dynamic>{'type': 'submitted'},
|
||||
]);
|
||||
|
||||
await expectLater(
|
||||
TemplateRepository(gateway).submit('Release', source),
|
||||
throwsA(
|
||||
isA<TemplateRepositoryException>().having(
|
||||
(error) => error.message,
|
||||
'message',
|
||||
'The server returned no pipeline detail.',
|
||||
),
|
||||
),
|
||||
);
|
||||
},
|
||||
);
|
||||
|
||||
test('submit propagates a server refusal verbatim', () async {
|
||||
const refusal = ServerRefusal('a pipeline named "Release" already exists');
|
||||
final gateway = RefusingGateway(refusal);
|
||||
|
||||
await expectLater(
|
||||
TemplateRepository(gateway).submit(' Release ', source),
|
||||
throwsA(same(refusal)),
|
||||
);
|
||||
expect(gateway.calls, ['submit']);
|
||||
});
|
||||
}
|
||||
@@ -0,0 +1,165 @@
|
||||
import 'package:flutter_test/flutter_test.dart';
|
||||
import 'package:pipelines_app/core/api_gateway.dart';
|
||||
import 'package:pipelines_app/features/templates/data/template_repository.dart';
|
||||
|
||||
import 'template_test_support.dart';
|
||||
|
||||
void main() {
|
||||
test(
|
||||
'list_templates sends only the type and preserves every field',
|
||||
() async {
|
||||
final catalogue = <JsonMap>[
|
||||
{
|
||||
'key': 'release',
|
||||
'name': 'Release',
|
||||
'description': 'Ship a version',
|
||||
'source': source,
|
||||
'scheduled': true,
|
||||
'future_field': {
|
||||
'nested': [1, 2, 3],
|
||||
},
|
||||
},
|
||||
{
|
||||
'key': 'blank',
|
||||
'name': '',
|
||||
'description': '',
|
||||
'source': 'name: empty',
|
||||
'scheduled': false,
|
||||
},
|
||||
];
|
||||
final gateway = RecordingGateway([
|
||||
{'templates': catalogue},
|
||||
]);
|
||||
|
||||
final entries = await TemplateRepository(gateway).listTemplates();
|
||||
|
||||
expect(gateway.calls, hasLength(1));
|
||||
expect(gateway.calls.single.type, 'list_templates');
|
||||
expect(gateway.calls.single.fields, isEmpty);
|
||||
expect(entries, hasLength(2));
|
||||
expect(entries.first.key, 'release');
|
||||
expect(entries.first.name, 'Release');
|
||||
expect(entries.first.description, 'Ship a version');
|
||||
expect(entries.first.source, source);
|
||||
expect(entries.first.scheduled, isTrue);
|
||||
expect(entries.first.raw['future_field'], {
|
||||
'nested': [1, 2, 3],
|
||||
});
|
||||
expect(entries.last.key, 'blank');
|
||||
expect(entries.last.scheduled, isFalse);
|
||||
expect(entries.last.raw, isNot(contains('missing')));
|
||||
},
|
||||
);
|
||||
|
||||
test('list_templates leaves the server reply untouched', () async {
|
||||
final first = <String, dynamic>{
|
||||
'key': 'one',
|
||||
'name': 'One',
|
||||
'source': source,
|
||||
'scheduled': true,
|
||||
'unknown': ['keep', 'me'],
|
||||
};
|
||||
final reply = <String, dynamic>{
|
||||
'templates': [first],
|
||||
'usage': {'templates': 1},
|
||||
};
|
||||
final gateway = RecordingGateway([reply]);
|
||||
|
||||
final entries = await TemplateRepository(gateway).listTemplates();
|
||||
|
||||
expect(reply['templates'], hasLength(1));
|
||||
expect(first, {
|
||||
'key': 'one',
|
||||
'name': 'One',
|
||||
'source': source,
|
||||
'scheduled': true,
|
||||
'unknown': ['keep', 'me'],
|
||||
});
|
||||
expect(entries.single.raw['unknown'], ['keep', 'me']);
|
||||
expect(
|
||||
() => entries.single.raw['name'] = 'changed',
|
||||
throwsUnsupportedError,
|
||||
);
|
||||
expect(first['name'], 'One');
|
||||
});
|
||||
|
||||
test('list_templates freezes nested unknown data in raw', () async {
|
||||
final nested = <String, dynamic>{
|
||||
'nested': [1, 2, 3],
|
||||
};
|
||||
final gateway = RecordingGateway([
|
||||
{
|
||||
'templates': [
|
||||
{
|
||||
'key': 'one',
|
||||
'name': 'One',
|
||||
'source': source,
|
||||
'scheduled': true,
|
||||
'future_field': nested,
|
||||
},
|
||||
],
|
||||
},
|
||||
]);
|
||||
|
||||
final entries = await TemplateRepository(gateway).listTemplates();
|
||||
final raw = entries.single.raw;
|
||||
final future = raw['future_field'] as Map;
|
||||
|
||||
expect(() => future['added'] = true, throwsUnsupportedError);
|
||||
expect(() => (future['nested'] as List).add(4), throwsUnsupportedError);
|
||||
expect(future['nested'], [1, 2, 3]);
|
||||
// The unmodifiable copies do not alias the server reply either.
|
||||
nested['added'] = true;
|
||||
expect(raw['future_field'], isNot(contains('added')));
|
||||
});
|
||||
|
||||
test('list_templates rejects a catalogue that is not a list', () async {
|
||||
final gateway = RecordingGateway([
|
||||
<String, dynamic>{
|
||||
'templates': {'key': 'not-a-list'},
|
||||
},
|
||||
]);
|
||||
|
||||
await expectLater(
|
||||
TemplateRepository(gateway).listTemplates(),
|
||||
throwsA(
|
||||
isA<TemplateRepositoryException>().having(
|
||||
(error) => error.message,
|
||||
'message',
|
||||
'The server returned no template catalogue.',
|
||||
),
|
||||
),
|
||||
);
|
||||
});
|
||||
|
||||
test(
|
||||
'list_templates rejects a catalogue entry that is not an object',
|
||||
() async {
|
||||
final gateway = RecordingGateway([
|
||||
<String, dynamic>{
|
||||
'templates': ['not-an-entry'],
|
||||
},
|
||||
]);
|
||||
|
||||
await expectLater(
|
||||
TemplateRepository(gateway).listTemplates(),
|
||||
throwsA(
|
||||
isA<TemplateRepositoryException>().having(
|
||||
(error) => error.message,
|
||||
'message',
|
||||
'The server returned a template that is not an object.',
|
||||
),
|
||||
),
|
||||
);
|
||||
},
|
||||
);
|
||||
|
||||
test('list_templates treats a reply with no catalogue as empty', () async {
|
||||
final gateway = RecordingGateway([<String, dynamic>{}]);
|
||||
|
||||
final entries = await TemplateRepository(gateway).listTemplates();
|
||||
|
||||
expect(entries, isEmpty);
|
||||
expect(gateway.calls.single.type, 'list_templates');
|
||||
});
|
||||
}
|
||||
@@ -0,0 +1,47 @@
|
||||
import 'package:pipelines_app/core/api_gateway.dart';
|
||||
|
||||
/// A recording gateway: it captures every request and answers from [replies].
|
||||
class RecordingGateway implements ApiGateway {
|
||||
RecordingGateway([List<JsonMap> replies = const []])
|
||||
: _replies = [...replies];
|
||||
|
||||
final List<JsonMap> _replies;
|
||||
final List<({String type, JsonMap fields})> calls = [];
|
||||
|
||||
@override
|
||||
Future<JsonMap> call(String type, [JsonMap fields = const {}]) async {
|
||||
calls.add((type: type, fields: Map<String, dynamic>.from(fields)));
|
||||
return _replies.removeAt(0);
|
||||
}
|
||||
|
||||
@override
|
||||
Stream<JsonMap> get frames => const Stream.empty();
|
||||
}
|
||||
|
||||
/// A gateway that refuses every request with the same server error object.
|
||||
class RefusingGateway implements ApiGateway {
|
||||
RefusingGateway(this.refusal);
|
||||
|
||||
final Object refusal;
|
||||
final List<String> calls = [];
|
||||
|
||||
@override
|
||||
Future<JsonMap> call(String type, [JsonMap fields = const {}]) async {
|
||||
calls.add(type);
|
||||
throw refusal;
|
||||
}
|
||||
|
||||
@override
|
||||
Stream<JsonMap> get frames => const Stream.empty();
|
||||
}
|
||||
|
||||
class ServerRefusal implements Exception {
|
||||
const ServerRefusal(this.message);
|
||||
final String message;
|
||||
@override
|
||||
String toString() => message;
|
||||
}
|
||||
|
||||
/// A source with Unicode, tabs, newlines and significant leading/trailing
|
||||
/// whitespace: every request must carry it unchanged.
|
||||
const source = ' name: "café"\n\tstages:\n - key: "añadir" 😀 \n';
|
||||
Reference in New Issue
Block a user