merge(app): land schedule-seed

This commit is contained in:
2026-10-22 17:47:35 +01:00
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');
});
}
@@ -0,0 +1,262 @@
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:flutter_test/flutter_test.dart';
import 'package:pipelines_app/core/api_gateway.dart';
import 'package:pipelines_app/core/transport/connection_state.dart';
import 'package:pipelines_app/features/runs/ui/run_detail_actions.dart';
import 'package:pipelines_app/features/schedules/ui/editor/schedule_editor_wording.dart';
import 'package:pipelines_app/features/schedules/ui/run/schedule_run_action.dart';
/// Records every `save_schedule` and refuses the first one on demand. No socket
/// or Pipelines server is involved.
class SaveGateway implements ApiGateway {
final calls = <Map<String, dynamic>>[];
bool refuseNext = false;
@override
Stream<JsonMap> get frames => const Stream.empty();
@override
Future<JsonMap> call(String type, [JsonMap fields = const {}]) async {
if (type != 'save_schedule') throw UnsupportedError('unexpected $type');
calls.add(Map<String, dynamic>.from(fields));
if (refuseNext) {
refuseNext = false;
throw const ProtocolError('conflict: revision 4 is stale');
}
final schedule = Map<String, dynamic>.from(fields['schedule'] as Map);
return {
'type': 'schedule_saved',
'schedule': {...schedule, 'id': 12, 'revision': 1},
};
}
}
JsonMap runFixture({
Object? version = 5,
Object? pipelineId = 4,
String title = 'Nightly build',
String prompt = 'Summarise the release notes.',
String workspace = '/srv/run-workspace',
}) => {
'id': 7,
'title': title,
'pipeline_id': ?pipelineId,
'version': ?version,
'status': 'succeeded',
'input': {
'prompt': prompt,
'nested': {'kept': true},
},
'workspace': workspace,
};
/// A run carrying every option the browser's run seed preserves.
JsonMap restrictedRunFixture() => {
...runFixture(),
'title_source': 'explicit',
'input': {
'prompt': 'Summarise the release notes.',
'topic': 'release notes',
'nested': {'kept': true},
},
'notify_mode': 'off',
'write_paths': ['src/file.go', 'notes'],
'allow_tools': true,
'model': 'provider/model',
'stage_models': {'research': 'provider/other'},
'output_format': 'json',
};
Future<void> pumpAction(
WidgetTester tester,
Widget child, {
bool cupertino = false,
}) async {
tester.view.physicalSize = const Size(390, 1600);
tester.view.devicePixelRatio = 1;
addTearDown(tester.view.resetPhysicalSize);
addTearDown(tester.view.resetDevicePixelRatio);
await tester.pumpWidget(
cupertino
? CupertinoApp(
home: CupertinoPageScaffold(child: SafeArea(child: child)),
)
: MaterialApp(
home: Scaffold(body: SafeArea(child: child)),
),
);
await tester.pumpAndSettle();
}
Future<void> tapSave(WidgetTester tester) async {
// A focused field keeps itself visible, so drop focus before scrolling.
FocusManager.instance.primaryFocus?.unfocus();
await tester.pumpAndSettle();
final finder = find.byKey(scheduleEditorSubmitKey);
await tester.ensureVisible(finder);
await tester.pump();
await tester.tap(finder);
await tester.pumpAndSettle();
}
String materialField(WidgetTester tester, Key key) =>
tester.widget<TextField>(find.byKey(key)).controller!.text;
void main() {
testWidgets('opens a create editor seeded from the run without sending', (
tester,
) async {
final gateway = SaveGateway();
await pumpAction(
tester,
ScheduleRunAction(run: runFixture(), gateway: gateway),
);
await tester.tap(find.byKey(scheduleRunActionKey));
await tester.pumpAndSettle();
expect(gateway.calls, isEmpty);
expect(find.text('Add schedule'), findsOneWidget);
expect(materialField(tester, scheduleEditorNameKey), 'Nightly build');
expect(
materialField(tester, scheduleEditorPromptKey),
'Summarise the release notes.',
);
expect(materialField(tester, scheduleEditorVersionKey), '5');
expect(
materialField(tester, scheduleEditorWorkspaceKey),
'/srv/run-workspace',
);
expect(materialField(tester, scheduleEditorEveryKey), '24h');
expect(materialField(tester, scheduleEditorTimezoneKey), '');
expect(gateway.calls, isEmpty);
});
testWidgets('saves the run\'s input, restrictions and overrides once, and '
'reuses the one key after a refusal', (tester) async {
final gateway = SaveGateway()..refuseNext = true;
await pumpAction(
tester,
ScheduleRunAction(run: restrictedRunFixture(), gateway: gateway),
);
await tester.tap(find.byKey(scheduleRunActionKey));
await tester.pumpAndSettle();
await tapSave(tester);
expect(gateway.calls, hasLength(1));
final key = gateway.calls.single['request_key'];
expect(key, isA<String>());
expect(key, isNotEmpty);
expect('$key'.length, lessThanOrEqualTo(128));
expect(
find.textContaining('conflict: revision 4 is stale'),
findsOneWidget,
);
expect(find.textContaining('Your draft is preserved.'), findsOneWidget);
expect(find.byKey(scheduleEditorSubmitKey), findsOneWidget);
expect(
materialField(tester, scheduleEditorPromptKey),
'Summarise the release notes.',
);
await tapSave(tester);
expect(gateway.calls, hasLength(2));
expect(gateway.calls[0]['request_key'], key);
expect(gateway.calls[1]['request_key'], key);
expect(gateway.calls[1]['schedule'], {
'id': 0,
'name': 'Nightly build',
'pipeline_id': 4,
'version': 5,
'enabled': true,
'input': {
'prompt': 'Summarise the release notes.',
'topic': 'release notes',
'nested': {'kept': true},
},
'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(find.byKey(scheduleEditorSubmitKey), findsNothing);
});
testWidgets('the action is wired into run detail and hidden without a pin', (
tester,
) async {
final gateway = SaveGateway();
await pumpAction(
tester,
RunDetailActions(
run: runFixture(),
gateway: gateway,
onRunChanged: (_) {},
),
);
expect(find.byKey(scheduleRunActionKey), findsOneWidget);
expect(find.text(scheduleRunActionLabel), findsOneWidget);
await pumpAction(
tester,
RunDetailActions(
run: runFixture(version: 0),
gateway: gateway,
onRunChanged: (_) {},
),
);
expect(find.byKey(scheduleRunActionKey), findsNothing);
await pumpAction(
tester,
RunDetailActions(
run: runFixture(pipelineId: null),
gateway: gateway,
onRunChanged: (_) {},
),
);
expect(find.byKey(scheduleRunActionKey), findsNothing);
});
testWidgets('Cupertino uses the native action and fields', (tester) async {
final gateway = SaveGateway();
await pumpAction(
tester,
ScheduleRunAction(run: runFixture(), gateway: gateway),
cupertino: true,
);
final action = find.byKey(scheduleRunActionKey);
expect(
find.widgetWithText(CupertinoButton, scheduleRunActionLabel),
findsOne,
);
await tester.tap(action);
await tester.pumpAndSettle();
expect(gateway.calls, isEmpty);
expect(find.byKey(scheduleEditorSubmitKey), findsOneWidget);
expect(
tester
.widget<CupertinoTextField>(find.byKey(scheduleEditorVersionKey))
.controller
?.text,
'5',
);
expect(
tester
.widget<CupertinoTextField>(find.byKey(scheduleEditorWorkspaceKey))
.controller
?.text,
'/srv/run-workspace',
);
});
}