Files
pipelines/app/test/features/pipelines/fixtures.dart
T

276 lines
7.1 KiB
Dart

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<JsonMap> Function(String type, JsonMap fields) handler;
final List<GatewayCall> calls = <GatewayCall>[];
@override
Future<JsonMap> call(String type, [JsonMap fields = const {}]) async {
calls.add(GatewayCall(type, Map<String, dynamic>.from(fields)));
return handler(type, fields);
}
@override
Stream<JsonMap> get frames => const Stream<JsonMap>.empty();
List<GatewayCall> callsOf(String type) =>
calls.where((call) => call.type == type).toList(growable: false);
}
Map<String, dynamic> 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<String, dynamic> 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<Map<String, dynamic>> versions = const [],
Map<String, dynamic>? graph,
Map<String, dynamic>? 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<String, dynamic> reviewGraph() => {
'name': 'Alpha review',
'settings': {'schedules': <dynamic>[]},
'stages': <dynamic>[
{
'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<String, dynamic> reviewGraphV1() => {
'name': 'Alpha review',
'settings': {'schedules': <dynamic>[]},
'stages': <dynamic>[
{
'key': 'review',
'type': 'model',
'prompt': 'Review the change.',
'output': 'notes',
'edges': {'success': '#success', 'error': '#'},
},
],
};
Map<String, dynamic> versionSummary(
int version, {
int stages = 1,
String createdAt = '2026-10-17T21:24:00',
}) => {'version': version, 'stages': stages, 'created_at': createdAt};
Map<String, dynamic> 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<Map<String, dynamic>> pipelines) => {
'type': 'pipelines',
'pipelines': pipelines,
};
JsonMap pipelineDetailReply(Map<String, dynamic> detail) => {
'type': 'pipeline',
'detail': detail,
};
JsonMap runListReply(List<Map<String, dynamic>> 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<Map<String, dynamic>> 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<Map<String, dynamic>> 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<Map<String, dynamic>> 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<String, Map<String, dynamic>> versionGraphs = {
'1@1': reviewGraphV1(),
'1@2': reviewGraph(),
};
Map<String, dynamic>? detailFor(int id, int version) {
final match = details.where((detail) => detail['id'] == id);
if (match.isEmpty) return null;
final detail = Map<String, dynamic>.from(match.first);
if (version > 0) {
detail['version'] = version;
for (final row in detail['versions'] as List<Map<String, dynamic>>) {
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;
}
}