69 lines
2.5 KiB
JavaScript
69 lines
2.5 KiB
JavaScript
#!/usr/bin/env node
|
|
|
|
const axios = require('axios');
|
|
|
|
// Test webhook endpoint
|
|
const WEBHOOK_URL = process.env.WEBHOOK_URL || 'http://localhost:3001';
|
|
|
|
// Sample listing data for testing
|
|
const sampleListing = {
|
|
id: `test-listing-${Date.now()}`,
|
|
title: 'Test Apartment for Webhook',
|
|
type: 'RENT',
|
|
status: 'ACTIVE',
|
|
price: 35000,
|
|
subcity: 'Bole',
|
|
houseType: 'Apartment',
|
|
createdAt: new Date().toISOString(),
|
|
url: 'https://yaltipia.com/listing/test-123'
|
|
};
|
|
|
|
async function testWebhook() {
|
|
console.log('🧪 Testing Webhook System...\n');
|
|
|
|
try {
|
|
// Test health endpoint
|
|
console.log('1. Testing health endpoint...');
|
|
const healthResponse = await axios.get(`${WEBHOOK_URL}/webhook/health`);
|
|
console.log('✅ Health check passed:', healthResponse.data.message);
|
|
|
|
// Test status endpoint
|
|
console.log('\n2. Testing status endpoint...');
|
|
const statusResponse = await axios.get(`${WEBHOOK_URL}/status`);
|
|
console.log('✅ Status check passed');
|
|
console.log(' Webhook running:', statusResponse.data.webhook.running);
|
|
console.log(' Notifications running:', statusResponse.data.automaticNotifications.isRunning);
|
|
|
|
// Test new listing webhook
|
|
console.log('\n3. Testing new listing webhook...');
|
|
const newListingResponse = await axios.post(`${WEBHOOK_URL}/webhook/new-listing`, sampleListing);
|
|
console.log('✅ New listing webhook passed:', newListingResponse.data.message);
|
|
console.log(' Processed listing ID:', newListingResponse.data.listingId);
|
|
|
|
// Test listing update webhook
|
|
console.log('\n4. Testing listing update webhook...');
|
|
const updatedListing = { ...sampleListing, price: 40000 };
|
|
const updateResponse = await axios.post(`${WEBHOOK_URL}/webhook/update-listing`, updatedListing);
|
|
console.log('✅ Update listing webhook passed:', updateResponse.data.message);
|
|
|
|
console.log('\n🎉 All webhook tests passed successfully!');
|
|
|
|
} catch (error) {
|
|
console.error('❌ Webhook test failed:', error.message);
|
|
|
|
if (error.response) {
|
|
console.error('Response status:', error.response.status);
|
|
console.error('Response data:', error.response.data);
|
|
}
|
|
|
|
if (error.code === 'ECONNREFUSED') {
|
|
console.error('\n💡 Make sure the bot is running and webhook server is started');
|
|
console.error(' Run: npm start');
|
|
}
|
|
|
|
process.exit(1);
|
|
}
|
|
}
|
|
|
|
// Run the test
|
|
testWebhook(); |