name: CI

on:
  push:
    branches:
      - master
  pull_request:

env:
  TURBO_TOKEN: ${{ secrets.TURBO_TOKEN }}
  TURBO_TEAM: svelte
  # we call `pnpm playwright install` instead
  PLAYWRIGHT_SKIP_BROWSER_DOWNLOAD: '1'

# cancel in-progress runs on new commits to same PR (gitub.event.number)
concurrency:
  group: ${{ github.workflow }}-${{ github.event.number || github.sha }}
  cancel-in-progress: true

jobs:
  Lint:
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v3
      - uses: pnpm/action-setup@v2.2.2
      - uses: actions/setup-node@v3
        with:
          node-version: '16.x'
          cache: pnpm
      - run: pnpm install --frozen-lockfile
      - run: cd packages/kit && pnpm build
      - run: pnpm turbo run lint check
  Tests:
    runs-on: ${{ matrix.os }}
    timeout-minutes: 30
    strategy:
      fail-fast: false
      matrix:
        node-version: [16]
        os: [ubuntu-latest, windows-latest]
        e2e-browser: ['chromium']
        include:
          - node-version: 16
            os: ubuntu-latest
            e2e-browser: 'firefox'
          - node-version: 16
            os: macOS-latest
            e2e-browser: 'webkit'
          - node-version: 18
            os: ubuntu-latest
            e2e-browser: 'chromium'
    env:
      TURBO_CACHE_KEY: ${{ matrix.os }}-${{ matrix.node-version }}
      KIT_E2E_BROWSER: ${{matrix.e2e-browser}}
    steps:
      - run: git config --global core.autocrlf false
      - uses: actions/checkout@v3
      - uses: pnpm/action-setup@v2.2.2
      - uses: actions/setup-node@v3
        with:
          node-version: ${{ matrix.node-version }}
          cache: pnpm
      - run: pnpm install --frozen-lockfile
      - run: pnpm playwright install ${{ matrix.e2e-browser }}
      - run: pnpm test
      - name: Archive test results
        if: failure()
        shell: bash
        run: find packages -type d -name test-results -not -empty | tar -czf test-results.tar.gz --files-from=-
      - name: Upload test results
        if: failure()
        uses: actions/upload-artifact@v3
        with:
          retention-days: 3
          name: test-failure-${{ github.run_id }}-${{ matrix.os }}-${{ matrix.node-version }}-${{ matrix.e2e-browser }}
          path: test-results.tar.gz
