import 'dart:async'; 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/retry_eligibility.dart'; import 'package:pipelines_app/features/runs/actions/retry_wording.dart'; import 'retry_test_support.dart'; Future _openAndConfirm(WidgetTester tester) async { await tester.tap(find.byKey(retryActionKey)); await tester.pumpAndSettle(); await tester.tap(find.byKey(retryConfirmKey)); } void main() { testWidgets('a refusal is shown at once and Keep does not wait for resync', ( tester, ) async { const refusal = 'run 7 is still executing; wait for it to stop completely'; final resync = Completer(); final gateway = FakeRetryGateway((type, fields) { if (type == 'retry_run') throw const ProtocolError(refusal); return resync.future; }); final changed = []; await pumpRetry(tester, gateway: gateway, changed: changed); await _openAndConfirm(tester); await tester.pump(); expect(gateway.calls.map((call) => call.type), ['retry_run', 'get_run']); expect(find.text(refusal), findsOneWidget); expect(find.byKey(retryErrorKey), findsOneWidget); expect( tester.widget(find.byKey(retryKeepKey)).onPressed, isNotNull, ); expect( tester.widget(find.byKey(retryConfirmKey)).onPressed, isNull, ); expect(changed, isEmpty); await tester.tap(find.byKey(retryKeepKey)); await tester.pumpAndSettle(); expect(find.byType(AlertDialog), findsNothing); resync.complete({'run_detail': failedRun(status: 'succeeded')}); await tester.pump(); await tester.pump(); expect(tester.takeException(), isNull); expect(changed.single['status'], 'succeeded'); expect(find.byKey(retryActionKey), findsNothing); }); testWidgets('server truth that is ineligible blocks a repeat action', ( tester, ) async { const refusal = 'only failed runs can be retried'; final finished = failedRun(status: 'succeeded'); final gateway = FakeRetryGateway((type, fields) async { if (type == 'retry_run') throw const ProtocolError(refusal); return {'run_detail': finished}; }); final changed = []; await pumpRetry(tester, gateway: gateway, changed: changed); await _openAndConfirm(tester); await tester.pumpAndSettle(); expect(gateway.calls.map((call) => call.type), ['retry_run', 'get_run']); expect(find.text(refusal), findsOneWidget); expect( tester.widget(find.byKey(retryConfirmKey)).onPressed, isNull, ); expect( tester.widget(find.byKey(retryKeepKey)).onPressed, isNotNull, ); await tester.tap(find.byKey(retryConfirmKey)); await tester.pump(); expect(gateway.calls.map((call) => call.type), ['retry_run', 'get_run']); await tester.tap(find.byKey(retryKeepKey)); await tester.pumpAndSettle(); expect(find.byType(AlertDialog), findsNothing); expect(find.byKey(retryActionKey), findsNothing); expect(changed.single, equals(finished)); expect(canRetryRun(changed.single), isFalse); }); testWidgets('a failed recheck keeps the same key for the uncertain request', ( tester, ) async { const refusal = 'connection dropped while retrying'; var attempts = 0; final gateway = FakeRetryGateway((type, fields) async { if (type == 'get_run') throw const NotConnectedError(); attempts += 1; if (attempts == 1) throw const ProtocolError(refusal); return {'run_detail': retriedRun()}; }); final changed = []; await pumpRetry(tester, gateway: gateway, changed: changed); await _openAndConfirm(tester); await tester.pumpAndSettle(); expect(find.text(refusal), findsOneWidget); expect( tester.widget(find.byKey(retryConfirmKey)).onPressed, isNotNull, ); expect(gateway.retries, hasLength(1)); await tester.tap(find.byKey(retryConfirmKey)); await tester.pumpAndSettle(); expect(gateway.retries, hasLength(2)); expect( gateway.retries.last.fields['request_key'], gateway.retries.first.fields['request_key'], ); expect( gateway.retries.every((call) => !call.fields.containsKey('retry_mode')), isTrue, ); expect(changed.single, equals(retriedRun())); expect(find.byType(AlertDialog), findsNothing); }); testWidgets('a restart retry keeps the mode and key across uncertainty', ( tester, ) async { var attempts = 0; final gateway = FakeRetryGateway((type, fields) async { if (type == 'get_run') throw const NotConnectedError(); attempts += 1; if (attempts == 1) throw const ProtocolError('network lost'); return {'run_detail': retriedRun()}; }); await pumpRetry(tester, gateway: gateway, changed: []); await tester.tap(find.byKey(retryActionKey)); await tester.pumpAndSettle(); await tester.tap(find.byKey(retryRestartKey)); await tester.pumpAndSettle(); await tester.tap(find.byKey(retryConfirmKey)); await tester.pumpAndSettle(); await tester.tap(find.byKey(retryConfirmKey)); await tester.pumpAndSettle(); expect(gateway.retries, hasLength(2)); expect(gateway.retries.first.fields['retry_mode'], 'restart'); expect(gateway.retries.last.fields['retry_mode'], 'restart'); expect( gateway.retries.last.fields['request_key'], gateway.retries.first.fields['request_key'], ); }); testWidgets('a deliberate reopen sends a fresh request key', (tester) async { final gateway = FakeRetryGateway((type, fields) async { if (type == 'get_run') throw const NotConnectedError(); throw const ProtocolError('server did not answer'); }); await pumpRetry(tester, gateway: gateway, changed: []); await _openAndConfirm(tester); await tester.pumpAndSettle(); await tester.tap(find.byKey(retryKeepKey)); await tester.pumpAndSettle(); await _openAndConfirm(tester); await tester.pumpAndSettle(); expect(gateway.retries, hasLength(2)); expect( gateway.retries.first.fields['request_key'], isNot(gateway.retries.last.fields['request_key']), ); }); testWidgets('a restart after a dismissed resume uses a fresh key', ( tester, ) async { var attempts = 0; final gateway = FakeRetryGateway((type, fields) async { if (type == 'get_run') throw const NotConnectedError(); attempts += 1; if (attempts == 1) throw const ProtocolError('resume refused'); return {'run_detail': retriedRun()}; }); await pumpRetry(tester, gateway: gateway, changed: []); await _openAndConfirm(tester); await tester.pumpAndSettle(); // A refusal leaves the prompt open; keep the mode frozen and dismiss. await tester.tap(find.byKey(retryKeepKey)); await tester.pumpAndSettle(); await tester.tap(find.byKey(retryActionKey)); await tester.pumpAndSettle(); await tester.tap(find.byKey(retryRestartKey)); await tester.pumpAndSettle(); await tester.tap(find.byKey(retryConfirmKey)); await tester.pumpAndSettle(); expect(gateway.retries, hasLength(2)); expect(gateway.retries.first.fields.containsKey('retry_mode'), isFalse); expect(gateway.retries.last.fields['retry_mode'], 'restart'); expect( gateway.retries.first.fields['request_key'], isNot(gateway.retries.last.fields['request_key']), ); }); }