using Big.Application; using Big.Domain; namespace Big.Tests; public class WithdrawRegistrationTests { private static Registration Submitted(string processInstanceId = "proc-1") { var registration = Registration.Submit("123456782"); registration.RecordProcessStarted(processInstanceId); return registration; } [Fact] public async Task Withdrawing_marks_the_registration_ingetrokken_and_persists_it() { var store = new FakeRegistrationStore(); var registration = Submitted(); store.Seed(registration); var handler = new WithdrawRegistration(store, new FakeWorkflowClient()); await handler.HandleAsync(new WithdrawRegistrationCommand(registration.Id)); var saved = await store.GetAsync(registration.Id); Assert.Equal(RegistrationStatus.Ingetrokken, saved!.Status); Assert.Equal(1, store.SaveCount); } [Fact] public async Task Withdrawing_cancels_the_running_process() { var store = new FakeRegistrationStore(); var registration = Submitted("proc-42"); store.Seed(registration); var workflow = new FakeWorkflowClient(); var handler = new WithdrawRegistration(store, workflow); await handler.HandleAsync(new WithdrawRegistrationCommand(registration.Id)); // The process the registration recorded at submit is cancelled (ADR-0014). Assert.Equal("proc-42", workflow.WithdrawnProcessInstanceId); } [Fact] public async Task Withdrawing_before_a_process_was_started_still_marks_ingetrokken() { // A registration with no recorded process (e.g. start never happened) can still be withdrawn; // there is simply no process to cancel. var store = new FakeRegistrationStore(); var registration = Registration.Submit("123456782"); // no RecordProcessStarted store.Seed(registration); var workflow = new FakeWorkflowClient(); var handler = new WithdrawRegistration(store, workflow); await handler.HandleAsync(new WithdrawRegistrationCommand(registration.Id)); Assert.Equal(RegistrationStatus.Ingetrokken, (await store.GetAsync(registration.Id))!.Status); Assert.Null(workflow.WithdrawnProcessInstanceId); } [Fact] public async Task Rejects_a_null_command_without_touching_the_store() { var store = new FakeRegistrationStore(); var handler = new WithdrawRegistration(store, new FakeWorkflowClient()); await Assert.ThrowsAsync(() => handler.HandleAsync(null!)); Assert.Equal(0, store.SaveCount); } [Fact] public async Task Withdrawing_an_unknown_registration_throws() { var store = new FakeRegistrationStore(); var handler = new WithdrawRegistration(store, new FakeWorkflowClient()); var ex = await Assert.ThrowsAsync( () => handler.HandleAsync(new WithdrawRegistrationCommand(RegistrationId.New()))); Assert.Contains("No registration", ex.Message); Assert.Equal(0, store.SaveCount); } [Fact] public async Task Re_withdrawing_an_already_ingetrokken_registration_is_idempotent() { var store = new FakeRegistrationStore(); var registration = Submitted(); store.Seed(registration); var workflow = new FakeWorkflowClient(); var handler = new WithdrawRegistration(store, workflow); await handler.HandleAsync(new WithdrawRegistrationCommand(registration.Id)); await handler.HandleAsync(new WithdrawRegistrationCommand(registration.Id)); // The second withdrawal is a no-op: the aggregate is not persisted again. Assert.Equal(1, store.SaveCount); Assert.Equal(RegistrationStatus.Ingetrokken, (await store.GetAsync(registration.Id))!.Status); } }