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/title_action.dart'; import 'package:pipelines_app/features/runs/actions/title_wording.dart'; import 'title_test_support.dart'; String _materialFieldText(WidgetTester tester) => tester.widget(find.byKey(runTitleFieldKey)).controller!.text; String _cupertinoFieldText(WidgetTester tester) => tester .widget(find.byKey(runTitleFieldKey)) .controller! .text; void main() { testWidgets( 'material rename starts from the stored title, typing sends nothing, ' 'Cancel sends nothing and Save sends the exact text', (tester) async { final accepted = renamedRun(title: 'Release build'); final gateway = FakeTitleGateway( (type, fields) async => {'run_detail': accepted}, ); final changed = []; await tester.pumpWidget( titleApp( TitleHost(gateway: gateway, initial: titledRun(), changed: changed), cupertino: false, ), ); expect(find.byKey(runTitleActionKey), findsOneWidget); expect(find.byType(OutlinedButton), findsOneWidget); expect(gateway.calls, isEmpty); await tester.tap(find.byKey(runTitleActionKey)); await tester.pumpAndSettle(); expect(find.byType(AlertDialog), findsOneWidget); expect(find.text(runTitleDialogTitle), findsWidgets); expect(find.text(runTitleFieldLabel), findsOneWidget); expect(_materialFieldText(tester), 'Nightly build'); await tester.enterText( find.byKey(runTitleFieldKey), ' 发布 🚀 — nightly ', ); await tester.pump(); expect(gateway.calls, isEmpty); await tester.tap(find.byKey(runTitleCancelKey)); await tester.pumpAndSettle(); expect(find.byType(AlertDialog), findsNothing); expect(gateway.calls, isEmpty); expect(changed, isEmpty); expect( tester.widget(find.byKey(titleHostTitleKey)).data, 'Nightly build', ); await tester.tap(find.byKey(runTitleActionKey)); await tester.pumpAndSettle(); expect(_materialFieldText(tester), 'Nightly build'); await tester.enterText( find.byKey(runTitleFieldKey), ' 发布 🚀 — nightly ', ); await tester.tap(find.byKey(runTitleSaveKey)); await tester.pumpAndSettle(); expect(gateway.calls.map((call) => call.type), ['set_run_title']); expect(gateway.calls.single.fields, { 'run_id': 7, 'title': ' 发布 🚀 — nightly ', }); expect(changed.single, equals(accepted)); expect(find.byType(AlertDialog), findsNothing); expect( tester.widget(find.byKey(titleHostTitleKey)).data, 'Release build', ); }, ); testWidgets('cupertino rename is native and sends only on Save', ( tester, ) async { final accepted = renamedRun(title: 'Release build'); final gateway = FakeTitleGateway( (type, fields) async => {'run_detail': accepted}, ); final changed = []; await tester.pumpWidget( titleApp( TitleHost(gateway: gateway, initial: titledRun(), changed: changed), cupertino: true, ), ); expect(find.byKey(runTitleActionKey), findsOneWidget); expect(find.byType(CupertinoButton), findsOneWidget); expect(find.byType(OutlinedButton), findsNothing); expect(gateway.calls, isEmpty); await tester.tap(find.byKey(runTitleActionKey)); await tester.pumpAndSettle(); expect(find.byType(CupertinoAlertDialog), findsOneWidget); expect(find.byType(AlertDialog), findsNothing); expect(_cupertinoFieldText(tester), 'Nightly build'); await tester.enterText(find.byKey(runTitleFieldKey), 'Typed only'); await tester.pump(); expect(gateway.calls, isEmpty); await tester.tap(find.byKey(runTitleCancelKey)); await tester.pumpAndSettle(); expect(find.byType(CupertinoAlertDialog), findsNothing); expect(gateway.calls, isEmpty); await tester.tap(find.byKey(runTitleActionKey)); await tester.pumpAndSettle(); await tester.enterText(find.byKey(runTitleFieldKey), 'Release build'); await tester.tap(find.byKey(runTitleSaveKey)); await tester.pumpAndSettle(); expect(gateway.calls.map((call) => call.type), ['set_run_title']); expect(gateway.calls.single.fields, { 'run_id': 7, 'title': 'Release build', }); expect(changed.single, equals(accepted)); expect(find.byType(CupertinoAlertDialog), findsNothing); }); testWidgets('a blank title is sent exactly and the server refusal is shown', ( tester, ) async { const refusal = 'title must not be blank'; final gateway = FakeTitleGateway( (type, fields) async => throw const ProtocolError(refusal), ); final changed = []; await tester.pumpWidget( titleApp( TitleHost(gateway: gateway, initial: titledRun(), changed: changed), cupertino: false, ), ); await tester.tap(find.byKey(runTitleActionKey)); await tester.pumpAndSettle(); await tester.enterText(find.byKey(runTitleFieldKey), ''); await tester.tap(find.byKey(runTitleSaveKey)); await tester.pumpAndSettle(); expect(gateway.calls.single.fields, {'run_id': 7, 'title': ''}); expect(find.text(refusal), findsOneWidget); expect(find.byKey(runTitleErrorKey), findsOneWidget); expect(_materialFieldText(tester), ''); expect(find.byType(AlertDialog), findsOneWidget); expect(changed, isEmpty); expect( tester.widget(find.byKey(titleHostTitleKey)).data, 'Nightly build', ); expect( tester.widget(find.byKey(runTitleSaveKey)).onPressed, isNotNull, ); }); testWidgets('the prompt is seeded from the stored title, never the prompt', ( tester, ) async { final gateway = FakeTitleGateway( (type, fields) async => {'run_detail': titledRun()}, ); final changed = []; await tester.pumpWidget( titleApp( TitleHost( gateway: gateway, initial: titledRun( title: 'Stored title', prompt: 'prompt fallback must not appear', ), changed: changed, ), cupertino: false, ), ); await tester.tap(find.byKey(runTitleActionKey)); await tester.pumpAndSettle(); expect(_materialFieldText(tester), 'Stored title'); expect(find.text('prompt fallback must not appear'), findsNothing); await tester.tap(find.byKey(runTitleCancelKey)); await tester.pumpAndSettle(); await tester.pumpWidget( titleApp( TitleHost( gateway: gateway, initial: titledRun(title: null, prompt: 'prompt is not a title'), changed: changed, ), cupertino: false, ), ); await tester.tap(find.byKey(runTitleActionKey)); await tester.pumpAndSettle(); expect(_materialFieldText(tester), ''); expect(find.text('prompt is not a title'), findsNothing); await tester.tap(find.byKey(runTitleCancelKey)); await tester.pumpAndSettle(); expect(gateway.calls, isEmpty); expect(changed, isEmpty); }); testWidgets('a malformed run has no rename entry point', (tester) async { final gateway = FakeTitleGateway( (type, fields) async => {'run_detail': titledRun()}, ); await tester.pumpWidget( titleApp( TitleHost( gateway: gateway, initial: {'title': 'No id'}, changed: [], ), cupertino: false, ), ); expect(find.byKey(runTitleActionKey), findsNothing); expect(gateway.calls, isEmpty); expect(runTitleId({'title': 'No id'}), isNull); expect(storedRunTitle({'title': 'Kept'}), 'Kept'); expect(storedRunTitle({}), ''); }); }