Files
pipelines/app/test/features/templates/ui/template_create_flow_test.dart
T
alan e69883b5b6 feat(app): add native create-from-template prompt
Validate the template source first, then submit the typed name create-only,
matching the browser's P5 flow and wording. Refusals keep the prompt and the
typed name; the accepted detail is reported once through the callback. The
labelled field, visible feedback and native Material/Cupertino controls are
covered by focused widget tests and a loopback browser probe.
2026-10-21 20:57:30 +01:00

172 lines
5.5 KiB
Dart

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 = <JsonMap>[];
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<TextField>(find.byKey(templateCreateNameKey)).enabled,
isTrue,
);
});
testWidgets('a pending validate guards a double submit', (tester) async {
final validate = Completer<JsonMap>();
final submit = Completer<JsonMap>();
var validates = 0;
final gateway = FakeTemplateCreateGateway((type, fields) {
if (type == 'validate') {
validates++;
return validate.future;
}
return submit.future;
});
final created = <JsonMap>[];
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<TextField>(find.byKey(templateCreateNameKey)).enabled,
isFalse,
);
expect(
tester.widget<TextButton>(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 = <JsonMap>[];
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<CupertinoDialogAction>(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<CupertinoTextField>(find.byKey(templateCreateNameKey))
.enabled,
isFalse,
);
expect(find.text('Close'), findsOneWidget);
});
}