using Heimdall.Application.Common; using Heimdall.Application.Groups.Commands.CreateGroup; using Heimdall.Application.Tests.Common; using Heimdall.Domain.Entities; using NSubstitute; using Microsoft.EntityFrameworkCore; namespace Heimdall.Application.Tests.Handlers.Groups; public class CreateGroupHandlerTests { [Fact] public async Task Create_group_should_return_success() { var context = Substitute.For(); context.Groups.Returns(MockDbSetHelper.CreateFor()); context.AuditLogs.Returns(MockDbSetHelper.CreateFor()); var handler = new CreateGroupCommandHandler(context); var result = await handler.Handle( new CreateGroupCommand("New Group", "A test group"), CancellationToken.None); result.IsSuccess.ShouldBeTrue(); result.Data.ShouldNotBeNull(); result.Data.Name.ShouldBe("New Group"); await context.Received(1).SaveChangesWithRetryAsync(Arg.Any(), Arg.Any()); } [Fact] public async Task Create_duplicate_group_name_should_return_conflict() { var existingGroup = new Group("Existing Group", null); var context = Substitute.For(); context.Groups.Returns(MockDbSetHelper.CreateFor([existingGroup])); var handler = new CreateGroupCommandHandler(context); var result = await handler.Handle( new CreateGroupCommand("Existing Group", null), CancellationToken.None); result.IsSuccess.ShouldBeFalse(); result.StatusCode.ShouldBe(409); } }