# Reusable workflow for running multi-language SDK tests against GreptimeDB
# Used by: multi-lang-tests.yml, release.yml, nightly-build.yml
# Supports both direct binary artifacts and tarball artifacts

name: Run Multi-language SDK Tests

on:
  workflow_call:
    inputs:
      artifact-name:
        required: true
        type: string
        description: 'Name of the artifact containing greptime binary'
      http-port:
        required: false
        type: string
        default: '4000'
        description: 'HTTP server port'
      mysql-port:
        required: false
        type: string
        default: '4002'
        description: 'MySQL server port'
      postgres-port:
        required: false
        type: string
        default: '4003'
        description: 'PostgreSQL server port'
      db-name:
        required: false
        type: string
        default: 'test_db'
        description: 'Test database name'
      username:
        required: false
        type: string
        default: 'greptime_user'
        description: 'Authentication username'
      password:
        required: false
        type: string
        default: 'greptime_pwd'
        description: 'Authentication password'
      timeout-minutes:
        required: false
        type: number
        default: 30
        description: 'Job timeout in minutes'
      artifact-is-tarball:
        required: false
        type: boolean
        default: false
        description: 'Whether the artifact is a tarball (tar.gz) that needs to be extracted'

jobs:
  run-tests:
    name: Run Multi-language SDK Tests
    runs-on: ubuntu-latest
    timeout-minutes: ${{ inputs.timeout-minutes }}
    steps:
      - name: Checkout greptimedb-tests repository
        uses: actions/checkout@v4
        with:
          repository: GreptimeTeam/greptimedb-tests
          persist-credentials: false

      - name: Download pre-built greptime binary
        uses: actions/download-artifact@v4
        with:
          name: ${{ inputs.artifact-name }}
          path: .

      - name: Unzip binaries
        run: tar -xvf ./${{ inputs.artifact-name }}.tar.gz

      - name: Print greptime binary info
        run: ls -lh ${{ inputs.artifact-name }} && file ${{ inputs.artifact-name }}/greptime

      - name: Setup Java 17
        uses: actions/setup-java@v4
        with:
          distribution: 'temurin'
          java-version: '17'
          cache: 'maven'

      - name: Setup Python 3.8
        uses: actions/setup-python@v5
        with:
          python-version: '3.8'

      - name: Setup uv
        uses: astral-sh/setup-uv@v3
        with:
          version: "latest"

      - name: Setup Go 1.24
        uses: actions/setup-go@v5
        with:
          go-version: '1.24'
          cache: true
          cache-dependency-path: go-tests/go.sum

      - name: Set up Node.js
        uses: actions/setup-node@v4
        with:
          node-version: '18'

      - name: Install Python dependencies
        run: |
          pip install mysql-connector-python psycopg2-binary
          python3 -c "import mysql.connector; print(f'mysql-connector-python {mysql.connector.__version__}')"
          python3 -c "import psycopg2; print(f'psycopg2 {psycopg2.__version__}')"

      - name: Install Go dependencies
        working-directory: go-tests
        run: |
          go mod download
          go mod verify
          go version

      - name: Kill existing GreptimeDB processes
        run: |
          pkill -f greptime || true
          sleep 2

      - name: Start GreptimeDB standalone
        run: |
          ./bins/greptime standalone start \
            --http-addr 0.0.0.0:${{ inputs.http-port }} \
            --grpc-bind-addr 0.0.0.0:4001 \
            --mysql-addr 0.0.0.0:${{ inputs.mysql-port }} \
            --postgres-addr 0.0.0.0:${{ inputs.postgres-port }} \
            --user-provider=static_user_provider:cmd:${{ inputs.username }}=${{ inputs.password }} > /tmp/greptimedb.log 2>&1 &

      - name: Wait for GreptimeDB to be ready
        run: |
          echo "Waiting for GreptimeDB..."
          for i in {1..60}; do
            if curl -sf http://localhost:${{ inputs.http-port }}/health > /dev/null; then
              echo "✅ GreptimeDB is ready"
              exit 0
            fi
            sleep 2
          done
          echo "❌ GreptimeDB failed to start"
          cat /tmp/greptimedb.log
          exit 1

      - name: Run multi-language tests
        env:
          DB_NAME: ${{ inputs.db-name }}
          MYSQL_HOST: 127.0.0.1
          MYSQL_PORT: ${{ inputs.mysql-port }}
          POSTGRES_HOST: 127.0.0.1
          POSTGRES_PORT: ${{ inputs.postgres-port }}
          HTTP_HOST: 127.0.0.1
          HTTP_PORT: ${{ inputs.http-port }}
          GREPTIME_USERNAME: ${{ inputs.username }}
          GREPTIME_PASSWORD: ${{ inputs.password }}
        run: |
          chmod +x ./run_tests.sh
          ./run_tests.sh

      - name: Collect logs on failure
        if: failure()
        run: |
          echo "=== GreptimeDB Logs ==="
          cat /tmp/greptimedb.log || true

      - name: Upload test logs on failure
        if: failure()
        uses: actions/upload-artifact@v4
        with:
          name: test-logs
          path: |
            /tmp/greptimedb.log
            java-tests/target/surefire-reports/
            python-tests/.pytest_cache/
            go-tests/*.log
            **/test-output/
          retention-days: 7

      - name: Cleanup
        if: always()
        run: |
          pkill -f greptime || true
