using Bunit; using Bunit.TestDoubles; using Fluxor; using GoodWood.Application.DTOs; using GoodWood.Application.Interfaces; using GoodWood.Web.Store.AuthState; using GoodWood.Web.Store.ClubState; using Microsoft.AspNetCore.Authorization; using Microsoft.AspNetCore.Http; using Microsoft.Extensions.DependencyInjection; using MudBlazor; using MudBlazor.Services; using System.Security.Claims; namespace GoodWood.Tests.Components; /// /// Tests for the Home page component (now redirects to Dashboard). /// public class HomePageTests : IDisposable { private readonly TestContext _ctx; private readonly Mock _userServiceMock; private readonly Mock _clubContextMock; private readonly Mock _dispatcherMock; private readonly Mock> _authStateMock; private readonly Mock> _clubStateMock; private readonly Mock _authorizationServiceMock; public HomePageTests() { _ctx = new TestContext(); _userServiceMock = new Mock(); _clubContextMock = new Mock(); _dispatcherMock = new Mock(); // Setup default club context _clubContextMock.Setup(c => c.ClubId).Returns(Guid.NewGuid()); _clubContextMock.Setup(c => c.ClubName).Returns("Test Club"); // Setup Fluxor states _authStateMock = new Mock>(); _authStateMock.Setup(s => s.Value).Returns(AuthState.Initial); _clubStateMock = new Mock>(); _clubStateMock.Setup(s => s.Value).Returns(ClubState.Initial); // Setup authorization service mock _authorizationServiceMock = new Mock(); _authorizationServiceMock .Setup(s => s.AuthorizeAsync( It.IsAny(), It.IsAny(), It.IsAny())) .ReturnsAsync(AuthorizationResult.Success()); // Add test authorization first before any other services _ctx.AddTestAuthorization().SetAuthorized("testuser"); // Register all services _ctx.Services.AddMudServices(options => { options.SnackbarConfiguration.PositionClass = Defaults.Classes.Position.BottomRight; }); _ctx.Services.AddSingleton(_userServiceMock.Object); _ctx.Services.AddSingleton(_clubContextMock.Object); _ctx.Services.AddSingleton(_dispatcherMock.Object); _ctx.Services.AddSingleton(_authStateMock.Object); _ctx.Services.AddSingleton(_clubStateMock.Object); _ctx.Services.AddSingleton(_authorizationServiceMock.Object); // Additional Fluxor services _ctx.Services.AddSingleton(new Mock().Object); _ctx.Services.AddSingleton(new Mock().Object); // ASP.NET Core services var httpContextAccessorMock = new Mock(); httpContextAccessorMock.Setup(x => x.HttpContext).Returns(new DefaultHttpContext()); _ctx.Services.AddSingleton(httpContextAccessorMock.Object); // Stub complex components _ctx.ComponentFactories.AddStub(); } public void Dispose() { _ctx.Dispose(); } [Fact] public void HomePage_RedirectsToDashboard_WhenAuthorized() { // Arrange _authStateMock.Setup(s => s.Value).Returns(CreateAuthState()); // Act _ctx.RenderComponent(); // Assert - Home page should redirect to Dashboard var navMan = _ctx.Services.GetRequiredService(); navMan.Uri.Should().EndWith("/dashboard"); } [Fact] public void HomePage_RendersNoContent_BecauseItRedirects() { // Arrange _authStateMock.Setup(s => s.Value).Returns(CreateAuthState()); // Act var cut = _ctx.RenderComponent(); // Assert - Home page renders no visible content since it redirects cut.Markup.Should().BeEmpty(); } private static AuthState CreateAuthState(string displayName = "Test User") { return AuthState.Initial with { IsAuthenticated = true, IsLoading = false, CurrentUser = new UserDto { ProfileId = Guid.NewGuid(), IdentityUserId = Guid.NewGuid(), DisplayName = displayName, Identity = new ApplicationUserDto { Id = Guid.NewGuid(), Email = "test@example.com", UserName = "test@example.com" } }, Roles = [], Error = null }; } }