feat(app): add saved-schedule repository and domain record
Port the web client's list/save/delete schedule request shapes and reply checks, with a typed record that preserves the whole server payload including unknown fields. Revision stays the CAS token; server refusals propagate unchanged.
This commit is contained in:
@@ -0,0 +1,101 @@
|
||||
import 'package:flutter_test/flutter_test.dart';
|
||||
import 'package:pipelines_app/features/schedules/domain/saved_schedule.dart';
|
||||
|
||||
import 'schedule_test_support.dart';
|
||||
|
||||
void main() {
|
||||
test('saved schedule exposes run-configuration provenance', () {
|
||||
final schedule = SavedSchedule(savedRecord());
|
||||
|
||||
expect(schedule.id, 8);
|
||||
expect(schedule.revision, 3);
|
||||
expect(schedule.pipelineId, 4);
|
||||
expect(schedule.version, 2);
|
||||
expect(schedule.name, 'Research batteries');
|
||||
expect(schedule.enabled, isTrue);
|
||||
expect(schedule.prompt, 'Research batteries');
|
||||
expect(schedule.input['custom'], {'nested': 'kept'});
|
||||
expect(schedule.workspace, '/srv/research');
|
||||
expect(schedule.writePaths, ['reports/research.md']);
|
||||
expect(schedule.allowTools, isTrue);
|
||||
expect(schedule.model, 'openai/gpt-5');
|
||||
expect(schedule.stageModels, {'one': 'openai/gpt-5'});
|
||||
expect(schedule.notifyMode, 'off');
|
||||
expect(schedule.title, 'Battery research');
|
||||
expect(schedule.outputFormat, 'markdown');
|
||||
expect(schedule.rule.every, '24h');
|
||||
expect(schedule.rule.days, ['mon', 'wed']);
|
||||
expect(schedule.rule.timezone, 'Europe/London');
|
||||
expect(schedule.rule.isClock, isFalse);
|
||||
expect(schedule.nextDue, '2026-10-22T09:00:00Z');
|
||||
expect(schedule.createdAt, '2026-10-20T09:00:00Z');
|
||||
expect(schedule.updatedAt, '2026-10-21T09:00:00Z');
|
||||
});
|
||||
|
||||
test('unknown fields survive the typed view and pause copy', () {
|
||||
final schedule = SavedSchedule(savedRecord());
|
||||
final paused = schedule.withEnabled(false);
|
||||
|
||||
expect(paused.enabled, isFalse);
|
||||
expect(paused.id, 8);
|
||||
expect(paused.revision, 3);
|
||||
expect(paused.toJson()['future_server_field'], {
|
||||
'nested': [
|
||||
1,
|
||||
2,
|
||||
{'deep': true},
|
||||
],
|
||||
});
|
||||
expect(paused.toJson()['input'], {
|
||||
'prompt': 'Research batteries',
|
||||
'custom': {'nested': 'kept'},
|
||||
});
|
||||
expect(paused.toJson()['rule'], savedRecord()['rule']);
|
||||
|
||||
// The original snapshot is untouched and still enabled.
|
||||
expect(schedule.enabled, isTrue);
|
||||
expect(schedule.revision, 3);
|
||||
});
|
||||
|
||||
test('toJson is a deep copy, not an alias of the server reply', () {
|
||||
final source = savedRecord();
|
||||
final schedule = SavedSchedule(source);
|
||||
|
||||
final first = schedule.toJson();
|
||||
((first['future_server_field'] as Map)['nested'] as List).add('changed');
|
||||
final second = schedule.toJson();
|
||||
expect((second['future_server_field'] as Map)['nested'], [
|
||||
1,
|
||||
2,
|
||||
{'deep': true},
|
||||
]);
|
||||
|
||||
final promptInput = schedule.input;
|
||||
promptInput['custom'] = 'changed';
|
||||
expect(schedule.input['custom'], {'nested': 'kept'});
|
||||
});
|
||||
|
||||
test('missing fields stay absent instead of being fabricated', () {
|
||||
final schedule = SavedSchedule(const {});
|
||||
|
||||
expect(schedule.id, isNull);
|
||||
expect(schedule.revision, isNull);
|
||||
expect(schedule.pipelineId, isNull);
|
||||
expect(schedule.version, isNull);
|
||||
expect(schedule.enabled, isFalse);
|
||||
expect(schedule.prompt, '');
|
||||
expect(schedule.workspace, '');
|
||||
expect(schedule.rule.every, '');
|
||||
expect(schedule.rule.times, isEmpty);
|
||||
expect(schedule.writePaths, isEmpty);
|
||||
expect(schedule.stageModels, isEmpty);
|
||||
expect(schedule.toJson(), isEmpty);
|
||||
});
|
||||
|
||||
test('a fractional revision is not truncated into a false token', () {
|
||||
final schedule = SavedSchedule(const {'id': 8, 'revision': 3.5});
|
||||
|
||||
expect(schedule.id, 8);
|
||||
expect(schedule.revision, isNull);
|
||||
});
|
||||
}
|
||||
@@ -0,0 +1,83 @@
|
||||
import 'dart:convert';
|
||||
|
||||
import 'package:pipelines_app/core/api_gateway.dart';
|
||||
|
||||
class RecordedCall {
|
||||
const RecordedCall(this.type, this.fields);
|
||||
final String type;
|
||||
final JsonMap fields;
|
||||
}
|
||||
|
||||
/// Records the exact request built by the repository and replays queued
|
||||
/// replies. Both are deep-copied, so a test can prove the repository neither
|
||||
/// mutated its input nor depended on alias mutation of a reply.
|
||||
class RecordingGateway implements ApiGateway {
|
||||
final calls = <RecordedCall>[];
|
||||
final replies = <JsonMap>[];
|
||||
Object? failure;
|
||||
|
||||
@override
|
||||
Future<JsonMap> call(String type, [JsonMap fields = const {}]) async {
|
||||
calls.add(RecordedCall(type, deepCopy(fields)));
|
||||
if (failure case final error?) throw error;
|
||||
if (replies.isEmpty) throw StateError('No recorded reply for $type');
|
||||
return deepCopy(replies.removeAt(0));
|
||||
}
|
||||
|
||||
@override
|
||||
Stream<JsonMap> get frames => const Stream.empty();
|
||||
}
|
||||
|
||||
JsonMap deepCopy(JsonMap value) => jsonDecode(jsonEncode(value)) as JsonMap;
|
||||
|
||||
/// A complete server record including fields this client does not interpret
|
||||
/// yet, an unknown nested rule field and unknown input.
|
||||
JsonMap savedRecord() => {
|
||||
'id': 8,
|
||||
'revision': 3,
|
||||
'name': 'Research batteries',
|
||||
'pipeline_id': 4,
|
||||
'version': 2,
|
||||
'enabled': true,
|
||||
'input': {
|
||||
'prompt': 'Research batteries',
|
||||
'custom': {'nested': 'kept'},
|
||||
},
|
||||
'workspace': '/srv/research',
|
||||
'write_paths': ['reports/research.md'],
|
||||
'allow_tools': true,
|
||||
'model': 'openai/gpt-5',
|
||||
'stage_models': {'one': 'openai/gpt-5'},
|
||||
'notify_mode': 'off',
|
||||
'title': 'Battery research',
|
||||
'output_format': 'markdown',
|
||||
'rule': {
|
||||
'every': '24h',
|
||||
'days': ['mon', 'wed'],
|
||||
'timezone': 'Europe/London',
|
||||
'future_rule_field': 'kept',
|
||||
},
|
||||
'next_due': '2026-10-22T09:00:00Z',
|
||||
'created_at': '2026-10-20T09:00:00Z',
|
||||
'updated_at': '2026-10-21T09:00:00Z',
|
||||
'future_server_field': {
|
||||
'nested': [
|
||||
1,
|
||||
2,
|
||||
{'deep': true},
|
||||
],
|
||||
},
|
||||
};
|
||||
|
||||
/// The create form of the record: id 0 is the server's create sentinel and no
|
||||
/// revision has been issued yet.
|
||||
JsonMap createRecord() => {
|
||||
'id': 0,
|
||||
'name': 'Research batteries',
|
||||
'pipeline_id': 4,
|
||||
'version': 2,
|
||||
'enabled': true,
|
||||
'input': {'prompt': 'Research batteries'},
|
||||
'workspace': '/srv/research',
|
||||
'rule': {'every': '24h', 'timezone': 'Europe/London'},
|
||||
};
|
||||
@@ -0,0 +1,131 @@
|
||||
import 'package:flutter_test/flutter_test.dart';
|
||||
import 'package:pipelines_app/core/transport/connection_state.dart';
|
||||
import 'package:pipelines_app/features/schedules/data/schedules_repository.dart';
|
||||
|
||||
import 'schedule_test_support.dart';
|
||||
|
||||
void main() {
|
||||
test('delete sends the exact ID and expected revision', () async {
|
||||
final gateway = RecordingGateway()
|
||||
..replies.add({'type': 'schedule_deleted', 'schedule_id': 8});
|
||||
final repository = SchedulesRepository(gateway);
|
||||
|
||||
await repository.deleteSchedule(8, 3);
|
||||
|
||||
expect(gateway.calls.single.type, 'delete_schedule');
|
||||
expect(gateway.calls.single.fields, {
|
||||
'schedule_id': 8,
|
||||
'expected_revision': 3,
|
||||
});
|
||||
});
|
||||
|
||||
test('delete refuses a confirmation for anything else', () async {
|
||||
final gateway = RecordingGateway();
|
||||
final repository = SchedulesRepository(gateway);
|
||||
for (final reply in [
|
||||
<String, dynamic>{},
|
||||
{'type': 'pong'},
|
||||
{'type': 'schedule_deleted'},
|
||||
{'type': 'schedule_deleted', 'schedule_id': 9},
|
||||
]) {
|
||||
gateway.replies.add(reply);
|
||||
await expectLater(
|
||||
repository.deleteSchedule(8, 3),
|
||||
throwsA(
|
||||
isA<SchedulesRepositoryException>().having(
|
||||
(error) => error.message,
|
||||
'message',
|
||||
'The server did not confirm deletion of this schedule.',
|
||||
),
|
||||
),
|
||||
);
|
||||
}
|
||||
});
|
||||
|
||||
test('server refusals and conflicts propagate verbatim', () async {
|
||||
final conflict = const ProtocolError(
|
||||
'schedule revision conflict; reload before editing',
|
||||
);
|
||||
final recording = RecordingGateway()..failure = conflict;
|
||||
final conflicting = SchedulesRepository(recording);
|
||||
await expectLater(
|
||||
conflicting.saveSchedule(createRecord()),
|
||||
throwsA(same(conflict)),
|
||||
);
|
||||
await expectLater(
|
||||
conflicting.deleteSchedule(8, 3),
|
||||
throwsA(same(conflict)),
|
||||
);
|
||||
|
||||
final unsupported = const ProtocolError(
|
||||
'unknown message type: list_schedules',
|
||||
);
|
||||
recording.failure = unsupported;
|
||||
await expectLater(
|
||||
conflicting.listSchedules(pipelineId: 4),
|
||||
throwsA(same(unsupported)),
|
||||
);
|
||||
});
|
||||
|
||||
test('missing and empty list replies are different facts', () async {
|
||||
final gateway = RecordingGateway();
|
||||
final repository = SchedulesRepository(gateway);
|
||||
|
||||
gateway.replies.add({'type': 'schedules'});
|
||||
expect(await repository.listSchedules(), isEmpty);
|
||||
|
||||
gateway.replies.add({'type': 'schedules', 'schedules': null});
|
||||
expect(await repository.listSchedules(), isEmpty);
|
||||
|
||||
for (final reply in [
|
||||
<String, dynamic>{},
|
||||
{'type': 'pong'},
|
||||
{'type': 'schedules', 'schedules': <String, dynamic>{}},
|
||||
{
|
||||
'type': 'schedules',
|
||||
'schedules': ['not a record'],
|
||||
},
|
||||
]) {
|
||||
gateway.replies.add(reply);
|
||||
await expectLater(
|
||||
repository.listSchedules(),
|
||||
throwsA(isA<SchedulesRepositoryException>()),
|
||||
);
|
||||
}
|
||||
});
|
||||
|
||||
test('save requires a saved schedule in the reply', () async {
|
||||
final gateway = RecordingGateway();
|
||||
final repository = SchedulesRepository(gateway);
|
||||
for (final reply in [
|
||||
<String, dynamic>{},
|
||||
{'type': 'schedule'},
|
||||
{'type': 'schedule', 'schedule': null},
|
||||
{'type': 'schedule', 'schedule': 'record'},
|
||||
]) {
|
||||
gateway.replies.add(reply);
|
||||
await expectLater(
|
||||
repository.saveSchedule(createRecord()),
|
||||
throwsA(
|
||||
isA<SchedulesRepositoryException>().having(
|
||||
(error) => error.message,
|
||||
'message',
|
||||
'The server returned no saved schedule.',
|
||||
),
|
||||
),
|
||||
);
|
||||
}
|
||||
});
|
||||
|
||||
test('an empty schedule object is not turned into a fake identity', () async {
|
||||
final gateway = RecordingGateway()
|
||||
..replies.add({'type': 'schedule', 'schedule': <String, dynamic>{}});
|
||||
final repository = SchedulesRepository(gateway);
|
||||
|
||||
final saved = await repository.saveSchedule(createRecord());
|
||||
|
||||
expect(saved.id, isNull);
|
||||
expect(saved.revision, isNull);
|
||||
expect(saved.toJson(), isEmpty);
|
||||
});
|
||||
}
|
||||
@@ -0,0 +1,81 @@
|
||||
import 'package:flutter_test/flutter_test.dart';
|
||||
import 'package:pipelines_app/features/schedules/data/schedules_repository.dart';
|
||||
|
||||
import 'schedule_test_support.dart';
|
||||
|
||||
void main() {
|
||||
test(
|
||||
'list rejects non-positive and unsafe pipeline IDs before sending',
|
||||
() async {
|
||||
final gateway = RecordingGateway();
|
||||
final repository = SchedulesRepository(gateway);
|
||||
|
||||
for (final id in [0, -1, 9007199254740992]) {
|
||||
await expectLater(
|
||||
repository.listSchedules(pipelineId: id),
|
||||
throwsA(
|
||||
isA<SchedulesRepositoryException>().having(
|
||||
(error) => error.message,
|
||||
'message',
|
||||
'A positive pipeline ID is required.',
|
||||
),
|
||||
),
|
||||
);
|
||||
}
|
||||
expect(gateway.calls, isEmpty);
|
||||
},
|
||||
);
|
||||
|
||||
test(
|
||||
'delete rejects an inexact identity or revision before sending',
|
||||
() async {
|
||||
final gateway = RecordingGateway();
|
||||
final repository = SchedulesRepository(gateway);
|
||||
|
||||
for (final pair in [
|
||||
(0, 1),
|
||||
(-1, 1),
|
||||
(8, 0),
|
||||
(8, -1),
|
||||
(8, 9007199254740992),
|
||||
]) {
|
||||
await expectLater(
|
||||
repository.deleteSchedule(pair.$1, pair.$2),
|
||||
throwsA(
|
||||
isA<SchedulesRepositoryException>().having(
|
||||
(error) => error.message,
|
||||
'message',
|
||||
'An exact schedule ID and revision are required.',
|
||||
),
|
||||
),
|
||||
);
|
||||
}
|
||||
expect(gateway.calls, isEmpty);
|
||||
},
|
||||
);
|
||||
|
||||
test(
|
||||
'request keys are bounded at 128 characters like the web client',
|
||||
() async {
|
||||
final gateway = RecordingGateway();
|
||||
final repository = SchedulesRepository(gateway);
|
||||
|
||||
await expectLater(
|
||||
repository.saveSchedule(createRecord(), requestKey: 'x' * 129),
|
||||
throwsA(
|
||||
isA<SchedulesRepositoryException>().having(
|
||||
(error) => error.message,
|
||||
'message',
|
||||
'Schedule request keys must be strings of at most 128 characters.',
|
||||
),
|
||||
),
|
||||
);
|
||||
expect(gateway.calls, isEmpty);
|
||||
|
||||
final key = 'x' * 128;
|
||||
gateway.replies.add({'type': 'schedule', 'schedule': savedRecord()});
|
||||
await repository.saveSchedule(createRecord(), requestKey: key);
|
||||
expect(gateway.calls.single.fields['request_key'], key);
|
||||
},
|
||||
);
|
||||
}
|
||||
@@ -0,0 +1,132 @@
|
||||
import 'package:flutter_test/flutter_test.dart';
|
||||
import 'package:pipelines_app/features/schedules/data/schedules_repository.dart';
|
||||
import 'package:pipelines_app/features/schedules/domain/saved_schedule.dart';
|
||||
|
||||
import 'schedule_test_support.dart';
|
||||
|
||||
void main() {
|
||||
test(
|
||||
'lists with the exact web envelope and retains every record field',
|
||||
() async {
|
||||
final record = savedRecord();
|
||||
final gateway = RecordingGateway()
|
||||
..replies.add({
|
||||
'type': 'schedules',
|
||||
'schedules': [record],
|
||||
});
|
||||
final repository = SchedulesRepository(gateway);
|
||||
|
||||
final listed = await repository.listSchedules(pipelineId: 4);
|
||||
|
||||
expect(gateway.calls.single.type, 'list_schedules');
|
||||
expect(gateway.calls.single.fields, {'pipeline_id': 4});
|
||||
expect(listed.single.toJson(), record);
|
||||
expect(listed.single.pipelineId, 4);
|
||||
expect(listed.single.version, 2);
|
||||
expect(listed.single.prompt, 'Research batteries');
|
||||
expect(listed.single.workspace, '/srv/research');
|
||||
expect(listed.single.enabled, isTrue);
|
||||
expect(listed.single.rule.every, '24h');
|
||||
expect(listed.single.rule.days, ['mon', 'wed']);
|
||||
expect(listed.single.rule.timezone, 'Europe/London');
|
||||
expect(listed.single.model, 'openai/gpt-5');
|
||||
expect(listed.single.notifyMode, 'off');
|
||||
expect(listed.single.toJson()['future_server_field'], {
|
||||
'nested': [
|
||||
1,
|
||||
2,
|
||||
{'deep': true},
|
||||
],
|
||||
});
|
||||
|
||||
gateway.replies.add({'type': 'schedules'});
|
||||
await repository.listSchedules();
|
||||
expect(gateway.calls.last.type, 'list_schedules');
|
||||
expect(gateway.calls.last.fields, isEmpty);
|
||||
},
|
||||
);
|
||||
|
||||
test(
|
||||
'create sends the web sentinel record without inventing a revision',
|
||||
() async {
|
||||
final create = createRecord();
|
||||
final original = deepCopy(create);
|
||||
final gateway = RecordingGateway()
|
||||
..replies.add({'type': 'schedule', 'schedule': savedRecord()});
|
||||
final repository = SchedulesRepository(gateway);
|
||||
|
||||
final saved = await repository.saveSchedule(create);
|
||||
|
||||
expect(gateway.calls.single.type, 'save_schedule');
|
||||
expect(gateway.calls.single.fields, {'schedule': create});
|
||||
expect(gateway.calls.single.fields.containsKey('request_key'), isFalse);
|
||||
expect(
|
||||
gateway.calls.single.fields.containsKey('expected_revision'),
|
||||
isFalse,
|
||||
);
|
||||
expect(create, original, reason: 'the caller record must be unchanged');
|
||||
expect(saved.id, 8);
|
||||
expect(saved.revision, 3);
|
||||
expect(saved.toJson(), savedRecord());
|
||||
},
|
||||
);
|
||||
|
||||
test(
|
||||
'a stable create key is forwarded exactly and blanks are omitted',
|
||||
() async {
|
||||
final create = createRecord();
|
||||
final gateway = RecordingGateway();
|
||||
final repository = SchedulesRepository(gateway);
|
||||
|
||||
gateway.replies.add({'type': 'schedule', 'schedule': savedRecord()});
|
||||
await repository.saveSchedule(
|
||||
create,
|
||||
requestKey: 'schedule:stable-editor-key',
|
||||
);
|
||||
expect(gateway.calls.last.fields, {
|
||||
'schedule': create,
|
||||
'request_key': 'schedule:stable-editor-key',
|
||||
});
|
||||
|
||||
gateway.replies.add({'type': 'schedule', 'schedule': savedRecord()});
|
||||
await repository.saveSchedule(create, requestKey: '');
|
||||
expect(gateway.calls.last.fields, {'schedule': create});
|
||||
},
|
||||
);
|
||||
|
||||
test('update reuses the read revision as the CAS token', () async {
|
||||
final current = SavedSchedule(savedRecord());
|
||||
final paused = current.withEnabled(false);
|
||||
expect(paused.id, 8);
|
||||
expect(paused.revision, 3);
|
||||
expect(paused.toJson()['future_server_field'], isNotNull);
|
||||
|
||||
final gateway = RecordingGateway()
|
||||
..replies.add({
|
||||
'type': 'schedule',
|
||||
'schedule': {...savedRecord(), 'enabled': false, 'revision': 4},
|
||||
});
|
||||
final repository = SchedulesRepository(gateway);
|
||||
|
||||
final updated = await repository.saveSchedule(paused.toJson());
|
||||
|
||||
final body = gateway.calls.single.fields['schedule'] as Map;
|
||||
expect(gateway.calls.single.type, 'save_schedule');
|
||||
expect(gateway.calls.single.fields.containsKey('request_key'), isFalse);
|
||||
expect(
|
||||
gateway.calls.single.fields.containsKey('expected_revision'),
|
||||
isFalse,
|
||||
);
|
||||
expect(body['id'], 8);
|
||||
expect(body['revision'], 3);
|
||||
expect(body['enabled'], isFalse);
|
||||
expect(body['future_server_field'], isNotNull);
|
||||
expect(updated.revision, 4);
|
||||
expect(updated.enabled, isFalse);
|
||||
|
||||
// The stale draft keeps its token: a conflict must not be papered over by
|
||||
// silently adopting the reply revision.
|
||||
expect(current.revision, 3);
|
||||
expect(current.enabled, isTrue);
|
||||
});
|
||||
}
|
||||
Reference in New Issue
Block a user