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); }); }