using Heimdall.Application.Common; using Heimdall.Application.DTOs; using Heimdall.Application.Sites.Commands.CreateSite; using Heimdall.Application.Tests.Common; using Heimdall.Domain.Entities; using NSubstitute; namespace Heimdall.Application.Tests.Handlers.Sites; public class CreateSiteHandlerTests { [Fact] public async Task Create_site_should_return_success() { var context = Substitute.For(); context.Sites.Returns(MockDbSetHelper.CreateFor()); var handler = new CreateSiteHandler(context); var command = new CreateSiteCommand("Test Site", "Paris", "Europe/Paris"); var result = await handler.Handle(command, CancellationToken.None); result.IsSuccess.ShouldBeTrue(); result.Data.ShouldNotBeNull(); result.Data.Name.ShouldBe("Test Site"); } [Fact] public async Task Duplicate_name_should_return_conflict() { var existingSite = new Site("Test Site", "Paris", "Europe/Paris"); var context = Substitute.For(); context.Sites.Returns(MockDbSetHelper.CreateFor([existingSite])); var handler = new CreateSiteHandler(context); var command = new CreateSiteCommand("Test Site", "Lyon", "Europe/Paris"); var result = await handler.Handle(command, CancellationToken.None); result.IsSuccess.ShouldBeFalse(); result.StatusCode.ShouldBe(409); } [Fact] public async Task Create_site_should_call_save_changes() { var context = Substitute.For(); context.Sites.Returns(MockDbSetHelper.CreateFor()); var handler = new CreateSiteHandler(context); var command = new CreateSiteCommand("New Site", null, "UTC"); await handler.Handle(command, CancellationToken.None); await context.Received(1).SaveChangesWithRetryAsync(Arg.Any(), Arg.Any()); } }