name: OpenWork Tests

on:
  pull_request:
    branches:
      - dev
  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:
          GITHUB_TOKEN: ${{ github.token }}
          OPENCODE_GITHUB_REPO: ${{ vars.OPENCODE_GITHUB_REPO || 'anomalyco/opencode' }}
          OPENCODE_VERSION: ${{ vars.OPENCODE_VERSION || '1.2.20' }}
        run: |
          set -euo pipefail

          repo="${OPENCODE_GITHUB_REPO:-anomalyco/opencode}"
          version="${OPENCODE_VERSION:-}"

          if [ -z "$version" ]; then
            version="$(node -p "require('./packages/desktop/package.json').opencodeVersion || ''" 2>/dev/null || true)"
          fi

          version="$(echo "$version" | tr -d '\r\n' | sed 's/^v//')"

          if [ -z "$version" ] || [ "$version" = "latest" ]; then
            latest=""
            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/${repo}/releases/latest" \
                | sed -n 's/.*"tag_name": *"v\([^"]*\)".*/\1/p')
            else
              latest=$(curl -fsSL \
                -H "X-GitHub-Api-Version: 2022-11-28" \
                "https://api.github.com/repos/${repo}/releases/latest" \
                | sed -n 's/.*"tag_name": *"v\([^"]*\)".*/\1/p')
            fi
            if [ -n "$latest" ]; then
              version="$latest"
            fi
          fi

          if [ -z "$version" ]; then
            echo "Unable to resolve OpenCode version (set OPENCODE_VERSION to pin)." >&2
            exit 1
          fi

          arch="$(uname -m)"
          case "${RUNNER_OS}" in
            Linux)
              if [ "$arch" = "aarch64" ] || [ "$arch" = "arm64" ]; then
                opencode_asset="opencode-linux-arm64.tar.gz"
              else
                opencode_asset="opencode-linux-x64-baseline.tar.gz"
              fi
              ;;
            macOS)
              if [ "$arch" = "arm64" ]; then
                opencode_asset="opencode-darwin-arm64.zip"
              else
                opencode_asset="opencode-darwin-x64-baseline.zip"
              fi
              ;;
            *)
              echo "Unsupported OS: ${RUNNER_OS}" >&2
              exit 1
              ;;
          esac

          url="https://github.com/${repo}/releases/download/v${version}/${opencode_asset}"
          tmp_dir="$RUNNER_TEMP/opencode"
          extract_dir="$tmp_dir/extracted"
          rm -rf "$tmp_dir"
          mkdir -p "$extract_dir"

          curl_headers=()
          if [ -n "${GITHUB_TOKEN:-}" ]; then
            curl_headers+=( -H "Authorization: Bearer ${GITHUB_TOKEN}" )
          fi

          curl -fsSL --retry 5 --retry-all-errors --retry-delay 2 "${curl_headers[@]}" -o "$tmp_dir/$opencode_asset" "$url"

          if [[ "$opencode_asset" == *.tar.gz ]]; then
            tar -xzf "$tmp_dir/$opencode_asset" -C "$extract_dir"
          else
            unzip -q "$tmp_dir/$opencode_asset" -d "$extract_dir"
          fi

          if [ -f "$extract_dir/opencode" ]; then
            bin_path="$extract_dir/opencode"
          elif [ -f "$extract_dir/opencode.exe" ]; then
            bin_path="$extract_dir/opencode.exe"
          else
            echo "OpenCode binary not found in archive" >&2
            ls -la "$extract_dir"
            exit 1
          fi

          install_dir="$HOME/.opencode/bin"
          mkdir -p "$install_dir"
          cp "$bin_path" "$install_dir/opencode"
          chmod 755 "$install_dir/opencode"
          echo "$install_dir" >> "$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-ui test:e2e
