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/title_wording.dart'; import 'title_test_support.dart'; Future _pump( WidgetTester tester, FakeTitleGateway gateway, List changed, { bool cupertino = false, double textScale = 1, JsonMap? run, }) async { await tester.pumpWidget( titleApp( TitleHost( gateway: gateway, initial: run ?? titledRun(), changed: changed, ), cupertino: cupertino, textScale: textScale, ), ); } Future _open(WidgetTester tester) async { await tester.tap(find.byKey(runTitleActionKey)); await tester.pumpAndSettle(); } String _fieldText(WidgetTester tester, {required bool cupertino}) { if (cupertino) { return tester .widget(find.byKey(runTitleFieldKey)) .controller! .text; } return tester .widget(find.byKey(runTitleFieldKey)) .controller! .text; } void main() { testWidgets('a failed save keeps the prompt open with the draft intact', ( tester, ) async { const refusal = 'rename refused: the run is locked'; final gateway = FakeTitleGateway( (type, fields) async => throw const ProtocolError(refusal), ); final changed = []; await _pump(tester, gateway, changed); await _open(tester); await tester.enterText(find.byKey(runTitleFieldKey), ' Renamed draft '); 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': ' Renamed draft ', }); expect(find.text(refusal), findsOneWidget); expect(find.byKey(runTitleErrorKey), findsOneWidget); expect(_fieldText(tester, cupertino: false), ' Renamed draft '); expect(find.byType(AlertDialog), findsOneWidget); expect(changed, isEmpty); expect( tester.widget(find.byKey(titleHostTitleKey)).data, 'Nightly build', ); }); testWidgets('a transport failure keeps the server words and the draft', ( tester, ) async { final gateway = FakeTitleGateway( (type, fields) async => throw const NotConnectedError(), ); final changed = []; await _pump(tester, gateway, changed, cupertino: true); await _open(tester); await tester.enterText(find.byKey(runTitleFieldKey), 'Unsent title'); await tester.tap(find.byKey(runTitleSaveKey)); await tester.pumpAndSettle(); expect(find.byKey(runTitleErrorKey), findsOneWidget); expect(_fieldText(tester, cupertino: true), 'Unsent title'); expect(find.byType(CupertinoAlertDialog), findsOneWidget); expect(changed, isEmpty); }); testWidgets('Save cannot send twice while the rename is in flight', ( tester, ) async { final reply = Completer(); final gateway = FakeTitleGateway((type, fields) => reply.future); final changed = []; await _pump(tester, gateway, changed); await _open(tester); await tester.enterText(find.byKey(runTitleFieldKey), 'One title'); final save = find.byKey(runTitleSaveKey); await tester.tap(save); await tester.tap(save); await tester.pump(); expect( gateway.calls.where((call) => call.type == 'set_run_title'), hasLength(1), ); expect(tester.widget(save).onPressed, isNull); expect( tester.widget(find.byKey(runTitleCancelKey)).onPressed, isNull, ); expect(find.byType(AlertDialog), findsOneWidget); reply.complete({'run_detail': renamedRun(title: 'One title')}); await tester.pumpAndSettle(); expect(find.byType(AlertDialog), findsNothing); expect(changed, hasLength(1)); expect(changed.single['title'], 'One title'); }); testWidgets('a late reply after disposal is ignored safely', (tester) async { final reply = Completer(); final gateway = FakeTitleGateway((type, fields) => reply.future); final changed = []; await _pump(tester, gateway, changed); await _open(tester); await tester.tap(find.byKey(runTitleSaveKey)); await tester.pump(); expect(gateway.calls, hasLength(1)); await tester.pumpWidget(const SizedBox()); reply.complete({'run_detail': renamedRun()}); await tester.pump(); expect(tester.takeException(), isNull); expect(changed, isEmpty); }); testWidgets('a reply for a run the widget no longer owns is not delivered', ( tester, ) async { final reply = Completer(); final gateway = FakeTitleGateway((type, fields) => reply.future); final changed = []; await _pump(tester, gateway, changed); await _open(tester); await tester.enterText(find.byKey(runTitleFieldKey), 'Run seven title'); await tester.tap(find.byKey(runTitleSaveKey)); await tester.pump(); await tester.pumpWidget( titleApp( TitleHost( gateway: gateway, initial: titledRun(id: 8, title: 'Another run'), changed: changed, ), cupertino: false, ), ); await tester.pump(); reply.complete({'run_detail': renamedRun(title: 'Run seven title')}); await tester.pumpAndSettle(); expect(tester.takeException(), isNull); expect(gateway.calls.single.fields, { 'run_id': 7, 'title': 'Run seven title', }); expect(changed, isEmpty); expect( tester.widget(find.byKey(titleHostTitleKey)).data, 'Another run', ); }); 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); const refusal = 'the title is too long for this run; shorten it'; for (final cupertino in [false, true]) { final gateway = FakeTitleGateway( (type, fields) async => throw const ProtocolError(refusal), ); await _pump( tester, gateway, [], cupertino: cupertino, textScale: 2, ); await _open(tester); expect(tester.takeException(), isNull, reason: 'cupertino=$cupertino'); expect( cupertino ? find.byType(CupertinoAlertDialog) : find.byType(AlertDialog), findsOneWidget, ); expect(find.text(runTitleFieldLabel), findsOneWidget); expect(gateway.calls, isEmpty); await tester.enterText( find.byKey(runTitleFieldKey), 'A deliberately long title that must scroll rather than overflow', ); await tester.tap(find.byKey(runTitleSaveKey)); await tester.pumpAndSettle(); expect(tester.takeException(), isNull, reason: 'cupertino=$cupertino'); expect(find.text(refusal), findsOneWidget); expect( _fieldText(tester, cupertino: cupertino), 'A deliberately long title that must scroll rather than overflow', ); await tester.tap(find.byKey(runTitleCancelKey)); await tester.pumpAndSettle(); expect(find.byType(AlertDialog), findsNothing); expect(find.byType(CupertinoAlertDialog), findsNothing); } }); }