import { defineConfig, devices } from '@playwright/test'

/**
 * Playwright E2E configuration for Fischer AgentKit frontend.
 *
 * Architecture:
 * - Backend (uvicorn direct, avoids agentkit serve interactive prompts) runs on
 *   port 18001 to match the Vite dev-server proxy target in vite.config.ts.
 * - Frontend (Vite dev server) runs on port 15173 (strictPort in vite.config.ts).
 * - Tests target the frontend at http://localhost:15173; API/WS calls are
 *   transparently proxied to the backend.
 *
 * The `globalSetup` script creates a test admin user in the auth DB before
 * any test runs, so login-based tests have valid credentials available.
 */

// Project root relative to this config file
// (src/agentkit/server/frontend/ → 4 levels up to project root)
const PROJECT_ROOT = '../../../..'

export default defineConfig({
  testDir: './e2e',
  fullyParallel: false,
  forbidOnly: !!process.env.CI,
  retries: process.env.CI ? 1 : 0,
  workers: 1,
  reporter: [['list'], ['html', { open: 'never' }]],
  timeout: 90_000,
  expect: { timeout: 15_000 },
  globalSetup: './e2e/global-setup.ts',

  use: {
    baseURL: 'http://localhost:15173',
    trace: 'on-first-retry',
    screenshot: 'only-on-failure',
    video: 'retain-on-failure',
    actionTimeout: 15_000,
    navigationTimeout: 30_000,
  },

  projects: [
    {
      name: 'chromium',
      use: {
        ...devices['Desktop Chrome'],
        // Use system Chrome to avoid slow browser downloads.
        channel: 'chrome',
      },
    },
  ],

  webServer: [
    {
      // Use uvicorn directly — `agentkit serve` has Confirm.ask() prompts
      // that fail in non-tty subprocess environments.
      // Env vars set inline to avoid Playwright's env property replacing
      // the entire process.env (which would lose PATH, API keys, etc.).
      //
      // AGENTKIT_JWT_SECRET must be set so AuthMiddleware can verify tokens
      // issued by /auth/login. Without it, get_jwt_secret() returns None →
      // middleware gets "" → _verify_jwt always returns None → 401.
      //
      // create_app(rate_limit=10000) is called explicitly (not via uvicorn
      // factory=True) because factory mode calls create_app() with no args,
      // letting server_config.rate_limit (60, from agentkit.yaml) override
      // the env var. Passing rate_limit as a parameter takes priority over
      // server_config.rate_limit (see app.py line 572-577).
      command:
        'AGENTKIT_JWT_SECRET=e2e-test-secret-do-not-use-in-prod ' +
        'AGENTKIT_GUI_MODE=1 NO_PROXY=127.0.0.1,localhost no_proxy=127.0.0.1,localhost ' +
        '.venv/bin/python -c "from agentkit.server.app import create_app; import uvicorn; ' +
        'app = create_app(rate_limit=10000); ' +
        'uvicorn.run(app, host=\'127.0.0.1\', port=18001)"',
      url: 'http://127.0.0.1:18001/api/v1/health',
      cwd: PROJECT_ROOT,
      reuseExistingServer: !process.env.CI,
      timeout: 120_000,
      stdout: 'pipe',
      stderr: 'pipe',
    },
    {
      command: 'npm run dev',
      url: 'http://localhost:15173',
      cwd: '.',
      reuseExistingServer: !process.env.CI,
      timeout: 60_000,
      stdout: 'pipe',
      stderr: 'pipe',
    },
  ],
})
