feat(app): add deliberate run cancellation action

Port the browser's canCancel rule and cancel_run request into a native
Material/Cupertino confirmation. Opening the prompt or choosing Keep sends
nothing; a refusal stays visible with the server's words and the run is
re-read so the owner always receives server truth. Includes eligibility,
double-submit, disposal and narrow 2x-text tests plus R2b evidence.
This commit is contained in:
2026-10-21 14:41:33 +01:00
parent 3e6f950991
commit faa5866410
8 changed files with 865 additions and 0 deletions
@@ -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,176 @@
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(
'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)),
);
}