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 _pump( WidgetTester tester, FakeCancelGateway gateway, List 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 _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 = []; 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 = []; 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(); 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 { final reply = Completer(); final gateway = FakeCancelGateway((type, fields) => reply.future); final changed = []; 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(find.byKey(cancelConfirmKey)).onPressed, isNull, ); expect( tester.widget(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(); final gateway = FakeCancelGateway((type, fields) => reply.future); final changed = []; 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, [], 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(); } }); }