name: HTTP-MCP Bridge Tests

on:
  push:
    paths:
      - 'examples/http-mcp-bridge.js'
      - 'tests/bridge/**'
      - 'tests/integration/test_bridge_integration.js'
      - '.github/workflows/bridge-tests.yml'
  pull_request:
    paths:
      - 'examples/http-mcp-bridge.js'  
      - 'tests/bridge/**'
      - 'tests/integration/test_bridge_integration.js'

jobs:
  test-bridge:
    runs-on: ubuntu-latest
    strategy:
      matrix:
        node-version: [18.x, 20.x]
    
    steps:
    - name: Checkout code
      uses: actions/checkout@v4
    
    - name: Setup Node.js ${{ matrix.node-version }}
      uses: actions/setup-node@v4
      with:
        node-version: ${{ matrix.node-version }}
    
    - name: Install bridge unit test dependencies
      run: npm install
      working-directory: tests/bridge
    
    - name: Install integration test dependencies
      run: npm install
      working-directory: tests/integration
    
    - name: Run bridge unit tests
      run: npm test
      working-directory: tests/bridge
    
    - name: Run integration tests
      run: npm test
      working-directory: tests/integration
    
    - name: Validate health endpoint fix
      run: |
        if ! grep -q "/api/health" examples/http-mcp-bridge.js; then
          echo "❌ ERROR: Health endpoint should be /api/health"
          exit 1
        fi
        echo "✅ Health endpoint check passed"
    
    - name: Validate status code handling fix
      run: |
        if grep -v "|| response.statusCode === 201" examples/http-mcp-bridge.js | grep -q "statusCode === 201"; then
          echo "❌ ERROR: Found hardcoded 201-only status check - server returns 200!"
          exit 1
        fi
        echo "✅ Status code handling check passed"
    
    - name: Validate URL construction fix
      run: |
        if grep -q "new URL(path, this.endpoint)" examples/http-mcp-bridge.js; then
          echo "❌ ERROR: Old URL construction bug still present"
          exit 1
        fi
        echo "✅ URL construction check passed"

  contract-validation:
    runs-on: ubuntu-latest
    
    steps:
    - name: Checkout code
      uses: actions/checkout@v4
    
    - name: Setup Node.js
      uses: actions/setup-node@v4
      with:
        node-version: '20.x'
    
    - name: Validate API contracts
      run: node -e "
        const path = require('path');
        const mocks = require('./tests/bridge/mock_responses.js');
        const memorySuccess = mocks.mockResponses.memories.createSuccess;
        if (memorySuccess.status !== 200) throw new Error('Memory creation should return 200');
        if (!memorySuccess.body.hasOwnProperty('success')) throw new Error('Memory response must have success field');
        console.log('✅ API contract validation passed');
        "
    
    - name: Generate contract report
      run: |
        echo "# API Contract Report" > contract-report.md
        echo "Generated: $(date)" >> contract-report.md
        echo "" >> contract-report.md
        echo "## Critical Endpoints Validated" >> contract-report.md
        echo "- ✅ POST /api/memories: Returns 200 with success field" >> contract-report.md
        echo "- ✅ GET /api/health: Correct endpoint path used" >> contract-report.md
        echo "- ✅ URL construction: Preserves /api base path" >> contract-report.md
        echo "- ✅ Status codes: Handles HTTP 200 correctly" >> contract-report.md
    
    - name: Upload contract report
      uses: actions/upload-artifact@v4
      if: always()
      with:
        name: api-contract-report
        path: contract-report.md

  quick-smoke-test:
    runs-on: ubuntu-latest
    
    steps:
    - name: Checkout code
      uses: actions/checkout@v4
      
    - name: Setup Node.js
      uses: actions/setup-node@v4
      with:
        node-version: '20.x'
    
    - name: Check JavaScript syntax
      run: node -c examples/http-mcp-bridge.js
    
    - name: Test bridge instantiation
      run: node -e "
        const Bridge = require('./examples/http-mcp-bridge.js');
        const bridge = new Bridge();
        console.log('✅ Bridge can be instantiated');
        "
    
    - name: Test URL construction
      run: |
        node -e "
        const Bridge = require('./examples/http-mcp-bridge.js');
        const bridge = new Bridge();
        bridge.endpoint = 'https://test.local:8443/api';
        const fullPath = '/memories';
        let baseUrl;
        if (bridge.endpoint.endsWith('/')) {
          baseUrl = bridge.endpoint.slice(0, -1);
        } else {
          baseUrl = bridge.endpoint;
        }
        if (baseUrl + fullPath !== 'https://test.local:8443/api/memories') {
          throw new Error('URL construction test failed');
        }
        console.log('✅ URL construction works correctly');
        "