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:
@@ -0,0 +1,167 @@
|
||||
import 'dart:js_interop';
|
||||
import 'dart:js_interop_unsafe';
|
||||
|
||||
import 'package:flutter/cupertino.dart';
|
||||
import 'package:flutter/material.dart';
|
||||
|
||||
import 'package:pipelines_app/core/api_gateway.dart';
|
||||
import 'package:pipelines_app/core/transport/connection_state.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';
|
||||
|
||||
/// Browser capture entrypoint for the create-from-template prompt.
|
||||
///
|
||||
/// The fixture gateway is in-process and records every request; nothing here
|
||||
/// opens a socket or contacts a real server. The state published at
|
||||
/// `globalThis.templateCreateProbe` is read by the probe driver, so it can
|
||||
/// prove that a validation refusal sent no submit and that an accepted create
|
||||
/// was reported once.
|
||||
const _source = ' name: "release"\n stages:\n - key: "añadir" 😀 \n';
|
||||
|
||||
class _ProbeGateway implements ApiGateway {
|
||||
_ProbeGateway(this.publish);
|
||||
|
||||
final VoidCallback publish;
|
||||
int validates = 0;
|
||||
int submits = 0;
|
||||
bool failNextValidate = true;
|
||||
String? lastSource;
|
||||
JsonMap? lastSubmit;
|
||||
final List<JsonMap> created = [];
|
||||
|
||||
@override
|
||||
Stream<JsonMap> get frames => const Stream.empty();
|
||||
|
||||
@override
|
||||
Future<JsonMap> call(String type, [JsonMap fields = const {}]) async {
|
||||
if (type == 'validate') {
|
||||
validates++;
|
||||
lastSource = fields['source'] as String?;
|
||||
publish();
|
||||
if (failNextValidate) {
|
||||
failNextValidate = false;
|
||||
throw const ProtocolError(
|
||||
'invalid definition: fixture stage "añadir" has no type',
|
||||
);
|
||||
}
|
||||
return {
|
||||
'graph': {'stages': []},
|
||||
};
|
||||
}
|
||||
if (type == 'submit') {
|
||||
submits++;
|
||||
lastSubmit = Map<String, dynamic>.from(fields);
|
||||
publish();
|
||||
return {
|
||||
'detail': {'id': 71, 'name': 'Probe release'},
|
||||
};
|
||||
}
|
||||
throw StateError('The create probe does not implement $type');
|
||||
}
|
||||
}
|
||||
|
||||
void main() => runApp(const _ProbeApp());
|
||||
|
||||
class _ProbeApp extends StatelessWidget {
|
||||
const _ProbeApp();
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
final params = Uri.base.queryParameters;
|
||||
final cupertino = params['host'] == 'cupertino';
|
||||
final scale = double.tryParse(params['scale'] ?? '') ?? 1;
|
||||
Widget scaled(BuildContext context, Widget? app) => MediaQuery(
|
||||
data: MediaQuery.of(
|
||||
context,
|
||||
).copyWith(textScaler: TextScaler.linear(scale)),
|
||||
child: app!,
|
||||
);
|
||||
if (cupertino) {
|
||||
return CupertinoApp(
|
||||
builder: scaled,
|
||||
home: const _ProbeHome(cupertino: true),
|
||||
);
|
||||
}
|
||||
return MaterialApp(
|
||||
theme: ThemeData(platform: TargetPlatform.android),
|
||||
builder: scaled,
|
||||
home: const _ProbeHome(cupertino: false),
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
class _ProbeHome extends StatefulWidget {
|
||||
const _ProbeHome({required this.cupertino});
|
||||
|
||||
final bool cupertino;
|
||||
|
||||
@override
|
||||
State<_ProbeHome> createState() => _ProbeHomeState();
|
||||
}
|
||||
|
||||
class _ProbeHomeState extends State<_ProbeHome> {
|
||||
late final _gateway = _ProbeGateway(_publish);
|
||||
|
||||
static const _template = TemplateEntry(
|
||||
key: 'release',
|
||||
name: 'Release workflow',
|
||||
description: 'Fixture template',
|
||||
source: _source,
|
||||
scheduled: false,
|
||||
raw: {},
|
||||
);
|
||||
|
||||
void _publish() {
|
||||
globalContext.setProperty(
|
||||
'templateCreateProbe'.toJS,
|
||||
<String, Object?>{
|
||||
'validates': _gateway.validates,
|
||||
'submits': _gateway.submits,
|
||||
'lastSource': _gateway.lastSource,
|
||||
'lastSubmit': _gateway.lastSubmit,
|
||||
'created': _gateway.created,
|
||||
}.jsify(),
|
||||
);
|
||||
}
|
||||
|
||||
Future<void> _open() async {
|
||||
await showTemplateCreateDialog(
|
||||
context: context,
|
||||
template: _template,
|
||||
repository: TemplateRepository(_gateway),
|
||||
cupertino: widget.cupertino,
|
||||
onCreated: (detail) {
|
||||
_gateway.created.add(detail);
|
||||
_publish();
|
||||
},
|
||||
);
|
||||
}
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
if (widget.cupertino) {
|
||||
return CupertinoPageScaffold(
|
||||
navigationBar: const CupertinoNavigationBar(
|
||||
middle: Text('Template create probe'),
|
||||
),
|
||||
child: Center(
|
||||
child: CupertinoButton.filled(
|
||||
onPressed: _open,
|
||||
child: const Text('Create pipeline from this template'),
|
||||
),
|
||||
),
|
||||
);
|
||||
}
|
||||
return Scaffold(
|
||||
appBar: AppBar(title: const Text('Template create probe')),
|
||||
body: Center(
|
||||
child: FilledButton(
|
||||
key: const ValueKey<String>('template-probe-open'),
|
||||
onPressed: _open,
|
||||
child: const Text('Create pipeline from this template'),
|
||||
),
|
||||
),
|
||||
);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user