Files
pipelines/app/test/features/runs/actions/decision_resilience_test.dart
T

352 lines
9.8 KiB
Dart

import 'dart:async';
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:flutter_test/flutter_test.dart';
import 'package:pipelines_app/core/api_gateway.dart';
import 'package:pipelines_app/core/transport/connection_state.dart';
import 'package:pipelines_app/features/runs/actions/decision_action.dart';
import 'package:pipelines_app/features/runs/actions/decision_wording.dart';
typedef _Handler = Future<JsonMap> Function(String type, JsonMap fields);
class _FakeGateway implements ApiGateway {
_FakeGateway(this.handler);
final _Handler handler;
final List<({String type, JsonMap fields})> calls = [];
@override
Future<JsonMap> call(String type, [JsonMap fields = const {}]) {
final copy = Map<String, dynamic>.from(fields);
calls.add((type: type, fields: copy));
return handler(type, copy);
}
@override
Stream<JsonMap> get frames => const Stream.empty();
}
JsonMap _waitingRun() => {
'id': 7,
'title': 'Report',
'status': 'waiting',
'current_stage': 'human',
'graph': {
'stages': [
{
'key': 'human',
'type': 'human',
'prompt': 'Graph prompt',
'edges': {'go': '#', 'skip': 'finish'},
},
],
},
'steps': [
{
'sequence': 1,
'status': 'waiting',
'stage': 'human',
'output': {
'prompt': 'Ship it?',
'options': ['go', 'skip'],
},
},
],
};
JsonMap _decided() => {
..._waitingRun(),
'status': 'succeeded',
'steps': const <JsonMap>[],
};
Widget _app(Widget child, {required bool cupertino, double textScale = 1}) {
Widget media(BuildContext context, Widget? app) => MediaQuery(
data: MediaQuery.of(
context,
).copyWith(textScaler: TextScaler.linear(textScale)),
child: app!,
);
if (cupertino) {
return CupertinoApp(
builder: media,
home: CupertinoPageScaffold(child: Center(child: child)),
);
}
return MaterialApp(
builder: media,
home: Scaffold(body: Center(child: child)),
);
}
Future<void> _pump(
WidgetTester tester,
_FakeGateway gateway,
List<JsonMap> changed, {
bool cupertino = false,
double textScale = 1,
}) async {
await tester.pumpWidget(
_app(
HumanDecisionAction(
run: _waitingRun(),
gateway: gateway,
onRunChanged: changed.add,
),
cupertino: cupertino,
textScale: textScale,
),
);
}
Future<void> _chooseGo(WidgetTester tester, {String feedback = ''}) async {
await tester.tap(find.byKey(decisionActionKey));
await tester.pumpAndSettle();
await tester.tap(find.text('go'));
await tester.pump();
if (feedback.isNotEmpty) {
await tester.enterText(find.byKey(decisionFeedbackKey), feedback);
}
}
void main() {
testWidgets('a refusal stays visible and keeps the selected draft', (
tester,
) async {
const refusal = 'option "go" is not a valid outcome; choose one of: skip';
final gateway = _FakeGateway((type, fields) async {
if (type == 'decide_run') throw const ProtocolError(refusal);
expect(type, 'get_run');
return {
'run_detail': {
..._waitingRun(),
'status': 'failed',
'steps': const <JsonMap>[],
},
};
});
final changed = <JsonMap>[];
await _pump(tester, gateway, changed);
await _chooseGo(tester, feedback: ' keep me ');
await tester.tap(find.text(decisionConfirmLabel));
await tester.pumpAndSettle();
expect(gateway.calls.map((call) => call.type), ['decide_run', 'get_run']);
expect(gateway.calls.last.fields, {'run_id': 7});
expect(find.text(refusal), findsOneWidget);
expect(find.byType(AlertDialog), findsOneWidget);
expect(
tester
.widget<RadioGroup<String>>(find.byType(RadioGroup<String>))
.groupValue,
'go',
);
expect(
tester
.widget<TextField>(find.byKey(decisionFeedbackKey))
.controller
?.text,
' keep me ',
);
expect(changed.single['status'], 'failed');
expect(find.byKey(decisionActionKey), findsNothing);
});
testWidgets('a refusal is shown before the resync keeps the draft', (
tester,
) async {
const refusal = 'option "go" is no longer valid';
final resync = Completer<JsonMap>();
final gateway = _FakeGateway((type, fields) {
if (type == 'decide_run') throw const ProtocolError(refusal);
return resync.future;
});
final changed = <JsonMap>[];
await _pump(tester, gateway, changed);
await _chooseGo(tester, feedback: ' keep me ');
await tester.tap(find.text(decisionConfirmLabel));
await tester.pump();
expect(find.text(refusal), findsOneWidget);
expect(find.byType(AlertDialog), findsOneWidget);
expect(
tester
.widget<RadioGroup<String>>(find.byType(RadioGroup<String>))
.groupValue,
'go',
);
expect(
tester
.widget<TextField>(find.byKey(decisionFeedbackKey))
.controller
?.text,
' keep me ',
);
expect(
tester
.widget<TextButton>(
find.widgetWithText(TextButton, decisionCancelLabel),
)
.onPressed,
isNotNull,
);
expect(changed, isEmpty);
resync.complete({'run_detail': _decided()});
await tester.pumpAndSettle();
expect(find.text(refusal), findsOneWidget);
expect(
tester
.widget<FilledButton>(
find.widgetWithText(FilledButton, decisionConfirmLabel),
)
.onPressed,
isNull,
);
expect(changed.single['status'], 'succeeded');
});
testWidgets('a failed resync keeps the draft and allows a retry', (
tester,
) async {
const refusal = 'server refused the decision';
final gateway = _FakeGateway((type, fields) async {
if (type == 'decide_run') throw const ProtocolError(refusal);
throw const NotConnectedError();
});
final changed = <JsonMap>[];
await _pump(tester, gateway, changed);
await _chooseGo(tester, feedback: ' keep me ');
await tester.tap(find.text(decisionConfirmLabel));
await tester.pumpAndSettle();
expect(gateway.calls.map((call) => call.type), ['decide_run', 'get_run']);
expect(find.text(refusal), findsOneWidget);
expect(
tester
.widget<RadioGroup<String>>(find.byType(RadioGroup<String>))
.groupValue,
'go',
);
expect(
tester
.widget<FilledButton>(
find.widgetWithText(FilledButton, decisionConfirmLabel),
)
.onPressed,
isNotNull,
);
expect(changed, isEmpty);
});
testWidgets('a late resync after disposal is ignored safely', (tester) async {
final resync = Completer<JsonMap>();
final gateway = _FakeGateway((type, fields) {
if (type == 'decide_run') throw const ProtocolError('no longer waiting');
return resync.future;
});
final changed = <JsonMap>[];
await _pump(tester, gateway, changed);
await _chooseGo(tester);
await tester.tap(find.text(decisionConfirmLabel));
await tester.pump();
expect(gateway.calls.map((call) => call.type), ['decide_run', 'get_run']);
await tester.pumpWidget(const SizedBox());
resync.complete({'run_detail': _decided()});
await tester.pump();
expect(tester.takeException(), isNull);
expect(changed, isEmpty);
});
testWidgets('confirm cannot submit twice while the decision is in flight', (
tester,
) async {
final reply = Completer<JsonMap>();
final gateway = _FakeGateway((type, fields) => reply.future);
final changed = <JsonMap>[];
await _pump(tester, gateway, changed);
await _chooseGo(tester);
final confirm = find.text(decisionConfirmLabel);
await tester.tap(confirm);
await tester.tap(confirm);
await tester.pump();
expect(
gateway.calls.where((call) => call.type == 'decide_run'),
hasLength(1),
);
expect(find.text(decisionSendingLabel), findsOneWidget);
expect(
tester
.widget<TextButton>(
find.widgetWithText(TextButton, decisionCancelLabel),
)
.onPressed,
isNull,
);
reply.complete({'run_detail': _decided()});
await tester.pumpAndSettle();
expect(find.byType(AlertDialog), findsNothing);
expect(changed, hasLength(1));
});
testWidgets('a late reply after disposal is ignored safely', (tester) async {
final reply = Completer<JsonMap>();
final gateway = _FakeGateway((type, fields) => reply.future);
final changed = <JsonMap>[];
await _pump(tester, gateway, changed);
await _chooseGo(tester);
await tester.tap(find.text(decisionConfirmLabel));
await tester.pump();
expect(gateway.calls, hasLength(1));
await tester.pumpWidget(const SizedBox());
reply.complete({'run_detail': _decided()});
await tester.pump();
expect(tester.takeException(), isNull);
expect(changed, isEmpty);
});
testWidgets('both prompts fit 2x text at a narrow width', (tester) async {
tester.view.physicalSize = const Size(320, 480);
tester.view.devicePixelRatio = 1;
addTearDown(tester.view.resetPhysicalSize);
addTearDown(tester.view.resetDevicePixelRatio);
for (final cupertino in [false, true]) {
final gateway = _FakeGateway(
(type, fields) async => {'run_detail': _decided()},
);
await _pump(
tester,
gateway,
<JsonMap>[],
cupertino: cupertino,
textScale: 2,
);
await tester.tap(find.byKey(decisionActionKey));
await tester.pumpAndSettle();
expect(tester.takeException(), isNull, reason: 'cupertino=$cupertino');
expect(find.text(decisionWaitingTitle), findsOneWidget);
expect(find.text('Ship it?'), findsOneWidget);
expect(
cupertino
? find.byType(CupertinoAlertDialog)
: find.byType(AlertDialog),
findsOneWidget,
);
}
});
}