Skip to content

~95% on SimpleQA (e.g. Qwen3.6-27B on a 3090). Supports all local and cloud LLMs (llama.cpp, Ollama, Google, ...). 10+ search engines - arXiv, PubMed, your private documents. Everything Local & Encrypted.

33.2 KB
Raw Blame
#!/usr/bin/env node
/**
 * Comprehensive Authentication UI Tests
 *
 * Tests for login, registration, password features, and session management.
 *
 * Run: node test_auth_comprehensive_ci.js
 */

const { setupTest, teardownTest, TestResults, log, delay, navigateTo, withTimeout, withFreshBrowserContext } = require('./test_lib');

// ============================================================================
// Login Page Tests
// ============================================================================
const LOGIN_FORM = 'form[action*="/auth/login"]';

async function navigateToLogin(page, baseUrl) {
    // Always load the document: navigateTo() intentionally reuses the current
    // path, which could leave a previous failed-login response in this check.
    const response = await page.goto(`${baseUrl}/auth/login`, { waitUntil: 'domcontentloaded' });
    const actual = new URL(page.url());
    if (response?.status() !== 200 || actual.origin !== new URL(baseUrl).origin
        || actual.pathname !== '/auth/login') {
        throw new Error(`Expected the login page, got HTTP ${response?.status()} at ${page.url()}`);
    }
}

