// test-webhook.js - Simple test script for webhook endpoint
const fetch = require('node-fetch');

const WEBHOOK_URL = 'http://localhost:8012/api/github/webhook';

// Test webhook with a sample GitHub push event
const testPayload = {
  ref: 'refs/heads/main',
  before: 'abc123',
  after: 'def456',
  repository: {
    id: 123456,
    name: 'test-repo',
    full_name: 'testuser/test-repo',
    owner: {
      login: 'testuser',
      id: 789
    }
  },
  pusher: {
    name: 'testuser',
    email: 'test@example.com'
  },
  commits: [
    {
      id: 'def456',
      message: 'Test commit',
      author: {
        name: 'Test User',
        email: 'test@example.com'
      }
    }
  ]
};

async function testWebhook() {
  try {
    console.log('🧪 Testing webhook endpoint...');
    console.log(`📡 Sending POST request to: ${WEBHOOK_URL}`);
    
    const response = await fetch(WEBHOOK_URL, {
      method: 'POST',
      headers: {
        'Content-Type': 'application/json',
        'X-GitHub-Event': 'push',
        'X-GitHub-Delivery': 'test-delivery-123'
      },
      body: JSON.stringify(testPayload)
    });

    const result = await response.json();
    
    console.log('📊 Response Status:', response.status);
    console.log('📋 Response Body:', JSON.stringify(result, null, 2));
    
    if (response.ok) {
      console.log('✅ Webhook test successful!');
    } else {
      console.log('❌ Webhook test failed!');
    }
    
  } catch (error) {
    console.error('❌ Error testing webhook:', error.message);
  }
}

// Run the test
testWebhook();


