merge(app): land corrected run-state foundation

This commit is contained in:
2026-10-21 13:37:26 +01:00
9 changed files with 1261 additions and 0 deletions
@@ -0,0 +1,87 @@
import 'package:flutter_test/flutter_test.dart';
import 'package:pipelines_app/core/api_gateway.dart';
import 'package:pipelines_app/features/runs/domain/run_decisions.dart';
import 'package:pipelines_app/features/runs/domain/run_events.dart';
import 'package:pipelines_app/features/runs/domain/run_models.dart';
void main() {
test(
'scheduled origin is immutable and export pending blocks attention/decision',
() {
final rows = <JsonMap>[
{
'id': 5,
'status': 'waiting',
'scheduled': true,
'schedule_id': 12,
'schedule_name': 'Nightly',
},
];
final changed = foldRunEvent(rows, {
'run_id': 5,
'status': 'waiting',
'scheduled': false,
'schedule_id': 99,
'schedule_name': 'Other',
'export_pending_sequence': 4,
});
expect(changed.runs.single['schedule_id'], 12);
expect(changed.runs.single['schedule_name'], 'Nightly');
expect(changed.runs.single['scheduled'], true);
expect(orderAttention(changed.runs), isEmpty);
expect(
pendingDecision({
...changed.runs.single,
'current_stage': 'human',
'steps': [],
}),
isNull,
);
},
);
test(
'pending decision uses recorded options then graph fallback and target labels',
() {
final run = <String, dynamic>{
'id': 7,
'status': 'waiting',
'current_stage': 'human',
'name': 'Build',
'graph': {
'stages': [
{
'key': 'human',
'type': 'human',
'prompt': 'Graph prompt',
'edges': {'go': '#', 'error': '#', 'skip': 'finish'},
},
],
},
'steps': [
{
'sequence': 1,
'status': 'waiting',
'stage': 'human',
'output': {
'prompt': 'Recorded prompt',
'options': ['go'],
},
},
],
};
final decision = pendingDecision(run)!;
expect(decision.instructions, 'Recorded prompt');
expect(decision.options.map((option) => option.key), ['go']);
expect(decision.options.single.target, 'SUCCESS');
final fallback = pendingDecision({
...run,
'steps': [
{'status': 'waiting', 'stage': 'human', 'output': {}},
],
})!;
expect(fallback.instructions, 'Graph prompt');
expect(fallback.options.map((option) => option.key), ['go', 'skip']);
},
);
}
@@ -0,0 +1,189 @@
import 'package:flutter_test/flutter_test.dart';
import 'package:pipelines_app/core/api_gateway.dart';
import 'package:pipelines_app/features/runs/domain/run_events.dart';
void main() {
group('event fold', () {
test('terminal status and resource wait reset are monotonic', () {
final initial = <JsonMap>[
{
'id': 1,
'status': 'succeeded',
'executing': false,
'completed_steps': 4,
},
];
final reset = foldRunEvent(initial, {
'run_id': 1,
'status': 'queued',
'resource_wait_set': true,
'resource_wait': null,
});
expect(reset.runs.single['status'], 'succeeded');
expect(reset.runs.single['executing'], false);
final competing = foldRunEvent(initial, {
'run_id': 1,
'status': 'failed',
'resource_wait_set': true,
});
expect(competing.runs.single['status'], 'succeeded');
expect(competing.runs.single['resource_wait'], isNull);
});
test(
'resource wait reset advances only nonterminal rows, even on stale status',
() {
final rows = <JsonMap>[
{
'id': 2,
'status': 'running',
'executing': true,
'resource_wait': {'resource': 'gpu'},
'completed_steps': 3,
},
];
final result = foldRunEvent(rows, {
'run_id': 2,
'status': 'queued',
'resource_wait_set': true,
'resource_wait': null,
'completed_steps': 1,
});
expect(result.runs.single['status'], 'queued');
expect(result.runs.single['resource_wait'], isNull);
expect(result.runs.single['completed_steps'], 3);
},
);
test(
'new waiting and terminal rows are not executing; identity-less frame resyncs',
() {
for (final status in ['waiting', 'succeeded']) {
final result = foldRunEvent([], {
'run_id': 8,
'pipeline_id': 4,
'name': 'Build',
'status': status,
});
expect(result.runs.single['executing'], false);
}
expect(
foldRunEvent([], {'run_id': 9, 'status': 'running'}).needsResync,
isTrue,
);
},
);
test(
'blank sparse step fields preserve graph evidence and terminal visit',
() {
final detail = <String, dynamic>{
'id': 1,
'status': 'running',
'steps': [
{
'sequence': 2,
'stage': 'review',
'status': 'completed',
'outcome': 'approve',
'target': 'publish',
'diagnostic': 'kept',
},
],
};
final next = foldRunEventDetail(detail, {
'run_id': 1,
'status': 'running',
'step': {
'sequence': 2,
'stage': '',
'status': 'running',
'outcome': ' ',
'target': '',
'diagnostic': '',
},
})!;
final step = (next['steps'] as List).single as JsonMap;
expect(step, containsPair('stage', 'review'));
expect(step, containsPair('status', 'completed'));
expect(step, containsPair('outcome', 'approve'));
expect(step, containsPair('target', 'publish'));
expect(step, containsPair('diagnostic', 'kept'));
},
);
test(
'cancel and cleanup flags stick; resource wait can clear; terminal clears it',
() {
final rows = <JsonMap>[
{
'id': 3,
'status': 'running',
'resource_wait': {'resource': 'gpu'},
},
];
final first = foldRunEvent(rows, {
'run_id': 3,
'status': 'running',
'cancel_requested': true,
'cleanup_unconfirmed': true,
});
final second = foldRunEvent(first.runs, {
'run_id': 3,
'status': 'running',
'cancel_requested': false,
'cleanup_unconfirmed': false,
'resource_wait_set': true,
'resource_wait': null,
});
expect(second.runs.single['cancel_requested'], true);
expect(second.runs.single['cleanup_unconfirmed'], true);
expect(second.runs.single['resource_wait'], isNull);
final terminal = foldRunEvent(second.runs, {
'run_id': 3,
'status': 'failed',
});
expect(terminal.runs.single['resource_wait'], isNull);
},
);
test(
'full sync triggers and list/detail folds agree across stale and current frames',
() {
expect(runDetailNeedsFullSync({'status': 'waiting'}), true);
expect(
runDetailNeedsFullSync({
'status': 'running',
'step': {'status': 'completed'},
}),
true,
);
final summary = <JsonMap>[
{'id': 1, 'status': 'running', 'completed_steps': 1},
];
final frame = {
'run_id': 1,
'status': 'succeeded',
'completed_steps': 2,
'outcome': 'done',
};
final list = foldRunEvent(summary, frame).runs.single;
final detail = foldRunEventDetail({
'id': 1,
'status': 'running',
'completed_steps': 1,
'steps': [],
}, frame)!;
expect(list['status'], detail['status']);
expect(list['completed_steps'], detail['completed_steps']);
final old = foldRunEventDetail(detail, {
'run_id': 1,
'status': 'running',
'outcome': '',
})!;
expect(old['status'], 'succeeded');
expect(old['outcome'], 'done');
},
);
});
}
@@ -0,0 +1,163 @@
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')),
);
},
);
}