Files
pipelines/app/test/features/runs/runs_repository_test.dart
T

164 lines
4.7 KiB
Dart

import 'package:flutter_test/flutter_test.dart';
import 'package:pipelines_app/core/api_gateway.dart';
import 'package:pipelines_app/features/runs/data/runs_repository.dart';
import 'package:pipelines_app/features/runs/domain/run_models.dart';
class FakeGateway implements ApiGateway {
String? type;
JsonMap fields = {};
JsonMap reply = {};
Object? failure;
@override
Future<JsonMap> call(String type, [JsonMap fields = const {}]) async {
this.type = type;
this.fields = Map.of(fields);
if (failure case final error?) throw error;
return reply;
}
@override
Stream<JsonMap> get frames => const Stream.empty();
}
void main() {
test(
'run models match identity, filtering, sorting and null usage behavior',
() {
expect(runIdentity(' Report ', 'Build').label, 'Report');
final rows = <JsonMap>[
{
'id': 1,
'pipeline_id': 2,
'status': 'failed',
'created_at': '2024-01-01T00:00:00Z',
'finished_at': '2024-01-01T00:00:10Z',
},
{
'id': 2,
'pipeline_id': 2,
'status': 'running',
'created_at': '2025-01-01T00:00:00Z',
},
];
expect(filterRuns(rows, statuses: {'failed'}).single['id'], 1);
expect(sortRuns(rows).first['id'], 2);
expect(sortRuns(rows, sort: 'shortest_duration').first['id'], 1);
final scheduled = [
{
'id': 3,
'scheduled': true,
'schedule_id': 9,
'created_at': '2025-03-10T12:00:00Z',
},
{'id': 4, 'scheduled': false, 'created_at': '2025-03-10T12:00:00Z'},
{'id': 5, 'created_at': '2025-03-10T12:00:00Z'},
];
expect(
filterRuns(scheduled, scheduled: 'scheduled').map((run) => run['id']),
[3],
);
expect(
filterRuns(
scheduled,
scheduled: 'unscheduled',
timeRange: 'today',
today: DateTime(2025, 3, 10),
).map((run) => run['id']),
[4],
);
expect(
runStatusLabel({'status': 'waiting', 'export_pending_sequence': 2}),
'Waiting for session export',
);
expect(RunListResult(const [], null).usage, isNull);
},
);
test('repository sends exact action and launch requests', () async {
final gateway = FakeGateway()
..reply = {
'run_detail': {'id': 3},
};
final repo = RunsRepository(gateway);
gateway.reply = {
'runs': [
{'id': 1, 'status': 'running'},
],
'usage': {'active': 1},
};
final listed = await repo.listRuns();
expect(gateway.type, 'list_runs');
expect(gateway.fields, isEmpty);
expect(listed.runs.single['id'], 1);
expect(listed.usage, {'active': 1});
gateway.reply = {
'run_detail': {'id': 3},
};
await repo.retryRun(3, requestKey: 'retry-1');
expect(gateway.type, 'retry_run');
expect(gateway.fields, {'run_id': 3, 'request_key': 'retry-1'});
await repo.retryRun(3, requestKey: 'retry-2', mode: 'restart');
expect(gateway.fields, {
'run_id': 3,
'request_key': 'retry-2',
'retry_mode': 'restart',
});
await repo.decideRun(3, stage: 'human', option: ' go ', feedback: ' yes ');
expect(gateway.fields, {
'run_id': 3,
'stage': 'human',
'option': 'go',
'feedback': 'yes',
});
await repo.cancelRun(3);
expect(gateway.type, 'cancel_run');
expect(gateway.fields, {'run_id': 3});
await repo.setRunTitle(3, 'New');
expect(gateway.type, 'set_run_title');
expect(gateway.fields, {'run_id': 3, 'title': 'New'});
await repo.startRun(
pipelineId: 4,
version: 2,
prompt: 'hello',
workspace: ' /work ',
requestKey: 'start-2',
title: ' Job ',
notifyMode: 'off',
outputFormat: 'json',
);
expect(gateway.type, 'run_pipeline');
expect(gateway.fields, {
'pipeline_id': 4,
'version': 2,
'request_key': 'start-2',
'workspace': '/work',
'input': {'prompt': 'hello'},
'title': 'Job',
'notify_mode': 'off',
'output_format': 'json',
});
});
test(
'repository propagates server refusals and rejects missing detail',
() async {
final gateway = FakeGateway()..reply = {};
final repo = RunsRepository(gateway);
await expectLater(
repo.getRun(3),
throwsA(isA<RunsRepositoryException>()),
);
await expectLater(
repo.decideRun(3, option: ' '),
throwsA(isA<RunsRepositoryException>()),
);
gateway.failure = const RunsRepositoryException('server refused');
await expectLater(
repo.cancelRun(3),
throwsA(predicate((error) => '$error' == 'server refused')),
);
},
);
}