feat(app): add deliberate human-decision action for run detail

Adds HumanDecisionAction plus its native prompt, wording constants and
tests. The action renders only while pendingDecision() reports a genuine
human wait, sends decide_run only after an option is chosen and Confirm
pressed, sends the pending stage/trimmed option/optional feedback, blocks
double submits, shows server refusals verbatim with the draft intact, and
re-reads the run so the owner always receives server truth.
This commit is contained in:
2026-10-21 14:22:29 +01:00
parent a2e7c43edb
commit 42b21b86cd
6 changed files with 974 additions and 0 deletions
@@ -0,0 +1,237 @@
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('the action is absent unless a run is genuinely waiting', (
tester,
) async {
final gateway = _FakeGateway(
(type, fields) async => {'run_detail': _waitingRun()},
);
final changed = <JsonMap>[];
for (final (label, run) in [
('not waiting', _waitingRun(status: 'running')),
('export pending', _waitingRun(exportPending: 2)),
('model stage', _waitingRun(type: 'model')),
]) {
await _pump(tester, gateway, run, cupertino: false, changed: changed);
expect(
find.byKey(decisionActionKey),
findsNothing,
reason: '$label must not offer a decision',
);
}
expect(gateway.calls, isEmpty);
expect(changed, isEmpty);
});
}
@@ -0,0 +1,240 @@
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('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,
);
}
});
}