merge(app): land native cancel action and responsive refusal handling
This commit is contained in:
@@ -0,0 +1,18 @@
|
||||
/// A refused operator action and, when one was started, the bounded re-read of
|
||||
/// server truth that follows it.
|
||||
///
|
||||
/// The prompt shows [message] and allows dismissal immediately; it must never
|
||||
/// wait for [stillPossible]. That future is only observed so a retry can be
|
||||
/// ruled out once server truth arrives: `true` means the re-read found the run
|
||||
/// still allows the action, `false` means it plainly does not, and `null` means
|
||||
/// the re-read failed or was overtaken, leaving a deliberate retry possible.
|
||||
class ActionRefusal {
|
||||
const ActionRefusal(this.message, {this.stillPossible});
|
||||
|
||||
final String message;
|
||||
final Future<bool?>? stillPossible;
|
||||
}
|
||||
|
||||
/// Local guards, distinct from a server refusal; they never claim success.
|
||||
const actionOptionRequired = 'Choose an option before continuing.';
|
||||
const actionAlreadySending = 'That action is still being sent.';
|
||||
@@ -0,0 +1,165 @@
|
||||
import 'package:flutter/cupertino.dart';
|
||||
import 'package:flutter/material.dart';
|
||||
|
||||
import 'action_refusal.dart';
|
||||
import 'cancel_wording.dart';
|
||||
|
||||
/// Sends the confirmed cancellation. Returns null once the server has accepted
|
||||
/// it, or an [ActionRefusal] carrying the server's words verbatim; the prompt
|
||||
/// then stays open with dismissal available immediately.
|
||||
typedef CancelSubmit = Future<ActionRefusal?> Function();
|
||||
|
||||
/// Opens the native cancel confirmation for [runId].
|
||||
///
|
||||
/// Nothing is sent until the operator presses the confirm action, and the
|
||||
/// dismiss action (`Keep`) only closes the prompt.
|
||||
Future<void> showCancelRunPrompt({
|
||||
required BuildContext context,
|
||||
required num runId,
|
||||
required bool cupertino,
|
||||
required CancelSubmit onSubmit,
|
||||
}) {
|
||||
final prompt = _CancelPrompt(
|
||||
runId: runId,
|
||||
cupertino: cupertino,
|
||||
onSubmit: onSubmit,
|
||||
);
|
||||
if (cupertino) {
|
||||
return showCupertinoDialog<void>(
|
||||
context: context,
|
||||
barrierDismissible: false,
|
||||
builder: (_) => prompt,
|
||||
);
|
||||
}
|
||||
return showDialog<void>(
|
||||
context: context,
|
||||
barrierDismissible: false,
|
||||
builder: (_) => prompt,
|
||||
);
|
||||
}
|
||||
|
||||
class _CancelPrompt extends StatefulWidget {
|
||||
const _CancelPrompt({
|
||||
required this.runId,
|
||||
required this.cupertino,
|
||||
required this.onSubmit,
|
||||
});
|
||||
|
||||
final num runId;
|
||||
final bool cupertino;
|
||||
final CancelSubmit onSubmit;
|
||||
|
||||
@override
|
||||
State<_CancelPrompt> createState() => _CancelPromptState();
|
||||
}
|
||||
|
||||
class _CancelPromptState extends State<_CancelPrompt> {
|
||||
bool _sending = false;
|
||||
bool _checking = false;
|
||||
bool _blocked = false;
|
||||
String? _error;
|
||||
|
||||
void _keep() {
|
||||
if (!_sending) Navigator.of(context).pop();
|
||||
}
|
||||
|
||||
Future<void> _confirm() async {
|
||||
if (_sending || _checking || _blocked) return;
|
||||
setState(() {
|
||||
_sending = true;
|
||||
_error = null;
|
||||
});
|
||||
final refusal = await widget.onSubmit();
|
||||
if (!mounted) return;
|
||||
if (refusal == null) {
|
||||
Navigator.of(context).pop();
|
||||
return;
|
||||
}
|
||||
final check = refusal.stillPossible;
|
||||
setState(() {
|
||||
_sending = false;
|
||||
_error = refusal.message;
|
||||
_checking = check != null;
|
||||
});
|
||||
if (check == null) return;
|
||||
bool? stillPossible;
|
||||
try {
|
||||
stillPossible = await check;
|
||||
} catch (_) {
|
||||
// The action reports re-read failures as null; never fabricate one here.
|
||||
}
|
||||
if (!mounted) return;
|
||||
setState(() {
|
||||
_checking = false;
|
||||
_blocked = stillPossible == false;
|
||||
});
|
||||
}
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
final dialog = widget.cupertino ? _cupertino(context) : _material(context);
|
||||
return PopScope(canPop: !_sending, child: dialog);
|
||||
}
|
||||
|
||||
bool get _confirmDisabled => _sending || _checking || _blocked;
|
||||
|
||||
Widget _material(BuildContext context) {
|
||||
final theme = Theme.of(context);
|
||||
return AlertDialog(
|
||||
scrollable: true,
|
||||
title: Text(cancelDialogTitle(widget.runId)),
|
||||
content: _body(errorStyle: TextStyle(color: theme.colorScheme.error)),
|
||||
actions: [
|
||||
TextButton(
|
||||
key: cancelKeepKey,
|
||||
onPressed: _sending ? null : _keep,
|
||||
child: const Text(cancelKeepLabel),
|
||||
),
|
||||
FilledButton(
|
||||
key: cancelConfirmKey,
|
||||
onPressed: _confirmDisabled ? null : _confirm,
|
||||
child: const Text(cancelConfirmLabel),
|
||||
),
|
||||
],
|
||||
);
|
||||
}
|
||||
|
||||
Widget _cupertino(BuildContext context) {
|
||||
return CupertinoAlertDialog(
|
||||
title: Text(cancelDialogTitle(widget.runId)),
|
||||
content: _body(
|
||||
errorStyle: TextStyle(
|
||||
color: CupertinoColors.systemRed.resolveFrom(context),
|
||||
),
|
||||
),
|
||||
actions: [
|
||||
CupertinoDialogAction(
|
||||
key: cancelKeepKey,
|
||||
onPressed: _sending ? null : _keep,
|
||||
child: const Text(cancelKeepLabel),
|
||||
),
|
||||
CupertinoDialogAction(
|
||||
key: cancelConfirmKey,
|
||||
isDestructiveAction: true,
|
||||
onPressed: _confirmDisabled ? null : _confirm,
|
||||
child: const Text(cancelConfirmLabel),
|
||||
),
|
||||
],
|
||||
);
|
||||
}
|
||||
|
||||
Widget _body({required TextStyle errorStyle}) {
|
||||
return Column(
|
||||
mainAxisSize: MainAxisSize.min,
|
||||
crossAxisAlignment: CrossAxisAlignment.stretch,
|
||||
children: [
|
||||
const Text(cancelDialogMessage),
|
||||
if (_error case final error?)
|
||||
Padding(
|
||||
padding: const EdgeInsets.only(top: 8),
|
||||
child: Text(error, key: cancelErrorKey, style: errorStyle),
|
||||
),
|
||||
],
|
||||
);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,31 @@
|
||||
import '../../../core/api_gateway.dart';
|
||||
|
||||
/// Whether the operator may cancel [run], matching `canCancel` in
|
||||
/// `web/src/actions.js` exactly:
|
||||
///
|
||||
/// `(executing || (status == "waiting" && export_pending_sequence > 0)) &&
|
||||
/// !cancel_requested && !cleanup_unconfirmed`
|
||||
///
|
||||
/// The status word alone is not enough: a stale `running` status without
|
||||
/// `executing` is not cancellable, while a run waiting on a pending session
|
||||
/// export is.
|
||||
bool canCancelRun(JsonMap? run) {
|
||||
if (run == null) return false;
|
||||
final exportPending = _number(run['export_pending_sequence']);
|
||||
return (run['executing'] == true ||
|
||||
(run['status'] == 'waiting' && exportPending > 0)) &&
|
||||
run['cancel_requested'] != true &&
|
||||
run['cleanup_unconfirmed'] != true;
|
||||
}
|
||||
|
||||
/// The run id used by the cancel request, or null for a malformed run.
|
||||
num? cancelRunId(JsonMap run) {
|
||||
final id = run['id'];
|
||||
return id is num ? id : null;
|
||||
}
|
||||
|
||||
/// `Number(value) > 0`, tolerating numeric strings as the browser does.
|
||||
double _number(dynamic value) {
|
||||
if (value is num) return value.toDouble();
|
||||
return double.tryParse('${value ?? ''}'.trim()) ?? 0;
|
||||
}
|
||||
@@ -0,0 +1,119 @@
|
||||
import 'package:flutter/cupertino.dart';
|
||||
import 'package:flutter/material.dart';
|
||||
|
||||
import '../../../core/api_gateway.dart';
|
||||
import '../data/runs_repository.dart';
|
||||
import 'action_refusal.dart';
|
||||
import 'cancel_dialog.dart';
|
||||
import 'cancel_eligibility.dart';
|
||||
import 'cancel_wording.dart';
|
||||
|
||||
/// The run detail's deliberate cancel entry point.
|
||||
///
|
||||
/// Renders one native button only while [run] is cancellable under the browser
|
||||
/// client's rule. Opening the prompt or choosing [cancelKeepLabel] sends
|
||||
/// nothing; only the confirm action asks the server to stop the run. A refusal
|
||||
/// is returned to the kept-open prompt immediately and the run is re-read
|
||||
/// separately, so [onRunChanged] still receives server truth without locking
|
||||
/// the prompt while that read is pending.
|
||||
class CancelRunAction extends StatefulWidget {
|
||||
const CancelRunAction({
|
||||
super.key,
|
||||
required this.run,
|
||||
required this.gateway,
|
||||
required this.onRunChanged,
|
||||
});
|
||||
|
||||
final JsonMap run;
|
||||
final ApiGateway gateway;
|
||||
final ValueChanged<JsonMap> onRunChanged;
|
||||
|
||||
@override
|
||||
State<CancelRunAction> createState() => _CancelRunActionState();
|
||||
}
|
||||
|
||||
class _CancelRunActionState extends State<CancelRunAction> {
|
||||
bool _dialogOpen = false;
|
||||
Future<bool?>? _resync;
|
||||
|
||||
bool _usesCupertino(BuildContext context) {
|
||||
final material = Localizations.of<MaterialLocalizations>(
|
||||
context,
|
||||
MaterialLocalizations,
|
||||
);
|
||||
return material == null;
|
||||
}
|
||||
|
||||
Future<void> _open() async {
|
||||
final id = cancelRunId(widget.run);
|
||||
if (id == null || !canCancelRun(widget.run) || _dialogOpen) return;
|
||||
_dialogOpen = true;
|
||||
try {
|
||||
await showCancelRunPrompt(
|
||||
context: context,
|
||||
runId: id,
|
||||
cupertino: _usesCupertino(context),
|
||||
onSubmit: () => _submit(id),
|
||||
);
|
||||
} finally {
|
||||
_dialogOpen = false;
|
||||
if (mounted) setState(() {});
|
||||
}
|
||||
}
|
||||
|
||||
Future<ActionRefusal?> _submit(num id) async {
|
||||
final repository = RunsRepository(widget.gateway);
|
||||
try {
|
||||
final run = await repository.cancelRun(id);
|
||||
if (!mounted) return null;
|
||||
if (_owns(id)) widget.onRunChanged(run);
|
||||
return null;
|
||||
} catch (error) {
|
||||
return ActionRefusal(
|
||||
describeCancelError(error),
|
||||
stillPossible: _resyncRun(id),
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
bool _owns(num id) => mounted && cancelRunId(widget.run) == id;
|
||||
|
||||
/// One bounded re-read per refusal. A refusal that arrives while a read is
|
||||
/// already in flight observes that same read instead of starting another.
|
||||
Future<bool?> _resyncRun(num id) {
|
||||
return _resync ??= _readRun(id);
|
||||
}
|
||||
|
||||
Future<bool?> _readRun(num id) async {
|
||||
try {
|
||||
final run = await RunsRepository(widget.gateway).getRun(id);
|
||||
if (!_owns(id)) return null;
|
||||
widget.onRunChanged(run);
|
||||
return canCancelRun(run);
|
||||
} catch (_) {
|
||||
// Unknown server truth: keep the refusal and leave a deliberate retry.
|
||||
return null;
|
||||
} finally {
|
||||
_resync = null;
|
||||
}
|
||||
}
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
if (cancelRunId(widget.run) == null || !canCancelRun(widget.run)) {
|
||||
return const SizedBox.shrink();
|
||||
}
|
||||
if (_usesCupertino(context)) {
|
||||
return CupertinoButton.filled(
|
||||
key: cancelActionKey,
|
||||
onPressed: _open,
|
||||
child: const Text(cancelActionLabel),
|
||||
);
|
||||
}
|
||||
return FilledButton(
|
||||
key: cancelActionKey,
|
||||
onPressed: _open,
|
||||
child: const Text(cancelActionLabel),
|
||||
);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,23 @@
|
||||
import 'package:flutter/widgets.dart';
|
||||
|
||||
/// The browser client's cancel wording, kept verbatim.
|
||||
const cancelActionLabel = 'Cancel run';
|
||||
const cancelDialogMessage = 'This asks the server to stop the active run.';
|
||||
const cancelConfirmLabel = 'Cancel run';
|
||||
const cancelKeepLabel = 'Keep';
|
||||
|
||||
/// Stable test/semantics keys shared by the action and its prompt.
|
||||
const cancelActionKey = ValueKey<String>('cancel-run-action');
|
||||
const cancelKeepKey = ValueKey<String>('cancel-run-keep');
|
||||
const cancelConfirmKey = ValueKey<String>('cancel-run-confirm');
|
||||
const cancelErrorKey = ValueKey<String>('cancel-run-error');
|
||||
|
||||
/// The cancel dialog's title: `Cancel run {id}?`.
|
||||
String cancelDialogTitle(num runId) => 'Cancel run $runId?';
|
||||
|
||||
/// The server's refusal, verbatim. Only a blank error gets a generic sentence
|
||||
/// so the prompt never claims a cancellation that did not happen.
|
||||
String describeCancelError(Object error) {
|
||||
final message = '$error'.trim();
|
||||
return message.isEmpty ? 'The server did not cancel the run.' : message;
|
||||
}
|
||||
@@ -5,6 +5,7 @@ import '../../../core/api_gateway.dart';
|
||||
import '../data/runs_repository.dart';
|
||||
import '../domain/run_decisions.dart';
|
||||
import '../domain/run_models.dart';
|
||||
import 'action_refusal.dart';
|
||||
import 'decision_dialog.dart';
|
||||
import 'decision_wording.dart';
|
||||
|
||||
@@ -13,8 +14,9 @@ import 'decision_wording.dart';
|
||||
/// Renders one native button only while [run] is genuinely waiting on a person.
|
||||
/// Nothing is sent until the operator opens the prompt, chooses an option and
|
||||
/// presses Confirm; cancel, selection and optional feedback stay local. A
|
||||
/// server refusal is shown verbatim in the prompt, the draft is preserved and
|
||||
/// the run is re-read so [onRunChanged] always receives server truth.
|
||||
/// server refusal is shown verbatim in the prompt immediately, the draft is
|
||||
/// preserved and the run is re-read separately so [onRunChanged] still receives
|
||||
/// server truth without locking the prompt while that read is pending.
|
||||
class HumanDecisionAction extends StatefulWidget {
|
||||
const HumanDecisionAction({
|
||||
super.key,
|
||||
@@ -39,6 +41,7 @@ class _HumanDecisionActionState extends State<HumanDecisionAction> {
|
||||
PendingDecision? _pending;
|
||||
bool _dialogOpen = false;
|
||||
bool _sending = false;
|
||||
Future<bool?>? _resync;
|
||||
|
||||
@override
|
||||
void initState() {
|
||||
@@ -78,9 +81,13 @@ class _HumanDecisionActionState extends State<HumanDecisionAction> {
|
||||
}
|
||||
}
|
||||
|
||||
Future<String?> _submit(PendingDecision decision, DecisionDraft draft) async {
|
||||
Future<ActionRefusal?> _submit(
|
||||
PendingDecision decision,
|
||||
DecisionDraft draft,
|
||||
) async {
|
||||
final option = draft.selected?.trim() ?? '';
|
||||
if (option.isEmpty || _sending) return null;
|
||||
if (option.isEmpty) return const ActionRefusal(actionOptionRequired);
|
||||
if (_sending) return const ActionRefusal(actionAlreadySending);
|
||||
_sending = true;
|
||||
final repository = RunsRepository(widget.gateway);
|
||||
try {
|
||||
@@ -93,29 +100,57 @@ class _HumanDecisionActionState extends State<HumanDecisionAction> {
|
||||
feedback: draft.feedback,
|
||||
);
|
||||
} catch (error) {
|
||||
final refusal = _describe(error);
|
||||
if (!mounted) return refusal;
|
||||
JsonMap? refreshed;
|
||||
try {
|
||||
refreshed = await repository.getRun(decision.runId);
|
||||
} catch (_) {
|
||||
// A failed re-read must not hide or replace the server's refusal.
|
||||
}
|
||||
if (refreshed != null && mounted) {
|
||||
setState(() => _pending = pendingDecision(refreshed));
|
||||
widget.onRunChanged(refreshed);
|
||||
}
|
||||
return refusal;
|
||||
return ActionRefusal(
|
||||
_describe(error),
|
||||
stillPossible: _resyncRun(decision),
|
||||
);
|
||||
}
|
||||
if (!mounted) return null;
|
||||
setState(() => _pending = pendingDecision(run));
|
||||
widget.onRunChanged(run);
|
||||
if (_owns(decision.runId)) {
|
||||
setState(() => _pending = pendingDecision(run));
|
||||
widget.onRunChanged(run);
|
||||
}
|
||||
return null;
|
||||
} finally {
|
||||
_sending = false;
|
||||
}
|
||||
}
|
||||
|
||||
bool _owns(num id) => mounted && widget.run['id'] == id;
|
||||
|
||||
/// One bounded re-read per refusal. A refusal that arrives while a read is
|
||||
/// already in flight observes that same read instead of starting another.
|
||||
Future<bool?> _resyncRun(PendingDecision decision) {
|
||||
return _resync ??= _readRun(decision);
|
||||
}
|
||||
|
||||
Future<bool?> _readRun(PendingDecision expected) async {
|
||||
try {
|
||||
final run = await RunsRepository(widget.gateway).getRun(expected.runId);
|
||||
if (!_owns(expected.runId)) return null;
|
||||
final next = pendingDecision(run);
|
||||
setState(() => _pending = next);
|
||||
widget.onRunChanged(run);
|
||||
return _stillDecidable(next, expected);
|
||||
} catch (_) {
|
||||
// Unknown server truth: keep the refusal and leave a deliberate retry.
|
||||
return null;
|
||||
} finally {
|
||||
_resync = null;
|
||||
}
|
||||
}
|
||||
|
||||
/// Whether the refreshed run offers the same decision the prompt is showing.
|
||||
bool _stillDecidable(PendingDecision? next, PendingDecision expected) {
|
||||
if (next == null ||
|
||||
next.runId != expected.runId ||
|
||||
next.stageKey != expected.stageKey) {
|
||||
return false;
|
||||
}
|
||||
final keys = next.options.map((option) => option.key).toSet();
|
||||
return expected.options.every((option) => keys.contains(option.key));
|
||||
}
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
final decision = _pending;
|
||||
|
||||
@@ -3,11 +3,13 @@ import 'package:flutter/material.dart';
|
||||
|
||||
import '../domain/run_decisions.dart';
|
||||
import '../domain/run_models.dart';
|
||||
import 'action_refusal.dart';
|
||||
import 'decision_wording.dart';
|
||||
|
||||
/// Sends the draft to the server. Returns null on success, or the server's
|
||||
/// refusal message verbatim; the prompt stays open with the draft intact.
|
||||
typedef DecisionSubmit = Future<String?> Function(DecisionDraft draft);
|
||||
/// Sends the draft to the server. Returns null on success, or an
|
||||
/// [ActionRefusal] with the server's words verbatim; the prompt stays open
|
||||
/// with the draft intact and dismissal available immediately.
|
||||
typedef DecisionSubmit = Future<ActionRefusal?> Function(DecisionDraft draft);
|
||||
|
||||
/// Opens the native decision prompt for [decision].
|
||||
Future<void> showHumanDecisionPrompt({
|
||||
@@ -54,6 +56,8 @@ class _DecisionPromptState extends State<_DecisionPrompt> {
|
||||
DecisionDraft _draft = const DecisionDraft();
|
||||
late final TextEditingController _feedback = TextEditingController();
|
||||
bool _sending = false;
|
||||
bool _checking = false;
|
||||
bool _blocked = false;
|
||||
String? _error;
|
||||
|
||||
@override
|
||||
@@ -74,7 +78,7 @@ class _DecisionPromptState extends State<_DecisionPrompt> {
|
||||
}
|
||||
|
||||
Future<void> _confirm() async {
|
||||
if (_sending || !_draft.canSubmit) return;
|
||||
if (_sending || _checking || _blocked || !_draft.canSubmit) return;
|
||||
setState(() {
|
||||
_sending = true;
|
||||
_error = null;
|
||||
@@ -85,9 +89,23 @@ class _DecisionPromptState extends State<_DecisionPrompt> {
|
||||
Navigator.of(context).pop();
|
||||
return;
|
||||
}
|
||||
final check = refusal.stillPossible;
|
||||
setState(() {
|
||||
_sending = false;
|
||||
_error = refusal;
|
||||
_error = refusal.message;
|
||||
_checking = check != null;
|
||||
});
|
||||
if (check == null) return;
|
||||
bool? stillPossible;
|
||||
try {
|
||||
stillPossible = await check;
|
||||
} catch (_) {
|
||||
// The action reports re-read failures as null; never fabricate one here.
|
||||
}
|
||||
if (!mounted) return;
|
||||
setState(() {
|
||||
_checking = false;
|
||||
_blocked = stillPossible == false;
|
||||
});
|
||||
}
|
||||
|
||||
@@ -97,6 +115,8 @@ class _DecisionPromptState extends State<_DecisionPrompt> {
|
||||
return PopScope(canPop: !_sending, child: dialog);
|
||||
}
|
||||
|
||||
bool get _confirmDisabled => _sending || _checking || _blocked;
|
||||
|
||||
Widget _material(BuildContext context) {
|
||||
final theme = Theme.of(context);
|
||||
return AlertDialog(
|
||||
@@ -138,7 +158,7 @@ class _DecisionPromptState extends State<_DecisionPrompt> {
|
||||
child: const Text(decisionCancelLabel),
|
||||
),
|
||||
FilledButton(
|
||||
onPressed: _sending || !_draft.canSubmit ? null : _confirm,
|
||||
onPressed: _confirmDisabled || !_draft.canSubmit ? null : _confirm,
|
||||
child: Text(_sending ? decisionSendingLabel : decisionConfirmLabel),
|
||||
),
|
||||
],
|
||||
@@ -203,7 +223,7 @@ class _DecisionPromptState extends State<_DecisionPrompt> {
|
||||
),
|
||||
CupertinoDialogAction(
|
||||
isDefaultAction: true,
|
||||
onPressed: _sending || !_draft.canSubmit ? null : _confirm,
|
||||
onPressed: _confirmDisabled || !_draft.canSubmit ? null : _confirm,
|
||||
child: Text(_sending ? decisionSendingLabel : decisionConfirmLabel),
|
||||
),
|
||||
],
|
||||
|
||||
@@ -0,0 +1,92 @@
|
||||
# R2b-refusal — immediate refusals, separate resync
|
||||
|
||||
Focused correction on the retained `faa5866` cancellation candidate. The action
|
||||
source is now shared by both deliberate run actions (cancel and human decision);
|
||||
no wiring, screen, controller, repository, socket, pubspec, web, server or
|
||||
device file was touched.
|
||||
|
||||
## What changed
|
||||
|
||||
| File | Lines | Responsibility |
|
||||
| --- | --- | --- |
|
||||
| `lib/features/runs/actions/action_refusal.dart` | 18 | Refusal message plus optional bounded re-read outcome; local guard words |
|
||||
| `lib/features/runs/actions/cancel_run_action.dart` | 119 | Immediate refusal return, single-flight resync, ownership checks |
|
||||
| `lib/features/runs/actions/cancel_dialog.dart` | 165 | Shows the refusal and re-enables Keep before the resync settles |
|
||||
| `lib/features/runs/actions/decision_action.dart` | 176 | Same async flow for `decide_run`, draft preserved |
|
||||
| `lib/features/runs/actions/decision_dialog.dart` | 268 | Shows refusal, keeps draft, disables retry only on server truth |
|
||||
| `test/features/runs/actions/cancel_resilience_test.dart` | 271 | Three new fake-gateway regressions |
|
||||
| `test/features/runs/actions/decision_resilience_test.dart` | 351 | Three new fake-gateway regressions |
|
||||
| `test/features/runs/actions/decision_action_test.dart` | 265 | Cancel/no-request coverage |
|
||||
|
||||
## Async flow
|
||||
|
||||
A refused mutation no longer awaits `get_run` before returning:
|
||||
|
||||
1. `_submit` catches the mutation error and computes the refusal text.
|
||||
2. It returns `ActionRefusal(message, stillPossible: _resyncRun(id))` at once.
|
||||
`_resyncRun` starts one `get_run` for that refusal; a concurrent refusal
|
||||
observes the same future instead of issuing another request.
|
||||
3. The prompt clears its sending state, shows the server's words and makes
|
||||
Keep/Cancel available in the same frame. Back is enabled too (`PopScope`
|
||||
only gates the in-flight mutation). Confirm stays disabled while the check
|
||||
is pending and after it proves the action impossible, so a doomed mutation
|
||||
cannot be resubmitted from the open prompt.
|
||||
4. The resync runs detached but bounded: exactly one `get_run`, no retries or
|
||||
polling. Every exception becomes `null` (unknown truth), which leaves the
|
||||
refusal visible and a deliberate retry available.
|
||||
5. Before delivering a refreshed run, the action checks `mounted` and that the
|
||||
run id it owns is still the request's id. A late read after disposal or
|
||||
navigation touches nothing and raises nothing.
|
||||
|
||||
On success the mutation reply is still used directly, the prompt closes, and no
|
||||
duplicate `get_run` is issued. The dialog and action keep the existing
|
||||
in-flight double-submit guard. The human-decision prompt keeps the selected
|
||||
option and feedback controller untouched while a refusal or resync is pending;
|
||||
its retry is disabled only when the refreshed run no longer offers the same
|
||||
decision (same stage and option keys).
|
||||
|
||||
No repository, protocol or socket code changed. The web's `cancel_run` plus
|
||||
silent refresh remains the reference; this version keeps the diagnostic in the
|
||||
open prompt rather than moving it to the web's separate Action-failed dialog.
|
||||
|
||||
## Regression coverage added
|
||||
|
||||
Fake gateway, no socket or real server:
|
||||
|
||||
- immediate `cancel_run` refusal plus a deliberately unresolved `get_run`: the
|
||||
exact message is visible, Keep is enabled, confirm is disabled and no run
|
||||
callback has fired **before** the resync completes; Keep closes the prompt;
|
||||
a late resync still delivers server truth safely;
|
||||
- late cancel resync after the whole action is disposed: no exception, no
|
||||
callback;
|
||||
- resync proving the run is no longer cancellable: refusal stays visible,
|
||||
confirm cannot submit again (an attempted tap adds no request), Keep
|
||||
dismisses and the ineligible run is delivered;
|
||||
- immediate `decide_run` refusal plus an unresolved `get_run`: exact message,
|
||||
selection and feedback retained, Cancel enabled, no callback yet; after the
|
||||
resync proves the decision gone, the message remains and confirm is disabled;
|
||||
- decision resync that fails: message and draft remain, Confirm is re-enabled
|
||||
because server truth is unknown;
|
||||
- late decision resync after disposal: no exception, no callback;
|
||||
- Cancel on an untouched decision draft sends nothing and keeps the action
|
||||
available.
|
||||
|
||||
## Checks actually run
|
||||
|
||||
```
|
||||
dart format lib/features/runs/actions test/features/runs/actions # clean
|
||||
flutter analyze # No issues found!
|
||||
flutter test test/features/runs/actions # 23 passed
|
||||
flutter test # 52 passed
|
||||
flutter build web # Built build/web
|
||||
```
|
||||
|
||||
## Honest limits
|
||||
|
||||
- Still **not wired into a run-detail screen** on this branch; there is no
|
||||
browser interaction or rendered capture for this widget. `flutter build web`
|
||||
proves compilation only.
|
||||
- Mobile remains unverified: no Android/iOS build, emulator, ADB or device.
|
||||
- No real server or run was mutated; all evidence uses local fakes.
|
||||
- No commit, push, deployment or canonical-URL change is claimed here beyond
|
||||
the candidate commit checked by the manager.
|
||||
@@ -0,0 +1,96 @@
|
||||
# R2b — native run cancellation
|
||||
|
||||
## What changed
|
||||
|
||||
New `lib/features/runs/actions/cancel*` slice plus its tests. No existing source
|
||||
file was edited.
|
||||
|
||||
| File | Lines | Responsibility |
|
||||
| --- | --- | --- |
|
||||
| `lib/features/runs/actions/cancel_eligibility.dart` | 31 | Web-exact `canCancelRun`, run-id extraction, numeric export counter |
|
||||
| `lib/features/runs/actions/cancel_wording.dart` | 23 | Web-verbatim words, dialog title, error text, test keys |
|
||||
| `lib/features/runs/actions/cancel_dialog.dart` | 145 | Native Material/Cupertino confirmation and inline refusal |
|
||||
| `lib/features/runs/actions/cancel_run_action.dart` | 101 | `CancelRunAction`, gating, send/refusal/resync orchestration |
|
||||
| `test/features/runs/actions/cancel_test_support.dart` | 101 | Fake gateway, run fixtures, stateful host, app wrappers |
|
||||
| `test/features/runs/actions/cancel_action_test.dart` | 192 | Eligibility and both-platform deliberate-confirm flows |
|
||||
| `test/features/runs/actions/cancel_resilience_test.dart` | 176 | Refusal/resync, resync failure, double submit, disposal, 2x/narrow |
|
||||
|
||||
`CancelRunAction` takes the run, the shared `ApiGateway` and an
|
||||
`onRunChanged` callback. It renders one native `FilledButton` (`Cancel run`) on
|
||||
Material and one `CupertinoButton.filled` on Cupertino, and nothing at all when
|
||||
the browser rule says the run is not cancellable. Platform style follows the
|
||||
enclosing app (`MaterialLocalizations` presence); no extra style parameter or
|
||||
dependency was added.
|
||||
|
||||
## Behaviour
|
||||
|
||||
Eligibility is a direct port of `canCancel` in `../web/src/actions.js`:
|
||||
|
||||
```
|
||||
(executing || (status == "waiting" && export_pending_sequence > 0))
|
||||
&& !cancel_requested && !cleanup_unconfirmed
|
||||
```
|
||||
|
||||
A run with a stale `running` status but `executing == false` is therefore not
|
||||
cancellable, while a `waiting` run with a pending session export is. A run
|
||||
without a numeric `id` is not offered because the request cannot be addressed
|
||||
(the pure eligibility function still matches the web rule exactly).
|
||||
|
||||
Opening the prompt sends nothing and `Keep` only closes it. The prompt uses the
|
||||
web wording (`Cancel run {id}?`, `This asks the server to stop the active run.`,
|
||||
`Cancel run`, `Keep`). Only the confirm action calls
|
||||
`RunsRepository.cancelRun(id)`, which sends the exact envelope
|
||||
`cancel_run {"run_id": id}`.
|
||||
|
||||
Both the dialog and the action guard the in-flight state: a second confirm tap
|
||||
or a `Keep` tap while sending is ignored, and the dialog's actions are disabled
|
||||
until the reply arrives. A successful reply closes the prompt and passes the
|
||||
server's returned `run_detail` to `onRunChanged`; no local status is invented.
|
||||
|
||||
A refusal keeps the prompt open and shows the server's message verbatim. The
|
||||
action then calls `RunsRepository.getRun(id)` and passes the refreshed run to
|
||||
`onRunChanged`, so the owner sees server truth (for example the run finished
|
||||
before cancellation). If the re-read also fails, the original refusal stays
|
||||
visible and no synthetic run is delivered. Every asynchronous resume checks
|
||||
`mounted` before `setState` or `onRunChanged`, so a late reply after disposal is
|
||||
ignored.
|
||||
|
||||
## Checks actually run
|
||||
|
||||
```
|
||||
flutter pub get # dependencies resolved
|
||||
dart format lib/features/runs/actions/cancel*.dart \
|
||||
test/features/runs/actions/cancel*.dart # clean
|
||||
flutter analyze # No issues found!
|
||||
flutter test test/features/runs/actions # 16 passed
|
||||
flutter test # 45 passed
|
||||
flutter build web # Built build/web (15.5s)
|
||||
```
|
||||
|
||||
Focused coverage maps to the requested acceptance:
|
||||
|
||||
- eligibility: executing, export-wait, waiting without export, stale `running`
|
||||
without `executing`, already stopping, cleanup unconfirmed, finished/failed;
|
||||
plus widget visibility for the same states;
|
||||
- both platforms: native prompt opens, nothing is sent on open or `Keep`, exact
|
||||
`cancel_run` envelope, callback receives the returned run, action disappears;
|
||||
- refusal: cancellation error shown verbatim, `get_run` resync delivered to the
|
||||
owner, and a failed resync leaves the refusal visible with no callback;
|
||||
- double submit: two confirm taps produce one `cancel_run`, both actions disable;
|
||||
- stale disposal: a reply after the tree is replaced causes no exception and no
|
||||
callback;
|
||||
- 320x480 at 2x text: both prompts open with no rendering exception.
|
||||
|
||||
## Honest limits
|
||||
|
||||
- **Not wired into a run-detail screen.** This branch has no run-detail screen
|
||||
or controller; the manager owns integration. There is therefore no real
|
||||
browser interaction or capture for this widget. `flutter build web` proves
|
||||
compilation only, not rendered behavior, and no visible feature is claimed.
|
||||
- **Mobile unverified.** No Android/iOS build, emulator, ADB or device was used.
|
||||
Source uses only portable Flutter/Cupertino/Material widgets.
|
||||
- No real server or run was mutated. Tests use a local fake gateway with no
|
||||
socket.
|
||||
- No controller/screen/main/domain/repository/pubspec, `web/`, Go server,
|
||||
installer or deployment file was changed. Nothing was committed, pushed or
|
||||
deployed.
|
||||
@@ -0,0 +1,192 @@
|
||||
import 'package:flutter/cupertino.dart';
|
||||
import 'package:flutter/material.dart';
|
||||
import 'package:flutter_test/flutter_test.dart';
|
||||
import 'package:pipelines_app/core/api_gateway.dart';
|
||||
import 'package:pipelines_app/features/runs/actions/cancel_eligibility.dart';
|
||||
import 'package:pipelines_app/features/runs/actions/cancel_wording.dart';
|
||||
|
||||
import 'cancel_test_support.dart';
|
||||
|
||||
void main() {
|
||||
test('cancel eligibility matches the web canCancel exactly', () {
|
||||
final cases = <(String, JsonMap, bool)>[
|
||||
('executing run', executingRun(), true),
|
||||
(
|
||||
'waiting while a session export is pending',
|
||||
executingRun(executing: false, status: 'waiting', exportPending: 2),
|
||||
true,
|
||||
),
|
||||
(
|
||||
'waiting with no pending export',
|
||||
executingRun(executing: false, status: 'waiting'),
|
||||
false,
|
||||
),
|
||||
(
|
||||
'status says running but the run is not executing',
|
||||
executingRun(executing: false),
|
||||
false,
|
||||
),
|
||||
(
|
||||
'cancellation already requested',
|
||||
executingRun(cancelRequested: true),
|
||||
false,
|
||||
),
|
||||
('cleanup unconfirmed', executingRun(cleanupUnconfirmed: true), false),
|
||||
(
|
||||
'completed run',
|
||||
executingRun(executing: false, status: 'succeeded'),
|
||||
false,
|
||||
),
|
||||
('failed run', executingRun(executing: false, status: 'failed'), false),
|
||||
];
|
||||
for (final (label, run, expected) in cases) {
|
||||
expect(canCancelRun(run), expected, reason: label);
|
||||
}
|
||||
expect(canCancelRun(null), isFalse);
|
||||
});
|
||||
|
||||
testWidgets(
|
||||
'material cancel asks first, Keep sends nothing, then sends the envelope',
|
||||
(tester) async {
|
||||
final accepted = acceptedCancelRun();
|
||||
final gateway = FakeCancelGateway(
|
||||
(type, fields) async => {'run_detail': accepted},
|
||||
);
|
||||
final changed = <JsonMap>[];
|
||||
await tester.pumpWidget(
|
||||
cancelApp(
|
||||
CancelHost(
|
||||
gateway: gateway,
|
||||
initial: executingRun(),
|
||||
changed: changed,
|
||||
),
|
||||
cupertino: false,
|
||||
),
|
||||
);
|
||||
|
||||
expect(find.byKey(cancelActionKey), findsOneWidget);
|
||||
expect(find.byType(FilledButton), findsOneWidget);
|
||||
expect(gateway.calls, isEmpty);
|
||||
|
||||
await tester.tap(find.byKey(cancelActionKey));
|
||||
await tester.pumpAndSettle();
|
||||
expect(find.byType(AlertDialog), findsOneWidget);
|
||||
expect(find.text(cancelDialogTitle(7)), findsOneWidget);
|
||||
expect(find.text(cancelDialogMessage), findsOneWidget);
|
||||
expect(find.text(cancelKeepLabel), findsOneWidget);
|
||||
expect(gateway.calls, isEmpty);
|
||||
|
||||
await tester.tap(find.byKey(cancelKeepKey));
|
||||
await tester.pumpAndSettle();
|
||||
expect(find.byType(AlertDialog), findsNothing);
|
||||
expect(find.byKey(cancelActionKey), findsOneWidget);
|
||||
expect(gateway.calls, isEmpty);
|
||||
|
||||
await tester.tap(find.byKey(cancelActionKey));
|
||||
await tester.pumpAndSettle();
|
||||
await tester.tap(find.byKey(cancelConfirmKey));
|
||||
await tester.pumpAndSettle();
|
||||
|
||||
expect(gateway.calls.map((call) => call.type), ['cancel_run']);
|
||||
expect(gateway.calls.single.fields, {'run_id': 7});
|
||||
expect(changed.single, equals(accepted));
|
||||
expect(find.byType(AlertDialog), findsNothing);
|
||||
expect(find.byKey(cancelActionKey), findsNothing);
|
||||
},
|
||||
);
|
||||
|
||||
testWidgets(
|
||||
'cupertino cancel uses a native prompt and sends only on confirm',
|
||||
(tester) async {
|
||||
final accepted = acceptedCancelRun();
|
||||
final gateway = FakeCancelGateway(
|
||||
(type, fields) async => {'run_detail': accepted},
|
||||
);
|
||||
final changed = <JsonMap>[];
|
||||
await tester.pumpWidget(
|
||||
cancelApp(
|
||||
CancelHost(
|
||||
gateway: gateway,
|
||||
initial: executingRun(),
|
||||
changed: changed,
|
||||
),
|
||||
cupertino: true,
|
||||
),
|
||||
);
|
||||
|
||||
expect(find.byKey(cancelActionKey), findsOneWidget);
|
||||
expect(find.byType(CupertinoButton), findsOneWidget);
|
||||
expect(find.byType(FilledButton), findsNothing);
|
||||
|
||||
await tester.tap(find.byKey(cancelActionKey));
|
||||
await tester.pumpAndSettle();
|
||||
expect(find.byType(CupertinoAlertDialog), findsOneWidget);
|
||||
expect(find.byType(AlertDialog), findsNothing);
|
||||
expect(find.text(cancelDialogTitle(7)), findsOneWidget);
|
||||
expect(find.text(cancelDialogMessage), findsOneWidget);
|
||||
expect(gateway.calls, isEmpty);
|
||||
|
||||
await tester.tap(find.byKey(cancelKeepKey));
|
||||
await tester.pumpAndSettle();
|
||||
expect(gateway.calls, isEmpty);
|
||||
expect(find.byType(CupertinoAlertDialog), findsNothing);
|
||||
|
||||
await tester.tap(find.byKey(cancelActionKey));
|
||||
await tester.pumpAndSettle();
|
||||
await tester.tap(find.byKey(cancelConfirmKey));
|
||||
await tester.pumpAndSettle();
|
||||
|
||||
expect(gateway.calls.map((call) => call.type), ['cancel_run']);
|
||||
expect(gateway.calls.single.fields, {'run_id': 7});
|
||||
expect(changed.single, equals(accepted));
|
||||
expect(find.byType(CupertinoAlertDialog), findsNothing);
|
||||
expect(find.byKey(cancelActionKey), findsNothing);
|
||||
},
|
||||
);
|
||||
|
||||
testWidgets('the action is absent for runs the web does not cancel', (
|
||||
tester,
|
||||
) async {
|
||||
final gateway = FakeCancelGateway(
|
||||
(type, fields) async => {'run_detail': executingRun()},
|
||||
);
|
||||
final changed = <JsonMap>[];
|
||||
final cases = <(String, JsonMap, bool)>[
|
||||
('executing', executingRun(), true),
|
||||
(
|
||||
'export wait',
|
||||
executingRun(executing: false, status: 'waiting', exportPending: 1),
|
||||
true,
|
||||
),
|
||||
(
|
||||
'no export wait',
|
||||
executingRun(executing: false, status: 'waiting'),
|
||||
false,
|
||||
),
|
||||
('stale running status', executingRun(executing: false), false),
|
||||
('already stopping', executingRun(cancelRequested: true), false),
|
||||
('cleanup unconfirmed', executingRun(cleanupUnconfirmed: true), false),
|
||||
('finished', executingRun(executing: false, status: 'succeeded'), false),
|
||||
];
|
||||
for (final (label, run, visible) in cases) {
|
||||
await tester.pumpWidget(
|
||||
cancelApp(
|
||||
CancelHost(
|
||||
key: ValueKey(label),
|
||||
gateway: gateway,
|
||||
initial: run,
|
||||
changed: changed,
|
||||
),
|
||||
cupertino: false,
|
||||
),
|
||||
);
|
||||
expect(
|
||||
find.byKey(cancelActionKey),
|
||||
visible ? findsOneWidget : findsNothing,
|
||||
reason: label,
|
||||
);
|
||||
}
|
||||
expect(gateway.calls, isEmpty);
|
||||
expect(changed, isEmpty);
|
||||
});
|
||||
}
|
||||
@@ -0,0 +1,271 @@
|
||||
import 'dart:async';
|
||||
|
||||
import 'package:flutter/cupertino.dart';
|
||||
import 'package:flutter/material.dart';
|
||||
import 'package:flutter_test/flutter_test.dart';
|
||||
import 'package:pipelines_app/core/api_gateway.dart';
|
||||
import 'package:pipelines_app/core/transport/connection_state.dart';
|
||||
import 'package:pipelines_app/features/runs/actions/cancel_wording.dart';
|
||||
|
||||
import 'cancel_test_support.dart';
|
||||
|
||||
Future<void> _pump(
|
||||
WidgetTester tester,
|
||||
FakeCancelGateway gateway,
|
||||
List<JsonMap> changed, {
|
||||
bool cupertino = false,
|
||||
double textScale = 1,
|
||||
JsonMap? run,
|
||||
}) async {
|
||||
await tester.pumpWidget(
|
||||
cancelApp(
|
||||
CancelHost(
|
||||
gateway: gateway,
|
||||
initial: run ?? executingRun(),
|
||||
changed: changed,
|
||||
),
|
||||
cupertino: cupertino,
|
||||
textScale: textScale,
|
||||
),
|
||||
);
|
||||
}
|
||||
|
||||
Future<void> _openAndConfirm(WidgetTester tester) async {
|
||||
await tester.tap(find.byKey(cancelActionKey));
|
||||
await tester.pumpAndSettle();
|
||||
await tester.tap(find.byKey(cancelConfirmKey));
|
||||
}
|
||||
|
||||
void main() {
|
||||
testWidgets('a refusal stays visible and delivers the resynced run', (
|
||||
tester,
|
||||
) async {
|
||||
const refusal = 'run 7 already completed; nothing to cancel';
|
||||
final refreshed = executingRun(executing: false, status: 'succeeded');
|
||||
final gateway = FakeCancelGateway((type, fields) async {
|
||||
if (type == 'cancel_run') throw const ProtocolError(refusal);
|
||||
expect(type, 'get_run');
|
||||
return {'run_detail': refreshed};
|
||||
});
|
||||
final changed = <JsonMap>[];
|
||||
await _pump(tester, gateway, changed);
|
||||
|
||||
await _openAndConfirm(tester);
|
||||
await tester.pumpAndSettle();
|
||||
|
||||
expect(gateway.calls.map((call) => call.type), ['cancel_run', 'get_run']);
|
||||
expect(gateway.calls.first.fields, {'run_id': 7});
|
||||
expect(gateway.calls.last.fields, {'run_id': 7});
|
||||
expect(find.text(refusal), findsOneWidget);
|
||||
expect(find.byKey(cancelErrorKey), findsOneWidget);
|
||||
expect(find.byType(AlertDialog), findsOneWidget);
|
||||
expect(changed.single, equals(refreshed));
|
||||
expect(find.byKey(cancelActionKey), findsNothing);
|
||||
});
|
||||
|
||||
testWidgets('a failed resync never hides the refusal or invents a run', (
|
||||
tester,
|
||||
) async {
|
||||
const refusal = 'server refused to stop the run';
|
||||
final gateway = FakeCancelGateway((type, fields) async {
|
||||
if (type == 'cancel_run') throw const ProtocolError(refusal);
|
||||
throw const NotConnectedError();
|
||||
});
|
||||
final changed = <JsonMap>[];
|
||||
await _pump(tester, gateway, changed);
|
||||
|
||||
await _openAndConfirm(tester);
|
||||
await tester.pumpAndSettle();
|
||||
|
||||
expect(gateway.calls.map((call) => call.type), ['cancel_run', 'get_run']);
|
||||
expect(find.text(refusal), findsOneWidget);
|
||||
expect(find.byType(AlertDialog), findsOneWidget);
|
||||
expect(changed, isEmpty);
|
||||
expect(find.byKey(cancelActionKey), findsOneWidget);
|
||||
});
|
||||
|
||||
testWidgets('a refusal and Keep do not wait for the resync', (tester) async {
|
||||
const refusal = 'run 7 is already stopping';
|
||||
final resync = Completer<JsonMap>();
|
||||
final gateway = FakeCancelGateway((type, fields) {
|
||||
if (type == 'cancel_run') throw const ProtocolError(refusal);
|
||||
return resync.future;
|
||||
});
|
||||
final changed = <JsonMap>[];
|
||||
await _pump(tester, gateway, changed);
|
||||
|
||||
await _openAndConfirm(tester);
|
||||
await tester.pump();
|
||||
|
||||
expect(find.text(refusal), findsOneWidget);
|
||||
expect(
|
||||
tester.widget<TextButton>(find.byKey(cancelKeepKey)).onPressed,
|
||||
isNotNull,
|
||||
);
|
||||
expect(
|
||||
tester.widget<FilledButton>(find.byKey(cancelConfirmKey)).onPressed,
|
||||
isNull,
|
||||
);
|
||||
expect(changed, isEmpty);
|
||||
|
||||
await tester.tap(find.byKey(cancelKeepKey));
|
||||
await tester.pumpAndSettle();
|
||||
expect(find.byType(AlertDialog), findsNothing);
|
||||
|
||||
resync.complete({
|
||||
'run_detail': executingRun(executing: false, status: 'succeeded'),
|
||||
});
|
||||
await tester.pump();
|
||||
|
||||
expect(tester.takeException(), isNull);
|
||||
expect(changed.single['status'], 'succeeded');
|
||||
});
|
||||
|
||||
testWidgets('a late resync after disposal is ignored safely', (tester) async {
|
||||
final resync = Completer<JsonMap>();
|
||||
final gateway = FakeCancelGateway((type, fields) {
|
||||
if (type == 'cancel_run') throw const ProtocolError('already stopped');
|
||||
return resync.future;
|
||||
});
|
||||
final changed = <JsonMap>[];
|
||||
await _pump(tester, gateway, changed);
|
||||
|
||||
await _openAndConfirm(tester);
|
||||
await tester.pump();
|
||||
expect(gateway.calls.map((call) => call.type), ['cancel_run', 'get_run']);
|
||||
|
||||
await tester.pumpWidget(const SizedBox());
|
||||
resync.complete({'run_detail': acceptedCancelRun()});
|
||||
await tester.pump();
|
||||
|
||||
expect(tester.takeException(), isNull);
|
||||
expect(changed, isEmpty);
|
||||
});
|
||||
|
||||
testWidgets('an ineligible resync disables confirm but keeps the refusal', (
|
||||
tester,
|
||||
) async {
|
||||
const refusal = 'run 7 already completed; nothing to cancel';
|
||||
final finished = executingRun(executing: false, status: 'succeeded');
|
||||
final gateway = FakeCancelGateway((type, fields) async {
|
||||
if (type == 'cancel_run') throw const ProtocolError(refusal);
|
||||
return {'run_detail': finished};
|
||||
});
|
||||
final changed = <JsonMap>[];
|
||||
await _pump(tester, gateway, changed);
|
||||
|
||||
await _openAndConfirm(tester);
|
||||
await tester.pumpAndSettle();
|
||||
|
||||
expect(gateway.calls.map((call) => call.type), ['cancel_run', 'get_run']);
|
||||
expect(find.text(refusal), findsOneWidget);
|
||||
expect(
|
||||
tester.widget<FilledButton>(find.byKey(cancelConfirmKey)).onPressed,
|
||||
isNull,
|
||||
);
|
||||
expect(
|
||||
tester.widget<TextButton>(find.byKey(cancelKeepKey)).onPressed,
|
||||
isNotNull,
|
||||
);
|
||||
|
||||
await tester.tap(find.byKey(cancelConfirmKey));
|
||||
await tester.pump();
|
||||
expect(gateway.calls.map((call) => call.type), ['cancel_run', 'get_run']);
|
||||
|
||||
await tester.tap(find.byKey(cancelKeepKey));
|
||||
await tester.pumpAndSettle();
|
||||
expect(find.byType(AlertDialog), findsNothing);
|
||||
expect(find.byKey(cancelActionKey), findsNothing);
|
||||
expect(changed.single, equals(finished));
|
||||
});
|
||||
|
||||
testWidgets(
|
||||
'confirm cannot submit twice while the cancellation is in flight',
|
||||
(tester) async {
|
||||
final reply = Completer<JsonMap>();
|
||||
final gateway = FakeCancelGateway((type, fields) => reply.future);
|
||||
final changed = <JsonMap>[];
|
||||
await _pump(tester, gateway, changed);
|
||||
|
||||
await tester.tap(find.byKey(cancelActionKey));
|
||||
await tester.pumpAndSettle();
|
||||
final confirm = find.byKey(cancelConfirmKey);
|
||||
await tester.tap(confirm);
|
||||
await tester.tap(confirm);
|
||||
await tester.pump();
|
||||
|
||||
expect(
|
||||
gateway.calls.where((call) => call.type == 'cancel_run'),
|
||||
hasLength(1),
|
||||
);
|
||||
expect(
|
||||
tester.widget<FilledButton>(find.byKey(cancelConfirmKey)).onPressed,
|
||||
isNull,
|
||||
);
|
||||
expect(
|
||||
tester.widget<TextButton>(find.byKey(cancelKeepKey)).onPressed,
|
||||
isNull,
|
||||
);
|
||||
expect(find.byType(AlertDialog), findsOneWidget);
|
||||
|
||||
reply.complete({'run_detail': acceptedCancelRun()});
|
||||
await tester.pumpAndSettle();
|
||||
expect(find.byType(AlertDialog), findsNothing);
|
||||
expect(changed, hasLength(1));
|
||||
},
|
||||
);
|
||||
|
||||
testWidgets('a late reply after disposal is ignored safely', (tester) async {
|
||||
final reply = Completer<JsonMap>();
|
||||
final gateway = FakeCancelGateway((type, fields) => reply.future);
|
||||
final changed = <JsonMap>[];
|
||||
await _pump(tester, gateway, changed);
|
||||
|
||||
await _openAndConfirm(tester);
|
||||
await tester.pump();
|
||||
expect(gateway.calls, hasLength(1));
|
||||
|
||||
await tester.pumpWidget(const SizedBox());
|
||||
reply.complete({'run_detail': acceptedCancelRun()});
|
||||
await tester.pump();
|
||||
|
||||
expect(tester.takeException(), isNull);
|
||||
expect(changed, isEmpty);
|
||||
});
|
||||
|
||||
testWidgets('both prompts fit 2x text at a narrow width', (tester) async {
|
||||
tester.view.physicalSize = const Size(320, 480);
|
||||
tester.view.devicePixelRatio = 1;
|
||||
addTearDown(tester.view.resetPhysicalSize);
|
||||
addTearDown(tester.view.resetDevicePixelRatio);
|
||||
|
||||
for (final cupertino in [false, true]) {
|
||||
final gateway = FakeCancelGateway(
|
||||
(type, fields) async => {'run_detail': acceptedCancelRun()},
|
||||
);
|
||||
await _pump(
|
||||
tester,
|
||||
gateway,
|
||||
<JsonMap>[],
|
||||
cupertino: cupertino,
|
||||
textScale: 2,
|
||||
);
|
||||
await tester.tap(find.byKey(cancelActionKey));
|
||||
await tester.pumpAndSettle();
|
||||
|
||||
expect(tester.takeException(), isNull, reason: 'cupertino=$cupertino');
|
||||
expect(find.text(cancelDialogTitle(7)), findsOneWidget);
|
||||
expect(find.text(cancelDialogMessage), findsOneWidget);
|
||||
expect(
|
||||
cupertino
|
||||
? find.byType(CupertinoAlertDialog)
|
||||
: find.byType(AlertDialog),
|
||||
findsOneWidget,
|
||||
);
|
||||
expect(gateway.calls, isEmpty);
|
||||
|
||||
await tester.tap(find.byKey(cancelKeepKey));
|
||||
await tester.pumpAndSettle();
|
||||
}
|
||||
});
|
||||
}
|
||||
@@ -0,0 +1,101 @@
|
||||
import 'package:flutter/cupertino.dart';
|
||||
import 'package:flutter/material.dart';
|
||||
import 'package:pipelines_app/core/api_gateway.dart';
|
||||
import 'package:pipelines_app/features/runs/actions/cancel_run_action.dart';
|
||||
|
||||
typedef CancelHandler = Future<JsonMap> Function(String type, JsonMap fields);
|
||||
|
||||
/// A fake gateway that records every request and never owns a socket.
|
||||
class FakeCancelGateway implements ApiGateway {
|
||||
FakeCancelGateway(this.handler);
|
||||
|
||||
final CancelHandler handler;
|
||||
final List<({String type, JsonMap fields})> calls = [];
|
||||
|
||||
@override
|
||||
Future<JsonMap> call(String type, [JsonMap fields = const {}]) {
|
||||
final copy = Map<String, dynamic>.from(fields);
|
||||
calls.add((type: type, fields: copy));
|
||||
return handler(type, copy);
|
||||
}
|
||||
|
||||
@override
|
||||
Stream<JsonMap> get frames => const Stream.empty();
|
||||
}
|
||||
|
||||
JsonMap executingRun({
|
||||
num id = 7,
|
||||
bool executing = true,
|
||||
String status = 'running',
|
||||
int exportPending = 0,
|
||||
bool cancelRequested = false,
|
||||
bool cleanupUnconfirmed = false,
|
||||
}) => {
|
||||
'id': id,
|
||||
'title': 'Nightly build',
|
||||
'status': status,
|
||||
'executing': executing,
|
||||
'export_pending_sequence': exportPending,
|
||||
'cancel_requested': cancelRequested,
|
||||
'cleanup_unconfirmed': cleanupUnconfirmed,
|
||||
};
|
||||
|
||||
JsonMap acceptedCancelRun() =>
|
||||
executingRun(executing: false, status: 'canceled', cancelRequested: true);
|
||||
|
||||
/// Owns the run like a run-detail screen would, so the callback's returned
|
||||
/// server detail is what the action renders next.
|
||||
class CancelHost extends StatefulWidget {
|
||||
const CancelHost({
|
||||
super.key,
|
||||
required this.gateway,
|
||||
required this.initial,
|
||||
required this.changed,
|
||||
});
|
||||
|
||||
final FakeCancelGateway gateway;
|
||||
final JsonMap initial;
|
||||
final List<JsonMap> changed;
|
||||
|
||||
@override
|
||||
State<CancelHost> createState() => _CancelHostState();
|
||||
}
|
||||
|
||||
class _CancelHostState extends State<CancelHost> {
|
||||
late JsonMap _run = widget.initial;
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
return CancelRunAction(
|
||||
run: _run,
|
||||
gateway: widget.gateway,
|
||||
onRunChanged: (run) {
|
||||
widget.changed.add(run);
|
||||
setState(() => _run = run);
|
||||
},
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
Widget cancelApp(
|
||||
Widget child, {
|
||||
required bool cupertino,
|
||||
double textScale = 1,
|
||||
}) {
|
||||
Widget media(BuildContext context, Widget? app) => MediaQuery(
|
||||
data: MediaQuery.of(
|
||||
context,
|
||||
).copyWith(textScaler: TextScaler.linear(textScale)),
|
||||
child: app!,
|
||||
);
|
||||
if (cupertino) {
|
||||
return CupertinoApp(
|
||||
builder: media,
|
||||
home: CupertinoPageScaffold(child: Center(child: child)),
|
||||
);
|
||||
}
|
||||
return MaterialApp(
|
||||
builder: media,
|
||||
home: Scaffold(body: Center(child: child)),
|
||||
);
|
||||
}
|
||||
@@ -212,6 +212,34 @@ void main() {
|
||||
},
|
||||
);
|
||||
|
||||
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 {
|
||||
|
||||
@@ -154,6 +154,117 @@ void main() {
|
||||
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 {
|
||||
|
||||
Reference in New Issue
Block a user