Files
pipelines/app/tool/template_create_probe.dart
T

182 lines
5.1 KiB
Dart

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'),
),
// One bounded, scrollable column keeps the opener reachable at 2x text,
// where a long button label is wider than a phone viewport.
child: Center(
child: SingleChildScrollView(
padding: const EdgeInsets.all(24),
child: CupertinoButton.filled(
onPressed: _open,
child: const Text(
'Create pipeline from this template',
textAlign: TextAlign.center,
),
),
),
),
);
}
return Scaffold(
appBar: AppBar(title: const Text('Template create probe')),
body: Center(
child: SingleChildScrollView(
padding: const EdgeInsets.all(24),
child: FilledButton(
key: const ValueKey<String>('template-probe-open'),
onPressed: _open,
child: const Text(
'Create pipeline from this template',
textAlign: TextAlign.center,
),
),
),
),
);
}
}