name: 'Build'

on:
  pull_request:
    branches: 
      - main
  push: 
    branches:
      - main

jobs:
  build:
    permissions:
      contents: read
    name: Build
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 #7.0.1
      - name: Setup Git
        run: |
          git config user.name github-actions
          git config user.email github-actions@github.com

      - uses: pnpm/action-setup@0977fd99725f1db4007ccb2928dbb4e90d06cc86
        name: Install pnpm
        with:
            version: 9
            run_install: false
      - uses: actions/setup-node@820762786026740c76f36085b0efc47a31fe5020 #7.0.0
        name: Install Node.js
        with:
            node-version: 24
            cache: 'pnpm'
      - name: Install dependencies
        run: pnpm install --frozen-lockfile --ignore-scripts
      - name: Build
        run: pnpm run build
      - name: Archive production artifacts
        uses: actions/upload-artifact@043fb46d1a93c77aae656e7c1c64a875d1fc6a0a #7.0.1
        with:
          name: expander-card
          path: |
              dist

  visual-tests:
    needs: build
    permissions:
      contents: read
    name: Visual Tests
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 #7.0.1

      - name: Download build artifact
        uses: actions/download-artifact@v8
        with:
          name: expander-card
          path: dist

      - name: Install uv
        uses: astral-sh/setup-uv@c771a70e6277c0a99b617c7a806ffedaca235ff9
        with:
          enable-cache: true

      - name: Set up Python
        run: uv python install

      - name: Build project wheel
        run: uv build --wheel

      - name: Install test dependencies (excluding project)
        run: uv sync --extra test --locked --no-build --no-install-project

      - name: Install project from prebuilt wheel
        env:
          INSTALL_CMD: "uv pip install --no-build --no-deps"
        run: |
          WHEEL_FILE=$(ls dist/*.whl)
          $INSTALL_CMD "$WHEEL_FILE"

      - name: Install Playwright browsers
        run: uv run --no-project --locked --no-build playwright install chromium

      - name: Run visual tests
        run: uv run --no-project --locked --no-build pytest tests/