/**
 * Playwright configuration for accessibility testing
 * @see https://playwright.dev/docs/test-configuration
 */
import { defineConfig, devices } from '@playwright/test';
import path from 'path';

const authFile = path.join(import.meta.dirname, 'tests/accessibility_tests/.auth/user.json');

export default defineConfig({
    testDir: './tests/accessibility_tests',
    outputDir: './tests/accessibility_tests/test-results',
    fullyParallel: true,
    forbidOnly: !!process.env.CI,
    retries: process.env.CI ? 1 : 0,
    workers: process.env.CI ? 1 : undefined,
    reporter: [
        ['list'],
        ['html', { open: 'never', outputFolder: 'tests/accessibility_tests/playwright-report' }]
    ],
    use: {
        baseURL: process.env.BASE_URL || 'http://localhost:5000',
        trace: 'on-first-retry',
        screenshot: 'only-on-failure',
        navigationTimeout: 10000,
        actionTimeout: 10000,
    },
    projects: [
        {
            name: 'setup',
            testMatch: /auth-setup\.js/,
            use: { ...devices['Desktop Chrome'] },
        },
        {
            name: 'chromium',
            dependencies: ['setup'],
            use: {
                ...devices['Desktop Chrome'],
                storageState: authFile,
            },
        },
    ],
    // In CI, the Flask server is started separately (e.g. inside Docker),
    // so reuseExistingServer skips launching a new one.
    // For local development, start the server manually before running tests,
    // or let this config start it via pdm.
    webServer: {
        command: 'pdm run python -m local_deep_research.web.app',
        url: 'http://localhost:5000',
        reuseExistingServer: !!process.env.CI,
        timeout: 120000,
        stdout: 'ignore',
        stderr: 'pipe',
    },
});
