name: Test

on:
  push:
    branches: [main]
  pull_request:
    branches: [main]

jobs:
  vitest:
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v4
      - uses: actions/setup-node@v4
        with:
          node-version: 22
          cache: npm
      - run: npm ci
      - run: npm run test:run

  playwright:
    runs-on: ubuntu-latest
    needs: vitest
    services:
      mongo:
        image: mongo:7
        ports:
          - 27017:27017
    env:
      MONGODB_URI: mongodb://localhost:27017/ghostguild-test
      JWT_SECRET: ci-test-jwt-secret
      NUXT_PUBLIC_COMING_SOON: 'false'
      NODE_ENV: development
    steps:
      - uses: actions/checkout@v4
      - uses: actions/setup-node@v4
        with:
          node-version: 22
          cache: npm
      - run: npm ci
      - run: npx playwright install --with-deps chromium
      - run: npm run build
      - name: Start server
        run: node .output/server/index.mjs &
        env:
          PORT: 3000
      - name: Wait for server
        run: npx wait-on http://localhost:3000 --timeout 30000
      - run: npx playwright test --ignore-snapshots
      - uses: actions/upload-artifact@v4
        if: failure()
        with:
          name: playwright-report
          path: |
            playwright-report/
            e2e/test-results/
          retention-days: 7

  visual:
    runs-on: ubuntu-latest
    needs: vitest
    continue-on-error: true
    services:
      mongo:
        image: mongo:7
        ports:
          - 27017:27017
    env:
      MONGODB_URI: mongodb://localhost:27017/ghostguild-test
      JWT_SECRET: ci-test-jwt-secret
      NUXT_PUBLIC_COMING_SOON: 'false'
      NODE_ENV: development
    steps:
      - uses: actions/checkout@v4
      - uses: actions/setup-node@v4
        with:
          node-version: 22
          cache: npm
      - run: npm ci
      - run: npx playwright install --with-deps chromium
      - run: npm run build
      - name: Start server
        run: node .output/server/index.mjs &
        env:
          PORT: 3000
      - name: Wait for server
        run: npx wait-on http://localhost:3000 --timeout 30000
      - run: npx playwright test e2e/visual/
      - uses: actions/upload-artifact@v4
        if: failure()
        with:
          name: visual-diffs
          path: e2e/test-results/
          retention-days: 7
