import 'dart:async'; 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/screen.dart'; /// Browser capture entrypoint for the reusable Templates gallery. /// /// The fixture gateway is in-process and records every request; nothing here /// opens a socket or contacts a real server. Query parameters: /// `host=material|cupertino`, `scale=` and /// `scenario=default|empty|refresh-refusal`. The state published at /// `globalThis.templatesGalleryProbe` is read by the driver. const _source = ' name: "release"\n\tstages:\n - key: "añadir" 😀 \n'; const _galleryRows = [ { 'key': 'release', 'name': 'Release workflow', 'description': 'Ship a version', 'source': _source, 'scheduled': true, }, { 'key': 'blank', 'name': '', 'description': 'No display name', 'source': 'name: "unnamed"\n', 'scheduled': false, }, ]; class _ProbeGateway implements ApiGateway { _ProbeGateway( this.publish, { required this.empty, required this.failingRefresh, }); final VoidCallback publish; final bool empty; final bool failingRefresh; int listCalls = 0; int validates = 0; int submits = 0; final List created = []; String? lastSource; JsonMap? lastSubmit; @override Stream get frames => const Stream.empty(); @override Future call(String type, [JsonMap fields = const {}]) async { if (type == 'list_templates') { listCalls++; publish(); if (failingRefresh) { // The catalogue loads, then a Refresh from the open template refuses so // the driver can prove the failure is visible above that detail. if (listCalls == 2) { await Future.delayed(const Duration(milliseconds: 250)); throw const ProtocolError('fixture refresh unavailable'); } return {'templates': _galleryRows}; } if (listCalls == 1 && !empty) { // The first read refuses, so the driver can exercise the honest error // state and Retry on a real host. await Future.delayed(const Duration(milliseconds: 250)); throw const ProtocolError('fixture catalogue unavailable'); } if (listCalls == 2) { // Enough time to capture the loading state after Retry. await Future.delayed(const Duration(milliseconds: 1200)); } if (empty) return {'templates': const []}; return { 'templates': [ ..._galleryRows, if (listCalls >= 3) { 'key': 'added', 'name': 'Added after refresh', 'description': 'Proves the catalogue was re-read', 'source': 'name: "added"\n', 'scheduled': false, }, ], }; } if (type == 'validate') { validates++; lastSource = fields['source'] as String?; publish(); await Future.delayed(const Duration(milliseconds: 200)); return { 'graph': { 'stages': const [ {'key': 'release'}, ], }, }; } if (type == 'submit') { submits++; lastSubmit = Map.from(fields); publish(); return { 'detail': {'id': 41, 'name': 'Probe created'}, }; } throw StateError('The templates probe does not implement $type'); } } void main() => runApp(const _ProbeApp()); class _ProbeApp extends StatefulWidget { const _ProbeApp(); @override State<_ProbeApp> createState() => _ProbeAppState(); } class _ProbeAppState extends State<_ProbeApp> { late final _params = Uri.base.queryParameters; late final _cupertino = _params['host'] == 'cupertino'; late final _scale = double.tryParse(_params['scale'] ?? '') ?? 1; late final _gateway = _ProbeGateway( _publish, empty: _params['scenario'] == 'empty', failingRefresh: _params['scenario'] == 'refresh-refusal', ); void _publish() { globalContext.setProperty( 'templatesGalleryProbe'.toJS, { 'listCalls': _gateway.listCalls, 'validates': _gateway.validates, 'submits': _gateway.submits, 'created': _gateway.created, 'lastSource': _gateway.lastSource, 'lastSubmit': _gateway.lastSubmit, }.jsify(), ); } void _created(JsonMap detail) { _gateway.created.add(detail); _publish(); } @override Widget build(BuildContext context) { Widget scaled(BuildContext context, Widget? app) => MediaQuery( data: MediaQuery.of( context, ).copyWith(textScaler: TextScaler.linear(_scale)), child: app!, ); final screen = TemplatesScreen(gateway: _gateway, onCreated: _created); if (_cupertino) { return CupertinoApp( theme: CupertinoThemeData(brightness: Brightness.dark), builder: scaled, home: screen, ); } return MaterialApp( theme: ThemeData(brightness: Brightness.dark), builder: scaled, home: screen, ); } }