name: Lint & Test

on:
  pull_request: # for PRs from forks
  push:
    branches-ignore:
      - 'update-snapshots'

jobs:
  lint:
    runs-on: ubuntu-latest
    # Skip `pull_request` runs on local PRs for which `push` runs are already triggered
    if: github.event_name == 'push' || github.event.pull_request.head.repo.full_name != github.repository

    steps:
      - name: Checkout 🏷️
        uses: actions/checkout@v3

      - name: Install ⚙️
        uses: pnpm/action-setup@v2
        with:
          version: 7.x

      - name: Use Node 16 🕹️
        uses: actions/setup-node@v2
        with:
          node-version: 16
          registry-url: 'https://registry.npmjs.org'
          cache: 'pnpm'

      - name: Install ⚙️
        run: pnpm install --frozen-lockfile

      - name: Lint 🤓
        run: pnpm prettier && pnpm lint

  test:
    runs-on: ubuntu-latest
    # Skip `pull_request` runs on local PRs for which `push` runs are already triggered
    if: github.event_name == 'push' || github.event.pull_request.head.repo.full_name != github.repository

    steps:
      - name: Checkout 🏷️
        uses: actions/checkout@v3

      - name: Install ⚙️
        uses: pnpm/action-setup@v2
        with:
          version: 7.x

      - name: Use Node 16 🕹️
        uses: actions/setup-node@v2
        with:
          node-version: 16
          registry-url: 'https://registry.npmjs.org'
          cache: 'pnpm'

      - name: Install ⚙️
        run: pnpm install --frozen-lockfile

      - name: Test 👓
        run: pnpm test
        env:
          CI: true

  e2e:
    runs-on: ubuntu-latest
    # Skip `pull_request` runs on local PRs for which `push` runs are already triggered
    if: github.event_name == 'push' || github.event.pull_request.head.repo.full_name != github.repository

    steps:
      - name: Checkout 🏷️
        uses: actions/checkout@v3

      - name: Install ⚙️
        uses: pnpm/action-setup@v2
        with:
          version: 7.x

      - name: Use Node 16 🕹️
        uses: actions/setup-node@v2
        with:
          node-version: 16
          registry-url: 'https://registry.npmjs.org'
          cache: 'pnpm'

      - name: Cache Cypress binary 📌
        uses: actions/cache@v2
        with:
          path: ~/.cache/Cypress
          key: cypress-${{ runner.os }}-${{ hashFiles('package-lock.json') }}

      - name: Install ⚙️
        run: pnpm install --frozen-lockfile

      - name: Build packages 📦
        run: pnpm packages

      - name: Build demo 🛠️
        run: pnpm build

      - name: Serve ⚡️
        run: pnpm serve & pnpx wait-on http://localhost:5173

      - name: Run Cypress 🌳
        run: pnpm cypress:run
        env:
          CYPRESS_TAKE_SNAPSHOTS: true

      - name: Upload debug screenshots and diffs on failure 🖼️
        uses: actions/upload-artifact@v2
        if: failure()
        with:
          name: cypress
          path: |
            cypress/debug/
            cypress/snapshots/**/__diff_output__/
