using EventSubscriber.Application; namespace EventSubscriber.Tests; /// Behaviour of the projector that turns NRC notifications into projection rows. /// The walking skeleton reacts only to a zaak being created (status INGEDIEND) and must /// tolerate duplicate and out-of-order deliveries (CLAUDE.md §8.6). public sealed class NotificationProjectorTests { private const string ZaakUrl = "http://openzaak:8000/zaken/api/v1/zaken/11111111-1111-1111-1111-111111111111"; private readonly InMemoryNotificationLog _log = new(); private readonly InMemoryProjectionStore _store = new(); private NotificationProjector Projector() => new(_log, _store); private static Notification ZaakCreated(string url = ZaakUrl) => new("zaken", "zaak", "create", new Uri(url)); [Fact] public async Task creating_a_zaak_writes_one_row_with_status_ingediend() { await Projector().HandleAsync(ZaakCreated()); var entry = Assert.Single(await _store.AllAsync()); Assert.Equal("11111111-1111-1111-1111-111111111111", entry.Id); Assert.Equal(RegistrationStatus.Ingediend, entry.Status); } [Fact] public async Task replaying_the_same_notification_keeps_a_single_row() { var projector = Projector(); await projector.HandleAsync(ZaakCreated()); await projector.HandleAsync(ZaakCreated()); Assert.Single(await _store.AllAsync()); } [Fact] public async Task a_replayed_notification_never_reaches_the_projection_store() { var projector = Projector(); await projector.HandleAsync(ZaakCreated()); await projector.HandleAsync(ZaakCreated()); // The duplicate is dropped at the log, before the (idempotent) upsert — so the store // is written exactly once. Row count alone can't see this; the upsert count can. Assert.Equal(1, _store.UpsertCount); } [Fact] public async Task two_different_zaken_each_get_their_own_row() { var projector = Projector(); await projector.HandleAsync(ZaakCreated()); await projector.HandleAsync(ZaakCreated(ZaakUrl[..^1] + "2")); // a distinct zaak url Assert.Equal(2, (await _store.AllAsync()).Count); } [Theory] [InlineData("documenten", "enkelvoudiginformatieobject", "create")] // wrong kanaal + resource [InlineData("documenten", "zaak", "create")] // wrong kanaal only [InlineData("zaken", "status", "create")] // wrong resource only [InlineData("zaken", "zaak", "update")] // wrong actie [InlineData("zaken", "zaak", "destroy")] // wrong actie public async Task only_a_zaken_zaak_create_notification_is_projected(string kanaal, string resource, string actie) { await Projector().HandleAsync(new Notification(kanaal, resource, actie, new Uri(ZaakUrl))); Assert.Empty(await _store.AllAsync()); } [Fact] public async Task rebuild_clears_stale_rows_and_repopulates_from_the_notification_log() { var projector = Projector(); await projector.HandleAsync(ZaakCreated()); // A stale row that is not backed by any logged notification must not survive a rebuild. await _store.UpsertAsync(new RegisterEntry("stale-9999", RegistrationStatus.Ingediend)); await projector.RebuildAsync(); var entry = Assert.Single(await _store.AllAsync()); Assert.Equal("11111111-1111-1111-1111-111111111111", entry.Id); Assert.Equal(RegistrationStatus.Ingediend, entry.Status); } }