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/templates/ui/template_create_wording.dart'; import '../data/template_test_support.dart' show source; import 'template_create_test_support.dart'; void main() { testWidgets('a submit refusal keeps the prompt and the typed name', ( tester, ) async { const refusal = 'a pipeline named "Release" already exists'; final gateway = FakeTemplateCreateGateway((type, fields) async { if (type == 'validate') return {'graph': {}}; throw const ProtocolError(refusal); }); final created = []; await tester.pumpWidget( templateCreateApp( TemplateCreateHost(gateway: gateway, onCreated: created.add), ), ); await tester.tap(find.byKey(templateCreateOpenKey)); await tester.pumpAndSettle(); await tester.enterText(find.byKey(templateCreateNameKey), 'Release'); await tester.pump(); await tester.tap(find.byKey(templateCreateConfirmKey)); await tester.pumpAndSettle(); expect(gateway.calls.map((call) => call.type), ['validate', 'submit']); expect(find.text(refusal), findsOneWidget); expect(materialTemplateName(tester), 'Release'); expect(created, isEmpty); expect(find.byType(AlertDialog), findsOneWidget); expect(find.text('Cancel'), findsOneWidget); expect( tester.widget(find.byKey(templateCreateNameKey)).enabled, isTrue, ); }); testWidgets('a pending validate guards a double submit', (tester) async { final validate = Completer(); final submit = Completer(); var validates = 0; final gateway = FakeTemplateCreateGateway((type, fields) { if (type == 'validate') { validates++; return validate.future; } return submit.future; }); final created = []; await tester.pumpWidget( templateCreateApp( TemplateCreateHost(gateway: gateway, onCreated: created.add), ), ); await tester.tap(find.byKey(templateCreateOpenKey)); await tester.pumpAndSettle(); await tester.enterText(find.byKey(templateCreateNameKey), 'Only once'); await tester.pump(); await tester.tap(find.byKey(templateCreateConfirmKey)); await tester.tap(find.byKey(templateCreateConfirmKey)); await tester.pump(); expect(validates, 1); expect(gateway.calls.where((call) => call.type == 'submit'), isEmpty); expect(find.text(templateCreateSendingLabel), findsOneWidget); // In flight the prompt cannot be dismissed or edited. expect( tester.widget(find.byKey(templateCreateNameKey)).enabled, isFalse, ); expect( tester.widget(find.byKey(templateCreateCancelKey)).onPressed, isNull, ); await tester.binding.handlePopRoute(); await tester.pump(); expect(find.byType(AlertDialog), findsOneWidget); validate.complete({'graph': {}}); await tester.pump(); await tester.pump(); await tester.tap(find.byKey(templateCreateConfirmKey)); await tester.pump(); expect(gateway.calls.where((call) => call.type == 'submit'), hasLength(1)); expect( gateway.calls.where((call) => call.type == 'validate'), hasLength(1), ); submit.complete({ 'detail': {'id': 2, 'name': 'Only once'}, }); await tester.pumpAndSettle(); expect(created.single, {'id': 2, 'name': 'Only once'}); expect( find.text('Created "Only once". It is in the Pipelines list.'), findsOneWidget, ); }); testWidgets('cupertino prompt uses native controls for the same flow', ( tester, ) async { final gateway = FakeTemplateCreateGateway( (type, fields) async => type == 'validate' ? {'graph': {}} : { 'detail': {'id': 3}, }, ); final created = []; await tester.pumpWidget( templateCreateApp( TemplateCreateHost( gateway: gateway, onCreated: created.add, cupertino: true, ), cupertino: true, ), ); await tester.tap(find.byKey(templateCreateOpenKey)); await tester.pumpAndSettle(); expect(find.byType(CupertinoAlertDialog), findsOneWidget); expect(find.byType(AlertDialog), findsNothing); expect(find.byType(CupertinoTextField), findsOneWidget); expect(find.text(templateCreateNameLabel), findsOneWidget); expect(cupertinoTemplateName(tester), ''); expect( tester .widget(find.byKey(templateCreateConfirmKey)) .onPressed, isNull, ); await tester.enterText(find.byKey(templateCreateNameKey), ' Unicode 🚀 '); await tester.pump(); await tester.tap(find.byKey(templateCreateConfirmKey)); await tester.pumpAndSettle(); expect(gateway.calls.map((call) => call.type), ['validate', 'submit']); expect(gateway.calls[1].fields, { 'name': 'Unicode 🚀', 'source': source, 'create_only': true, }); expect( find.text('Created "Unicode 🚀". It is in the Pipelines list.'), findsOneWidget, ); expect(created.single, {'id': 3}); expect( tester .widget(find.byKey(templateCreateNameKey)) .enabled, isFalse, ); expect(find.text('Close'), findsOneWidget); }); }