name: E2E (Qwen Code)

# Live end-to-end check: drive the real Qwen Code CLI through oneharness and
# assert the JSON contract (scripts/e2e-qwen.sh). Unlike ci.yml this needs
# network, a model call, and an OpenAI-compatible provider key (OPENAI_API_KEY),
# so it lives in its own workflow gated to trusted contexts only.
#
# Security: a secret must never be reachable by untrusted code. GitHub already
# withholds secrets from fork pull requests; the job `if` below is defense in
# depth — it runs only in the canonical repo and never for a PR whose head is a
# fork, so this workflow can be edited in a fork PR without exposing the key.

on:
  push:
    branches: [main]
  pull_request:
  workflow_dispatch:

permissions:
  contents: read

concurrency:
  group: e2e-qwen-${{ github.ref }}
  cancel-in-progress: true

env:
  CARGO_TERM_COLOR: always
  # In CI the harness is installed and auth verified up front, so any e2e skip
  # means detection/install/spawn broke (e.g. an unresolved .cmd shim on Windows)
  # — fail instead of going green having tested nothing. See scripts/e2e-lib.sh.
  OH_E2E_NO_SKIP: 1

jobs:
  live:
    name: qwen live e2e (${{ matrix.os }})
    if: >-
      github.repository == 'nickderobertis/oneharness' &&
      (github.event_name != 'pull_request' ||
       github.event.pull_request.head.repo.full_name == github.repository)
    strategy:
      fail-fast: false
      matrix:
        os: [ubuntu-latest, macos-latest, windows-latest]
    runs-on: ${{ matrix.os }}
    defaults:
      run:
        shell: bash
    timeout-minutes: 20
    steps:
      - uses: actions/checkout@v4

      - name: Verify the provider key is present
        env:
          OPENAI_API_KEY: ${{ secrets.OPENAI_API_KEY }}
        run: |
          if [ -z "${OPENAI_API_KEY:-}" ]; then
            echo "::error::OPENAI_API_KEY secret is not set. Add an OpenAI-compatible key (with OPENAI_BASE_URL / QWEN_E2E_MODEL as needed), then sync it (just secrets-sync) or set it: gh secret set OPENAI_API_KEY --repo ${{ github.repository }}"
            exit 1
          fi

      - uses: dtolnay/rust-toolchain@stable
      - uses: Swatinem/rust-cache@v2
      - uses: taiki-e/install-action@v2
        with:
          tool: just

      - uses: actions/setup-node@v4
        with:
          node-version: "22"
      - name: Install Qwen Code CLI
        run: |
          npm install -g @qwen-code/qwen-code
          qwen --version

      # Qwen's default endpoint is DashScope; to drive it with OPENAI_API_KEY we
      # point it at OpenAI explicitly and pick a model the key can call. Override
      # either via repo variables (QWEN_OPENAI_BASE_URL / QWEN_E2E_MODEL).
      - name: Live e2e against the real CLI
        env:
          OPENAI_API_KEY: ${{ secrets.OPENAI_API_KEY }}
          OPENAI_BASE_URL: ${{ vars.QWEN_OPENAI_BASE_URL || 'https://api.openai.com/v1' }}
          OPENAI_MODEL: ${{ vars.QWEN_E2E_MODEL || 'gpt-4o-mini' }}
          QWEN_E2E_MODEL: ${{ vars.QWEN_E2E_MODEL || 'gpt-4o-mini' }}
        run: just live-qwen
