diff --git a/app/lib/features/runs/actions/action_refusal.dart b/app/lib/features/runs/actions/action_refusal.dart new file mode 100644 index 00000000..87e03995 --- /dev/null +++ b/app/lib/features/runs/actions/action_refusal.dart @@ -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? 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.'; diff --git a/app/lib/features/runs/actions/cancel_dialog.dart b/app/lib/features/runs/actions/cancel_dialog.dart index 96f4c204..1654f442 100644 --- a/app/lib/features/runs/actions/cancel_dialog.dart +++ b/app/lib/features/runs/actions/cancel_dialog.dart @@ -1,11 +1,13 @@ 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 the server's refusal verbatim; the prompt then stays open. -typedef CancelSubmit = Future Function(); +/// it, or an [ActionRefusal] carrying the server's words verbatim; the prompt +/// then stays open with dismissal available immediately. +typedef CancelSubmit = Future Function(); /// Opens the native cancel confirmation for [runId]. /// @@ -53,6 +55,8 @@ class _CancelPrompt extends StatefulWidget { class _CancelPromptState extends State<_CancelPrompt> { bool _sending = false; + bool _checking = false; + bool _blocked = false; String? _error; void _keep() { @@ -60,7 +64,7 @@ class _CancelPromptState extends State<_CancelPrompt> { } Future _confirm() async { - if (_sending) return; + if (_sending || _checking || _blocked) return; setState(() { _sending = true; _error = null; @@ -71,9 +75,23 @@ class _CancelPromptState extends State<_CancelPrompt> { 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; }); } @@ -83,6 +101,8 @@ class _CancelPromptState extends State<_CancelPrompt> { return PopScope(canPop: !_sending, child: dialog); } + bool get _confirmDisabled => _sending || _checking || _blocked; + Widget _material(BuildContext context) { final theme = Theme.of(context); return AlertDialog( @@ -97,7 +117,7 @@ class _CancelPromptState extends State<_CancelPrompt> { ), FilledButton( key: cancelConfirmKey, - onPressed: _sending ? null : _confirm, + onPressed: _confirmDisabled ? null : _confirm, child: const Text(cancelConfirmLabel), ), ], @@ -121,7 +141,7 @@ class _CancelPromptState extends State<_CancelPrompt> { CupertinoDialogAction( key: cancelConfirmKey, isDestructiveAction: true, - onPressed: _sending ? null : _confirm, + onPressed: _confirmDisabled ? null : _confirm, child: const Text(cancelConfirmLabel), ), ], diff --git a/app/lib/features/runs/actions/cancel_run_action.dart b/app/lib/features/runs/actions/cancel_run_action.dart index e78564bd..19bdf181 100644 --- a/app/lib/features/runs/actions/cancel_run_action.dart +++ b/app/lib/features/runs/actions/cancel_run_action.dart @@ -3,6 +3,7 @@ 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'; @@ -12,8 +13,9 @@ import 'cancel_wording.dart'; /// 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 shown verbatim in the kept-open prompt and the run is re-read, so -/// [onRunChanged] always receives server truth instead of a local guess. +/// 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, @@ -32,6 +34,7 @@ class CancelRunAction extends StatefulWidget { class _CancelRunActionState extends State { bool _dialogOpen = false; + Future? _resync; bool _usesCupertino(BuildContext context) { final material = Localizations.of( @@ -58,25 +61,40 @@ class _CancelRunActionState extends State { } } - Future _submit(num id) async { + Future _submit(num id) async { final repository = RunsRepository(widget.gateway); try { final run = await repository.cancelRun(id); if (!mounted) return null; - widget.onRunChanged(run); + if (_owns(id)) widget.onRunChanged(run); return null; } catch (error) { - final refusal = describeCancelError(error); - if (!mounted) return refusal; - JsonMap? refreshed; - try { - refreshed = await repository.getRun(id); - } catch (_) { - // A failed re-read must not hide or replace the server's refusal. - } - if (!mounted) return refusal; - if (refreshed != null) widget.onRunChanged(refreshed); - return refusal; + 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 _resyncRun(num id) { + return _resync ??= _readRun(id); + } + + Future _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; } } diff --git a/app/lib/features/runs/actions/decision_action.dart b/app/lib/features/runs/actions/decision_action.dart index 5405accf..246c66fb 100644 --- a/app/lib/features/runs/actions/decision_action.dart +++ b/app/lib/features/runs/actions/decision_action.dart @@ -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 { PendingDecision? _pending; bool _dialogOpen = false; bool _sending = false; + Future? _resync; @override void initState() { @@ -78,9 +81,13 @@ class _HumanDecisionActionState extends State { } } - Future _submit(PendingDecision decision, DecisionDraft draft) async { + Future _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 { 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 _resyncRun(PendingDecision decision) { + return _resync ??= _readRun(decision); + } + + Future _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; diff --git a/app/lib/features/runs/actions/decision_dialog.dart b/app/lib/features/runs/actions/decision_dialog.dart index 9649ea16..81aa717c 100644 --- a/app/lib/features/runs/actions/decision_dialog.dart +++ b/app/lib/features/runs/actions/decision_dialog.dart @@ -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 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 Function(DecisionDraft draft); /// Opens the native decision prompt for [decision]. Future 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 _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), ), ], diff --git a/app/notes/R2b-refusal.md b/app/notes/R2b-refusal.md new file mode 100644 index 00000000..65851110 --- /dev/null +++ b/app/notes/R2b-refusal.md @@ -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. diff --git a/app/test/features/runs/actions/cancel_resilience_test.dart b/app/test/features/runs/actions/cancel_resilience_test.dart index e3fa15ee..675c58b2 100644 --- a/app/test/features/runs/actions/cancel_resilience_test.dart +++ b/app/test/features/runs/actions/cancel_resilience_test.dart @@ -84,6 +84,101 @@ void main() { 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(); + final gateway = FakeCancelGateway((type, fields) { + if (type == 'cancel_run') throw const ProtocolError(refusal); + return resync.future; + }); + final changed = []; + await _pump(tester, gateway, changed); + + await _openAndConfirm(tester); + await tester.pump(); + + expect(find.text(refusal), findsOneWidget); + expect( + tester.widget(find.byKey(cancelKeepKey)).onPressed, + isNotNull, + ); + expect( + tester.widget(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(); + final gateway = FakeCancelGateway((type, fields) { + if (type == 'cancel_run') throw const ProtocolError('already stopped'); + return resync.future; + }); + final changed = []; + 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 = []; + 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(find.byKey(cancelConfirmKey)).onPressed, + isNull, + ); + expect( + tester.widget(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 { diff --git a/app/test/features/runs/actions/decision_action_test.dart b/app/test/features/runs/actions/decision_action_test.dart index 9d424b8f..b756db79 100644 --- a/app/test/features/runs/actions/decision_action_test.dart +++ b/app/test/features/runs/actions/decision_action_test.dart @@ -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 = []; + 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 { diff --git a/app/test/features/runs/actions/decision_resilience_test.dart b/app/test/features/runs/actions/decision_resilience_test.dart index d23981b5..e755ddc5 100644 --- a/app/test/features/runs/actions/decision_resilience_test.dart +++ b/app/test/features/runs/actions/decision_resilience_test.dart @@ -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(); + final gateway = _FakeGateway((type, fields) { + if (type == 'decide_run') throw const ProtocolError(refusal); + return resync.future; + }); + final changed = []; + 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>(find.byType(RadioGroup)) + .groupValue, + 'go', + ); + expect( + tester + .widget(find.byKey(decisionFeedbackKey)) + .controller + ?.text, + ' keep me ', + ); + expect( + tester + .widget( + 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( + 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 = []; + 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>(find.byType(RadioGroup)) + .groupValue, + 'go', + ); + expect( + tester + .widget( + find.widgetWithText(FilledButton, decisionConfirmLabel), + ) + .onPressed, + isNotNull, + ); + expect(changed, isEmpty); + }); + + testWidgets('a late resync after disposal is ignored safely', (tester) async { + final resync = Completer(); + final gateway = _FakeGateway((type, fields) { + if (type == 'decide_run') throw const ProtocolError('no longer waiting'); + return resync.future; + }); + final changed = []; + 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 {