name: Build, Lint, and Test

on:
  workflow_call:

jobs:
  prepare:
    name: Prepare
    runs-on: ubuntu-latest
    steps:
      - name: Checkout and setup environment
        uses: MetaMask/action-checkout-and-setup@v1
        with:
          is-high-risk-environment: false
          cache-node-modules: true
      - name: Enable pnpm
        run: corepack enable && corepack prepare pnpm@9.15.0 --activate
      - name: Install dependencies
        run: pnpm install --frozen-lockfile

  build:
    name: Build
    runs-on: ubuntu-latest
    needs:
      - prepare
    steps:
      - name: Checkout and setup environment
        uses: MetaMask/action-checkout-and-setup@v1
        with:
          is-high-risk-environment: false
      - name: Enable pnpm
        run: corepack enable && corepack prepare pnpm@9.15.0 --activate
      - name: Install dependencies
        run: pnpm install --frozen-lockfile
      - name: Build
        run: pnpm run build
      - name: Upload Snap build artifact
        uses: actions/upload-artifact@v4
        with:
          name: snap-${{ runner.os }}-${{ github.sha }}
          path: ./packages/snap/dist
          retention-days: 1
      - name: Require clean working directory
        shell: bash
        run: |
          if ! git diff --exit-code; then
            echo "Working tree dirty at end of job"
            exit 1
          fi

  lint:
    name: Lint
    runs-on: ubuntu-latest
    needs:
      - prepare
    steps:
      - name: Checkout and setup environment
        uses: MetaMask/action-checkout-and-setup@v1
        with:
          is-high-risk-environment: false
      - name: Enable pnpm
        run: corepack enable && corepack prepare pnpm@9.15.0 --activate
      - name: Install dependencies
        run: pnpm install --frozen-lockfile
      - name: Lint
        run: pnpm run lint
      - name: Require clean working directory
        shell: bash
        run: |
          if ! git diff --exit-code; then
            echo "Working tree dirty at end of job"
            exit 1
          fi

  e2e-test:
    name: End-to-end Test
    runs-on: ubuntu-latest
    needs:
      - prepare
      - build
    steps:
      - name: Checkout and setup environment
        uses: MetaMask/action-checkout-and-setup@v1
        with:
          is-high-risk-environment: false
      - name: Download Snap build artifact
        uses: actions/download-artifact@v4
        with:
          name: snap-${{ runner.os }}-${{ github.sha }}
          path: ./packages/snap/dist
      - name: Enable pnpm
        run: corepack enable && corepack prepare pnpm@9.15.0 --activate
      - name: Install dependencies
        run: pnpm install --frozen-lockfile
      - name: Run Snap unit tests
        run: pnpm --filter chain138-snap run test
      - name: Install Playwright browsers
        run: npx playwright install chromium --with-deps
      - name: Run Playwright E2E (companion site)
        run: pnpm run test:e2e
        env:
          CI: true
        timeout-minutes: 8
      - name: Require clean working directory
        shell: bash
        run: |
          if ! git diff --exit-code; then
            echo "Working tree dirty at end of job"
            exit 1
          fi
