name: Test
on:
  pull_request: {}
  push:
    branches: [livekit, full-mesh]
jobs:
  vitest:
    name: Run unit tests
    runs-on: ubuntu-latest
    steps:
      - name: Checkout code
        uses: actions/checkout@34e114876b0b11c390a56381ad16ebd13914f8d5 # v4
        with:
          persist-credentials: false
      - name: Enable Corepack
        run: corepack enable
      - name: pnpm cache
        uses: actions/setup-node@49933ea5288caeca8642d1e84afbd3f7d6820020 # v4
        with:
          cache: "pnpm"
          node-version-file: ".node-version"
      - name: Install dependencies
        # ignore-pnpmfile should never be commited. Make CI crash if it happened (`pnpmfileChecksum` is present)
        run: "pnpm install --frozen-lockfile --ignore-pnpmfile"
      - name: Vitest
        run: "pnpm run test:coverage"
      - name: Upload to codecov
        uses: codecov/codecov-action@75cd11691c0faa626561e295848008c8a7dddffe # v5
        env:
          CODECOV_TOKEN: ${{ secrets.CODECOV_TOKEN }}
        with:
          flags: unittests
          fail_ci_if_error: true
  playwright:
    name: Run end-to-end tests
    timeout-minutes: 60
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@34e114876b0b11c390a56381ad16ebd13914f8d5 # v4
        with:
          persist-credentials: false
      - name: Enable Corepack
        run: corepack enable
      - uses: actions/setup-node@49933ea5288caeca8642d1e84afbd3f7d6820020 # v4
        with:
          cache: "pnpm"
          node-version-file: ".node-version"
      - name: Install dependencies
        # ignore-pnpmfile should never be commited. Make CI crash if it happened (`pnpmfileChecksum` is present)
        run: pnpm install --frozen-lockfile --ignore-pnpmfile
      - name: Install Playwright Browsers
        run: pnpm exec playwright install --with-deps
      - name: Run backend components
        run: |
          docker compose -f playwright-backend-docker-compose.yml -f playwright-backend-docker-compose.override.yml pull
          docker compose -f playwright-backend-docker-compose.yml -f playwright-backend-docker-compose.override.yml up -d
          docker ps
      - name: Run Playwright tests
        env:
          USE_DOCKER: 1
        run: pnpm exec playwright test
      - uses: actions/upload-artifact@043fb46d1a93c77aae656e7c1c64a875d1fc6a0a # v7.0.1
        if: ${{ !cancelled() }}
        with:
          name: html-report
          path: playwright-report
          if-no-files-found: error
          retention-days: 4
