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'),
|
||||
),
|
||||
),
|
||||
);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,316 @@
|
||||
import { createServer } from 'node:http';
|
||||
import { readFile, mkdir, mkdtemp, rm, writeFile } from 'node:fs/promises';
|
||||
import { spawn } from 'node:child_process';
|
||||
import { once } from 'node:events';
|
||||
import { setTimeout as delay } from 'node:timers/promises';
|
||||
import { join, extname, resolve, sep } from 'node:path';
|
||||
import { tmpdir } from 'node:os';
|
||||
|
||||
// Real-browser acceptance for the create-from-template prompt.
|
||||
//
|
||||
// Usage (from app/):
|
||||
// flutter build web --target=tool/template_create_probe.dart \
|
||||
// -o build/template_create_probe_web
|
||||
// OPENCODE_ARTIFACT_DIR=<dir> node tool/template_create_probe.mjs
|
||||
//
|
||||
// Optional PROBE_WIDTH/PROBE_HEIGHT (viewport), PROBE_SCALE (text scale),
|
||||
// PROBE_HOST (material|cupertino) and PROBE_CAPTURE_PREFIX. The bundle's
|
||||
// fixture gateway is in-process, so this never contacts a real Pipelines
|
||||
// server and never submits to one. Requires Google Chrome.
|
||||
|
||||
const root = resolve(process.env.PROBE_WEB_ROOT ?? 'build/template_create_probe_web');
|
||||
const artifactDir = process.env.OPENCODE_ARTIFACT_DIR;
|
||||
if (!artifactDir) throw new Error('Set OPENCODE_ARTIFACT_DIR for the probe captures');
|
||||
await mkdir(artifactDir, { recursive: true });
|
||||
const width = Number(process.env.PROBE_WIDTH ?? 390);
|
||||
const height = Number(process.env.PROBE_HEIGHT ?? 844);
|
||||
const textScale = process.env.PROBE_SCALE ?? '1';
|
||||
const host = process.env.PROBE_HOST ?? 'material';
|
||||
const capturePrefix = process.env.PROBE_CAPTURE_PREFIX ?? 't1-create';
|
||||
|
||||
const MIME = {
|
||||
'.html': 'text/html',
|
||||
'.js': 'text/javascript',
|
||||
'.json': 'application/json',
|
||||
'.wasm': 'application/wasm',
|
||||
'.png': 'image/png',
|
||||
'.ico': 'image/x-icon',
|
||||
'.otf': 'font/otf',
|
||||
'.ttf': 'font/ttf',
|
||||
};
|
||||
const server = createServer(async (req, res) => {
|
||||
try {
|
||||
const pathname = decodeURIComponent(new URL(req.url, 'http://localhost').pathname);
|
||||
const path = resolve(root, `.${pathname}`);
|
||||
if (path !== root && !path.startsWith(root + sep)) {
|
||||
res.writeHead(403).end();
|
||||
return;
|
||||
}
|
||||
const filePath = path === root || path.endsWith('/') ? join(path, 'index.html') : path;
|
||||
const file = await readFile(filePath);
|
||||
res.writeHead(200, { 'content-type': MIME[extname(filePath)] ?? 'application/octet-stream' }).end(file);
|
||||
} catch {
|
||||
res.writeHead(404).end('not found');
|
||||
}
|
||||
});
|
||||
server.listen(0, '127.0.0.1');
|
||||
await once(server, 'listening');
|
||||
const origin = `http://127.0.0.1:${server.address().port}`;
|
||||
|
||||
const profile = await mkdtemp(join(tmpdir(), 'template-create-chrome-'));
|
||||
const chrome = spawn('/Applications/Google Chrome.app/Contents/MacOS/Google Chrome', [
|
||||
'--headless=new',
|
||||
'--no-first-run',
|
||||
'--no-default-browser-check',
|
||||
'--disable-gpu',
|
||||
'--remote-debugging-port=0',
|
||||
`--user-data-dir=${profile}`,
|
||||
'about:blank',
|
||||
], { stdio: ['ignore', 'ignore', 'pipe'] });
|
||||
let stderr = '';
|
||||
chrome.stderr.on('data', (data) => {
|
||||
stderr += data;
|
||||
});
|
||||
|
||||
async function waitFor(check, label) {
|
||||
for (let attempt = 0; attempt < 160; attempt++) {
|
||||
const value = await check();
|
||||
if (value) return value;
|
||||
await delay(100);
|
||||
}
|
||||
throw new Error(`Timed out waiting for ${label}\n${stderr.slice(-1200)}`);
|
||||
}
|
||||
|
||||
let ws;
|
||||
try {
|
||||
const endpoint = await waitFor(
|
||||
() => stderr.match(/DevTools listening on (ws:\/\/[^\s]+)/)?.[1],
|
||||
'Chrome DevTools',
|
||||
);
|
||||
ws = new WebSocket(endpoint);
|
||||
await once(ws, 'open');
|
||||
let id = 0;
|
||||
const pending = new Map();
|
||||
ws.addEventListener('message', (event) => {
|
||||
const message = JSON.parse(event.data);
|
||||
if (message.method === 'Runtime.exceptionThrown') {
|
||||
console.error('PAGE EXCEPTION', JSON.stringify(message.params?.exceptionDetails?.exception?.description));
|
||||
}
|
||||
if (message.id && pending.has(message.id)) {
|
||||
pending.get(message.id)(message);
|
||||
pending.delete(message.id);
|
||||
}
|
||||
});
|
||||
const send = (method, params = {}, sessionId) =>
|
||||
new Promise((resolveCall) => {
|
||||
const callId = ++id;
|
||||
pending.set(callId, resolveCall);
|
||||
ws.send(JSON.stringify({ id: callId, method, params, ...(sessionId ? { sessionId } : {}) }));
|
||||
});
|
||||
|
||||
const target = (await send('Target.createTarget', { url: 'about:blank' })).result.targetId;
|
||||
const session = (await send('Target.attachToTarget', { targetId: target, flatten: true })).result.sessionId;
|
||||
await send('Page.enable', {}, session);
|
||||
await send('Runtime.enable', {}, session);
|
||||
await send(
|
||||
'Emulation.setDeviceMetricsOverride',
|
||||
{ width, height, deviceScaleFactor: 1, mobile: true },
|
||||
session,
|
||||
);
|
||||
|
||||
const evaluate = async (expression) => {
|
||||
const response = await send('Runtime.evaluate', { expression, returnByValue: true, awaitPromise: true }, session);
|
||||
if (response.error) throw new Error(`Runtime.evaluate failed: ${JSON.stringify(response)}`);
|
||||
if (response.result?.exceptionDetails) {
|
||||
throw new Error(`Page exception: ${JSON.stringify(response.result.exceptionDetails)}`);
|
||||
}
|
||||
return response.result?.result?.value;
|
||||
};
|
||||
|
||||
const semanticText = () =>
|
||||
evaluate(`(() => {
|
||||
const chunks = [];
|
||||
for (const el of document.querySelectorAll('flt-semantics')) {
|
||||
const label = el.getAttribute('aria-label');
|
||||
if (label) chunks.push(label);
|
||||
if (!el.children.length && el.innerText) chunks.push(el.innerText);
|
||||
}
|
||||
for (const el of document.querySelectorAll('input, textarea')) {
|
||||
if (el.getAttribute('aria-label')) chunks.push(el.getAttribute('aria-label'));
|
||||
if (el.value) chunks.push(String(el.value));
|
||||
}
|
||||
if (document.body?.innerText) chunks.push(document.body.innerText);
|
||||
return [...new Set(chunks)].join('\\n');
|
||||
})()`);
|
||||
|
||||
const assertBody = async (text) => {
|
||||
try {
|
||||
await waitFor(
|
||||
async () => (await semanticText()).includes(text),
|
||||
`body to contain ${JSON.stringify(text)}`,
|
||||
);
|
||||
} catch (error) {
|
||||
throw new Error(`${error.message}\nBody now:\n${(await semanticText()).slice(0, 3000)}`);
|
||||
}
|
||||
};
|
||||
|
||||
const clickLabel = async (label) => {
|
||||
const point = await evaluate(`(() => {
|
||||
const needle = ${JSON.stringify(label)};
|
||||
const candidates = [...document.querySelectorAll('flt-semantics')].filter(
|
||||
(el) => (el.getAttribute('aria-label') || el.innerText || '').trim() === needle,
|
||||
);
|
||||
const target = candidates.find((el) =>
|
||||
['button', 'tab', 'link', 'checkbox', 'menuitem'].includes(el.getAttribute('role')),
|
||||
) ?? candidates[0];
|
||||
if (!target) return null;
|
||||
const r = target.getBoundingClientRect();
|
||||
return { x: r.x + r.width / 2, y: r.y + r.height / 2 };
|
||||
})()`);
|
||||
if (!point) {
|
||||
const labels = await evaluate(
|
||||
"[...document.querySelectorAll('flt-semantics')].map((el) => (el.getAttribute('aria-label') || el.innerText || '').trim()).filter(Boolean)",
|
||||
);
|
||||
throw new Error(`Could not click ${JSON.stringify(label)}. Labels: ${JSON.stringify(labels)}`);
|
||||
}
|
||||
await send('Input.dispatchMouseEvent', { type: 'mousePressed', x: point.x, y: point.y, button: 'left', clickCount: 1 }, session);
|
||||
await send('Input.dispatchMouseEvent', { type: 'mouseReleased', x: point.x, y: point.y, button: 'left', clickCount: 1 }, session);
|
||||
await delay(400);
|
||||
};
|
||||
|
||||
// Null while the prompt is closed; true/false is the Create button's state.
|
||||
const createDisabled = () =>
|
||||
evaluate(`(() => {
|
||||
const el = [...document.querySelectorAll('flt-semantics')].find(
|
||||
(node) =>
|
||||
(node.getAttribute('aria-label') || node.innerText || '').trim() === 'Create' &&
|
||||
node.getAttribute('role') === 'button',
|
||||
);
|
||||
return el ? el.getAttribute('aria-disabled') === 'true' : null;
|
||||
})()`);
|
||||
|
||||
const field = () =>
|
||||
evaluate(`(() => {
|
||||
const input = document.querySelector('input');
|
||||
return input
|
||||
? { value: input.value, disabled: input.disabled, label: input.getAttribute('aria-label') }
|
||||
: null;
|
||||
})()`);
|
||||
|
||||
const typeName = async (value) => {
|
||||
const focused = await evaluate(`(() => {
|
||||
const input = document.querySelector('input');
|
||||
if (!input) return false;
|
||||
input.focus();
|
||||
input.select?.();
|
||||
input.setSelectionRange?.(0, (input.value || '').length);
|
||||
return true;
|
||||
})()`);
|
||||
if (!focused) throw new Error('The name field was not exposed as a semantics input');
|
||||
await delay(300);
|
||||
await send('Input.insertText', { text: value }, session);
|
||||
await delay(400);
|
||||
};
|
||||
|
||||
const state = () => evaluate('globalThis.templateCreateProbe ?? null');
|
||||
const capture = async (name) => {
|
||||
const data = (await send('Page.captureScreenshot', { format: 'png' }, session)).result.data;
|
||||
const path = join(artifactDir, `${name}.png`);
|
||||
await writeFile(path, Buffer.from(data, 'base64'));
|
||||
return path;
|
||||
};
|
||||
|
||||
const checks = [];
|
||||
const captures = [];
|
||||
await send('Page.navigate', { url: `${origin}/?scale=${textScale}&host=${host}` }, session);
|
||||
await waitFor(
|
||||
() => evaluate("Boolean(document.querySelector('flt-semantics-placeholder'))"),
|
||||
'semantics placeholder',
|
||||
);
|
||||
await evaluate("document.querySelector('flt-semantics-placeholder')?.click()");
|
||||
await delay(900);
|
||||
await assertBody('Create pipeline from this template');
|
||||
|
||||
await clickLabel('Create pipeline from this template');
|
||||
await waitFor(() => createDisabled().then((value) => value !== null), 'the create prompt');
|
||||
await assertBody('Create from Release workflow');
|
||||
await assertBody('New pipeline name (required)');
|
||||
await assertBody("Creates a new draft pipeline on the server from this template's source; it is not run.");
|
||||
// CupertinoDialogAction does not expose aria-disabled on the web semantics
|
||||
// layer; its disabled state is covered by the widget tests.
|
||||
if (host !== 'cupertino' && (await createDisabled()) !== true) {
|
||||
throw new Error('Create was not disabled with a blank name');
|
||||
}
|
||||
const blank = await field();
|
||||
if (blank?.value !== '' || blank?.disabled !== false) {
|
||||
throw new Error(`The field is not blank and editable: ${JSON.stringify(blank)}`);
|
||||
}
|
||||
if (blank?.label !== 'New pipeline name (required)') {
|
||||
throw new Error(`The name field has no accessible name: ${JSON.stringify(blank)}`);
|
||||
}
|
||||
captures.push(await capture(`${capturePrefix}-blank-name-disabled`));
|
||||
checks.push({ check: 'blank name keeps Create disabled', field: blank });
|
||||
|
||||
await typeName('Probe release');
|
||||
if (host !== 'cupertino' && (await createDisabled()) !== false) {
|
||||
throw new Error('Create stayed disabled after a non-blank name');
|
||||
}
|
||||
await clickLabel('Create');
|
||||
await assertBody('invalid definition: fixture stage "añadir" has no type');
|
||||
const refused = await state();
|
||||
if (refused?.validates !== 1 || refused?.submits !== 0 || refused?.created?.length !== 0) {
|
||||
throw new Error(`A validation refusal sent more than validate: ${JSON.stringify(refused)}`);
|
||||
}
|
||||
if ((await field())?.value !== 'Probe release') {
|
||||
throw new Error(`The typed name was lost: ${JSON.stringify(await field())}`);
|
||||
}
|
||||
captures.push(await capture(`${capturePrefix}-validation-refusal`));
|
||||
checks.push({
|
||||
check: 'validation refusal sent no submit and kept the typed name',
|
||||
validates: refused.validates,
|
||||
submits: refused.submits,
|
||||
created: refused.created.length,
|
||||
field: await field(),
|
||||
});
|
||||
|
||||
// Retrying the same name is the normal correction after a server refusal.
|
||||
await clickLabel('Create');
|
||||
await assertBody('Created "Probe release". It is in the Pipelines list.');
|
||||
const accepted = await state();
|
||||
if (accepted?.validates !== 2 || accepted?.submits !== 1 || accepted?.created?.length !== 1) {
|
||||
throw new Error(`The accepted create was not reported exactly once: ${JSON.stringify(accepted)}`);
|
||||
}
|
||||
if (accepted.lastSubmit?.name !== 'Probe release' || accepted.lastSubmit?.create_only !== true) {
|
||||
throw new Error(`Submit payload drifted: ${JSON.stringify(accepted.lastSubmit)}`);
|
||||
}
|
||||
const verbatim = ' name: "release"\n stages:\n - key: "añadir" 😀 \n';
|
||||
if (accepted.lastSubmit?.source !== accepted.lastSource || accepted.lastSource !== verbatim) {
|
||||
throw new Error(`Source was not sent verbatim: ${JSON.stringify(accepted)}`);
|
||||
}
|
||||
const done = await field();
|
||||
if (done?.disabled !== true) throw new Error(`The field did not lock after success: ${JSON.stringify(done)}`);
|
||||
await assertBody('Close');
|
||||
captures.push(await capture(`${capturePrefix}-accepted-success`));
|
||||
checks.push({
|
||||
check: 'accepted create showed the exact line, reported once and locked the field',
|
||||
validates: accepted.validates,
|
||||
submits: accepted.submits,
|
||||
created: accepted.created.length,
|
||||
lastSubmit: accepted.lastSubmit,
|
||||
field: done,
|
||||
});
|
||||
|
||||
await clickLabel('Close');
|
||||
await waitFor(async () => (await createDisabled()) === null, 'the prompt to close');
|
||||
checks.push({
|
||||
check: 'Close dismissed the prompt without another request',
|
||||
state: await state(),
|
||||
});
|
||||
console.log(JSON.stringify({ ok: true, origin, viewport: { width, height }, textScale, host, checks, captures }, null, 2));
|
||||
} finally {
|
||||
ws?.close();
|
||||
chrome.kill('SIGTERM');
|
||||
await Promise.race([once(chrome, 'exit'), delay(3000)]);
|
||||
server.close();
|
||||
await rm(profile, { recursive: true, force: true });
|
||||
}
|
||||
Reference in New Issue
Block a user