name: Contract Tests

on:
  workflow_dispatch:
  push:
    paths:
      - 'api/openapi.yaml'
      - 'internal/api/server_gen.go'
      - 'internal/api/adapter.go'
      - '.forgejo/workflows/contract-test.yml'
  pull_request:
    paths:
      - 'api/openapi.yaml'
      - 'internal/api/server_gen.go'
      - 'internal/api/adapter.go'

concurrency:
  group: ${{ github.workflow }}-${{ github.ref }}
  cancel-in-progress: true

permissions:
  contents: read

env:
  GO_VERSION: '1.25.0'

jobs:
  spec-drift-check:
    name: Spec Drift Detection
    runs-on: self-hosted
    timeout-minutes: 10

    steps:
    - name: Checkout code
      uses: actions/checkout@v5

    - name: Set up Go
      run: |
        go version || (wget https://go.dev/dl/go${GO_VERSION}.linux-amd64.tar.gz && \
          sudo rm -rf /usr/local/go && sudo tar -C /usr/local -xzf go${GO_VERSION}.linux-amd64.tar.gz)
        echo "PATH=$PATH:/usr/local/go/bin" >> $GITHUB_ENV
        go version

    - name: Install oapi-codegen
      run: |
        go install github.com/oapi-codegen/oapi-codegen/v2/cmd/oapi-codegen@latest
        echo "PATH=$PATH:$(go env GOPATH)/bin" >> $GITHUB_ENV

    - name: Verify spec matches implementation
      run: make openapi-check-implementation

  contract-test:
    name: API Contract Tests
    runs-on: self-hosted
    timeout-minutes: 15
    needs: spec-drift-check

    services:
      redis:
        image: redis:7
        ports:
          - 6379:6379
        options: >-
          --health-cmd "redis-cli ping"
          --health-interval 10s
          --health-timeout 5s
          --health-retries 5

    steps:
    - name: Checkout code
      uses: actions/checkout@v5

    - name: Set up Go
      run: |
        go version || (wget https://go.dev/dl/go${GO_VERSION}.linux-amd64.tar.gz && \
          sudo rm -rf /usr/local/go && sudo tar -C /usr/local -xzf go${GO_VERSION}.linux-amd64.tar.gz)
        echo "PATH=$PATH:/usr/local/go/bin" >> $GITHUB_ENV
        go version

    - name: Build API server
      run: |
        go build -o api-server ./cmd/api-server

    - name: Start API server
      run: |
        ./api-server --config configs/api/dev-local.yaml &
        echo "API_PID=$!" >> $GITHUB_ENV
        sleep 5

    - name: Install schemathesis
      run: |
        pip install schemathesis

    - name: Run contract tests
      run: |
        schemathesis run api/openapi.yaml \
          --base-url http://localhost:8080 \
          --checks all \
          --max-response-time 5000 \
          --hypothesis-max-examples 50
      continue-on-error: true  # Allow failures until all endpoints are fully implemented

    - name: Stop API server
      if: always()
      run: |
        if [ -n "$API_PID" ]; then
          kill $API_PID || true
        fi

    - name: Basic endpoint verification
      run: |
        echo "Testing /health endpoint..."
        curl -s http://localhost:8080/health || echo "Server not running, skipping"
        
        echo "Testing /v1/experiments endpoint..."
        curl -s http://localhost:8080/v1/experiments || echo "Server not running, skipping"
        
        echo "Testing /v1/tasks endpoint..."
        curl -s http://localhost:8080/v1/tasks || echo "Server not running, skipping"
      continue-on-error: true
