using FluentAssertions; using Microsoft.EntityFrameworkCore; using Microsoft.Extensions.Time.Testing; using NSubstitute; using SlpModularCms.Modules.Contact.Config; using SlpModularCms.Modules.Contact.Data; using SlpModularCms.Modules.Contact.Data.Entities; using SlpModularCms.Modules.Contact.Models; using SlpModularCms.Modules.Contact.Repositories; using SlpModularCms.Modules.Contact.Services; namespace SlpModularCms.Modules.Contact.Tests; public class ContactSettingsServiceTests { private sealed class Setup : IDisposable { public FakeTimeProvider Time { get; } = new(new DateTimeOffset(2026, 11, 9, 12, 0, 0, TimeSpan.Zero)); public ContactOptions Options { get; } = new() { Enabled = true }; public ContactDbContext Db { get; } = new(new DbContextOptionsBuilder() .UseInMemoryDatabase(Guid.NewGuid().ToString()).Options); public ContactSettingsCache Cache { get; } public IContactMailSignal Signal { get; } = Substitute.For(); public ContactSettingsService Sut { get; } public Setup() { Cache = new ContactSettingsCache(Time); Sut = new ContactSettingsService( new ContactRepository(Db), Cache, Microsoft.Extensions.Options.Options.Create(Options), Time, Signal); } public void Dispose() => Db.Dispose(); } [Fact] public async Task WithoutARow_TheSwitchIsOff() { using var s = new Setup(); (await s.Sut.GetAsync(CancellationToken.None)).Should().Be(new ContactSettingsDto(false, true)); (await s.Sut.IsEnabledAsync(CancellationToken.None)).Should().BeFalse(); } [Fact] public async Task HostEnabled_ReflectsTheHostSwitch_ReadOnly() { using var s = new Setup(); s.Options.Enabled = false; (await s.Sut.GetAsync(CancellationToken.None)).HostEnabled.Should().BeFalse(); } [Fact] public async Task SettingTheSwitch_CreatesTheSingletonRow_ThenUpdatesIt_AndRemembersWhoAndWhen() { using var s = new Setup(); var user = Guid.NewGuid(); var on = await s.Sut.SetEnabledAsync(true, user, CancellationToken.None); s.Time.Advance(TimeSpan.FromMinutes(5)); var off = await s.Sut.SetEnabledAsync(false, Guid.NewGuid(), CancellationToken.None); on.Should().Be(new ContactSettingsDto(true, true)); off.Should().Be(new ContactSettingsDto(false, true)); var rows = await s.Db.ContactSettings.AsNoTracking().ToListAsync(); rows.Should().ContainSingle().Which.Id.Should().Be(ContactSettings.SingletonId); rows[0].Enabled.Should().BeFalse(); rows[0].UpdatedAt.Should().Be(s.Time.GetUtcNow()); } [Fact] public async Task TheUserWhoChangedItIsStored() { using var s = new Setup(); var user = Guid.NewGuid(); await s.Sut.SetEnabledAsync(true, user, CancellationToken.None); (await s.Db.ContactSettings.AsNoTracking().SingleAsync()).UpdatedByUserId.Should().Be(user); } [Fact] public async Task ThePublicCheck_IsCachedBriefly_AndRefreshedAfterwards() { using var s = new Setup(); await s.Sut.SetEnabledAsync(true, Guid.NewGuid(), CancellationToken.None); (await s.Sut.IsEnabledAsync(CancellationToken.None)).Should().BeTrue(); // fills the cache // Another instance flips the row behind this one's back. var row = await s.Db.ContactSettings.SingleAsync(); row.Enabled = false; await s.Db.SaveChangesAsync(); s.Time.Advance(TimeSpan.FromSeconds(4)); (await s.Sut.IsEnabledAsync(CancellationToken.None)).Should().BeTrue("still within the cache lifetime"); s.Time.Advance(TimeSpan.FromSeconds(2)); (await s.Sut.IsEnabledAsync(CancellationToken.None)).Should().BeFalse("the cache expired"); } [Fact] public async Task ChangingTheSwitch_TakesEffectAtOnceInThisProcess() { using var s = new Setup(); (await s.Sut.IsEnabledAsync(CancellationToken.None)).Should().BeFalse(); // cached: off await s.Sut.SetEnabledAsync(true, Guid.NewGuid(), CancellationToken.None); (await s.Sut.IsEnabledAsync(CancellationToken.None)).Should().BeTrue(); } [Fact] public async Task TheAdministratorsOwnView_IsNeverServedFromTheCache() { using var s = new Setup(); await s.Sut.SetEnabledAsync(true, Guid.NewGuid(), CancellationToken.None); await s.Sut.IsEnabledAsync(CancellationToken.None); var row = await s.Db.ContactSettings.SingleAsync(); row.Enabled = false; await s.Db.SaveChangesAsync(); (await s.Sut.GetAsync(CancellationToken.None)).Enabled.Should().BeFalse(); } [Fact] public void TheCacheLifetime_IsFiveSeconds() { ContactSettingsCache.Lifetime.Should().Be(TimeSpan.FromSeconds(5)); } } public class ContactFormGateTests { private static IContactFormGate Layer(bool open) { var gate = Substitute.For(); gate.IsOpenAsync(Arg.Any()).Returns(open); return gate; } [Theory] [InlineData(true, true, true)] [InlineData(true, false, false)] [InlineData(false, true, false)] [InlineData(false, false, false)] public async Task TheFormIsOpen_OnlyWhenEveryLayerIsOpen(bool host, bool cms, bool expected) { var gate = new CompositeContactFormGate([Layer(host), Layer(cms)]); (await gate.IsOpenAsync(CancellationToken.None)).Should().Be(expected); } [Fact] public async Task WhenTheFirstLayerIsClosed_TheSecondIsNeverAsked_SoTheDatabaseIsNotTouched() { var host = Layer(false); var cms = Layer(true); await new CompositeContactFormGate([host, cms]).IsOpenAsync(CancellationToken.None); await host.Received(1).IsOpenAsync(Arg.Any()); await cms.DidNotReceive().IsOpenAsync(Arg.Any()); } [Fact] public async Task TheCmsLayer_FollowsTheSettingsService() { var settings = Substitute.For(); settings.IsEnabledAsync(Arg.Any()).Returns(true, false); var gate = new CmsFlagContactFormGate(settings); (await gate.IsOpenAsync(CancellationToken.None)).Should().BeTrue(); (await gate.IsOpenAsync(CancellationToken.None)).Should().BeFalse(); } }