Merge branch 'main' into flutter/s1-list
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,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,90 @@
|
||||
import 'package:flutter_test/flutter_test.dart';
|
||||
import 'package:pipelines_app/features/search/domain/search_controller.dart';
|
||||
import 'package:pipelines_app/features/search/domain/search_query.dart';
|
||||
import 'search_test_support.dart';
|
||||
|
||||
void main() {
|
||||
test('a server error is shown, and retry issues a fresh request', () async {
|
||||
final gateway = QueuedGateway();
|
||||
final controller = controllerFor(gateway);
|
||||
addTearDown(controller.dispose);
|
||||
|
||||
controller.updateQuery('plan');
|
||||
await pumpEventQueue();
|
||||
gateway.fail(0, StateError('server is down'));
|
||||
await pumpEventQueue();
|
||||
|
||||
expect(controller.bodyKind, SearchBodyKind.error);
|
||||
expect(controller.error, contains('server is down'));
|
||||
|
||||
final retry = controller.retry();
|
||||
await pumpEventQueue();
|
||||
expect(gateway.calls, hasLength(2));
|
||||
expect(gateway.lastSearch, {'query': 'plan', 'kind': '', 'limit': 0});
|
||||
|
||||
gateway.succeed(
|
||||
1,
|
||||
envelope([
|
||||
{
|
||||
'kind': 'pipeline',
|
||||
'target': 'pipeline:7',
|
||||
'pipeline': {'id': 7},
|
||||
},
|
||||
]),
|
||||
);
|
||||
await retry;
|
||||
expect(controller.error, isNull);
|
||||
expect(controller.results, hasLength(1));
|
||||
expect(controller.bodyKind, SearchBodyKind.results);
|
||||
});
|
||||
|
||||
test('a stale failure never replaces the newest query state', () async {
|
||||
final gateway = QueuedGateway();
|
||||
final controller = controllerFor(gateway);
|
||||
addTearDown(controller.dispose);
|
||||
|
||||
controller.updateQuery('first');
|
||||
await pumpEventQueue();
|
||||
controller.updateQuery('second');
|
||||
await pumpEventQueue();
|
||||
|
||||
gateway.fail(0, StateError('stale failure'));
|
||||
await pumpEventQueue();
|
||||
|
||||
expect(controller.error, isNull);
|
||||
expect(controller.loading, isTrue);
|
||||
expect(controller.bodyKind, SearchBodyKind.searching);
|
||||
|
||||
gateway.succeed(
|
||||
1,
|
||||
envelope([
|
||||
{
|
||||
'kind': 'pipeline',
|
||||
'target': 'pipeline:9',
|
||||
'pipeline': {'id': 9},
|
||||
},
|
||||
]),
|
||||
);
|
||||
await pumpEventQueue();
|
||||
expect(controller.error, isNull);
|
||||
expect(controller.results.single['target'], 'pipeline:9');
|
||||
expect(controller.bodyKind, SearchBodyKind.results);
|
||||
});
|
||||
|
||||
test(
|
||||
'a missing search_results payload reports the unsupported copy',
|
||||
() async {
|
||||
final gateway = QueuedGateway();
|
||||
final controller = controllerFor(gateway);
|
||||
addTearDown(controller.dispose);
|
||||
|
||||
controller.updateQuery('plan');
|
||||
await pumpEventQueue();
|
||||
gateway.succeed(0, {'type': 'pong'});
|
||||
await pumpEventQueue();
|
||||
|
||||
expect(controller.bodyKind, SearchBodyKind.error);
|
||||
expect(controller.error, unsupportedSearchCopy);
|
||||
},
|
||||
);
|
||||
}
|
||||
@@ -0,0 +1,240 @@
|
||||
import 'package:flutter_test/flutter_test.dart';
|
||||
import 'package:pipelines_app/features/search/domain/search_controller.dart';
|
||||
import 'package:pipelines_app/features/search/domain/search_models.dart';
|
||||
import 'search_test_support.dart';
|
||||
|
||||
void main() {
|
||||
test('empty query with no kind is idle and sends no request', () async {
|
||||
final gateway = QueuedGateway();
|
||||
final controller = controllerFor(gateway);
|
||||
addTearDown(controller.dispose);
|
||||
|
||||
controller.updateQuery(' ');
|
||||
await pumpEventQueue();
|
||||
|
||||
expect(gateway.calls, isEmpty);
|
||||
expect(controller.bodyKind, SearchBodyKind.idle);
|
||||
expect(controller.results, isEmpty);
|
||||
expect(controller.loaded, isFalse);
|
||||
expect(controller.loading, isFalse);
|
||||
});
|
||||
|
||||
test('the request carries the parsed query, kind and limit 0', () async {
|
||||
final gateway = QueuedGateway();
|
||||
final controller = controllerFor(gateway);
|
||||
addTearDown(controller.dispose);
|
||||
|
||||
controller.updateQuery('t:run Plan TYPE:artifact');
|
||||
await pumpEventQueue();
|
||||
|
||||
expect(gateway.calls, hasLength(1));
|
||||
expect(gateway.lastSearch, {
|
||||
'query': 'Plan',
|
||||
'kind': 'artifact',
|
||||
'limit': 0,
|
||||
});
|
||||
});
|
||||
|
||||
test(
|
||||
'a kind selector change removes type tokens and sends at once',
|
||||
() async {
|
||||
final gateway = QueuedGateway();
|
||||
final controller = controllerFor(gateway);
|
||||
addTearDown(controller.dispose);
|
||||
|
||||
controller.updateQuery('plan t:run');
|
||||
await pumpEventQueue();
|
||||
expect(gateway.lastSearch['kind'], 'run');
|
||||
|
||||
controller.selectKind(SearchKind.pipeline);
|
||||
await pumpEventQueue();
|
||||
|
||||
expect(controller.query.trim(), 'plan');
|
||||
expect(controller.query, isNot(contains('t:run')));
|
||||
expect(controller.selectedKind, SearchKind.pipeline);
|
||||
expect(gateway.calls, hasLength(2));
|
||||
expect(gateway.lastSearch, {
|
||||
'query': 'plan',
|
||||
'kind': 'pipeline',
|
||||
'limit': 0,
|
||||
});
|
||||
},
|
||||
);
|
||||
|
||||
test(
|
||||
'kind-only browsing sends a blank query and reports the empty copy',
|
||||
() async {
|
||||
final gateway = QueuedGateway();
|
||||
final controller = controllerFor(gateway);
|
||||
addTearDown(controller.dispose);
|
||||
|
||||
controller.selectKind(SearchKind.pipeline);
|
||||
await pumpEventQueue();
|
||||
expect(gateway.lastSearch, {'query': '', 'kind': 'pipeline', 'limit': 0});
|
||||
expect(controller.bodyKind, SearchBodyKind.searching);
|
||||
|
||||
gateway.succeed(0, envelope(const []));
|
||||
await pumpEventQueue();
|
||||
expect(controller.bodyKind, SearchBodyKind.empty);
|
||||
expect(controller.emptyMessage, 'No pipelines yet.');
|
||||
},
|
||||
);
|
||||
|
||||
test('the latest query wins and a stale reply is dropped', () async {
|
||||
final gateway = QueuedGateway();
|
||||
final controller = controllerFor(gateway);
|
||||
addTearDown(controller.dispose);
|
||||
|
||||
controller.updateQuery('first');
|
||||
await pumpEventQueue();
|
||||
controller.updateQuery('second');
|
||||
await pumpEventQueue();
|
||||
expect(gateway.calls, hasLength(2));
|
||||
|
||||
gateway.succeed(
|
||||
0,
|
||||
envelope([
|
||||
{
|
||||
'kind': 'run',
|
||||
'target': 'run:1',
|
||||
'run': {'id': 1},
|
||||
},
|
||||
]),
|
||||
);
|
||||
await pumpEventQueue();
|
||||
expect(controller.results, isEmpty);
|
||||
expect(controller.loading, isTrue);
|
||||
expect(controller.bodyKind, SearchBodyKind.searching);
|
||||
|
||||
gateway.succeed(
|
||||
1,
|
||||
envelope([
|
||||
{
|
||||
'kind': 'run',
|
||||
'target': 'run:2',
|
||||
'run': {'id': 2},
|
||||
},
|
||||
]),
|
||||
);
|
||||
await pumpEventQueue();
|
||||
expect(controller.results.single['target'], 'run:2');
|
||||
expect(controller.loading, isFalse);
|
||||
expect(controller.bodyKind, SearchBodyKind.results);
|
||||
});
|
||||
|
||||
test('truncation and scan bounds come from the server envelope', () async {
|
||||
final gateway = QueuedGateway();
|
||||
final controller = controllerFor(gateway);
|
||||
addTearDown(controller.dispose);
|
||||
|
||||
controller.updateQuery('plan');
|
||||
await pumpEventQueue();
|
||||
gateway.succeed(
|
||||
0,
|
||||
envelope(
|
||||
[
|
||||
{
|
||||
'kind': 'field',
|
||||
'target': 'run:11:4:output',
|
||||
'field': {'run_id': 11},
|
||||
},
|
||||
],
|
||||
truncated: true,
|
||||
scannedSteps: 2000,
|
||||
scannedBytes: 1048576,
|
||||
cap: 50,
|
||||
),
|
||||
);
|
||||
await pumpEventQueue();
|
||||
|
||||
expect(controller.truncated, isTrue);
|
||||
expect(controller.scannedSteps, 2000);
|
||||
expect(controller.scannedBytes, 1048576);
|
||||
expect(controller.cap, 50);
|
||||
expect(controller.boundsText, contains('truncated'));
|
||||
expect(controller.boundsText, contains('cap 50'));
|
||||
expect(controller.boundsText, contains('2000 steps'));
|
||||
});
|
||||
|
||||
test(
|
||||
'a truncated empty reply reports the scan bound instead of a complete empty',
|
||||
() async {
|
||||
final gateway = QueuedGateway();
|
||||
final controller = controllerFor(gateway);
|
||||
addTearDown(controller.dispose);
|
||||
|
||||
controller.updateQuery('needle');
|
||||
await pumpEventQueue();
|
||||
gateway.succeed(
|
||||
0,
|
||||
envelope(
|
||||
const [],
|
||||
truncated: true,
|
||||
scannedSteps: 2000,
|
||||
scannedBytes: 1048576,
|
||||
cap: 50,
|
||||
),
|
||||
);
|
||||
await pumpEventQueue();
|
||||
|
||||
expect(controller.bodyKind, SearchBodyKind.empty);
|
||||
expect(
|
||||
controller.emptyMessage,
|
||||
'No matches for "needle" within the server\'s scan limit.',
|
||||
);
|
||||
expect(controller.boundsText, contains('Results truncated'));
|
||||
expect(controller.boundsText, contains('0 results'));
|
||||
expect(controller.boundsText, contains('2000 steps'));
|
||||
expect(controller.boundsText, contains('scan limit 1.0 MiB'));
|
||||
},
|
||||
);
|
||||
|
||||
test(
|
||||
'a complete empty reply stays a plain empty with no bounds line',
|
||||
() async {
|
||||
final gateway = QueuedGateway();
|
||||
final controller = controllerFor(gateway);
|
||||
addTearDown(controller.dispose);
|
||||
|
||||
controller.updateQuery('needle');
|
||||
await pumpEventQueue();
|
||||
gateway.succeed(0, envelope(const []));
|
||||
await pumpEventQueue();
|
||||
|
||||
expect(controller.bodyKind, SearchBodyKind.empty);
|
||||
expect(controller.emptyMessage, 'No matches for "needle".');
|
||||
expect(controller.boundsText, isNull);
|
||||
},
|
||||
);
|
||||
|
||||
test('in-hand kind narrowing keeps the list honest while loading', () async {
|
||||
final gateway = QueuedGateway();
|
||||
final controller = controllerFor(gateway);
|
||||
addTearDown(controller.dispose);
|
||||
|
||||
controller.updateQuery('plan');
|
||||
await pumpEventQueue();
|
||||
gateway.succeed(
|
||||
0,
|
||||
envelope([
|
||||
{
|
||||
'kind': 'run',
|
||||
'target': 'run:1',
|
||||
'run': {'id': 1},
|
||||
},
|
||||
{
|
||||
'kind': 'pipeline',
|
||||
'target': 'pipeline:7',
|
||||
'pipeline': {'id': 7},
|
||||
},
|
||||
]),
|
||||
);
|
||||
await pumpEventQueue();
|
||||
expect(controller.shownResults, hasLength(2));
|
||||
|
||||
controller.selectKind(SearchKind.run);
|
||||
await pumpEventQueue();
|
||||
expect(controller.shownResults, hasLength(1));
|
||||
expect(controller.shownResults.single['target'], 'run:1');
|
||||
});
|
||||
}
|
||||
@@ -0,0 +1,118 @@
|
||||
import 'package:flutter_test/flutter_test.dart';
|
||||
import 'package:pipelines_app/features/search/domain/search_models.dart';
|
||||
import 'package:pipelines_app/features/search/domain/search_query.dart';
|
||||
|
||||
const unsupported =
|
||||
'This server does not support search. Update the Pipelines server, then try again.';
|
||||
|
||||
void main() {
|
||||
test('kind vocabulary is exactly the server vocabulary, All kinds first', () {
|
||||
expect(SearchKind.values.map((kind) => kind.wire), [
|
||||
'',
|
||||
'pipeline',
|
||||
'run',
|
||||
'step',
|
||||
'field',
|
||||
'artifact',
|
||||
'version',
|
||||
]);
|
||||
expect(SearchKind.values.map((kind) => kind.label), [
|
||||
'All kinds',
|
||||
'Pipelines',
|
||||
'Runs',
|
||||
'Steps',
|
||||
'Fields',
|
||||
'Artifacts',
|
||||
'Versions',
|
||||
]);
|
||||
expect(SearchKind.fromWire('run'), SearchKind.run);
|
||||
expect(SearchKind.fromWire(''), SearchKind.all);
|
||||
expect(SearchKind.fromWire('prompts'), SearchKind.all);
|
||||
expect(SearchKind.fromWire(null), SearchKind.all);
|
||||
});
|
||||
|
||||
test('both type aliases and kinds are case insensitive', () {
|
||||
for (final kind in SearchKind.values.where(
|
||||
(kind) => kind != SearchKind.all,
|
||||
)) {
|
||||
for (final prefix in ['t', 'type', 'T', 'TyPe']) {
|
||||
final parsed = parseSearchQuery(
|
||||
'$prefix:${kind.wire.toUpperCase()} plan',
|
||||
);
|
||||
expect(parsed.query, ' plan');
|
||||
expect(parsed.kind, kind);
|
||||
expect(parsed.hasType, isTrue);
|
||||
}
|
||||
}
|
||||
});
|
||||
|
||||
test('normal, quoted and unsupported text is preserved byte for byte', () {
|
||||
const preserved = [
|
||||
' ordinary text\twith\nspacing ',
|
||||
'"t:pipeline" "hello world"',
|
||||
"'type:artifact'",
|
||||
'"escaped \\" t:run"',
|
||||
'"unclosed t:run',
|
||||
't:prompt type:unknown t: t:PIPELINES',
|
||||
'not:t:run t:run,',
|
||||
'prefix"t:run"',
|
||||
'\\t:run',
|
||||
];
|
||||
for (final query in preserved) {
|
||||
final parsed = parseSearchQuery(query, SearchKind.field);
|
||||
expect(parsed.query, query);
|
||||
expect(parsed.kind, SearchKind.field);
|
||||
expect(parsed.hasType, isFalse);
|
||||
}
|
||||
expect(
|
||||
parseSearchQuery('t:artifact "t:pipeline" report').query,
|
||||
' "t:pipeline" report',
|
||||
);
|
||||
});
|
||||
|
||||
test('last supported token wins over the selector; unknown tokens stay', () {
|
||||
final parsed = parseSearchQuery(
|
||||
't:run plan TYPE:artifact t:unknown',
|
||||
SearchKind.pipeline,
|
||||
);
|
||||
expect(parsed.query, ' plan t:unknown');
|
||||
expect(parsed.kind, SearchKind.artifact);
|
||||
expect(parsed.hasType, isTrue);
|
||||
expect(parseSearchQuery('plan', SearchKind.step).kind, SearchKind.step);
|
||||
});
|
||||
|
||||
test('type-only query is distinguishable from literal and blank input', () {
|
||||
final typed = parseSearchQuery('t:pipeline');
|
||||
expect(typed.query, '');
|
||||
expect(typed.kind, SearchKind.pipeline);
|
||||
expect(typed.hasType, isTrue);
|
||||
final blank = parseSearchQuery('');
|
||||
expect(blank.query, '');
|
||||
expect(blank.kind, SearchKind.all);
|
||||
expect(blank.hasType, isFalse);
|
||||
});
|
||||
|
||||
test('blank text is sendable only with a supported nonempty kind', () {
|
||||
for (final kind in SearchKind.values) {
|
||||
expect(searchQuerySendable(' \n\t ', kind), kind != SearchKind.all);
|
||||
}
|
||||
expect(searchQuerySendable('', SearchKind.all), isFalse);
|
||||
expect(searchQuerySendable('plan', SearchKind.all), isTrue);
|
||||
expect(searchQuerySendable(' plan ', SearchKind.all), isTrue);
|
||||
});
|
||||
|
||||
test(
|
||||
'failure copy distinguishes an unsupported server from a plain error',
|
||||
() {
|
||||
expect(searchFailureText(null), unsupported);
|
||||
expect(searchFailureText(' '), unsupported);
|
||||
expect(searchFailureText('unknown command: search'), unsupported);
|
||||
expect(searchFailureText('unsupported command'), unsupported);
|
||||
expect(searchFailureText('boom'), 'boom');
|
||||
expect(
|
||||
searchFailureText('request timed out after 30000ms'),
|
||||
'request timed out after 30000ms',
|
||||
);
|
||||
},
|
||||
);
|
||||
}
|
||||
@@ -0,0 +1,106 @@
|
||||
import 'package:flutter_test/flutter_test.dart';
|
||||
import 'package:pipelines_app/core/api_gateway.dart';
|
||||
import 'package:pipelines_app/features/search/data/search_repository.dart';
|
||||
import 'package:pipelines_app/features/search/domain/search_models.dart';
|
||||
|
||||
class RecordingGateway implements ApiGateway {
|
||||
String? type;
|
||||
JsonMap fields = {};
|
||||
JsonMap reply = {};
|
||||
Object? failure;
|
||||
|
||||
@override
|
||||
Future<JsonMap> call(String type, [JsonMap fields = const {}]) async {
|
||||
this.type = type;
|
||||
this.fields = Map.of(fields);
|
||||
if (failure case final error?) throw error;
|
||||
return reply;
|
||||
}
|
||||
|
||||
@override
|
||||
Stream<JsonMap> get frames => const Stream.empty();
|
||||
}
|
||||
|
||||
void main() {
|
||||
test(
|
||||
'search sends exactly search/query/kind/limit and parses bounds',
|
||||
() async {
|
||||
final gateway = RecordingGateway()
|
||||
..reply = {
|
||||
'search_results': {
|
||||
'query': 'plan',
|
||||
'kind': 'run',
|
||||
'results': [
|
||||
{
|
||||
'kind': 'run',
|
||||
'target': 'run:11',
|
||||
'run': {'id': 11},
|
||||
},
|
||||
],
|
||||
'truncated': true,
|
||||
'scanned_steps': 27,
|
||||
'scanned_bytes': 4096,
|
||||
'cap': 50,
|
||||
'scan_bound_bytes': 1048576,
|
||||
},
|
||||
};
|
||||
final repository = SearchRepository(gateway);
|
||||
|
||||
final response = await repository.search(
|
||||
query: 'plan',
|
||||
kind: SearchKind.run,
|
||||
);
|
||||
|
||||
expect(gateway.type, 'search');
|
||||
expect(gateway.fields, {
|
||||
'search': {'query': 'plan', 'kind': 'run', 'limit': 0},
|
||||
});
|
||||
expect(response, isNotNull);
|
||||
expect(response!.query, 'plan');
|
||||
expect(response.kind, SearchKind.run);
|
||||
expect(response.results.single['target'], 'run:11');
|
||||
expect(response.truncated, isTrue);
|
||||
expect(response.scannedSteps, 27);
|
||||
expect(response.scannedBytes, 4096);
|
||||
expect(response.cap, 50);
|
||||
expect(response.scanBoundBytes, 1048576);
|
||||
},
|
||||
);
|
||||
|
||||
test('all-kinds search still sends the empty kind field', () async {
|
||||
final gateway = RecordingGateway()
|
||||
..reply = {
|
||||
'search_results': {'results': <JsonMap>[]},
|
||||
};
|
||||
await SearchRepository(gateway).search(query: 'plan', kind: SearchKind.all);
|
||||
expect(gateway.fields, {
|
||||
'search': {'query': 'plan', 'kind': '', 'limit': 0},
|
||||
});
|
||||
});
|
||||
|
||||
test(
|
||||
'a reply without search_results is null, not an empty success',
|
||||
() async {
|
||||
final gateway = RecordingGateway()..reply = {'type': 'pong'};
|
||||
final response = await SearchRepository(
|
||||
gateway,
|
||||
).search(query: 'plan', kind: SearchKind.all);
|
||||
expect(response, isNull);
|
||||
},
|
||||
);
|
||||
|
||||
test('server refusals propagate to the caller unchanged', () async {
|
||||
final gateway = RecordingGateway()
|
||||
..failure = StateError('permission denied');
|
||||
await expectLater(
|
||||
SearchRepository(gateway).search(query: 'plan', kind: SearchKind.all),
|
||||
throwsA(
|
||||
isA<StateError>().having(
|
||||
(error) => error.message,
|
||||
'message',
|
||||
'permission denied',
|
||||
),
|
||||
),
|
||||
);
|
||||
});
|
||||
}
|
||||
@@ -0,0 +1,161 @@
|
||||
import 'package:flutter_test/flutter_test.dart';
|
||||
import 'package:pipelines_app/core/api_gateway.dart';
|
||||
import 'package:pipelines_app/features/search/domain/search_row.dart';
|
||||
|
||||
JsonMap pipelineHit([JsonMap overrides = const {}]) => {
|
||||
'kind': 'pipeline',
|
||||
'target': 'pipeline:7',
|
||||
'pipeline': {
|
||||
'id': 7,
|
||||
'name': 'release',
|
||||
'version': 3,
|
||||
'stages': 4,
|
||||
...overrides,
|
||||
},
|
||||
};
|
||||
|
||||
JsonMap versionHit() => {
|
||||
'kind': 'version',
|
||||
'target': 'pipeline:9:v2',
|
||||
'recency': '2026-01-01T00:00:00Z',
|
||||
'version': {
|
||||
'pipeline_id': 9,
|
||||
'version': 2,
|
||||
'content_hash': 'abc',
|
||||
'created_at': '2026-01-01T00:00:00Z',
|
||||
},
|
||||
};
|
||||
|
||||
JsonMap runHit([JsonMap overrides = const {}]) => {
|
||||
'kind': 'run',
|
||||
'target': 'run:11',
|
||||
'run': {
|
||||
'id': 11,
|
||||
'pipeline_name': 'release',
|
||||
'status': 'succeeded',
|
||||
'created_at': '2026-01-01T00:00:00Z',
|
||||
...overrides,
|
||||
},
|
||||
};
|
||||
|
||||
JsonMap stepHit([JsonMap overrides = const {}]) => {
|
||||
'kind': 'step',
|
||||
'target': 'run:11:4',
|
||||
'step': {
|
||||
'run_id': 11,
|
||||
'sequence': 4,
|
||||
'stage': 'builder',
|
||||
'visit': 2,
|
||||
'outcome': 'success',
|
||||
...overrides,
|
||||
},
|
||||
};
|
||||
|
||||
JsonMap fieldHit([JsonMap overrides = const {}]) => {
|
||||
'kind': 'field',
|
||||
'target': 'run:11:4:output',
|
||||
'matched_field': 'output',
|
||||
'field': {
|
||||
'run_id': 11,
|
||||
'sequence': 4,
|
||||
'field': 'output',
|
||||
'snippet': 'the release is ready',
|
||||
...overrides,
|
||||
},
|
||||
};
|
||||
|
||||
JsonMap artifactHit([JsonMap overrides = const {}]) => {
|
||||
'kind': 'artifact',
|
||||
'target': '/artifacts/11/4/notes.md',
|
||||
'artifact': {
|
||||
'run_id': 11,
|
||||
'sequence': 4,
|
||||
'name': 'notes.md',
|
||||
'content_type': 'text/markdown',
|
||||
'size_bytes': 2048,
|
||||
...overrides,
|
||||
},
|
||||
};
|
||||
|
||||
void main() {
|
||||
test('per-kind rows mirror the browser titles and secondary lines', () {
|
||||
final pipeline = searchRowView(pipelineHit(), 'release');
|
||||
expect(pipeline.title, 'release');
|
||||
expect(pipeline.secondary, 'pipeline 7 · v3 · 4 stages');
|
||||
expect(pipeline.kindLabel, 'Pipeline');
|
||||
expect(pipeline.target, 'pipeline:7');
|
||||
|
||||
final blankName = searchRowView(pipelineHit({'name': ' '}), 'x');
|
||||
expect(blankName.title, 'pipeline 7');
|
||||
|
||||
final version = searchRowView(versionHit(), 'release');
|
||||
expect(version.title, 'pipeline 9 · v2');
|
||||
expect(version.secondary, startsWith('content abc · '));
|
||||
expect(version.recency, isNotNull);
|
||||
|
||||
final run = searchRowView(runHit(), 'release');
|
||||
expect(run.title, 'run #11 · release');
|
||||
expect(run.secondary, startsWith('succeeded · '));
|
||||
expect(run.secondary.split(' · '), hasLength(2));
|
||||
|
||||
final unnamed = searchRowView(runHit({'pipeline_name': ''}), '');
|
||||
expect(unnamed.title, 'run #11');
|
||||
|
||||
final step = searchRowView(stepHit({'target': '#success'}), '');
|
||||
expect(step.title, 'run #11 · builder visit 2');
|
||||
expect(step.secondary, 'success · → #success');
|
||||
|
||||
final artifact = searchRowView(artifactHit(), 'notes');
|
||||
expect(artifact.title, 'notes.md');
|
||||
expect(artifact.secondary, 'run #11 · step 4 · text/markdown · 2.0 KiB');
|
||||
});
|
||||
|
||||
test('a field snippet marks the first case-insensitive query occurrence', () {
|
||||
final row = searchRowView(fieldHit(), 'RELEASE');
|
||||
expect(row.snippet, 'the release is ready');
|
||||
expect(row.snippetSpans.map((span) => (span.text, span.matched)), [
|
||||
('the ', false),
|
||||
('release', true),
|
||||
(' is ready', false),
|
||||
]);
|
||||
expect(row.snippetSpans.where((span) => span.matched), hasLength(1));
|
||||
|
||||
final absent = searchRowView(fieldHit({'snippet': 'no match'}), 'absent');
|
||||
expect(absent.snippetSpans.single.matched, isFalse);
|
||||
expect(absent.snippetSpans.single.text, 'no match');
|
||||
|
||||
final blank = searchRowView(fieldHit({'snippet': ''}), 'release');
|
||||
expect(blank.snippetSpans, isEmpty);
|
||||
});
|
||||
|
||||
test('unknown or targetless hits are read-only and never open a guess', () {
|
||||
final unknown = searchRowView({
|
||||
'kind': 'future_kind',
|
||||
'target': 'widget:7',
|
||||
}, 'x');
|
||||
expect(unknown.title, 'widget:7');
|
||||
expect(unknown.secondary, 'kind "future_kind" · read-only');
|
||||
expect(unknown.openable, isFalse);
|
||||
expect(unknown.target, isNull);
|
||||
expect(unknown.kindLabel, 'Unrecognised result');
|
||||
|
||||
final targetless = searchRowView({
|
||||
'kind': 'run',
|
||||
'run': {'id': 11},
|
||||
}, 'x');
|
||||
expect(targetless.openable, isFalse);
|
||||
expect(targetless.target, isNull);
|
||||
|
||||
expect(searchRowView(pipelineHit(), '').openable, isTrue);
|
||||
expect(searchRowView(versionHit(), '').openable, isTrue);
|
||||
expect(searchRowView(artifactHit(), '').openable, isTrue);
|
||||
});
|
||||
|
||||
test('byte sizes use the shared binary rendering', () {
|
||||
expect(humanBytes(0), '0 B');
|
||||
expect(humanBytes(1023), '1023 B');
|
||||
expect(humanBytes(1024), '1.0 KiB');
|
||||
expect(humanBytes(2048), '2.0 KiB');
|
||||
expect(humanBytes(1048576), '1.0 MiB');
|
||||
});
|
||||
}
|
||||
@@ -0,0 +1,146 @@
|
||||
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/search/search_screen.dart';
|
||||
|
||||
class RecordingGateway implements ApiGateway {
|
||||
RecordingGateway(this.reply);
|
||||
|
||||
final JsonMap Function(String query, String kind) reply;
|
||||
final calls = <JsonMap>[];
|
||||
|
||||
@override
|
||||
Future<JsonMap> call(String type, [JsonMap fields = const {}]) async {
|
||||
calls.add(Map.of(fields));
|
||||
final search = Map<String, dynamic>.from(fields['search'] as Map);
|
||||
return reply(search['query'] as String, search['kind'] as String);
|
||||
}
|
||||
|
||||
@override
|
||||
Stream<JsonMap> get frames => const Stream.empty();
|
||||
}
|
||||
|
||||
JsonMap response(
|
||||
List<JsonMap> results, {
|
||||
bool truncated = false,
|
||||
int scannedSteps = 2,
|
||||
int scannedBytes = 128,
|
||||
}) => {
|
||||
'search_results': {
|
||||
'results': results,
|
||||
'truncated': truncated,
|
||||
'scanned_steps': scannedSteps,
|
||||
'scanned_bytes': scannedBytes,
|
||||
'cap': 50,
|
||||
'scan_bound_bytes': 1 << 20,
|
||||
},
|
||||
};
|
||||
|
||||
final pipeline = {
|
||||
'kind': 'pipeline',
|
||||
'target': 'pipeline:7',
|
||||
'pipeline': {'id': 7, 'name': 'Release planner', 'version': 3, 'stages': 4},
|
||||
};
|
||||
|
||||
Widget material(Widget child) => MaterialApp(home: child);
|
||||
|
||||
void main() {
|
||||
testWidgets(
|
||||
'a fresh screen sends no request and idle is not an empty result',
|
||||
(tester) async {
|
||||
final gateway = RecordingGateway((_, _) => response([pipeline]));
|
||||
await tester.pumpWidget(
|
||||
material(SearchScreen(gateway: gateway, onOpen: (_) {})),
|
||||
);
|
||||
|
||||
expect(gateway.calls, isEmpty);
|
||||
expect(
|
||||
find.text('Search pipelines, runs and artifacts.'),
|
||||
findsOneWidget,
|
||||
);
|
||||
expect(find.textContaining('No matches'), findsNothing);
|
||||
},
|
||||
);
|
||||
|
||||
testWidgets('an empty server result shows the query-specific empty copy', (
|
||||
tester,
|
||||
) async {
|
||||
final gateway = RecordingGateway((_, _) => response(const []));
|
||||
await tester.pumpWidget(
|
||||
material(SearchScreen(gateway: gateway, onOpen: (_) {})),
|
||||
);
|
||||
|
||||
await tester.enterText(find.byKey(const ValueKey('search.field')), 'zzz');
|
||||
await tester.pump(const Duration(milliseconds: 300));
|
||||
await tester.pumpAndSettle();
|
||||
|
||||
expect(gateway.calls, hasLength(1));
|
||||
expect(find.text('No matches for "zzz".'), findsOneWidget);
|
||||
expect(find.text('Searching…'), findsNothing);
|
||||
});
|
||||
|
||||
testWidgets(
|
||||
'a truncated empty reply keeps the server scan warning on screen',
|
||||
(tester) async {
|
||||
final gateway = RecordingGateway(
|
||||
(_, _) => response(
|
||||
const [],
|
||||
truncated: true,
|
||||
scannedSteps: 2000,
|
||||
scannedBytes: 1048576,
|
||||
),
|
||||
);
|
||||
await tester.pumpWidget(
|
||||
material(SearchScreen(gateway: gateway, onOpen: (_) {})),
|
||||
);
|
||||
|
||||
await tester.enterText(
|
||||
find.byKey(const ValueKey('search.field')),
|
||||
'needle',
|
||||
);
|
||||
await tester.pump(const Duration(milliseconds: 300));
|
||||
await tester.pumpAndSettle();
|
||||
|
||||
expect(
|
||||
find.text('No matches for "needle" within the server\'s scan limit.'),
|
||||
findsOneWidget,
|
||||
);
|
||||
expect(find.textContaining('Results truncated'), findsOneWidget);
|
||||
expect(find.textContaining('scanned 2000 steps'), findsOneWidget);
|
||||
expect(find.textContaining('scan limit 1.0 MiB'), findsOneWidget);
|
||||
},
|
||||
);
|
||||
|
||||
testWidgets('changing Kind clears a typed token and requests the new kind', (
|
||||
tester,
|
||||
) async {
|
||||
tester.view.physicalSize = const Size(390, 844);
|
||||
tester.view.devicePixelRatio = 1;
|
||||
addTearDown(tester.view.resetPhysicalSize);
|
||||
addTearDown(tester.view.resetDevicePixelRatio);
|
||||
final gateway = RecordingGateway((_, _) => response([pipeline]));
|
||||
await tester.pumpWidget(
|
||||
material(SearchScreen(gateway: gateway, onOpen: (_) {})),
|
||||
);
|
||||
|
||||
await tester.enterText(
|
||||
find.byKey(const ValueKey('search.field')),
|
||||
'plan t:run',
|
||||
);
|
||||
await tester.pump(const Duration(milliseconds: 300));
|
||||
await tester.pumpAndSettle();
|
||||
expect((gateway.calls.single['search'] as Map)['kind'], 'run');
|
||||
|
||||
await tester.tap(find.byKey(const ValueKey('search.kind')));
|
||||
await tester.pumpAndSettle();
|
||||
await tester.tap(find.text('Pipelines').last);
|
||||
await tester.pumpAndSettle();
|
||||
|
||||
final field = tester.widget<SearchBar>(
|
||||
find.byKey(const ValueKey('search.field')),
|
||||
);
|
||||
expect(field.controller!.text.trim(), 'plan');
|
||||
expect(field.controller!.text, isNot(contains('t:run')));
|
||||
expect((gateway.calls.last['search'] as Map)['kind'], 'pipeline');
|
||||
});
|
||||
}
|
||||
@@ -0,0 +1,223 @@
|
||||
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/search/domain/search_models.dart';
|
||||
import 'package:pipelines_app/features/search/search_screen.dart';
|
||||
|
||||
class ImmediateGateway implements ApiGateway {
|
||||
ImmediateGateway(this.reply);
|
||||
|
||||
final JsonMap Function(String query, String kind) reply;
|
||||
final calls = <JsonMap>[];
|
||||
|
||||
@override
|
||||
Future<JsonMap> call(String type, [JsonMap fields = const {}]) async {
|
||||
calls.add(Map.of(fields));
|
||||
final search = Map<String, dynamic>.from(fields['search'] as Map);
|
||||
return reply(search['query'] as String, search['kind'] as String);
|
||||
}
|
||||
|
||||
@override
|
||||
Stream<JsonMap> get frames => const Stream.empty();
|
||||
}
|
||||
|
||||
class FailingThenSucceedingGateway implements ApiGateway {
|
||||
FailingThenSucceedingGateway(this.success);
|
||||
|
||||
final JsonMap success;
|
||||
var calls = 0;
|
||||
|
||||
@override
|
||||
Future<JsonMap> call(String type, [JsonMap fields = const {}]) async {
|
||||
calls++;
|
||||
if (calls == 1) throw StateError('unknown command: search');
|
||||
return success;
|
||||
}
|
||||
|
||||
@override
|
||||
Stream<JsonMap> get frames => const Stream.empty();
|
||||
}
|
||||
|
||||
JsonMap hit({
|
||||
required String kind,
|
||||
required String target,
|
||||
required Map<String, dynamic> payload,
|
||||
}) => {'kind': kind, 'target': target, ...payload};
|
||||
|
||||
JsonMap response(List<JsonMap> results) => {
|
||||
'search_results': {
|
||||
'results': results,
|
||||
'truncated': false,
|
||||
'scanned_steps': 4,
|
||||
'scanned_bytes': 512,
|
||||
'cap': 50,
|
||||
'scan_bound_bytes': 1 << 20,
|
||||
},
|
||||
};
|
||||
|
||||
final pipeline = hit(
|
||||
kind: 'pipeline',
|
||||
target: 'pipeline:7',
|
||||
payload: {
|
||||
'pipeline': {'id': 7, 'name': 'Release planner', 'version': 3, 'stages': 4},
|
||||
},
|
||||
);
|
||||
final field = hit(
|
||||
kind: 'field',
|
||||
target: 'run:11:4:output',
|
||||
payload: {
|
||||
'matched_field': 'output',
|
||||
'field': {
|
||||
'run_id': 11,
|
||||
'sequence': 4,
|
||||
'field': 'output',
|
||||
'snippet': 'the release is ready',
|
||||
},
|
||||
},
|
||||
);
|
||||
|
||||
void useNarrowSurface(WidgetTester tester) {
|
||||
tester.view.physicalSize = const Size(390, 844);
|
||||
tester.view.devicePixelRatio = 1;
|
||||
addTearDown(tester.view.resetPhysicalSize);
|
||||
addTearDown(tester.view.resetDevicePixelRatio);
|
||||
}
|
||||
|
||||
Widget largeText(Widget child) => MediaQuery(
|
||||
data: const MediaQueryData(textScaler: TextScaler.linear(2)),
|
||||
child: child,
|
||||
);
|
||||
|
||||
Future<void> search(WidgetTester tester, String query) async {
|
||||
await tester.enterText(find.byKey(const ValueKey('search.field')), query);
|
||||
await tester.pump(const Duration(milliseconds: 300));
|
||||
await tester.pumpAndSettle();
|
||||
}
|
||||
|
||||
void main() {
|
||||
for (final width in [390.0, 1000.0]) {
|
||||
testWidgets(
|
||||
'Material search is native and usable at width $width and 2x text',
|
||||
(tester) async {
|
||||
useNarrowSurface(tester);
|
||||
tester.view.physicalSize = Size(width, 844);
|
||||
final gateway = ImmediateGateway((_, _) => response([pipeline, field]));
|
||||
JsonMap? opened;
|
||||
await tester.pumpWidget(
|
||||
MaterialApp(
|
||||
home: largeText(
|
||||
SearchScreen(
|
||||
gateway: gateway,
|
||||
onOpen: (value) => opened = value,
|
||||
cupertino: false,
|
||||
),
|
||||
),
|
||||
),
|
||||
);
|
||||
|
||||
expect(find.byType(SearchBar), findsOneWidget);
|
||||
expect(find.byType(DropdownButton<SearchKind>), findsOneWidget);
|
||||
expect(find.text('All kinds'), findsOneWidget);
|
||||
expect(gateway.calls, isEmpty);
|
||||
|
||||
await search(tester, 'release');
|
||||
|
||||
expect(gateway.calls.single, {
|
||||
'search': {'query': 'release', 'kind': '', 'limit': 0},
|
||||
});
|
||||
expect(find.text('Release planner'), findsOneWidget);
|
||||
expect(find.textContaining('the release is ready'), findsOneWidget);
|
||||
expect(find.textContaining('4 results'), findsNothing);
|
||||
expect(tester.takeException(), isNull);
|
||||
|
||||
await tester.tap(
|
||||
find.byKey(const ValueKey('search.result.pipeline:7')),
|
||||
);
|
||||
expect(opened?['target'], 'pipeline:7');
|
||||
expect(opened?['kind'], 'pipeline');
|
||||
},
|
||||
);
|
||||
}
|
||||
|
||||
testWidgets(
|
||||
'Cupertino search uses native controls and opens the real target',
|
||||
(tester) async {
|
||||
useNarrowSurface(tester);
|
||||
final gateway = ImmediateGateway((_, _) => response([pipeline]));
|
||||
JsonMap? opened;
|
||||
await tester.pumpWidget(
|
||||
CupertinoApp(
|
||||
home: largeText(
|
||||
SearchScreen(
|
||||
gateway: gateway,
|
||||
onOpen: (value) => opened = value,
|
||||
cupertino: true,
|
||||
),
|
||||
),
|
||||
),
|
||||
);
|
||||
|
||||
expect(find.byType(CupertinoSearchTextField), findsOneWidget);
|
||||
await search(tester, 'release');
|
||||
|
||||
expect(find.text('Release planner'), findsOneWidget);
|
||||
expect(tester.takeException(), isNull);
|
||||
await tester.tap(find.byKey(const ValueKey('search.result.pipeline:7')));
|
||||
expect(opened?['target'], 'pipeline:7');
|
||||
},
|
||||
);
|
||||
|
||||
testWidgets('a read-only hit cannot be opened', (tester) async {
|
||||
useNarrowSurface(tester);
|
||||
final unknown = hit(
|
||||
kind: 'future_kind',
|
||||
target: 'widget:7',
|
||||
payload: const {},
|
||||
);
|
||||
final gateway = ImmediateGateway((_, _) => response([unknown]));
|
||||
JsonMap? opened;
|
||||
await tester.pumpWidget(
|
||||
MaterialApp(
|
||||
home: SearchScreen(
|
||||
gateway: gateway,
|
||||
onOpen: (value) => opened = value,
|
||||
cupertino: false,
|
||||
),
|
||||
),
|
||||
);
|
||||
await search(tester, 'widget');
|
||||
|
||||
expect(find.textContaining('read-only'), findsOneWidget);
|
||||
await tester.tap(find.byKey(const ValueKey('search.result.widget:7')));
|
||||
expect(opened, isNull);
|
||||
});
|
||||
|
||||
testWidgets('an unsupported server shows its sentence and retry recovers', (
|
||||
tester,
|
||||
) async {
|
||||
useNarrowSurface(tester);
|
||||
final gateway = FailingThenSucceedingGateway(response([pipeline]));
|
||||
await tester.pumpWidget(
|
||||
MaterialApp(
|
||||
home: SearchScreen(gateway: gateway, onOpen: (_) {}, cupertino: false),
|
||||
),
|
||||
);
|
||||
await search(tester, 'release');
|
||||
|
||||
expect(
|
||||
find.text(
|
||||
'This server does not support search. Update the Pipelines server, then try again.',
|
||||
),
|
||||
findsOneWidget,
|
||||
);
|
||||
expect(find.byKey(const ValueKey('search.retry')), findsOneWidget);
|
||||
|
||||
await tester.tap(find.byKey(const ValueKey('search.retry')));
|
||||
await tester.pumpAndSettle();
|
||||
|
||||
expect(gateway.calls, 2);
|
||||
expect(find.text('Release planner'), findsOneWidget);
|
||||
expect(find.byKey(const ValueKey('search.retry')), findsNothing);
|
||||
});
|
||||
}
|
||||
@@ -0,0 +1,50 @@
|
||||
import 'dart:async';
|
||||
|
||||
import 'package:pipelines_app/core/api_gateway.dart';
|
||||
import 'package:pipelines_app/features/search/data/search_repository.dart';
|
||||
import 'package:pipelines_app/features/search/domain/search_controller.dart';
|
||||
|
||||
class QueuedGateway implements ApiGateway {
|
||||
final calls = <JsonMap>[];
|
||||
final pending = <Completer<JsonMap>>[];
|
||||
|
||||
@override
|
||||
Future<JsonMap> call(String type, [JsonMap fields = const {}]) {
|
||||
calls.add(Map.of(fields));
|
||||
final completer = Completer<JsonMap>();
|
||||
pending.add(completer);
|
||||
return completer.future;
|
||||
}
|
||||
|
||||
@override
|
||||
Stream<JsonMap> get frames => const Stream.empty();
|
||||
|
||||
void succeed(int index, JsonMap reply) => pending[index].complete(reply);
|
||||
void fail(int index, Object error) => pending[index].completeError(error);
|
||||
|
||||
JsonMap get lastSearch =>
|
||||
Map<String, dynamic>.from((calls.last['search'] as Map));
|
||||
}
|
||||
|
||||
JsonMap envelope(
|
||||
List<JsonMap> results, {
|
||||
bool truncated = false,
|
||||
int scannedSteps = 0,
|
||||
int scannedBytes = 0,
|
||||
int cap = 50,
|
||||
int scanBoundBytes = 1 << 20,
|
||||
}) => {
|
||||
'search_results': {
|
||||
'results': results,
|
||||
'truncated': truncated,
|
||||
'scanned_steps': scannedSteps,
|
||||
'scanned_bytes': scannedBytes,
|
||||
'cap': cap,
|
||||
'scan_bound_bytes': scanBoundBytes,
|
||||
},
|
||||
};
|
||||
|
||||
OmniSearchController controllerFor(ApiGateway gateway) => OmniSearchController(
|
||||
repository: SearchRepository(gateway),
|
||||
debounce: Duration.zero,
|
||||
);
|
||||
@@ -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