diff --git a/dotnet/OpenWaf.Tests.Unit/DurabilityHealthTests.cs b/dotnet/OpenWaf.Tests.Unit/DurabilityHealthTests.cs new file mode 100644 index 0000000..781f894 --- /dev/null +++ b/dotnet/OpenWaf.Tests.Unit/DurabilityHealthTests.cs @@ -0,0 +1,79 @@ +using OpenWaf.Infrastructure.Persistence; + +namespace OpenWaf.Tests.Unit; + +/// +/// OPEN-31's second acceptance criterion — "EntriesLost is cumulative, not a +/// gauge: a later successful write does not erase what was already lost" — was +/// written into the code and into the item, and no test would have noticed if it +/// stopped being true. The integration suite asserts the count after one failed +/// flush and after a later success, and both of those read the same value under +/// `EntriesLost = entriesLost` as under `EntriesLost += entriesLost`: the +/// difference only shows across two failed writes carrying different numbers of +/// rows, which nothing was doing. A criterion with no test that can fail is a +/// comment with a number next to it, so the invariant gets its own test here, +/// and the mutation was run: changing `+=` to `=` fails this file and nothing +/// else. +/// +public sealed class DurabilityHealthTests +{ + [Fact] + public void Rows_lost_in_separate_failed_writes_add_up_rather_than_replace_each_other() + { + var h = new DurabilityHealth(); + h.RecordFailure(DurabilityHealth.Observations, "gate: durable write blocked", 2); + h.RecordFailure(DurabilityHealth.Observations, "gate: durable write blocked", 3); + + var p = h.Get(DurabilityHealth.Observations); + Assert.Equal(5, p.EntriesLost); + // The failure counter is consecutive-since-last-success; the lost-row + // counter is a running total. Two different claims, one object. + Assert.Equal(2, p.Failures); + } + + [Fact] + public void A_later_successful_write_clears_the_failure_counter_and_keeps_the_rows_lost() + { + var h = new DurabilityHealth(); + h.RecordFailure(DurabilityHealth.Alerts, "gate: durable write blocked", 4); + h.RecordSuccess(DurabilityHealth.Alerts, 1); + + var p = h.Get(DurabilityHealth.Alerts); + Assert.Equal(0, p.Failures); + Assert.Null(p.LastError); + Assert.NotNull(p.LastSuccessAt); + // The row that never landed is still missing. Saying "healthy" and + // forgetting the count is how the surface starts lying again. + Assert.Equal(4, p.EntriesLost); + } + + [Fact] + public void A_success_on_one_pillar_does_not_clear_another_pillar_failure() + { + // Recovery is earned by the pillar that lost the rows. A retention prune + // that runs happily says nothing about whether the alert writer can + // write, and clearing across pillars would be the same lie pointing the + // other way. + var h = new DurabilityHealth(); + h.RecordFailure(DurabilityHealth.Alerts, "gate: durable write blocked", 1); + h.RecordSuccess(DurabilityHealth.Retention, 12); + + Assert.True(h.Get(DurabilityHealth.Alerts).Failing); + Assert.False(h.Get(DurabilityHealth.Retention).Failing); + } + + [Fact] + public void A_failure_records_the_reason_underneath_the_wrapper_not_the_wrapper() + { + // Measured: EF's DbUpdateException.Message is "An error occurred while + // saving the entity changes…". The thing an operator can act on — + // "no such table: policy_settings" — is one level down. + var wrapper = new InvalidOperationException("An error occurred while saving the entity changes.", + new InvalidOperationException("no such table: policy_settings")); + var h = new DurabilityHealth(); + h.RecordFailure(DurabilityHealth.Bans, wrapper, 1); + + Assert.Equal("no such table: policy_settings", + h.Get(DurabilityHealth.Bans).LastError); + } +}