name: NPM Telegram Beta E2E

on:
  workflow_dispatch:
    inputs:
      package_spec:
        description: Published OpenClaw package spec to test when no artifact is supplied
        required: true
        default: openclaw@beta
        type: string
      package_label:
        description: Optional display label for an artifact-backed package candidate
        required: false
        default: ""
        type: string
      package_artifact_name:
        description: Advanced package-under-test artifact name; leave blank for registry install
        required: false
        default: ""
        type: string
      package_artifact_run_id:
        description: Advanced run id containing package_artifact_name; blank downloads from this run
        required: false
        default: ""
        type: string
      harness_ref:
        description: Source ref for the private QA harness; defaults to the dispatched workflow ref
        required: false
        default: ""
        type: string
      provider_mode:
        description: QA provider mode
        required: true
        default: mock-openai
        type: choice
        options:
          - mock-openai
          - live-frontier
      scenario:
        description: Optional comma-separated Telegram scenario ids
        required: false
        type: string
      advisory:
        description: Treat package Telegram failures as advisory for the caller
        required: false
        default: false
        type: boolean
  workflow_call:
    inputs:
      advisory:
        description: Treat package Telegram failures as advisory for the caller
        required: false
        default: false
        type: boolean
      package_spec:
        description: Published OpenClaw package spec to test when no artifact is supplied
        required: true
        type: string
      package_artifact_name:
        description: Optional package-under-test artifact from the current or specified workflow run
        required: false
        default: ""
        type: string
      package_artifact_run_id:
        description: Optional run id containing package_artifact_name
        required: false
        default: ""
        type: string
      package_label:
        description: Optional display label for an artifact-backed package candidate
        required: false
        default: ""
        type: string
      harness_ref:
        description: Source ref for the private QA harness; defaults to the called workflow ref
        required: false
        default: ""
        type: string
      provider_mode:
        description: QA provider mode
        required: false
        default: mock-openai
        type: string
      scenario:
        description: Optional comma-separated Telegram scenario ids
        required: false
        default: ""
        type: string
    secrets:
      OPENAI_API_KEY:
        required: false
      OPENCLAW_QA_CONVEX_SITE_URL:
        required: false
      OPENCLAW_QA_CONVEX_SECRET_CI:
        required: false

permissions:
  contents: read

concurrency:
  group: npm-telegram-beta-e2e-${{ github.run_id }}
  cancel-in-progress: false

env:
  FORCE_JAVASCRIPT_ACTIONS_TO_NODE24: "true"
  NODE_VERSION: "24.15.0"