const LoginPageTests = {
    async loginFormElements(page, baseUrl) {
        await navigateToLogin(page, baseUrl);
        const result = await page.evaluate((selector) => {
            const form = document.querySelector(selector);
            const username = form?.querySelector('input[name="username"]');
            const password = form?.querySelector('input[name="password"]');
            const submit = form?.querySelector('button[type="submit"], input[type="submit"]');
            const visible = (element) => !!element && element.getClientRects().length > 0
                && getComputedStyle(element).visibility !== 'hidden';
            return {
                hasForm: !!form && form.method === 'post'
                    && new URL(form.action).pathname === '/auth/login',
                hasUsername: visible(username) && username.type === 'text',
                hasPassword: visible(password) && password.type === 'password',
                hasSubmit: visible(submit) && !submit.disabled,
                submitText: submit?.textContent?.trim() || submit?.value,
            };
        }, LOGIN_FORM);
        return {
            passed: result.hasForm && result.hasUsername && result.hasPassword && result.hasSubmit,
            message: `Login form: username=${result.hasUsername}, password=${result.hasPassword}, submit=${result.hasSubmit} ("${result.submitText}")`,
        };
    },

    async loginEmptyFieldValidation(page, baseUrl) {
        await navigateToLogin(page, baseUrl);
        await page.click(`${LOGIN_FORM} button[type="submit"]`);
        const result = await page.evaluate((selector) => {
            const form = document.querySelector(selector);
            const username = form?.querySelector('input[name="username"]');
            const password = form?.querySelector('input[name="password"]');
            return {
                usernameMissing: username?.validity.valueMissing === true,
                passwordMissing: password?.validity.valueMissing === true,
            };
        }, LOGIN_FORM);
        return {
            passed: result.usernameMissing && result.passwordMissing
                && new URL(page.url()).pathname === '/auth/login',
            message: `Empty login fields refused (username=${result.usernameMissing}, password=${result.passwordMissing})`,
        };
    },

    async loginInvalidCredentials(page, baseUrl) {
        await navigateToLogin(page, baseUrl);
        await page.type(`${LOGIN_FORM} input[name="username"]`, `invalid_user_${Date.now()}`);
        await page.type(`${LOGIN_FORM} input[name="password"]`, 'wrong_password_12345');
        const [response] = await Promise.all([
            page.waitForNavigation({ waitUntil: 'domcontentloaded' }),
            page.click(`${LOGIN_FORM} button[type="submit"]`),
        ]);
        const errorText = await page.$$eval('.ldr-auth-container .alert', (alerts) =>
            alerts.filter((element) => element.getClientRects().length > 0
                && getComputedStyle(element).visibility !== 'hidden')
                .map((element) => element.textContent.trim())
                .find((text) => /invalid username or password/i.test(text)) || ''
        );
        const passed = response?.status() === 401 && response.request().method() === 'POST'
            && new URL(page.url()).pathname === '/auth/login' && errorText.length > 0;
        return {
            passed,
            message: `Invalid login returned HTTP ${response?.status()} with visible credential error: "${errorText}"`,
        };
    },

    async loginRememberMeCheckbox(page, baseUrl) {
        await navigateToLogin(page, baseUrl);

        const result = await page.evaluate(() => {
            const rememberMe = document.querySelector('input[name="remember"], input[name="remember_me"], #remember, #remember_me');
            const label = document.querySelector('label[for="remember"], label[for="remember_me"]');

            if (!rememberMe) {
                // Look for checkbox with related text
                const checkboxes = document.querySelectorAll('input[type="checkbox"]');
                for (const cb of checkboxes) {
                    const parentText = cb.parentElement?.textContent?.toLowerCase() || '';
                    if (parentText.includes('remember')) {
                        return {
                            hasRememberMe: true,
                            isCheckbox: cb.type === 'checkbox',
                            labelText: parentText.substring(0, 50)
                        };
                    }
                }
            }

            return {
                hasRememberMe: !!rememberMe,
                isCheckbox: rememberMe?.type === 'checkbox',
                labelText: label?.textContent?.trim()
            };
        });

        if (!result.hasRememberMe) {
            return { passed: false, message: 'Remember me checkbox is missing from the login page' };
        }

        return {
            passed: result.isCheckbox,
            message: `Remember me checkbox found: "${result.labelText}"`
        };
    },

    async loginNavigateToRegister(page, baseUrl) {
        await navigateToLogin(page, baseUrl);
        await delay(300);

        const result = await page.evaluate(() => {
            const registerLink = document.querySelector('a[href*="register"]');
            // Also check for text-based links
            if (!registerLink) {
                const allLinks = document.querySelectorAll('a');
                for (const link of allLinks) {
                    const text = link.textContent?.toLowerCase() || '';
                    if (text.includes('register') || text.includes('sign up') || text.includes('create account')) {
                        return {
                            hasLink: true,
                            href: link.href,
                            text: link.textContent?.trim()
                        };
                    }
                }
            }
            return {
                hasLink: !!registerLink,
                href: registerLink?.href,
                text: registerLink?.textContent?.trim()
            };
        });

        if (!result.hasLink) {
            // Skip instead of fail - some setups may not have register link on login page
            return { passed: null, skipped: true, message: 'No register link found on login page (may be disabled)' };
        }

        // Click and navigate
        await Promise.all([
            page.click('a[href*="register"]'),
            page.waitForNavigation({ waitUntil: 'domcontentloaded', timeout: 10000 }).catch(() => {})
        ]);

        const onRegisterPage = page.url().includes('register');

        return {
            passed: onRegisterPage,
            message: onRegisterPage
                ? `Register link works: "${result.text}"`
                : `Register link found but navigation failed (href: ${result.href})`
        };
    }
};

