import 'dart:async'; import 'package:pipelines_app/core/api_gateway.dart'; /// Deterministic in-memory data for the browser fixture preview. This is not a /// live connection and is used only by `tool/pipelines_probe_entry.dart`. class PipelinesProbeGateway implements ApiGateway { PipelinesProbeGateway({this.delay = const Duration(milliseconds: 120)}); final Duration delay; static const _pipelines = >[ { 'id': 3, 'name': 'Release verification', 'version': 3, 'stages': 4, 'created_at': '2026-09-28T10:12:00', 'updated_at': '2026-10-21T09:40:00', 'scheduled': true, 'schedule_count': 2, 'enabled_schedule_count': 1, }, { 'id': 7, 'name': 'Nightly documentation', 'version': 1, 'stages': 2, 'created_at': '2026-10-02T18:20:00', 'updated_at': '2026-10-20T22:05:00', 'scheduled': true, 'schedule_count': 1, 'enabled_schedule_count': 1, }, { 'id': 11, 'name': 'Hotfix triage', 'version': 2, 'stages': 3, 'created_at': '2026-10-11T08:00:00', 'updated_at': '2026-10-19T16:32:00', 'scheduled': false, 'schedule_count': 0, 'enabled_schedule_count': 0, }, { 'id': 14, 'name': 'Legacy import (no schedule metadata)', 'version': 1, 'stages': 1, 'created_at': '2026-08-04T11:45:00', 'updated_at': '2026-10-14T12:10:00', 'scheduled': null, 'schedule_count': null, 'enabled_schedule_count': null, }, { 'id': 21, 'name': 'Unparsed definition', 'version': 1, 'stages': 1, 'created_at': '2026-07-19T09:05:00', 'updated_at': '2026-09-30T14:00:00', 'scheduled': false, 'schedule_count': 0, 'enabled_schedule_count': 0, }, { 'id': 22, 'name': 'Empty definition', 'version': 1, 'stages': 0, 'created_at': '2026-06-02T16:30:00', 'updated_at': '2026-08-22T10:20:00', 'scheduled': false, 'schedule_count': 0, 'enabled_schedule_count': 0, }, ]; static const _runs = >[ { 'id': 488, 'pipeline_id': 3, 'status': 'succeeded', 'created_at': '2026-10-21T09:30:00', 'finished_at': '2026-10-21T09:34:12', 'executing': false, }, { 'id': 487, 'pipeline_id': 7, 'status': 'running', 'created_at': '2026-10-21T09:20:00', 'finished_at': '', 'executing': true, }, { 'id': 486, 'pipeline_id': 11, 'status': 'failed', 'created_at': '2026-10-19T16:20:00', 'finished_at': '2026-10-19T16:28:40', 'executing': false, }, { 'id': 480, 'pipeline_id': 3, 'status': 'waiting', 'created_at': '2026-10-18T13:00:00', 'finished_at': '', 'executing': false, }, ]; static const _versionRows = >>{ 3: [ {'version': 1, 'stages': 2, 'created_at': '2026-09-28T10:12:00'}, {'version': 2, 'stages': 3, 'created_at': '2026-10-08T15:44:00'}, {'version': 3, 'stages': 4, 'created_at': '2026-10-21T09:12:00'}, ], 7: [ {'version': 1, 'stages': 2, 'created_at': '2026-10-02T18:20:00'}, ], 11: [ {'version': 1, 'stages': 2, 'created_at': '2026-10-11T08:00:00'}, {'version': 2, 'stages': 3, 'created_at': '2026-10-18T09:30:00'}, ], 14: [ {'version': 1, 'stages': 1, 'created_at': '2026-08-04T11:45:00'}, ], 21: [ {'version': 1, 'stages': 1, 'created_at': '2026-07-19T09:05:00'}, ], 22: [ {'version': 1, 'stages': 0, 'created_at': '2026-06-02T16:30:00'}, ], }; /// The pinned definition for each fixture pipeline, in the server's own /// `graph` shape. Only the definitions are here: no run evidence is present. static const _graphs = >{ 3: { 'name': 'Release verification', 'stages': [ { 'key': 'inspect', 'type': 'model', 'prompt': 'Inspect the candidate release.', 'output': 'report', 'edges': {'pass': 'verify', 'fail': '#failure'}, }, { 'key': 'verify', 'type': 'model', 'prompt': 'Verify the report against the checklist.', 'output': 'verdict', 'edges': {'pass': 'package', 'fail': '#failure'}, }, { 'key': 'package', 'type': 'human', 'prompt': 'Approve the package.', 'output': 'approval', 'edges': {'approve': 'publish', 'hold': '#success'}, }, { 'key': 'publish', 'type': 'model', 'prompt': 'Publish the release.', 'output': 'release', 'edges': {'done': '#success'}, }, ], }, 7: { 'name': 'Nightly documentation', 'stages': [ { 'key': 'collect', 'type': 'model', 'edges': {'done': 'write'}, }, { 'key': 'write', 'type': 'model', 'edges': {'done': '#success'}, }, ], }, 11: { 'name': 'Hotfix triage', 'stages': [ { 'key': 'classify', 'type': 'model', 'edges': {'urgent': 'patch', 'normal': 'document'}, }, { 'key': 'patch', 'type': 'model', 'edges': {'done': '#success'}, }, { 'key': 'document', 'type': 'human', 'edges': {'done': '#success'}, }, ], }, 14: { 'name': 'Legacy import', 'stages': [ { 'key': 'import', 'type': 'model', 'edges': {'done': '#success'}, }, ], }, // 21 intentionally has no entry: the reply carries no definition graph. 22: {'name': 'Empty definition', 'stages': []}, }; static const _sources = { 3: ''' name: Release verification stages: inspect: type: model output: report edges: pass: verify fail: '#failure' verify: type: model output: verdict edges: pass: package fail: '#failure' package: type: human output: approval edges: approve: publish hold: '#success' publish: type: model output: release edges: done: '#success' ''', 7: ''' name: Nightly documentation stages: collect: type: model output: notes edges: done: write write: type: model output: report edges: done: '#success' ''', 11: ''' name: Hotfix triage stages: classify: type: model output: severity edges: urgent: patch normal: document patch: type: model output: change edges: done: '#success' document: type: human output: note edges: done: '#success' ''', 14: ''' name: Legacy import stages: import: type: model output: summary edges: done: '#success' ''', 21: ''' name: Unparsed definition stages: import: type: model output: summary edges: done: '#success' ''', 22: ''' name: Empty definition stages: {} ''', }; @override Stream get frames => const Stream.empty(); @override Future call(String type, [JsonMap fields = const {}]) async { if (delay > Duration.zero) await Future.delayed(delay); switch (type) { case 'list_pipelines': return {'type': 'pipelines', 'pipelines': _pipelines}; case 'list_runs': return {'type': 'runs', 'runs': _runs}; case 'get_pipeline': return _detail( fields['pipeline_id'] as int? ?? 0, fields['version'] as int? ?? 0, ); } return {'type': 'error', 'error': 'The fixture does not answer $type.'}; } JsonMap _detail(int id, int requestedVersion) { final summary = _pipelines.where((pipeline) => pipeline['id'] == id).first; final versions = _versionRows[id] ?? const >[]; final latest = versions.isEmpty ? summary['version'] as int : versions.last['version'] as int; final resolved = requestedVersion > 0 ? requestedVersion : latest; final version = versions.firstWhere( (row) => row['version'] == resolved, orElse: () => versions.isEmpty ? summary : versions.last, ); return { 'type': 'pipeline', 'detail': { ...summary, 'version': resolved, 'stages': version['stages'] as int, 'latest_version': latest, 'version_created_at': _versionCreatedAt(versions, resolved), 'content_hash': 'sha256:fixture-$id-v$resolved', 'source': '${_sources[id]}\n# selected version $resolved\n', 'graph': _graphs[id], 'versions': versions, }, }; } static String _versionCreatedAt( List> versions, int resolved, ) { for (final version in versions) { if (version['version'] == resolved) { return version['created_at'] as String; } } return versions.isEmpty ? '' : versions.last['created_at'] as String; } }