feat(app): schedule a recorded run with its options

The run detail now offers the web's "Schedule this run" action when the
run names a positive pipeline and pinned version. It opens the existing
create editor seeded from the run and sends nothing until Save.

The seed preserves every field the browser's scheduleDraft({ run })
copies: title (following the title_source rule), all input keys with
prompt kept editable, pinned version, workspace, notify mode, write
paths, tool access, model, stage models and output format. build() emits
the same conditional payload as serializeSchedule.

One request key is minted per editor opening so a refused save retries
without duplicating the schedule. Advanced options are seed-only for
now: the editor has no controls for them yet.
This commit is contained in:
2026-10-22 17:28:28 +01:00
parent cf6da4acaa
commit f2c33d2a1e
6 changed files with 715 additions and 3 deletions
@@ -0,0 +1,214 @@
import 'package:flutter_test/flutter_test.dart';
import 'package:pipelines_app/core/api_gateway.dart';
import 'package:pipelines_app/features/schedules/domain/editor/schedule_draft.dart';
import 'draft_fixtures.dart';
/// A run that exercised every run-level option: extra input keys, a non-auto
/// notification mode, write restrictions, tool access, model overrides, an
/// explicit title and an output format.
JsonMap restrictedRun() => {
'id': 7,
'title': 'Nightly build',
'title_source': 'explicit',
'pipeline_id': 4,
'version': 5,
'input': {
'prompt': ' keep\n\nspacing ',
'topic': 'release notes',
'pwd': '/srv/work',
'nested': {'alpha': 1},
},
'workspace': '/srv/run-workspace',
'rule': {
'every': '2h',
'days': ['mon'],
'timezone': 'Europe/London',
},
'notify_mode': 'off',
'write_paths': ['src/file.go', 'notes'],
'allow_tools': true,
'model': 'provider/model',
'stage_models': {'research': 'provider/other'},
'output_format': 'json',
};
void main() {
test('a run seeds every option the browser copies into a create', () {
final run = restrictedRun();
final snapshot = deepCopy(run);
final draft = ScheduleDraft.fromRun(run);
expect(draft.source, isNull);
expect(draft.pipelineId, 4);
expect(draft.name, 'Nightly build');
expect(draft.prompt, ' keep\n\nspacing ');
expect(draft.version, '5');
expect(draft.workspace, '/srv/run-workspace');
// The run is a prompt snapshot, not a saved timer: create defaults stand.
expect(draft.enabled, isTrue);
expect(draft.mode, ScheduleTimerMode.interval);
expect(draft.every, '24h');
expect(draft.times, '');
expect(draft.days, isEmpty);
expect(draft.timezone, '');
// Run-level options are seeded verbatim.
expect(draft.notifyMode, 'off');
expect(draft.model, 'provider/model');
expect(draft.stageModels, {'research': 'provider/other'});
expect(draft.writePaths, ['src/file.go', 'notes']);
expect(draft.allowTools, isTrue);
expect(draft.title, 'Nightly build');
expect(draft.outputFormat, 'json');
expect(draft.otherInput, {
'topic': 'release notes',
'pwd': '/srv/work',
'nested': {'alpha': 1},
});
final result = draft.build();
expect(result.errors, isEmpty);
expect(result.record, {
'id': 0,
'name': 'Nightly build',
'pipeline_id': 4,
'version': 5,
'enabled': true,
'input': {
'prompt': ' keep\n\nspacing ',
'topic': 'release notes',
'pwd': '/srv/work',
'nested': {'alpha': 1},
},
'workspace': '/srv/run-workspace',
'rule': {'every': '24h'},
'notify_mode': 'off',
'write_paths': ['src/file.go', 'notes'],
'allow_tools': true,
'model': 'provider/model',
'stage_models': {'research': 'provider/other'},
'title': 'Nightly build',
'output_format': 'json',
});
expect(run, snapshot);
});
test('a run without the advanced options still saves a minimal create', () {
final draft = ScheduleDraft.fromRun({
'title': 'Plain run',
'pipeline_id': 4,
'version': 5,
'input': {'prompt': 'P'},
'workspace': '/srv',
});
expect(draft.notifyMode, 'auto');
expect(draft.model, '');
expect(draft.stageModels, isEmpty);
expect(draft.writePaths, isEmpty);
expect(draft.allowTools, isFalse);
expect(draft.otherInput, isEmpty);
expect(draft.title, 'Plain run');
expect(draft.outputFormat, '');
final record = draft.build().record!;
expect(record, {
'id': 0,
'name': 'Plain run',
'pipeline_id': 4,
'version': 5,
'enabled': true,
'input': {'prompt': 'P'},
'workspace': '/srv',
'rule': {'every': '24h'},
'notify_mode': 'auto',
// A run that does not report a title source keeps its title.
'title': 'Plain run',
});
});
test('a generated run title is not copied into the schedule title', () {
for (final source in ['model', 'fallback']) {
final draft = ScheduleDraft.fromRun({
...restrictedRun(),
'title_source': source,
});
// The schedule name still uses the run title, as `scheduleDraft` does.
expect(draft.name, 'Nightly build');
expect(draft.title, '');
expect(draft.build().record!.containsKey('title'), isFalse);
}
for (final source in ['explicit', 'user', null]) {
final run = restrictedRun();
if (source == null) {
run.remove('title_source');
} else {
run['title_source'] = source;
}
expect(ScheduleDraft.fromRun(run).title, 'Nightly build');
}
});
test('a run without pipeline, version, prompt or name stays invalid', () {
final draft = ScheduleDraft.fromRun({
'version': 0,
'input': {'prompt': ''},
});
expect(draft.pipelineId, isNull);
expect(draft.name, '');
expect(draft.prompt, '');
expect(draft.version, '0');
expect(draft.workspace, '');
final result = draft.build();
expect(result.isValid, isFalse);
expect(result.record, isNull);
expect(
result.errors.keys,
containsAll(['pipelineId', 'name', 'prompt', 'version', 'workspace']),
);
});
test('numeric variants render like the browser string and the run is read '
'only', () {
final run = <String, dynamic>{
'title': 'T',
'pipeline_id': 4.0,
'version': 5.0,
'input': {'prompt': 'P'},
'workspace': '/srv',
};
final snapshot = Map<String, dynamic>.from(run);
final draft = ScheduleDraft.fromRun(run);
expect(draft.pipelineId, 4);
expect(draft.version, '5');
expect(draft.build().isValid, isTrue);
expect(run, snapshot);
expect(
ScheduleDraft.fromRun({'pipeline_id': 4, 'version': '6'}).version,
'6',
);
// `String(value)` for a non-integral number, rather than hiding it.
expect(
ScheduleDraft.fromRun({'pipeline_id': '4', 'version': 5.5}).version,
'5.5',
);
});
test('built input never aliases the run map', () {
final run = restrictedRun();
final draft = ScheduleDraft.fromRun(run);
final record = draft.build().record!;
((record['input'] as JsonMap)['nested'] as JsonMap)['extra'] = true;
(record['stage_models'] as JsonMap)['research'] = 'changed';
expect((run['input'] as JsonMap)['nested'], {'alpha': 1});
expect(draft.otherInput['nested'], {'alpha': 1});
expect(draft.stageModels['research'], 'provider/other');
});
}