// ============================================================================
// Registration Page Tests
// ============================================================================
const RegistrationTests = {
    async registerFormElements(page, baseUrl) {
        await navigateTo(page, `${baseUrl}/auth/register`);

        const result = await page.evaluate(() => {
            const form = document.querySelector('form');
            const usernameInput = document.querySelector('input[name="username"]');
            const passwordInput = document.querySelector('input[name="password"]');
            const confirmInput = document.querySelector('input[name="confirm"], input[name="confirm_password"], input[name="password_confirm"]');
            const submitBtn = document.querySelector('button[type="submit"]');
            const loginLink = document.querySelector('a[href*="login"]');

            return {
                hasForm: !!form,
                hasUsername: !!usernameInput,
                hasPassword: !!passwordInput,
                hasConfirm: !!confirmInput,
                hasSubmit: !!submitBtn,
                hasLoginLink: !!loginLink,
                usernamePattern: usernameInput?.pattern,
                usernameMinLength: usernameInput?.minLength
            };
        });

        const passed = result.hasForm && result.hasUsername && result.hasPassword && result.hasSubmit;
        return {
            passed,
            message: `Register form: username=${result.hasUsername}, password=${result.hasPassword}, confirm=${result.hasConfirm}, submit=${result.hasSubmit}`
        };
    },

    async registerUsernameValidation(page, baseUrl) {
        await navigateTo(page, `${baseUrl}/auth/register`);

        const result = await page.evaluate(() => {
            const usernameInput = document.querySelector('input[name="username"]');
            if (!usernameInput) return { hasInput: false };

            // Check validation attributes
            const minLength = usernameInput.minLength;
            const maxLength = usernameInput.maxLength;
            const pattern = usernameInput.pattern;
            const required = usernameInput.required;

            // Test with short username
            usernameInput.value = 'ab';
            const shortValid = usernameInput.validity.valid;

            // Test with special characters
            usernameInput.value = 'test@user!';
            const specialValid = usernameInput.validity.valid;

            // Test with valid username
            usernameInput.value = 'valid_user123';
            const validUsername = usernameInput.validity.valid;

            return {
                hasInput: true,
                minLength,
                maxLength,
                pattern,
                required,
                shortValid,
                specialValid,
                validUsername
            };
        });

        if (!result.hasInput) {
            return { passed: false, message: 'Username input not found' };
        }

        const hasValidation = result.minLength > 0 || result.pattern || !result.shortValid || !result.specialValid;

        return {
            passed: hasValidation,
            message: hasValidation
                ? `Username validation: minLength=${result.minLength}, pattern=${result.pattern ? 'yes' : 'no'}, shortValid=${result.shortValid}`
                : 'No username validation detected'
        };
    },

    async registerPasswordStrengthIndicator(page, baseUrl) {
        await navigateTo(page, `${baseUrl}/auth/register`);

        // Type a weak password
        const passwordInput = await page.$('input[name="password"]');
        if (!passwordInput) {
            return { passed: null, skipped: true, message: 'Password input not found' };
        }

        await passwordInput.type('weak');
        await delay(300);

        const result = await page.evaluate(() => {
            // Look for strength indicator elements
            const strengthBar = document.querySelector('.password-strength, .strength-bar, [class*="strength"], .progress-bar');
            const strengthText = document.querySelector('.strength-text, .password-feedback, [class*="strength-label"]');
            const strengthMeter = document.querySelector('meter[id*="strength"], progress[id*="strength"]');

            // Check for color-coded indicators
            const allElements = document.querySelectorAll('[class*="password"], [class*="strength"]');
            let hasColorIndicator = false;
            for (const el of allElements) {
                const style = window.getComputedStyle(el);
                if (style.backgroundColor !== 'rgba(0, 0, 0, 0)' && style.backgroundColor !== 'transparent') {
                    hasColorIndicator = true;
                    break;
                }
            }

            return {
                hasStrengthBar: !!strengthBar,
                hasStrengthText: !!strengthText,
                hasStrengthMeter: !!strengthMeter,
                hasColorIndicator,
                strengthTextContent: strengthText?.textContent?.trim()
            };
        });

        const hasIndicator = result.hasStrengthBar || result.hasStrengthText || result.hasStrengthMeter || result.hasColorIndicator;

        if (!hasIndicator) {
            return { passed: null, skipped: true, message: 'No password strength indicator found' };
        }

        return {
            passed: true,
            message: `Password strength indicator: bar=${result.hasStrengthBar}, text="${result.strengthTextContent || 'none'}"`
        };
    },

    async registerPasswordConfirmMatch(page, baseUrl) {
        await navigateTo(page, `${baseUrl}/auth/register`);
        await delay(500);

        const result = await page.evaluate(() => {
            const passwordInput = document.querySelector('input[name="password"], #password');
            const confirmInput = document.querySelector('input[name="confirm"], input[name="confirm_password"], input[name="password_confirm"], #confirm, #confirm_password');

            if (!passwordInput || !confirmInput) {
                return { hasInputs: false };
            }

            // Set mismatched passwords
            passwordInput.value = 'TestPassword123!';
            confirmInput.value = 'DifferentPassword456!';

            // Trigger validation
            confirmInput.dispatchEvent(new Event('input', { bubbles: true }));
            confirmInput.dispatchEvent(new Event('blur', { bubbles: true }));

            // Check for mismatch indication
            const confirmValid = confirmInput.validity.valid;
            const hasErrorClass = confirmInput.classList.contains('error') ||
                                 confirmInput.classList.contains('invalid') ||
                                 confirmInput.classList.contains('is-invalid') ||
                                 confirmInput.classList.contains('ldr-invalid');

            // Look for error message - broader search
            const errorMsg = document.querySelector('.password-mismatch, .confirm-error, [class*="mismatch"], .ldr-error');
            const alertVisible = document.querySelector('.alert:not(.d-none), [role="alert"]:not(.d-none)');

            return {
                hasInputs: true,
                confirmValid,
                hasErrorClass,
                hasErrorMsg: !!errorMsg || !!alertVisible,
                customValidity: confirmInput.validationMessage
            };
        });

        if (!result.hasInputs) {
            return { passed: null, skipped: true, message: 'Password confirm input not found' };
        }

        // Check for any validation - be more lenient in CI
        const hasMismatchValidation = !result.confirmValid || result.hasErrorClass ||
                                     result.hasErrorMsg || result.customValidity;

        // If no validation detected, skip instead of fail for CI resilience
        if (!hasMismatchValidation) {
            return { passed: null, skipped: true, message: 'Password mismatch validation not detected (may use server-side validation)' };
        }

        return {
            passed: true,
            message: `Password mismatch validation works (valid=${result.confirmValid}, errorClass=${result.hasErrorClass})`
        };
    },

    async registerAcknowledgeCheckbox(page, baseUrl) {
        await navigateTo(page, `${baseUrl}/auth/register`);

        const result = await page.evaluate(() => {
            // Look for acknowledgment/terms checkbox
            const checkboxes = document.querySelectorAll('input[type="checkbox"]');
            let acknowledgeCheckbox = null;

            for (const cb of checkboxes) {
                const parentText = cb.parentElement?.textContent?.toLowerCase() || '';
                const labelText = document.querySelector(`label[for="${cb.id}"]`)?.textContent?.toLowerCase() || '';
                const text = parentText + labelText;

                if (text.includes('acknowledge') || text.includes('understand') ||
                    text.includes('agree') || text.includes('terms') || text.includes('accept')) {
                    acknowledgeCheckbox = cb;
                    break;
                }
            }

            if (!acknowledgeCheckbox) return { hasCheckbox: false };

            return {
                hasCheckbox: true,
                isRequired: acknowledgeCheckbox.required,
                labelText: acknowledgeCheckbox.parentElement?.textContent?.trim()?.substring(0, 100)
            };
        });

        if (!result.hasCheckbox) {
            return { passed: null, skipped: true, message: 'No acknowledgment checkbox found' };
        }

        return {
            passed: result.isRequired,
            message: `Acknowledgment checkbox: required=${result.isRequired}, text="${result.labelText}..."`
        };
    },

    async registerSecurityWarnings(page, baseUrl) {
        await navigateTo(page, `${baseUrl}/auth/register`);

        const result = await page.evaluate(() => {
            const pageText = document.body.textContent?.toLowerCase() || '';

            return {
                mentionsEncryption: pageText.includes('encrypt'),
                mentionsRecovery: pageText.includes('recover') || pageText.includes('cannot be recovered'),
                mentionsPassword: pageText.includes('password') && (pageText.includes('important') || pageText.includes('warning')),
                mentionsSecurity: pageText.includes('security') || pageText.includes('secure'),
                hasWarningElement: !!document.querySelector('.warning, .alert-warning, [class*="warning"]')
            };
        });

        const hasSecurityInfo = result.mentionsEncryption || result.mentionsRecovery || result.hasWarningElement;

        return {
            passed: hasSecurityInfo,
            message: hasSecurityInfo
                ? `Security warnings present (encryption=${result.mentionsEncryption}, recovery=${result.mentionsRecovery})`
                : 'No security warnings found'
        };
    }
};

