name: E2E (Claude Code)

# Live end-to-end check: drive the real `claude` CLI through oneharness and
# assert the JSON contract (scripts/e2e-claude.sh). Unlike ci.yml this needs
# network, a model call, and the CLAUDE_CODE_OAUTH_TOKEN secret, 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 token.

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

permissions:
  contents: read

concurrency:
  group: e2e-claude-${{ 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: claude 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 auth token is present
        env:
          CLAUDE_CODE_OAUTH_TOKEN: ${{ secrets.CLAUDE_CODE_OAUTH_TOKEN }}
        run: |
          if [ -z "${CLAUDE_CODE_OAUTH_TOKEN:-}" ]; then
            echo "::error::CLAUDE_CODE_OAUTH_TOKEN secret is not set. Mint one with 'claude setup-token', then sync it (just secrets-sync) or set it: gh secret set CLAUDE_CODE_OAUTH_TOKEN --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: lts/*
      - name: Install Claude Code CLI
        run: |
          npm install -g @anthropic-ai/claude-code
          claude --version

      - name: Live e2e against the real CLI
        env:
          CLAUDE_CODE_OAUTH_TOKEN: ${{ secrets.CLAUDE_CODE_OAUTH_TOKEN }}
          CLAUDE_E2E_MODEL: ${{ vars.CLAUDE_E2E_MODEL }}
        run: just live-claude
