name: Run Docker integration tests

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

jobs:
  test:
    runs-on: ubuntu-latest
    timeout-minutes: 15
    env:
      # Database configuration - these will be used by dev-compose.yaml
      LETTA_PG_DB: letta
      LETTA_PG_USER: letta
      LETTA_PG_PASSWORD: letta
      LETTA_PG_HOST: pgvector_db  # Internal Docker service name
      LETTA_PG_PORT: 5432
      # Server configuration for tests
      LETTA_SERVER_PASS: test_server_token
      LETTA_SERVER_URL: http://localhost:8283
      # API keys
      OPENAI_API_KEY: ${{ secrets.OPENAI_API_KEY }}
      # Additional API keys that dev-compose.yaml expects (optional)
      GROQ_API_KEY: ""
      ANTHROPIC_API_KEY: ""
      OLLAMA_BASE_URL: ""
      AZURE_API_KEY: ""
      AZURE_BASE_URL: ""
      AZURE_API_VERSION: ""
      GEMINI_API_KEY: ""
      VLLM_API_BASE: ""
      OPENLLM_AUTH_TYPE: ""
      OPENLLM_API_KEY: ""
    steps:
    - name: Checkout
      uses: actions/checkout@v4

    - name: Set up python 3.11
      id: setup-python
      uses: actions/setup-python@v5
      with:
        python-version: 3.11

    - name: Install uv
      run: |
          curl -LsSf https://astral.sh/uv/install.sh | sh
          echo "$HOME/.cargo/bin" >> $GITHUB_PATH

    - name: Set permissions for log directory
      run: |
        mkdir -p /home/runner/.letta/logs
        sudo chown -R $USER:$USER /home/runner/.letta/logs
        chmod -R 755 /home/runner/.letta/logs

    - name: Build and run docker dev server
      run: |
        # dev-compose.yaml will use the environment variables we set above
        docker compose -f dev-compose.yaml up --build -d

    - name: Wait for service
      run: bash scripts/wait_for_service.sh http://localhost:8283 -- echo "Service is ready"

    - name: Run tests with pytest
      env:
        PYTHONPATH: ${{ github.workspace }}:${{ env.PYTHONPATH }}
        LETTA_PG_URI: postgresql+pg8000://${{ env.LETTA_PG_USER }}:${{ env.LETTA_PG_PASSWORD }}@localhost:${{ env.LETTA_PG_PORT }}/${{ env.LETTA_PG_DB }}
      run: |
        uv sync --extra dev --extra postgres --extra sqlite
        uv run pytest -s tests/test_client.py

    - name: Print docker logs if tests fail
      if: failure()
      run: |
        echo "Printing Docker Logs..."
        docker compose -f dev-compose.yaml logs
