name: Integration Tests

on:
  pull_request:
    paths:
      - 'src/**.py'
      - 'tests/**.py'
      - 'pyproject.toml'
      - 'uv.lock'
      - '.github/workflows/test-integration.yml'
  workflow_dispatch:
    inputs:
      use_local_images:
        description: 'Build images locally instead of pulling from DockerHub'
        required: false
        type: boolean
        default: false

jobs:
  tests:
    runs-on: [self-hosted, linux, ARM64, langflow-ai-arm64-40gb]
    env:
      # Prefer repository/environment variable first, then secret, then a sane fallback
      OPENSEARCH_PASSWORD: ${{ vars.OPENSEARCH_PASSWORD || secrets.OPENSEARCH_PASSWORD || 'OpenRag#2025!' }}
      OPENAI_API_KEY: ${{ secrets.OPENAI_API_KEY }}
      LANGFLOW_AUTO_LOGIN: "True"
      LANGFLOW_NEW_USER_IS_ACTIVE: "True"
      LANGFLOW_ENABLE_SUPERUSER_CLI: "True"
      LANGFLOW_CHAT_FLOW_ID: ${{ vars.LANGFLOW_CHAT_FLOW_ID || '1098eea1-6649-4e1d-aed1-b77249fb8dd0' }}
      NUDGES_FLOW_ID: ${{ vars.NUDGES_FLOW_ID || 'ebc01d31-1976-46ce-a385-b0240327226c' }}

    steps:
      - run: df -h
        #- name: "node-cleanup"
        #run: |
        #  sudo rm -rf /usr/share/dotnet /usr/local/lib/android /opt/ghc /opt/hostedtoolcache/CodeQL
        #  sudo docker image prune --all --force
        #  sudo docker builder prune -a
      - run: df -h
      - name: Checkout
        uses: actions/checkout@v4

      - name: Set up UV
        uses: astral-sh/setup-uv@v3
        with:
          version: latest

      - name: Python version
        run: uv python install 3.13

      - name: Install dependencies
        run: uv sync

      - name: Run integration tests
        env:
          OPENSEARCH_HOST: localhost
          OPENSEARCH_PORT: 9200
          OPENSEARCH_USERNAME: admin
          OPENSEARCH_PASSWORD: ${{ env.OPENSEARCH_PASSWORD }}
          LOG_LEVEL: DEBUG
          # Force no-auth mode so tests bypass OAuth
          GOOGLE_OAUTH_CLIENT_ID: ""
          GOOGLE_OAUTH_CLIENT_SECRET: ""
          # Disable startup ingest noise unless a test enables it
          DISABLE_STARTUP_INGEST: "true"
        run: |
          if [ "${{ inputs.use_local_images }}" == "true" ]; then
            echo "Running tests with locally built images..."
            make test-ci-local
          else
            echo "Running tests with DockerHub images..."
            make test-ci
          fi
          echo "Keys directory after tests:"
          ls -la keys/ || echo "No keys directory"
