name: Tests
on:
  push:
  pull_request:
concurrency:
  # Group based on workflow name and PR if it exists, if no PR, let it run so carryforward flags work
  group: ${{ github.workflow }}-${{ github.head_ref || github.run_id }}
  cancel-in-progress: true
jobs:
  tests:
    name: Tests
    runs-on: ubuntu-latest
    env:
      TURBO_TOKEN: ${{ secrets.TURBO_TOKEN }}
      TURBO_TEAM: ${{ secrets.TURBO_TEAM }}
    steps:
      - name: Checkout repository
        uses: actions/checkout@v4
        with:
          fetch-depth: 0

      - name: Install Node.js v20
        uses: actions/setup-node@v4
        with:
          node-version: 20

      - name: Install dependencies
        uses: ./packages/actions/src/pnpmCache

      - name: Build dependencies (PR)
        if: ${{ github.event_name != 'push' }}
        run: pnpm exec turbo run build --filter="...[origin/${{ github.event_name == 'pull_request' && github.event.pull_request.base.ref || 'main' }}]" --concurrency=4

      - name: Build dependencies (Push)
        if: ${{ github.event_name == 'push' }}
        run: pnpm exec turbo run build --filter="...[HEAD^1]" --concurrency=4

      - name: Tests (PR)
        if: ${{ github.event_name != 'push' }}
        run: pnpm exec turbo run test --filter="...[origin/${{ github.event_name == 'pull_request' && github.event.pull_request.base.ref || 'main' }}]" --concurrency=4

      - name: Tests (Push)
        if: ${{ github.event_name == 'push' }}
        run: pnpm exec turbo run test --filter="...[HEAD^1]" --concurrency=4

      - name: ESLint (PR)
        if: ${{ github.event_name != 'push' }}
        run: pnpm exec turbo run lint --filter="...[origin/${{ github.event_name == 'pull_request' && github.event.pull_request.base.ref || 'main' }}]" --concurrency=4 -- --format=compact

      - name: ESLint (Push)
        if: ${{ github.event_name == 'push' }}
        run: pnpm exec turbo run lint --filter="...[HEAD^1]" --concurrency=4 -- --format=compact

      - name: Docs (PR)
        if: ${{ github.event_name != 'push' }}
        run: pnpm exec turbo run docs --filter="...[origin/${{ github.event_name == 'pull_request' && github.event.pull_request.base.ref || 'main' }}]" --concurrency=4

      - name: Docs (Push)
        if: ${{ github.event_name == 'push' }}
        run: pnpm exec turbo run docs --filter="...[HEAD^1]" --concurrency=4

      - name: Upload Coverage
        if: github.repository_owner == 'discordjs'
        uses: ./packages/actions/src/uploadCoverage
        with:
          codecov_token: ${{ secrets.CODECOV_TOKEN }}