// ============================================================================
// Session Management Tests
// ============================================================================
const SessionTests = {
    async sessionPersistsOnRefresh(page, baseUrl) {
        // This test requires being logged in first
        await navigateTo(page, `${baseUrl}/`);
        await delay(500);

        // Check if we're logged in - check multiple indicators
        const loggedInBefore = await page.evaluate(() => {
            const logoutLink = document.querySelector('a[href*="logout"]');
            const logoutForm = document.querySelector('form[action*="logout"]');
            const userMenu = document.querySelector('.user-menu, .user-dropdown, [class*="user-info"]');
            const notOnLogin = !window.location.href.includes('/login');
            const hasAuthCookie = document.cookie.includes('session') || document.cookie.includes('auth');
            return !!logoutLink || !!logoutForm || !!userMenu || (notOnLogin && hasAuthCookie);
        });

        if (!loggedInBefore) {
            // In CI, authentication state may vary - skip instead of fail
            return { passed: null, skipped: true, message: 'Not logged in - cannot test session persistence' };
        }

        // Refresh the page
        await page.reload({ waitUntil: 'domcontentloaded' });
        await delay(500);

        const loggedInAfter = await page.evaluate(() => {
            const logoutLink = document.querySelector('a[href*="logout"]');
            const logoutForm = document.querySelector('form[action*="logout"]');
            const notOnLogin = !window.location.href.includes('/login');
            return !!logoutLink || !!logoutForm || notOnLogin;
        });

        // If session lost in CI, skip instead of fail (may be infrastructure issue)
        if (!loggedInAfter) {
            return { passed: null, skipped: true, message: 'Session state unclear after refresh (CI environment)' };
        }

        return {
            passed: true,
            message: 'Session persists after page refresh'
        };
    },

    async logoutFunctionality(page, baseUrl) {
        await navigateTo(page, `${baseUrl}/`);

        const result = await page.evaluate(() => {
            const logoutLink = document.querySelector('a[href*="logout"]');
            const logoutBtn = document.querySelector('button[onclick*="logout"], .logout-btn');
            return {
                hasLogout: !!logoutLink || !!logoutBtn,
                href: logoutLink?.href
            };
        });

        if (!result.hasLogout) {
            return { passed: null, skipped: true, message: 'Logout link not found (user may not be logged in)' };
        }

        // Click logout
        await Promise.all([
            page.click('a[href*="logout"]').catch(() => page.click('.logout-btn')),
            page.waitForNavigation({ waitUntil: 'domcontentloaded', timeout: 10000 }).catch(() => {})
        ]);

        // Verify logged out
        const isLoggedOut = await page.evaluate(() => {
            const loginLink = document.querySelector('a[href*="login"]');
            const logoutLink = document.querySelector('a[href*="logout"]');
            return !!loginLink && !logoutLink;
        });

        return {
            passed: isLoggedOut,
            message: isLoggedOut
                ? 'Logout functionality works correctly'
                : 'Logout did not redirect to login page'
        };
    },

    async protectedPageRedirect(page, baseUrl) {
        // Request the protected page from a new browser context instead of
        // clearing this page's cookies: a response to a request this page
        // sent before the clear can put the session cookie back (see
        // withFreshBrowserContext). This page also stays signed in.
        return withFreshBrowserContext(page, async (freshPage) => {
            const response = await freshPage.goto(`${baseUrl}/settings`, { waitUntil: 'domcontentloaded' });

            // Judge the server's answer (a redirect chain ending on the login
            // page, or a 401/403), not where the page's scripts may move the
            // browser after it loads.
            const finalUrl = response ? response.url() : freshPage.url();
            const status = response?.status();
            const serverRedirected = !!response && response.request().redirectChain().length > 0;
            const redirectedToLogin = status === 401 || status === 403
                || (serverRedirected && new URL(finalUrl).pathname === '/auth/login');

            return {
                passed: redirectedToLogin,
                message: redirectedToLogin
                    ? 'Protected pages redirect to login'
                    : `No redirect for protected page (stayed at ${finalUrl}, status: ${status})`
            };
        });
    }
};

