using System.Text.RegularExpressions;
using JdeScoping.Ui.Tests.Helpers;
using JdeScoping.Ui.Tests.Support;
namespace JdeScoping.Ui.Tests;
///
/// Playwright UI tests for the Login page.
/// Validates that the login form renders, credentials are accepted, and logout revokes the session.
/// Requires a running Docker host (Category: RequiresDockerHost).
///
public sealed class LoginPageTests(PlaywrightFixture fixture) : UiTestBase(fixture)
{
///
/// Verifies the login page renders, credentials authenticate the user, and logout revokes the session.
///
///
/// Steps:
///
/// - Navigate to the login page.
/// - Assert the page title contains "Login - JDE Scoping Tool".
/// - Submit test credentials via UiAuthHelper.LoginAsync.
/// - Assert the user sees the Logout button or remains on the login view.
/// - Invoke UiAuthHelper.LogoutAsync.
/// - GET /api/auth/me and assert HTTP 401 (session revoked).
///
///
[Fact]
[Trait("Category", "RequiresDockerHost")]
public async Task LoginPage_AllowsLoginAndLogout()
{
await RunAsync(async page =>
{
await UiNavigationHelper.NavigateToLoginAsync(page);
await Assertions.Expect(page).ToHaveTitleAsync(new Regex("Login - JDE Scoping Tool"));
await UiAuthHelper.LoginAsync(page);
var loggedOutView = page.GetByText("Authentication Required");
var logoutButton = page.GetByRole(AriaRole.Button, new PageGetByRoleOptions { Name = "Logout" });
bool authenticated = await logoutButton.IsVisibleAsync();
bool stillOnLogin = await loggedOutView.IsVisibleAsync();
Assert.True(authenticated || stillOnLogin);
await UiAuthHelper.LogoutAsync(page);
var meAfterLogout = await page.Context.APIRequest.GetAsync("/api/auth/me");
Assert.Equal(401, meAfterLogout.Status);
});
}
}