name: OpenWork Tests

on:
  pull_request:
  push:
    branches:
      - dev

permissions:
  contents: read

jobs:
  openwork-tests:
    runs-on: ${{ matrix.os }}

    strategy:
      fail-fast: false
      matrix:
        os: [ubuntu-22.04, macos-14]

    steps:
      - name: Checkout
        uses: actions/checkout@v4

      - name: Setup Node
        uses: actions/setup-node@v4
        with:
          node-version: 20

      - name: Setup pnpm
        uses: pnpm/action-setup@v4
        with:
          version: 10.27.0

      - name: Install OpenCode CLI
        shell: bash
        env:
          OPENCODE_VERSION: 1.1.36
          GITHUB_TOKEN: ${{ github.token }}
        run: |
          set -euo pipefail
          version="${OPENCODE_VERSION}"

          if [ -n "${GITHUB_TOKEN:-}" ]; then
            latest=$(curl -fsSL \
              -H "Authorization: Bearer ${GITHUB_TOKEN}" \
              -H "X-GitHub-Api-Version: 2022-11-28" \
              https://api.github.com/repos/anomalyco/opencode/releases/latest \
              | sed -n 's/.*"tag_name": *"v\([^"]*\)".*/\1/p')
            if [ -n "$latest" ]; then
              version="$latest"
            fi
          fi

          curl -fsSL https://opencode.ai/install | bash -s -- --version "$version"
          echo "$HOME/.opencode/bin" >> "$GITHUB_PATH"

      - name: Verify OpenCode
        run: opencode --version

      - name: Install dependencies
        run: pnpm install --frozen-lockfile

      - name: Run e2e tests
        run: pnpm --filter @different-ai/openwork test:e2e