// ============================================================================
// Password Change Tests
// ============================================================================
const PasswordChangeTests = {
    async passwordChangeFormExists(page, baseUrl) {
        // The change-password form is served at /auth/change-password (auth/routes.py)
        // and renders auth/change_password.html with current_password / new_password /
        // confirm_password fields (all type=password) and a submit button. The old test
        // guessed 4 possible URLs and SKIPPED if none matched — so a moved/renamed route
        // would silently pass-as-skip. Target the real route directly and assert the
        // form's fields, failing (not skipping) if absent.
        await navigateTo(page, `${baseUrl}/auth/change-password`);

        const result = await page.evaluate(() => {
            const current = document.querySelector('#current_password, input[name="current_password"]');
            const next = document.querySelector('#new_password, input[name="new_password"]');
            const confirm = document.querySelector('#confirm_password, input[name="confirm_password"]');
            const submit = document.querySelector('form button[type="submit"], form input[type="submit"]');
            const isPw = (el) => !!el && el.type === 'password';
            return {
                hasForm: !!(current && current.closest('form')),
                hasCurrent: isPw(current),
                hasNew: isPw(next),
                hasConfirm: isPw(confirm),
                hasSubmit: !!submit,
            };
        });

        const passed = result.hasForm && result.hasCurrent && result.hasNew
            && result.hasConfirm && result.hasSubmit;
        return {
            passed,
            message: passed
                ? 'Change-password form present at /auth/change-password (current + new + confirm password fields + submit)'
                : `Change-password form incomplete (form=${result.hasForm}, current=${result.hasCurrent}, new=${result.hasNew}, confirm=${result.hasConfirm}, submit=${result.hasSubmit})`
        };
    }
};

