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/retry_eligibility.dart'; import 'package:pipelines_app/features/runs/actions/retry_intent.dart'; import 'package:pipelines_app/features/runs/actions/retry_wording.dart'; import 'retry_test_support.dart'; void main() { test('retry eligibility matches the web canRetry exactly', () { final cases = <(String, JsonMap, bool)>[ ('failed and stopped', failedRun(), true), ('failed but still executing', failedRun(executing: true), false), ('running', failedRun(status: 'running'), false), ('waiting', failedRun(status: 'waiting'), false), ('succeeded', failedRun(status: 'succeeded'), false), ('canceled', failedRun(status: 'canceled'), false), ]; for (final (label, run, expected) in cases) { expect(canRetryRun(run), expected, reason: label); } expect(canRetryRun(null), isFalse); expect(retryRunId(failedRun()), 7); expect(retryRunId({'id': '7'}), isNull); }); test('a generated retry key is bounded, run-scoped and fresh', () { final first = newRetryRequestKey(7); final second = newRetryRequestKey(7); expect(first, startsWith('app-retry-7-')); expect(first.length, lessThanOrEqualTo(128)); expect(second.length, lessThanOrEqualTo(128)); expect(first, isNot(second)); expect( newRetryRequestKey(7, now: DateTime.utc(2026, 1, 1), random: 36), 'app-retry-7-${DateTime.utc(2026, 1, 1).microsecondsSinceEpoch}-10', ); }); test('the full entropy width still forms a bounded request key', () { // The web build once rejected every retry because `1 << 32` is 0 there; // the generator now uses a literal 2^32 bound. This pins the largest // entropy value it may consume. final key = newRetryRequestKey( 7, now: DateTime.utc(2026, 1, 1), random: 0xFFFFFFFF, ); expect( key, 'app-retry-7-${DateTime.utc(2026, 1, 1).microsecondsSinceEpoch}-1z141z3', ); expect(key.length, lessThanOrEqualTo(128)); }); testWidgets( 'material retry asks first, Keep sends nothing, then omits retry_mode', (tester) async { final accepted = retriedRun(); final gateway = FakeRetryGateway( (type, fields) async => {'run_detail': accepted}, ); final changed = []; await pumpRetry(tester, gateway: gateway, changed: changed); expect(find.byKey(retryActionKey), findsOneWidget); // One native run-action style: outlined on Material. expect(find.byType(OutlinedButton), findsOneWidget); expect(gateway.calls, isEmpty); await tester.tap(find.byKey(retryActionKey)); await tester.pumpAndSettle(); expect(find.byType(AlertDialog), findsOneWidget); expect(find.text(retryDialogTitle(7)), findsOneWidget); expect(find.text(retryDialogMessage), findsOneWidget); expect( find.descendant( of: find.byType(AlertDialog), matching: find.text(retryConfirmLabel), ), findsOneWidget, ); expect(find.text(restartConfirmLabel), findsOneWidget); expect(gateway.calls, isEmpty); await tester.tap(find.byKey(retryKeepKey)); await tester.pumpAndSettle(); expect(find.byType(AlertDialog), findsNothing); expect(find.byKey(retryActionKey), findsOneWidget); expect(gateway.calls, isEmpty); await tester.tap(find.byKey(retryActionKey)); await tester.pumpAndSettle(); await tester.tap(find.byKey(retryConfirmKey)); await tester.pumpAndSettle(); expect(gateway.retries, hasLength(1)); final fields = gateway.retries.single.fields; expect(fields.keys, containsAll(['run_id', 'request_key'])); expect(fields.containsKey('retry_mode'), isFalse); expect(fields['run_id'], 7); expect(fields['request_key'], startsWith('app-retry-7-')); expect(fields['request_key'].length, lessThanOrEqualTo(128)); expect(changed.single, equals(accepted)); expect(find.byType(AlertDialog), findsNothing); expect(find.byKey(retryActionKey), findsNothing); }, ); testWidgets( 'material restart has its own confirmation and sends retry_mode', (tester) async { final accepted = retriedRun(); final gateway = FakeRetryGateway( (type, fields) async => {'run_detail': accepted}, ); final changed = []; await pumpRetry(tester, gateway: gateway, changed: changed); await tester.tap(find.byKey(retryActionKey)); await tester.pumpAndSettle(); await tester.tap(find.byKey(retryRestartKey)); await tester.pumpAndSettle(); expect(find.text(restartDialogTitle(7)), findsOneWidget); expect(find.text(restartDialogMessage), findsOneWidget); expect(find.text(retryDialogTitle(7)), findsNothing); expect(find.text(retryDialogMessage), findsNothing); expect(find.text(restartConfirmLabel), findsOneWidget); expect(find.byKey(retryRestartKey), findsNothing); expect(gateway.calls, isEmpty); await tester.tap(find.byKey(retryConfirmKey)); await tester.pumpAndSettle(); expect(gateway.retries, hasLength(1)); final fields = gateway.retries.single.fields; expect(fields['run_id'], 7); expect(fields['retry_mode'], 'restart'); expect(fields['request_key'], startsWith('app-retry-7-')); expect(changed.single, equals(accepted)); expect(find.byType(AlertDialog), findsNothing); }, ); testWidgets( 'cupertino retry uses a native prompt and sends only on confirm', (tester) async { final accepted = retriedRun(); final gateway = FakeRetryGateway( (type, fields) async => {'run_detail': accepted}, ); final changed = []; await pumpRetry( tester, gateway: gateway, changed: changed, cupertino: true, ); expect(find.byKey(retryActionKey), findsOneWidget); expect(find.byType(CupertinoButton), findsOneWidget); expect(find.byType(OutlinedButton), findsNothing); await tester.tap(find.byKey(retryActionKey)); await tester.pumpAndSettle(); expect(find.byType(CupertinoAlertDialog), findsOneWidget); expect(find.byType(AlertDialog), findsNothing); expect(find.text(retryDialogTitle(7)), findsOneWidget); expect(find.text(retryDialogMessage), findsOneWidget); expect(gateway.calls, isEmpty); await tester.tap(find.byKey(retryKeepKey)); await tester.pumpAndSettle(); expect(gateway.calls, isEmpty); expect(find.byType(CupertinoAlertDialog), findsNothing); await tester.tap(find.byKey(retryActionKey)); await tester.pumpAndSettle(); await tester.tap(find.byKey(retryConfirmKey)); await tester.pumpAndSettle(); expect(gateway.retries, hasLength(1)); final fields = gateway.retries.single.fields; expect(fields['run_id'], 7); expect(fields.containsKey('retry_mode'), isFalse); expect(fields['request_key'], startsWith('app-retry-7-')); expect(changed.single, equals(accepted)); expect(find.byType(CupertinoAlertDialog), findsNothing); }, ); testWidgets('cupertino restart switches to the restart confirmation', ( tester, ) async { final gateway = FakeRetryGateway( (type, fields) async => {'run_detail': retriedRun()}, ); await pumpRetry( tester, gateway: gateway, changed: [], cupertino: true, ); await tester.tap(find.byKey(retryActionKey)); await tester.pumpAndSettle(); await tester.tap(find.byKey(retryRestartKey)); await tester.pumpAndSettle(); expect(find.byType(CupertinoAlertDialog), findsOneWidget); expect(find.text(restartDialogTitle(7)), findsOneWidget); expect(find.text(restartDialogMessage), findsOneWidget); expect(gateway.calls, isEmpty); await tester.tap(find.byKey(retryConfirmKey)); await tester.pumpAndSettle(); expect(gateway.retries.single.fields['retry_mode'], 'restart'); }); testWidgets('the action is absent for runs the web does not retry', ( tester, ) async { final gateway = FakeRetryGateway( (type, fields) async => {'run_detail': retriedRun()}, ); final changed = []; final cases = <(String, JsonMap, bool)>[ ('failed', failedRun(), true), ('executing', failedRun(executing: true), false), ('running', failedRun(status: 'running'), false), ('waiting', failedRun(status: 'waiting'), false), ('succeeded', failedRun(status: 'succeeded'), false), ('canceled', failedRun(status: 'canceled'), false), ]; for (final cupertino in [false, true]) { for (final (label, run, visible) in cases) { await tester.pumpWidget( retryApp( RetryHost( key: ValueKey('$label-$cupertino'), gateway: gateway, initial: run, changed: changed, ), cupertino: cupertino, ), ); expect( find.byKey(retryActionKey), visible ? findsOneWidget : findsNothing, reason: '$label cupertino=$cupertino', ); } } expect(gateway.calls, isEmpty); expect(changed, isEmpty); }); }