Files
pipelines/app/tool/schedule_pipeline_probe_app.dart
T

209 lines
6.0 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/features/schedules/ui/pipeline/schedule_pipeline_section.dart';
import 'package:pipelines_app/features/settings/settings_store.dart';
/// Isolated browser fixture for the pipeline Summary schedule section.
///
/// flutter build web -t tool/schedule_pipeline_probe_app.dart
///
/// Query: `host=cupertino`, `brightness=dark`, `scale=2`. The fake gateway keeps
/// its schedules in memory, accepts one create, refuses the first edit with a
/// stale revision, then accepts the retry. It never opens a socket or contacts a
/// real server. State is published at `globalThis.schedulePipelineProbe`.
void main() => runApp(const _ProbeApp());
class _Refusal implements Exception {
const _Refusal();
@override
String toString() => 'conflict: revision 3 is stale';
}
class _ProbeApp extends StatelessWidget {
const _ProbeApp();
@override
Widget build(BuildContext context) {
final parameters = Uri.base.queryParameters;
final cupertino = parameters['host'] == 'cupertino';
final dark = parameters['brightness'] == 'dark';
final scale = double.tryParse(parameters['scale'] ?? '') ?? 1.0;
Widget media(BuildContext context, Widget? app) => MediaQuery(
data: MediaQuery.of(
context,
).copyWith(textScaler: TextScaler.linear(scale)),
child: app!,
);
final gateway = _FixtureGateway();
final section = SchedulePipelineSection(
gateway: gateway,
pipelineId: 4,
version: 3,
settingsStore: MemorySettingsStore(workspace: '/srv/research'),
);
final body = SafeArea(
child: ListView(children: [section, const SizedBox(height: 24)]),
);
if (cupertino) {
return CupertinoApp(
debugShowCheckedModeBanner: false,
theme: CupertinoThemeData(
brightness: dark ? Brightness.dark : Brightness.light,
),
builder: media,
home: CupertinoPageScaffold(
navigationBar: const CupertinoNavigationBar(
middle: Text('Alpha review'),
),
child: body,
),
);
}
return MaterialApp(
debugShowCheckedModeBanner: false,
theme: ThemeData(
brightness: dark ? Brightness.dark : Brightness.light,
platform: TargetPlatform.android,
),
builder: media,
home: Scaffold(
appBar: AppBar(title: const Text('Alpha review')),
body: body,
),
);
}
}
class _FixtureGateway implements ApiGateway {
_FixtureGateway() {
_publish();
}
final List<Map<String, dynamic>> _rows = [
{
'id': 8,
'revision': 3,
'name': 'Research batteries',
'pipeline_id': 4,
'version': 3,
'enabled': true,
'input': {'prompt': 'Find battery chemistry sources'},
'workspace': '/srv/research',
'rule': {
'every': '24h',
'days': ['mon', 'wed'],
'timezone': 'Europe/London',
},
'next_due': '2026-10-22T09:00:00Z',
},
{
'id': 9,
'revision': 1,
'name': 'Nightly report',
'pipeline_id': 4,
'version': 3,
'enabled': false,
'input': {'prompt': 'Summarise the open issues'},
'workspace': '/srv/reports',
'rule': {
'times': ['10:00', '18:30'],
'days': <String>[],
'timezone': 'Local',
},
},
];
int _nextId = 10;
int _lists = 0;
int _saves = 0;
int _deletes = 0;
bool _refuseEdit = true;
JsonMap? _lastSaved;
JsonMap? _lastDeleted;
String? _lastRequestKey;
void _publish() {
globalContext.setProperty(
'schedulePipelineProbe'.toJS,
<String, Object?>{
'lists': _lists,
'saves': _saves,
'deletes': _deletes,
'lastSaved': _lastSaved,
'lastDeleted': _lastDeleted,
'lastRequestKey': _lastRequestKey,
'rows': [
for (final row in _rows)
{
'id': row['id'],
'revision': row['revision'],
'name': row['name'],
'enabled': row['enabled'],
},
],
}.jsify(),
);
}
@override
Future<JsonMap> call(String type, [JsonMap fields = const {}]) async {
switch (type) {
case 'list_schedules':
_lists++;
_publish();
return {
'type': 'schedules',
'schedules': [
for (final row in _rows) Map<String, dynamic>.from(row),
],
};
case 'save_schedule':
_saves++;
final sent = Map<String, dynamic>.from(fields['schedule'] as Map);
_lastSaved = sent;
_lastRequestKey = fields['request_key'] as String?;
if (sent['id'] == 0) {
final stored = {
...sent,
'id': _nextId++,
'revision': 1,
'next_due': '2026-10-23T09:00:00Z',
};
_rows.add(stored);
_publish();
return {'type': 'schedule', 'schedule': stored};
}
if (_refuseEdit && sent['id'] == 8) {
_refuseEdit = false;
_publish();
throw const _Refusal();
}
final index = _rows.indexWhere((row) => row['id'] == sent['id']);
final stored = {
...sent,
'revision': (sent['revision'] as int) + 1,
'next_due': sent['enabled'] == true ? '2026-10-23T09:00:00Z' : null,
};
if (index >= 0) _rows[index] = stored;
_publish();
return {'type': 'schedule', 'schedule': stored};
case 'delete_schedule':
_deletes++;
final id = fields['schedule_id'];
_lastDeleted = Map<String, dynamic>.from(fields);
_rows.removeWhere((row) => row['id'] == id);
_publish();
return {'type': 'schedule_deleted', 'schedule_id': id};
default:
throw StateError('unexpected $type');
}
}
@override
Stream<JsonMap> get frames => const Stream.empty();
}