feat(app): add run foundation repository and models

This commit is contained in:
2026-10-21 13:21:30 +01:00
parent 264f64c0bb
commit 444bd62996
4 changed files with 128 additions and 0 deletions
@@ -0,0 +1,59 @@
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_events.dart';
import 'package:pipelines_app/features/runs/domain/run_models.dart';
class FakeGateway implements ApiGateway {
String? type;
JsonMap fields = {};
JsonMap reply = {};
@override
Future<JsonMap> call(String type, [JsonMap fields = const {}]) async {
this.type = type;
this.fields = Map.of(fields);
return reply;
}
@override
Stream<JsonMap> get frames => const Stream.empty();
}
void main() {
test('filter/sort identities and unknown usage remain distinct', () {
expect(runIdentity(' Report ', 'Build').label, 'Report');
final rows = [{'id': 1, 'pipeline_id': 2, 'status': 'failed', 'created_at': '2024-01-01'}, {'id': 2, 'pipeline_id': 2, 'status': 'running', 'created_at': '2025-01-01'}];
expect(filterRuns(rows, statuses: {'failed'}).single['id'], 1);
expect(sortRuns(rows).first['id'], 2);
expect(RunListResult(const [], null).usage, isNull);
});
test('fold is monotonic and explicit resource wait clearing is honored', () {
final rows = <JsonMap>[{'id': 1, 'status': 'running', 'completed_steps': 3, 'resource_wait': {'resource': 'gpu'}}];
final stale = foldRunEvent(rows, {'run_id': 1, 'status': 'queued', 'completed_steps': 1});
expect(stale.runs.single['status'], 'running');
final cleared = foldRunEvent(rows, {'run_id': 1, 'status': 'queued', 'resource_wait_set': true, 'resource_wait': null});
expect(cleared.runs.single['status'], 'queued');
expect(cleared.runs.single['resource_wait'], isNull);
expect(cleared.runs.single['completed_steps'], 3);
expect(runDetailNeedsFullSync({'status': 'waiting'}), isTrue);
});
test('decision guard and attention dismissal identity', () {
expect(const DecisionDraft().canSubmit, isFalse);
expect(const DecisionDraft(selected: ' continue ').canSubmit, isTrue);
expect(attentionKey(7, 'Decision'), '7:Decision');
final ordered = orderAttention([{'id': 8, 'status': 'failed'}, {'id': 7, 'status': 'waiting'}]);
expect(ordered.first['id'], 7);
});
test('repository emits exact action envelopes and requires idempotency key', () async {
final gateway = FakeGateway()..reply = {'run_detail': {'id': 3}};
final repo = RunsRepository(gateway);
await repo.retryRun(3, requestKey: 'retry-1');
expect(gateway.type, 'retry_run');
expect(gateway.fields, {'run_id': 3, 'request_key': 'retry-1'});
await repo.startRun(pipelineId: 4, version: 2, prompt: 'hello', workspace: ' /work ', requestKey: 'start-2');
expect(gateway.fields, {'pipeline_id': 4, 'version': 2, 'request_key': 'start-2', 'workspace': '/work', 'input': {'prompt': 'hello'}});
await expectLater(repo.decideRun(3, option: ' '), throwsA(isA<RunsRepositoryException>()));
});
}