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( 'material prompt keeps blank Create disabled, Cancel sends nothing and ' 'Create validates then submits the trimmed name and verbatim source', (tester) async { final gateway = FakeTemplateCreateGateway(acceptedTemplateCreate); final created = []; await tester.pumpWidget( templateCreateApp( TemplateCreateHost(gateway: gateway, onCreated: created.add), ), ); await tester.tap(find.byKey(templateCreateOpenKey)); await tester.pumpAndSettle(); expect(find.byType(AlertDialog), findsOneWidget); expect(find.text('Create from Starter template'), findsOneWidget); expect(find.text(templateCreateNameLabel), findsOneWidget); expect(find.text(templateCreateNote), findsOneWidget); expect(materialTemplateName(tester), ''); expect( tester .widget(find.byKey(templateCreateConfirmKey)) .onPressed, isNull, ); await tester.enterText(find.byKey(templateCreateNameKey), ' '); await tester.pump(); expect( tester .widget(find.byKey(templateCreateConfirmKey)) .onPressed, isNull, ); await tester.enterText(find.byKey(templateCreateNameKey), ' Café '); await tester.pump(); expect( tester .widget(find.byKey(templateCreateConfirmKey)) .onPressed, isNotNull, ); await tester.tap(find.byKey(templateCreateCancelKey)); await tester.pumpAndSettle(); expect(find.byType(AlertDialog), findsNothing); expect(gateway.calls, isEmpty); expect(created, isEmpty); await tester.tap(find.byKey(templateCreateOpenKey)); await tester.pumpAndSettle(); expect(materialTemplateName(tester), ''); await tester.enterText(find.byKey(templateCreateNameKey), ' Café '); await tester.pump(); await tester.tap(find.byKey(templateCreateConfirmKey)); await tester.pumpAndSettle(); expect(gateway.calls.map((call) => call.type), ['validate', 'submit']); expect(gateway.calls[0].fields, {'source': source}); expect(gateway.calls[1].fields, { 'name': 'Café', 'source': source, 'create_only': true, }); expect(gateway.calls[1].fields.keys, ['name', 'source', 'create_only']); expect( find.text('Created "Server name". It is in the Pipelines list.'), findsOneWidget, ); expect(created.single, {'id': 1, 'name': 'Server name'}); expect( tester.widget(find.byKey(templateCreateNameKey)).enabled, isFalse, ); expect(find.text('Close'), findsOneWidget); expect( tester .widget(find.byKey(templateCreateConfirmKey)) .onPressed, isNull, ); await tester.tap(find.byKey(templateCreateCancelKey)); await tester.pumpAndSettle(); expect(find.byType(AlertDialog), findsNothing); expect(created, hasLength(1)); }, ); testWidgets( 'a validation refusal sends no submit, keeps the typed name and shows ' 'the server error', (tester) async { const refusal = 'invalid stage "añadir": unknown key'; final gateway = FakeTemplateCreateGateway( (type, fields) async => 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), 'Keep me'); await tester.pump(); await tester.tap(find.byKey(templateCreateConfirmKey)); await tester.pumpAndSettle(); expect(gateway.calls.map((call) => call.type), ['validate']); expect(gateway.calls.single.fields, {'source': source}); expect(find.text(refusal), findsOneWidget); expect(find.byKey(templateCreateErrorKey), findsOneWidget); expect(find.byKey(templateCreateSuccessKey), findsNothing); expect(materialTemplateName(tester), 'Keep me'); expect(created, isEmpty); expect(find.byType(AlertDialog), findsOneWidget); expect( tester .widget(find.byKey(templateCreateConfirmKey)) .onPressed, isNotNull, ); }, ); }