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.
This commit is contained in:
2026-10-21 20:57:30 +01:00
parent 7be4456fbf
commit e69883b5b6
10 changed files with 1711 additions and 0 deletions
@@ -0,0 +1,225 @@
import 'package:flutter/material.dart';
import 'package:flutter/rendering.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 'template_create_test_support.dart';
/// A 360×640 phone: the short viewport a large text scale runs out of room on.
void _shortPhone(WidgetTester tester) {
tester.view.devicePixelRatio = 1;
tester.view.physicalSize = const Size(360, 640);
addTearDown(tester.view.resetPhysicalSize);
addTearDown(tester.view.resetDevicePixelRatio);
}
/// The Material scrollable region between the title and the pinned actions.
Rect _contentViewport(WidgetTester tester) => tester.getRect(
find
.descendant(
of: find.byType(AlertDialog),
matching: find.byType(SingleChildScrollView),
)
.first,
);
void _expectInside(Rect viewport, Rect child, String what) {
expect(
child.top,
greaterThanOrEqualTo(viewport.top - 0.5),
reason: '$what is scrolled above the visible content',
);
expect(
child.bottom,
lessThanOrEqualTo(viewport.bottom + 0.5),
reason: '$what is below the visible content',
);
}
void main() {
testWidgets(
'at 2x on a short phone the full label and a refusal are visible without '
'scrolling',
(tester) async {
_shortPhone(tester);
const refusal = 'invalid definition: fixture stage "añadir" has no type';
final gateway = FakeTemplateCreateGateway(
(type, fields) async => throw const ProtocolError(refusal),
);
await tester.pumpWidget(
templateCreateApp(
TemplateCreateHost(gateway: gateway, onCreated: (_) {}),
textScale: 2,
),
);
await tester.tap(find.byKey(templateCreateOpenKey));
await tester.pumpAndSettle();
expect(tester.takeException(), isNull);
final label = find.text(templateCreateNameLabel);
expect(label, findsOneWidget);
final paragraph = tester.renderObject<RenderParagraph>(label);
expect(paragraph.text.toPlainText(), templateCreateNameLabel);
expect(
paragraph.didExceedMaxLines,
isFalse,
reason: 'the required suffix must not be ellipsized',
);
final unscaled = TextPainter(
text: paragraph.text,
textDirection: paragraph.textDirection,
textScaler: paragraph.textScaler,
)..layout();
expect(
unscaled.width,
greaterThan(paragraph.size.width),
reason: 'at 2x the label wraps instead of shrinking to one line',
);
unscaled.dispose();
await tester.enterText(
find.byKey(templateCreateNameKey),
'Narrow attempt',
);
await tester.pump();
await tester.tap(find.byKey(templateCreateConfirmKey));
await tester.pumpAndSettle();
expect(find.text(refusal), findsOneWidget);
_expectInside(
_contentViewport(tester),
tester.getRect(find.byKey(templateCreateErrorKey)),
'the validation refusal',
);
expect(gateway.calls.map((call) => call.type), ['validate']);
},
);
testWidgets('at 2x on a short phone the accepted line is visible without '
'scrolling', (tester) async {
_shortPhone(tester);
final gateway = FakeTemplateCreateGateway(acceptedTemplateCreate);
final created = <JsonMap>[];
await tester.pumpWidget(
templateCreateApp(
TemplateCreateHost(gateway: gateway, onCreated: created.add),
textScale: 2,
),
);
await tester.tap(find.byKey(templateCreateOpenKey));
await tester.pumpAndSettle();
await tester.enterText(find.byKey(templateCreateNameKey), 'Probe release');
await tester.pump();
await tester.tap(find.byKey(templateCreateConfirmKey));
await tester.pumpAndSettle();
expect(tester.takeException(), isNull);
expect(
find.byKey(templateCreateSuccessKey),
findsOneWidget,
reason: 'the success line is shown, not merely off-screen',
);
_expectInside(
_contentViewport(tester),
tester.getRect(find.byKey(templateCreateSuccessKey)),
'the accepted create line',
);
expect(created.single, {'id': 1, 'name': 'Server name'});
});
testWidgets('a refusal is revealed after the content was scrolled away', (
tester,
) async {
tester.view.devicePixelRatio = 1;
tester.view.physicalSize = const Size(320, 480);
addTearDown(tester.view.resetPhysicalSize);
addTearDown(tester.view.resetDevicePixelRatio);
const refusal = 'invalid stage';
final gateway = FakeTemplateCreateGateway(
(type, fields) async => throw const ProtocolError(refusal),
);
await tester.pumpWidget(
templateCreateApp(
TemplateCreateHost(gateway: gateway, onCreated: (_) {}),
textScale: 3,
),
);
await tester.tap(find.byKey(templateCreateOpenKey));
await tester.pumpAndSettle();
await tester.enterText(find.byKey(templateCreateNameKey), 'Scrolled away');
await tester.pump();
// Scroll the prompt content to its end, as a reader would.
final position = tester
.state<ScrollableState>(
find
.descendant(
of: find.byType(SingleChildScrollView),
matching: find.byType(Scrollable),
)
.first,
)
.position;
expect(
position.maxScrollExtent,
greaterThan(0),
reason: 'the 3x content must overflow for this check to mean anything',
);
position.jumpTo(position.maxScrollExtent);
await tester.pumpAndSettle();
await tester.tap(find.byKey(templateCreateConfirmKey));
await tester.pumpAndSettle();
expect(find.text(refusal), findsOneWidget);
final viewport = _contentViewport(tester);
final feedback = tester.getRect(find.byKey(templateCreateErrorKey));
// At 3x the message can be taller than the viewport; its start must still
// be brought back into view after the content was scrolled away.
expect(
feedback.top,
greaterThanOrEqualTo(viewport.top - 0.5),
reason: 'the refusal is scrolled above the visible content',
);
expect(
feedback.top,
lessThan(viewport.bottom),
reason: 'the refusal start is below the visible content',
);
});
testWidgets('the name field is labelled for screen readers on both hosts', (
tester,
) async {
final handle = tester.ensureSemantics();
for (final cupertino in <bool>[false, true]) {
await tester.pumpWidget(
templateCreateApp(
TemplateCreateHost(
gateway: FakeTemplateCreateGateway(acceptedTemplateCreate),
onCreated: (_) {},
cupertino: cupertino,
),
cupertino: cupertino,
textScale: 2,
),
);
await tester.tap(find.byKey(templateCreateOpenKey));
await tester.pumpAndSettle();
await tester.enterText(find.byKey(templateCreateNameKey), 'Scaled');
await tester.pump();
final data = tester
.getSemantics(find.byKey(templateCreateNameKey))
.getSemanticsData();
expect(data.label, templateCreateNameLabel);
expect(data.value, 'Scaled');
expect(data.flagsCollection.isTextField, isTrue);
}
handle.dispose();
});
}
@@ -0,0 +1,138 @@
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 = <JsonMap>[];
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<FilledButton>(find.byKey(templateCreateConfirmKey))
.onPressed,
isNull,
);
await tester.enterText(find.byKey(templateCreateNameKey), ' ');
await tester.pump();
expect(
tester
.widget<FilledButton>(find.byKey(templateCreateConfirmKey))
.onPressed,
isNull,
);
await tester.enterText(find.byKey(templateCreateNameKey), ' Café ');
await tester.pump();
expect(
tester
.widget<FilledButton>(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<TextField>(find.byKey(templateCreateNameKey)).enabled,
isFalse,
);
expect(find.text('Close'), findsOneWidget);
expect(
tester
.widget<FilledButton>(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 = <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), '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<FilledButton>(find.byKey(templateCreateConfirmKey))
.onPressed,
isNotNull,
);
},
);
}
@@ -0,0 +1,171 @@
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);
});
}
@@ -0,0 +1,127 @@
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/features/templates/data/template_repository.dart';
import 'package:pipelines_app/features/templates/domain/template_entry.dart';
import 'package:pipelines_app/features/templates/ui/template_create_dialog.dart';
import 'package:pipelines_app/features/templates/ui/template_create_wording.dart';
import '../data/template_test_support.dart' show source;
typedef TemplateCreateHandler =
Future<JsonMap> Function(String type, JsonMap fields);
/// A recording gateway whose replies are supplied per request type.
class FakeTemplateCreateGateway implements ApiGateway {
FakeTemplateCreateGateway(this.handler);
final TemplateCreateHandler handler;
final List<({String type, JsonMap fields})> calls = [];
@override
Future<JsonMap> call(String type, [JsonMap fields = const {}]) {
final copy = Map<String, dynamic>.from(fields);
calls.add((type: type, fields: copy));
return handler(type, copy);
}
@override
Stream<JsonMap> get frames => const Stream.empty();
}
const templateCreateFixture = TemplateEntry(
key: 'starter',
name: 'Starter template',
description: 'A fixture template',
source: source,
scheduled: false,
raw: {},
);
const templateCreateOpenKey = ValueKey<String>('open-template-create');
String materialTemplateName(WidgetTester tester) => tester
.widget<TextField>(find.byKey(templateCreateNameKey))
.controller!
.text;
String cupertinoTemplateName(WidgetTester tester) => tester
.widget<CupertinoTextField>(find.byKey(templateCreateNameKey))
.controller!
.text;
Future<JsonMap> acceptedTemplateCreate(String type, JsonMap fields) async {
if (type == 'validate') {
return {'graph': {}};
}
if (type == 'submit') {
return {
'detail': {'id': 1, 'name': 'Server name'},
};
}
throw StateError('Unexpected request $type');
}
/// Opens the prompt from a normal button, like a detail's action would.
class TemplateCreateHost extends StatelessWidget {
const TemplateCreateHost({
super.key,
required this.gateway,
required this.onCreated,
this.cupertino = false,
this.template = templateCreateFixture,
});
final FakeTemplateCreateGateway gateway;
final ValueChanged<JsonMap> onCreated;
final bool cupertino;
final TemplateEntry template;
void _open(BuildContext context) {
showTemplateCreateDialog(
context: context,
template: template,
repository: TemplateRepository(gateway),
cupertino: cupertino,
onCreated: onCreated,
);
}
@override
Widget build(BuildContext context) {
final body = Center(
child: cupertino
? CupertinoButton(
key: templateCreateOpenKey,
onPressed: () => _open(context),
child: const Text('Open'),
)
: FilledButton(
key: templateCreateOpenKey,
onPressed: () => _open(context),
child: const Text('Open'),
),
);
if (cupertino) {
return CupertinoPageScaffold(child: body);
}
return Scaffold(body: body);
}
}
Widget templateCreateApp(
Widget child, {
bool cupertino = false,
double textScale = 1,
}) {
Widget media(BuildContext context, Widget? app) => MediaQuery(
data: MediaQuery.of(
context,
).copyWith(textScaler: TextScaler.linear(textScale)),
child: app!,
);
return cupertino
? CupertinoApp(builder: media, home: child)
: MaterialApp(builder: media, home: child);
}
@@ -0,0 +1,43 @@
import 'package:flutter_test/flutter_test.dart';
import 'package:pipelines_app/core/transport/connection_state.dart';
import 'package:pipelines_app/features/templates/domain/template_entry.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() {
test('the browser wording and name fallbacks stay exact', () {
const unnamed = TemplateEntry(
key: 'starter-key',
name: ' ',
description: '',
source: source,
scheduled: false,
raw: {},
);
expect(templateCreateTitle(unnamed), 'Create from starter-key');
expect(
templateCreateTitle(templateCreateFixture),
'Create from Starter template',
);
expect(
templateCreatedName({'name': ' From server '}, 'Typed'),
'From server',
);
expect(templateCreatedName({'name': ' '}, ' Typed '), 'Typed');
expect(templateCreatedName({}, ' Typed '), 'Typed');
expect(
templateCreateSuccess('Release'),
'Created "Release". It is in the Pipelines list.',
);
expect(
describeTemplateCreateError(const ProtocolError('server words')),
'server words',
);
expect(
describeTemplateCreateError(''),
'The server did not create the pipeline.',
);
});
}