#!/usr/bin/env node /** * Test sync health endpoint * Verifies graceful handling of MongoDB disconnection */ require('dotenv').config(); const fetch = require('node-fetch'); const { generateToken } = require('../src/utils/jwt.util'); async function testSyncHealth() { try { // Generate admin token using proper utility const token = generateToken({ userId: 'admin', role: 'admin' }); console.log('Testing sync health endpoint...\n'); // Test sync health endpoint const response = await fetch('http://localhost:9000/api/admin/sync/health', { method: 'GET', headers: { 'Authorization': `Bearer ${token}`, 'Content-Type': 'application/json' } }); const data = await response.json(); console.log('Status:', response.status); console.log('Response:', JSON.stringify(data, null, 2)); if (response.status === 200 && data.success) { console.log('\n✓ Endpoint responding correctly'); if (data.health.status === 'warning' && data.health.message.includes('Database not connected')) { console.log('✓ MongoDB disconnection handled gracefully'); console.log('✓ Recommendations provided:', data.health.recommendations); } else if (data.health.status === 'healthy') { console.log('✓ MongoDB connected and sync healthy'); } } else { console.log('\n✗ Endpoint returned error'); } } catch (error) { console.error('✗ Test failed:', error.message); process.exit(1); } } testSyncHealth();