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 [ {}, {'type': 'pong'}, {'type': 'schedule_deleted'}, {'type': 'schedule_deleted', 'schedule_id': 9}, ]) { gateway.replies.add(reply); await expectLater( repository.deleteSchedule(8, 3), throwsA( isA().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 [ {}, {'type': 'pong'}, {'type': 'schedules', 'schedules': {}}, { 'type': 'schedules', 'schedules': ['not a record'], }, ]) { gateway.replies.add(reply); await expectLater( repository.listSchedules(), throwsA(isA()), ); } }); test('save requires a saved schedule in the reply', () async { final gateway = RecordingGateway(); final repository = SchedulesRepository(gateway); for (final reply in [ {}, {'type': 'schedule'}, {'type': 'schedule', 'schedule': null}, {'type': 'schedule', 'schedule': 'record'}, ]) { gateway.replies.add(reply); await expectLater( repository.saveSchedule(createRecord()), throwsA( isA().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': {}}); final repository = SchedulesRepository(gateway); final saved = await repository.saveSchedule(createRecord()); expect(saved.id, isNull); expect(saved.revision, isNull); expect(saved.toJson(), isEmpty); }); }