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

266 lines
7.4 KiB
Dart

import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:flutter_test/flutter_test.dart';
import 'package:pipelines_app/core/api_gateway.dart';
import 'package:pipelines_app/features/runs/actions/decision_action.dart';
import 'package:pipelines_app/features/runs/actions/decision_wording.dart';
typedef _Handler = Future<JsonMap> Function(String type, JsonMap fields);
class _FakeGateway implements ApiGateway {
_FakeGateway(this.handler);
final _Handler handler;
final List<({String type, JsonMap fields})> calls = [];
@override
Future<JsonMap> call(String type, [JsonMap fields = const {}]) {
final copy = Map<String, dynamic>.from(fields);
calls.add((type: type, fields: copy));
return handler(type, copy);
}
@override
Stream<JsonMap> get frames => const Stream.empty();
}
JsonMap _waitingRun({
String status = 'waiting',
int exportPending = 0,
String type = 'human',
List<String> options = const ['go', 'skip'],
}) => {
'id': 7,
'title': 'Report',
'name': 'Build',
'status': status,
'current_stage': 'human',
'export_pending_sequence': exportPending,
'graph': {
'stages': [
{
'key': 'human',
'type': type,
'prompt': 'Graph prompt',
'edges': {'go': '#', 'skip': 'finish'},
},
],
},
'steps': [
{
'sequence': 1,
'status': 'waiting',
'stage': 'human',
'output': {'prompt': 'Ship it?', 'options': options},
},
],
};
JsonMap _decided() => {
..._waitingRun(),
'status': 'succeeded',
'steps': const <JsonMap>[],
};
Widget _app(Widget child, {required bool cupertino, double textScale = 1}) {
if (cupertino) {
return CupertinoApp(
builder: (context, app) => MediaQuery(
data: MediaQuery.of(
context,
).copyWith(textScaler: TextScaler.linear(textScale)),
child: app!,
),
home: CupertinoPageScaffold(child: Center(child: child)),
);
}
return MaterialApp(
builder: (context, app) => MediaQuery(
data: MediaQuery.of(
context,
).copyWith(textScaler: TextScaler.linear(textScale)),
child: app!,
),
home: Scaffold(body: Center(child: child)),
);
}
Future<void> _pump(
WidgetTester tester,
_FakeGateway gateway,
JsonMap run, {
required bool cupertino,
required List<JsonMap> changed,
double textScale = 1,
}) async {
await tester.pumpWidget(
_app(
HumanDecisionAction(
run: run,
gateway: gateway,
onRunChanged: changed.add,
),
cupertino: cupertino,
textScale: textScale,
),
);
}
Future<void> _open(WidgetTester tester) async {
await tester.tap(find.byKey(decisionActionKey));
await tester.pumpAndSettle();
}
void main() {
testWidgets(
'material decision waits for option and confirm, then sends exact fields',
(tester) async {
final gateway = _FakeGateway(
(type, fields) async => {
'run_detail': type == 'decide_run' ? _decided() : _waitingRun(),
},
);
final changed = <JsonMap>[];
await _pump(
tester,
gateway,
_waitingRun(),
cupertino: false,
changed: changed,
);
expect(find.byKey(decisionActionKey), findsOneWidget);
expect(find.byType(FilledButton), findsOneWidget);
expect(gateway.calls, isEmpty);
await _open(tester);
expect(find.byType(AlertDialog), findsOneWidget);
expect(find.text(decisionWaitingTitle), findsOneWidget);
expect(find.text('Ship it?'), findsOneWidget);
expect(find.text('→ SUCCESS'), findsOneWidget);
expect(gateway.calls, isEmpty);
final confirm = find.widgetWithText(FilledButton, decisionConfirmLabel);
expect(tester.widget<FilledButton>(confirm).onPressed, isNull);
await tester.tap(find.text('go'));
await tester.pump();
expect(gateway.calls, isEmpty);
expect(tester.widget<FilledButton>(confirm).onPressed, isNotNull);
await tester.enterText(find.byKey(decisionFeedbackKey), ' looks good ');
await tester.tap(find.text(decisionConfirmLabel));
await tester.pumpAndSettle();
expect(gateway.calls.map((call) => call.type), ['decide_run']);
expect(gateway.calls.single.fields, {
'run_id': 7,
'stage': 'human',
'option': 'go',
'feedback': 'looks good',
});
expect(changed.single['status'], 'succeeded');
expect(find.byType(AlertDialog), findsNothing);
expect(find.byKey(decisionActionKey), findsNothing);
},
);
testWidgets(
'cupertino decision uses a native dialog and omits empty feedback',
(tester) async {
final gateway = _FakeGateway(
(type, fields) async => {
'run_detail': type == 'decide_run' ? _decided() : _waitingRun(),
},
);
final changed = <JsonMap>[];
await _pump(
tester,
gateway,
_waitingRun(),
cupertino: true,
changed: changed,
);
expect(find.byKey(decisionActionKey), findsOneWidget);
expect(find.byType(FilledButton), findsNothing);
await _open(tester);
expect(find.byType(CupertinoAlertDialog), findsOneWidget);
expect(find.text(decisionWaitingTitle), findsOneWidget);
expect(gateway.calls, isEmpty);
final confirm = find.widgetWithText(
CupertinoDialogAction,
decisionConfirmLabel,
);
expect(tester.widget<CupertinoDialogAction>(confirm).onPressed, isNull);
await tester.tap(find.text('skip'));
await tester.pump();
expect(gateway.calls, isEmpty);
await tester.tap(find.text(decisionConfirmLabel));
await tester.pumpAndSettle();
expect(gateway.calls.map((call) => call.type), ['decide_run']);
expect(gateway.calls.single.fields, {
'run_id': 7,
'stage': 'human',
'option': 'skip',
});
expect(changed.single['status'], 'succeeded');
expect(find.byType(CupertinoAlertDialog), findsNothing);
},
);
testWidgets('Cancel dismisses the draft without sending anything', (
tester,
) async {
final gateway = _FakeGateway(
(type, fields) async => {'run_detail': _decided()},
);
final changed = <JsonMap>[];
await _pump(
tester,
gateway,
_waitingRun(),
cupertino: false,
changed: changed,
);
await _open(tester);
await tester.tap(find.text('go'));
await tester.pump();
await tester.enterText(find.byKey(decisionFeedbackKey), 'not sent');
await tester.tap(find.widgetWithText(TextButton, decisionCancelLabel));
await tester.pumpAndSettle();
expect(find.byType(AlertDialog), findsNothing);
expect(gateway.calls, isEmpty);
expect(changed, isEmpty);
expect(find.byKey(decisionActionKey), findsOneWidget);
});
testWidgets('the action is absent unless a run is genuinely waiting', (
tester,
) async {
final gateway = _FakeGateway(
(type, fields) async => {'run_detail': _waitingRun()},
);
final changed = <JsonMap>[];
for (final (label, run) in [
('not waiting', _waitingRun(status: 'running')),
('export pending', _waitingRun(exportPending: 2)),
('model stage', _waitingRun(type: 'model')),
]) {
await _pump(tester, gateway, run, cupertino: false, changed: changed);
expect(
find.byKey(decisionActionKey),
findsNothing,
reason: '$label must not offer a decision',
);
}
expect(gateway.calls, isEmpty);
expect(changed, isEmpty);
});
}