// ============================================================================
// Main Test Runner
// ============================================================================
async function main() {
    log.section('Comprehensive Authentication Tests');

    const ctx = await setupTest({ authenticate: true });
    const results = new TestResults('Auth Comprehensive Tests');
    const { page } = ctx;
    const { baseUrl } = ctx.config;

    try {
        // Each login test starts signed out, independently of the signed-in
        // page used for registration, session and password-change checks.
        log.section('Login Page');

        const loginFormResult = await withFreshBrowserContext(page, (loginPage) => LoginPageTests.loginFormElements(loginPage, baseUrl));
        if (loginFormResult.skipped) {
            results.skip('Login', 'Form Elements', loginFormResult.message);
        } else {
            results.add('Login', 'Form Elements', loginFormResult.passed, loginFormResult.message);
        }

        const loginValidationResult = await withFreshBrowserContext(page, (loginPage) => LoginPageTests.loginEmptyFieldValidation(loginPage, baseUrl));
        if (loginValidationResult.skipped) {
            results.skip('Login', 'Empty Field Validation', loginValidationResult.message);
        } else {
            results.add('Login', 'Empty Field Validation', loginValidationResult.passed, loginValidationResult.message);
        }

        const loginInvalidResult = await withFreshBrowserContext(page, (loginPage) => LoginPageTests.loginInvalidCredentials(loginPage, baseUrl));
        if (loginInvalidResult.skipped) {
            results.skip('Login', 'Invalid Credentials Handling', loginInvalidResult.message);
        } else {
            results.add('Login', 'Invalid Credentials Handling', loginInvalidResult.passed, loginInvalidResult.message);
        }

        const rememberMeResult = await withFreshBrowserContext(page, (loginPage) => LoginPageTests.loginRememberMeCheckbox(loginPage, baseUrl));
        if (rememberMeResult.skipped) {
            results.skip('Login', 'Remember Me Checkbox', rememberMeResult.message);
        } else {
            results.add('Login', 'Remember Me Checkbox', rememberMeResult.passed, rememberMeResult.message);
        }

        const registerLinkResult = await withFreshBrowserContext(page, (loginPage) => LoginPageTests.loginNavigateToRegister(loginPage, baseUrl));
        if (registerLinkResult.skipped) {
            results.skip('Login', 'Navigate to Register', registerLinkResult.message);
        } else {
            results.add('Login', 'Navigate to Register', registerLinkResult.passed, registerLinkResult.message);
        }

        // Registration Tests
        log.section('Registration Page');

        const registerFormResult = await RegistrationTests.registerFormElements(page, baseUrl);
        if (registerFormResult.skipped) {
            results.skip('Register', 'Form Elements', registerFormResult.message);
        } else {
            results.add('Register', 'Form Elements', registerFormResult.passed, registerFormResult.message);
        }

        const usernameValidResult = await RegistrationTests.registerUsernameValidation(page, baseUrl);
        if (usernameValidResult.skipped) {
            results.skip('Register', 'Username Validation', usernameValidResult.message);
        } else {
            results.add('Register', 'Username Validation', usernameValidResult.passed, usernameValidResult.message);
        }

        const strengthResult = await RegistrationTests.registerPasswordStrengthIndicator(page, baseUrl);
        if (strengthResult.skipped) {
            results.skip('Register', 'Password Strength Indicator', strengthResult.message);
        } else {
            results.add('Register', 'Password Strength Indicator', strengthResult.passed, strengthResult.message);
        }

        const confirmMatchResult = await RegistrationTests.registerPasswordConfirmMatch(page, baseUrl);
        if (confirmMatchResult.skipped) {
            results.skip('Register', 'Password Confirm Match', confirmMatchResult.message);
        } else {
            results.add('Register', 'Password Confirm Match', confirmMatchResult.passed, confirmMatchResult.message);
        }

        const acknowledgeResult = await RegistrationTests.registerAcknowledgeCheckbox(page, baseUrl);
        if (acknowledgeResult.skipped) {
            results.skip('Register', 'Acknowledge Checkbox', acknowledgeResult.message);
        } else {
            results.add('Register', 'Acknowledge Checkbox', acknowledgeResult.passed, acknowledgeResult.message);
        }

        const securityResult = await RegistrationTests.registerSecurityWarnings(page, baseUrl);
        if (securityResult.skipped) {
            results.skip('Register', 'Security Warnings', securityResult.message);
        } else {
            results.add('Register', 'Security Warnings', securityResult.passed, securityResult.message);
        }

        // Session Tests
        log.section('Session Management');

        const sessionResult = await SessionTests.sessionPersistsOnRefresh(page, baseUrl);
        if (sessionResult.skipped) {
            results.skip('Session', 'Persists on Refresh', sessionResult.message);
        } else {
            results.add('Session', 'Persists on Refresh', sessionResult.passed, sessionResult.message);
        }

        const protectedResult = await SessionTests.protectedPageRedirect(page, baseUrl);
        results.add('Session', 'Protected Page Redirect', protectedResult.passed, protectedResult.message);

        // Make sure this page is signed in for the remaining tests. The
        // protected-page check above uses its own browser context and does
        // not sign this page out, so this normally finds it still signed in.
        // Cap at 30s — if the server is slow after session tests, skip
        // password tests rather than hang for 300s.
        let reAuthOk = false;
        try {
            await withTimeout(
                ctx.authHelper.ensureAuthenticated(),
                30000,
                'Re-authentication after session tests'
            );
            reAuthOk = true;
        } catch (error) {
            log.warning(`Re-authentication timed out: ${error.message}`);
        }

        // Password Change Tests
        log.section('Password Change');

        if (!reAuthOk) {
            results.skip('Password', 'Change Form Exists', 'Skipped — re-authentication timed out after session tests');
        } else {
            const passwordChangeResult = await PasswordChangeTests.passwordChangeFormExists(page, baseUrl);
            if (passwordChangeResult.skipped) {
                results.skip('Password', 'Change Form Exists', passwordChangeResult.message);
            } else {
                results.add('Password', 'Change Form Exists', passwordChangeResult.passed, passwordChangeResult.message);
            }
        }

    } catch (error) {
        results.add('Suite', 'Execution', false, error.message);
        log.error(`Fatal error: ${error.message}`);
        console.error(error.stack);
    } finally {
        results.print();
        results.save();
        await teardownTest(ctx);
        process.exit(results.exitCode());
    }
}

// Run if executed directly
if (require.main === module) {
    main().catch(error => {
        console.error('Test runner failed:', error);
        process.exit(1);
    });
}

module.exports = { LoginPageTests, RegistrationTests, SessionTests, PasswordChangeTests };