Files
pipelines/app/test/features/runs/actions/cancel_test_support.dart
T
alan faa5866410 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.
2026-10-21 14:41:33 +01:00

102 lines
2.6 KiB
Dart

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)),
);
}