jobs:
  run_package_telegram_e2e:
    name: Run package Telegram E2E
    runs-on: blacksmith-32vcpu-ubuntu-2404
    continue-on-error: ${{ inputs.advisory }}
    timeout-minutes: 60
    environment: qa-live-shared
    permissions:
      actions: read
      contents: read
    env:
      DOCKER_BUILD_SUMMARY: "false"
      DOCKER_BUILD_RECORD_UPLOAD: "false"
    steps:
      - name: Checkout dispatch ref
        uses: actions/checkout@v6
        with:
          ref: ${{ inputs.harness_ref || github.sha }}
          fetch-depth: 1

      - name: Set up Blacksmith Docker Builder
        uses: useblacksmith/setup-docker-builder@722e97d12b1d06a961800dd6c05d79d951ad3c80 # v1
        with:
          max-cache-size-mb: 800000

      - name: Build Docker E2E image
        uses: useblacksmith/build-push-action@fb9e3e6a9299c78462bfadd0d93352c316adc9b8 # v2
        with:
          context: .
          file: ./scripts/e2e/Dockerfile
          target: build
          platforms: linux/amd64
          tags: openclaw-docker-e2e:local
          load: true
          push: false
          provenance: false

      - name: Setup Node environment
        uses: ./.github/actions/setup-node-env
        with:
          node-version: ${{ env.NODE_VERSION }}
          install-bun: "true"

      - name: Validate inputs and secrets
        env:
          PACKAGE_SPEC: ${{ inputs.package_spec }}
          PACKAGE_ARTIFACT_NAME: ${{ inputs.package_artifact_name || '' }}
          PROVIDER_MODE: ${{ inputs.provider_mode }}
          OPENAI_API_KEY: ${{ secrets.OPENAI_API_KEY }}
          OPENCLAW_QA_CONVEX_SITE_URL: ${{ secrets.OPENCLAW_QA_CONVEX_SITE_URL }}
          OPENCLAW_QA_CONVEX_SECRET_CI: ${{ secrets.OPENCLAW_QA_CONVEX_SECRET_CI }}
        shell: bash
        run: |
          set -euo pipefail

          if [[ -z "${PACKAGE_ARTIFACT_NAME// }" ]]; then
            if [[ ! "${PACKAGE_SPEC}" =~ ^openclaw@(alpha|beta|latest|[0-9]{4}\.[1-9][0-9]*\.[1-9][0-9]*(-[1-9][0-9]*|-(alpha|beta)\.[1-9][0-9]*)?)$ ]]; then
              echo "package_spec must be openclaw@alpha, openclaw@beta, openclaw@latest, or an exact OpenClaw release version; got: ${PACKAGE_SPEC}" >&2
              exit 1
            fi
          fi
          case "${PROVIDER_MODE}" in
            mock-openai | live-frontier) ;;
            *)
              echo "provider_mode must be mock-openai or live-frontier; got: ${PROVIDER_MODE}" >&2
              exit 1
              ;;
          esac

          require_var() {
            local key="$1"
            if [[ -z "${!key:-}" ]]; then
              echo "Missing required ${key}." >&2
              exit 1
            fi
          }

          require_var OPENCLAW_QA_CONVEX_SITE_URL
          require_var OPENCLAW_QA_CONVEX_SECRET_CI
          if [[ "${PROVIDER_MODE}" == "live-frontier" ]]; then
            require_var OPENAI_API_KEY
          fi

      - name: Download package-under-test artifact
        if: inputs.package_artifact_name != '' && inputs.package_artifact_run_id == ''
        uses: actions/download-artifact@v8
        with:
          name: ${{ inputs.package_artifact_name }}
          path: .artifacts/telegram-package-under-test

      - name: Download package-under-test artifact from release run
        if: inputs.package_artifact_name != '' && inputs.package_artifact_run_id != ''
        uses: actions/download-artifact@v8
        with:
          name: ${{ inputs.package_artifact_name }}
          path: .artifacts/telegram-package-under-test
          run-id: ${{ inputs.package_artifact_run_id }}
          github-token: ${{ github.token }}

      - name: Run package Telegram E2E
        id: run_lane
        shell: bash
        env:
          OPENAI_API_KEY: ${{ secrets.OPENAI_API_KEY }}
          OPENCLAW_SKIP_DOCKER_BUILD: "1"
          OPENCLAW_DOCKER_E2E_IMAGE: openclaw-docker-e2e:local
          OPENCLAW_NPM_TELEGRAM_PACKAGE_SPEC: ${{ inputs.package_spec }}
          OPENCLAW_NPM_TELEGRAM_PACKAGE_LABEL: ${{ inputs.package_label }}
          OPENCLAW_NPM_TELEGRAM_PROVIDER_MODE: ${{ inputs.provider_mode }}
          OPENCLAW_NPM_TELEGRAM_CREDENTIAL_SOURCE: convex
          OPENCLAW_NPM_TELEGRAM_CREDENTIAL_ROLE: ci
          OPENCLAW_QA_CONVEX_SITE_URL: ${{ secrets.OPENCLAW_QA_CONVEX_SITE_URL }}
          OPENCLAW_QA_CONVEX_SECRET_CI: ${{ secrets.OPENCLAW_QA_CONVEX_SECRET_CI }}
          OPENCLAW_QA_CREDENTIAL_ACQUIRE_TIMEOUT_MS: "1800000"
          OPENCLAW_QA_REDACT_PUBLIC_METADATA: "1"
          OPENCLAW_QA_TELEGRAM_CAPTURE_CONTENT: "1"
          INPUT_SCENARIO: ${{ inputs.scenario }}
          PACKAGE_ARTIFACT_NAME: ${{ inputs.package_artifact_name || '' }}
        run: |
          set -euo pipefail

          output_dir=".artifacts/qa-e2e/npm-telegram-beta-${GITHUB_RUN_ID}-${GITHUB_RUN_ATTEMPT}"
          echo "output_dir=${output_dir}" >> "$GITHUB_OUTPUT"
          export OPENCLAW_NPM_TELEGRAM_OUTPUT_DIR="${output_dir}"

          append_telegram_summary() {
            local status=$?
            local report="${output_dir}/telegram-qa-report.md"
            if [[ -n "${GITHUB_STEP_SUMMARY:-}" && -f "${report}" ]]; then
              {
                echo "## Package Telegram E2E"
                echo
                echo "- Package: ${OPENCLAW_NPM_TELEGRAM_PACKAGE_LABEL:-${OPENCLAW_NPM_TELEGRAM_PACKAGE_SPEC}}"
                echo "- Provider mode: ${OPENCLAW_NPM_TELEGRAM_PROVIDER_MODE}"
                echo
                cat "${report}"
              } >> "${GITHUB_STEP_SUMMARY}"
            fi
            return "${status}"
          }
          trap append_telegram_summary EXIT

          if [[ -n "${PACKAGE_ARTIFACT_NAME// }" ]]; then
            mapfile -t package_tgzs < <(find .artifacts/telegram-package-under-test -type f -name "*.tgz" | sort)
            if [[ "${#package_tgzs[@]}" -ne 1 ]]; then
              echo "package artifact ${PACKAGE_ARTIFACT_NAME} must contain exactly one .tgz; found ${#package_tgzs[@]}" >&2
              exit 1
            fi
            export OPENCLAW_NPM_TELEGRAM_PACKAGE_TGZ="${package_tgzs[0]}"
            if [[ -z "${OPENCLAW_NPM_TELEGRAM_PACKAGE_LABEL// }" ]]; then
              export OPENCLAW_NPM_TELEGRAM_PACKAGE_LABEL="$(basename "${package_tgzs[0]}")"
            fi
          elif [[ -z "${OPENCLAW_NPM_TELEGRAM_PACKAGE_LABEL// }" ]]; then
            export OPENCLAW_NPM_TELEGRAM_PACKAGE_LABEL="${OPENCLAW_NPM_TELEGRAM_PACKAGE_SPEC}"
          fi

          if [[ -n "${INPUT_SCENARIO// }" ]]; then
            export OPENCLAW_NPM_TELEGRAM_SCENARIOS="${INPUT_SCENARIO}"
          fi

          pnpm test:docker:npm-telegram-live

      - name: Upload npm Telegram E2E artifacts
        if: always()
        uses: actions/upload-artifact@v7
        with:
          name: npm-telegram-beta-e2e-${{ github.run_id }}-${{ github.run_attempt }}
          path: .artifacts/qa-e2e/
          retention-days: 14
          if-no-files-found: warn
