name: 'Verify an NPM release'
description: 'Fetches a package from NPM and does some basic smoke tests'

inputs:
  npm-package:
    description: 'NPM Package'
    required: true
    default: '@google/gemini-cli@latest'
  expected-version:
    description: 'Expected version'
    required: true
  gemini_api_key:
    description: 'The API key for running integration tests.'
    required: true
  ref:
    description: 'The branch, tag, or SHA to release from.'
    required: false
    type: 'string'
    default: 'main'

runs:
  using: 'composite'
  steps:
    - name: '📝 Print Inputs'
      shell: 'bash'
      run: 'echo "${{ toJSON(inputs) }}"'

    - name: 'Checkout'
      uses: 'actions/checkout@08eba0b27e820071cde6df949e0beb9ba4906955' # ratchet:actions/checkout@v4
      with:
        path: 'verify'
        ref: '${{ github.event.inputs.ref }}'
        fetch-depth: 0

    - name: 'Install from NPM'
      uses: 'nick-fields/retry@ce71cc2ab81d554ebbe88c79ab5975992d79ba08' # ratchet:nick-fields/retry@v3
      with:
        timeout_seconds: 900
        retry_wait_seconds: 30
        max_attempts: 10
        command: 'cd ./verify && pkg="${{ inputs.npm-package }}" && npm install --prefer-online --no-cache -g "$pkg"'

    - name: 'Basic smoke test'
      shell: 'bash'
      working-directory: './verify'
      run: |-
        gemini_version=$(gemini --version)
        if [ "$gemini_version" != "${{ inputs.expected-version }}" ]; then
          echo "❌ Version mismatch: Got $gemini_version from ${{ inputs.npm-package }}, expected ${{ inputs.expected-version }}"
          exit 1
        fi

    - name: 'Install dependencies for integration tests'
      shell: 'bash'
      working-directory: './verify'
      run: 'npm install'

    - name: '🔬 Run integration tests against NPM release'
      working-directory: './verify'
      env:
        GEMINI_API_KEY: '${{ inputs.gemini_api_key }}'
        INTEGRATION_TEST_USE_INSTALLED_GEMINI: 'true'
      shell: 'bash'
      run: 'npm run test:integration:sandbox:none'
