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 = []; await tester.pumpWidget( cancelApp( CancelHost( gateway: gateway, initial: executingRun(), changed: changed, ), cupertino: false, ), ); expect(find.byKey(cancelActionKey), findsOneWidget); // One native run-action style: outlined on Material. expect(find.byType(OutlinedButton), 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 = []; 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(OutlinedButton), 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 = []; 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); }); }