import 'dart:async'; import 'package:pipelines_app/core/api_gateway.dart'; /// One recorded gateway call, so tests can assert exact request shapes. class GatewayCall { GatewayCall(this.type, this.fields); final String type; final JsonMap fields; @override String toString() => '$type $fields'; } /// A hand-written gateway. Tests decide every reply; nothing here touches a /// server, socket or shared state. class FakeGateway implements ApiGateway { FakeGateway(this.handler); final FutureOr Function(String type, JsonMap fields) handler; final List calls = []; @override Future call(String type, [JsonMap fields = const {}]) async { calls.add(GatewayCall(type, Map.from(fields))); return handler(type, fields); } @override Stream get frames => const Stream.empty(); List callsOf(String type) => calls.where((call) => call.type == type).toList(growable: false); } Map pipelineSummary({ required int id, required String name, int version = 1, int stages = 1, String updatedAt = '2026-10-17T21:26:00', int? scheduleCount = 0, bool? scheduled = false, }) => { 'id': id, 'name': name, 'version': version, 'stages': stages, 'created_at': '2026-10-01T09:00:00', 'updated_at': updatedAt, 'scheduled': scheduled, 'schedule_count': scheduleCount, 'enabled_schedule_count': scheduleCount, }; Map pipelineDetail({ required int id, required String name, required int version, required int latestVersion, String source = 'name: alpha', int stages = 1, String contentHash = 'abc123', int? scheduleCount = 0, bool? scheduled = false, List> versions = const [], Map? graph, Map? shape, }) => { ...pipelineSummary( id: id, name: name, version: version, stages: stages, scheduleCount: scheduleCount, scheduled: scheduled, ), 'latest_version': latestVersion, 'version_created_at': '2026-10-17T21:24:00', 'content_hash': contentHash, 'source': source, 'versions': versions, 'graph': ?graph, 'shape': ?shape, }; /// The two-stage definition pinned into the Alpha detail fixture. Map reviewGraph() => { 'name': 'Alpha review', 'settings': {'schedules': []}, 'stages': [ { 'key': 'review', 'type': 'model', 'prompt': 'Review the change.', 'output': 'notes', 'edges': {'success': 'publish', 'error': '#'}, 'fallback_error': true, 'future_field': {'kept': true}, }, { 'key': 'publish', 'type': 'model', 'output': 'release', 'edges': {'success': '#success', 'error': '#failure'}, }, ], }; /// The one-stage definition pinned by Alpha review v1. /// /// A real version pin never reuses the newer graph, so the fixture keeps a /// distinct v1 definition too: a version switch must not keep a newer node's /// selection merely because the node key survived. Map reviewGraphV1() => { 'name': 'Alpha review', 'settings': {'schedules': []}, 'stages': [ { 'key': 'review', 'type': 'model', 'prompt': 'Review the change.', 'output': 'notes', 'edges': {'success': '#success', 'error': '#'}, }, ], }; Map versionSummary( int version, { int stages = 1, String createdAt = '2026-10-17T21:24:00', }) => {'version': version, 'stages': stages, 'created_at': createdAt}; Map pipelineRun({ required int id, required int pipelineId, String status = 'succeeded', String createdAt = '2026-10-17T19:00:00', String finishedAt = '2026-10-17T19:01:05', bool executing = false, }) => { 'id': id, 'pipeline_id': pipelineId, 'status': status, 'created_at': createdAt, 'finished_at': finishedAt, 'executing': executing, }; JsonMap pipelineListReply(List> pipelines) => { 'type': 'pipelines', 'pipelines': pipelines, }; JsonMap pipelineDetailReply(Map detail) => { 'type': 'pipeline', 'detail': detail, }; JsonMap runListReply(List> runs) => { 'type': 'runs', 'runs': runs, }; JsonMap errorReply(String message) => {'type': 'error', 'error': message}; /// A small complete fixture used by widget/browser-facing tests. class PipelineFixture { PipelineFixture({this.now}); final DateTime? now; late final List> summaries = [ pipelineSummary( id: 1, name: 'Alpha review', version: 2, stages: 2, scheduleCount: 1, scheduled: true, ), pipelineSummary( id: 2, name: 'Beta deploy', version: 1, stages: 1, updatedAt: '2026-10-16T08:05:00', scheduleCount: 0, scheduled: false, ), pipelineSummary( id: 3, name: 'Gamma legacy', version: 1, stages: 3, updatedAt: '2026-10-15T12:00:00', scheduleCount: null, scheduled: null, ), ]; late final List> details = [ pipelineDetail( id: 1, name: 'Alpha review', version: 2, latestVersion: 2, stages: 2, contentHash: 'hash-alpha', scheduleCount: 1, scheduled: true, source: 'name: Alpha review\nstages:\n review:\n type: model', versions: [versionSummary(1, stages: 1), versionSummary(2, stages: 2)], graph: reviewGraph(), shape: reviewGraph(), ), pipelineDetail( id: 2, name: 'Beta deploy', version: 1, latestVersion: 1, contentHash: 'hash-beta', source: 'name: Beta deploy\nstages:\n deploy:\n type: human', versions: [versionSummary(1)], ), pipelineDetail( id: 3, name: 'Gamma legacy', version: 1, latestVersion: 1, stages: 3, contentHash: 'hash-gamma', scheduleCount: null, scheduled: null, source: 'name: Gamma legacy', versions: [versionSummary(1, stages: 3)], ), ]; late final List> runs = [ pipelineRun(id: 42, pipelineId: 1), pipelineRun( id: 41, pipelineId: 2, status: 'failed', createdAt: '2026-10-16T08:10:00', finishedAt: '2026-10-16T08:11:00', ), ]; /// The graph pinned by each recorded version, when it differs from latest. late final Map> versionGraphs = { '1@1': reviewGraphV1(), '1@2': reviewGraph(), }; Map? detailFor(int id, int version) { final match = details.where((detail) => detail['id'] == id); if (match.isEmpty) return null; final detail = Map.from(match.first); if (version > 0) { detail['version'] = version; for (final row in detail['versions'] as List>) { if (row['version'] == version) detail['stages'] = row['stages']; } final graph = versionGraphs['$id@$version']; if (graph != null) detail['graph'] = graph; if (version < (detail['latest_version'] as int)) { detail['source'] = '${detail['source']}\n# selected version $version'; } } return detail; } }