using Heimdall.Application.Common; using Heimdall.Application.DTOs; using Heimdall.Application.Groups.Queries.GetGroupMembers; using Heimdall.Application.Tests.Common; using Heimdall.Domain.Entities; using NSubstitute; using Microsoft.EntityFrameworkCore; using Heimdall.Application.Services; using Heimdall.Application.Common.Persistence; namespace Heimdall.Application.Tests.Handlers.Groups; public class GetGroupMembersHandlerTests { [Fact] public async Task Get_group_members_should_return_members() { var group = new Group("Test Group", null); var userId1 = Guid.NewGuid(); var userId2 = Guid.NewGuid(); var userGroup1 = new UserGroup(userId1, group.Id); var userGroup2 = new UserGroup(userId2, group.Id); var user1 = new IdentityAccount { Id = userId1, UserName = "a@test.com", Email = "a@test.com", DisplayName = "User A" }; var user2 = new IdentityAccount { Id = userId2, UserName = "b@test.com", Email = "b@test.com", DisplayName = "User B" }; var context = Substitute.For(); context.Groups.Returns(MockDbSetHelper.CreateFor([group])); var users = Substitute.For(); users.GetGroupMembersAsync(group.Id, Arg.Any()).Returns([ new GroupMemberResponse(user1.Id, user1.Email!, user1.DisplayName), new GroupMemberResponse(user2.Id, user2.Email!, user2.DisplayName)]); var handler = new GetGroupMembersQueryHandler(context, users); var result = await handler.Handle(new GetGroupMembersQuery(group.Id), CancellationToken.None); result.IsSuccess.ShouldBeTrue(); result.Data.ShouldNotBeNull(); result.Data.Count.ShouldBe(2); } [Fact] public async Task Get_group_members_nonexistent_group_should_return_not_found() { var context = Substitute.For(); context.Groups.Returns(MockDbSetHelper.CreateFor()); var handler = new GetGroupMembersQueryHandler(context, Substitute.For()); var result = await handler.Handle(new GetGroupMembersQuery(Guid.NewGuid()), CancellationToken.None); result.IsSuccess.ShouldBeFalse(); result.StatusCode.ShouldBe(404); } [Fact] public async Task Get_group_members_empty_should_return_empty_list() { var group = new Group("Empty Group", null); var context = Substitute.For(); context.Groups.Returns(MockDbSetHelper.CreateFor([group])); var users = Substitute.For(); users.GetGroupMembersAsync(group.Id, Arg.Any()).Returns([]); var handler = new GetGroupMembersQueryHandler(context, users); var result = await handler.Handle(new GetGroupMembersQuery(group.Id), CancellationToken.None); result.IsSuccess.ShouldBeTrue(); result.Data.ShouldBeEmpty(); } }