140 lines
4.8 KiB
JavaScript
140 lines
4.8 KiB
JavaScript
#!/usr/bin/env node
|
||
/**
|
||
* Test script for the Property Brochure PDF Generator API
|
||
* Run this after starting the server to test the API endpoints
|
||
*/
|
||
|
||
const fs = require('fs');
|
||
const path = require('path');
|
||
|
||
// Test HTML content
|
||
const testHTML = fs.readFileSync('test.html', 'utf8');
|
||
|
||
// Test the API endpoints
|
||
async function testAPI() {
|
||
console.log('🧪 Testing Property Brochure PDF Generator API...\n');
|
||
|
||
// Test 1: Root endpoint
|
||
console.log('1️⃣ Testing GET / endpoint...');
|
||
try {
|
||
const response = await fetch('http://localhost:8000/');
|
||
const data = await response.json();
|
||
console.log('✅ Root endpoint working:', data.message);
|
||
console.log(' Version:', data.version);
|
||
console.log(' Status:', data.status);
|
||
} catch (error) {
|
||
console.log('❌ Root endpoint failed:', error.message);
|
||
}
|
||
|
||
// Test 2: Health endpoint
|
||
console.log('\n2️⃣ Testing GET /health endpoint...');
|
||
try {
|
||
const response = await fetch('http://localhost:8000/health');
|
||
const data = await response.json();
|
||
console.log('✅ Health endpoint working:', data.status);
|
||
console.log(' Service:', data.service);
|
||
} catch (error) {
|
||
console.log('❌ Health endpoint failed:', error.message);
|
||
}
|
||
|
||
// Test 3: PDF generation
|
||
console.log('\n3️⃣ Testing POST /generate-pdf endpoint...');
|
||
try {
|
||
const response = await fetch('http://localhost:8000/generate-pdf', {
|
||
method: 'POST',
|
||
headers: {
|
||
'Content-Type': 'application/json',
|
||
},
|
||
body: JSON.stringify({
|
||
input: testHTML,
|
||
output: 'test_brochure.pdf'
|
||
})
|
||
});
|
||
|
||
if (response.ok) {
|
||
const buffer = await response.arrayBuffer();
|
||
const outputPath = path.join(__dirname, 'test_brochure.pdf');
|
||
fs.writeFileSync(outputPath, Buffer.from(buffer));
|
||
console.log('✅ PDF generation successful!');
|
||
console.log(' File saved as: test_brochure.pdf');
|
||
console.log(' File size:', (buffer.byteLength / 1024).toFixed(2), 'KB');
|
||
} else {
|
||
const error = await response.json();
|
||
console.log('❌ PDF generation failed:', error.error);
|
||
}
|
||
} catch (error) {
|
||
console.log('❌ PDF generation failed:', error.message);
|
||
}
|
||
|
||
// Test 4: Error handling - empty input
|
||
console.log('\n4️⃣ Testing error handling (empty input)...');
|
||
try {
|
||
const response = await fetch('http://localhost:8000/generate-pdf', {
|
||
method: 'POST',
|
||
headers: {
|
||
'Content-Type': 'application/json',
|
||
},
|
||
body: JSON.stringify({
|
||
input: '',
|
||
output: 'error_test.pdf'
|
||
})
|
||
});
|
||
|
||
if (response.status === 400) {
|
||
const error = await response.json();
|
||
console.log('✅ Error handling working:', error.error);
|
||
} else {
|
||
console.log('❌ Expected error 400, got:', response.status);
|
||
}
|
||
} catch (error) {
|
||
console.log('❌ Error handling test failed:', error.message);
|
||
}
|
||
|
||
// Test 5: Alternative format (Apex compatibility)
|
||
console.log('\n5️⃣ Testing alternative format (Apex compatibility)...');
|
||
try {
|
||
const response = await fetch('http://localhost:8000/generate-pdf', {
|
||
method: 'POST',
|
||
headers: {
|
||
'Content-Type': 'application/json',
|
||
},
|
||
body: JSON.stringify({
|
||
html_content: testHTML,
|
||
filename: 'apex_test.pdf'
|
||
})
|
||
});
|
||
|
||
if (response.ok) {
|
||
const buffer = await response.arrayBuffer();
|
||
const outputPath = path.join(__dirname, 'apex_test.pdf');
|
||
fs.writeFileSync(outputPath, Buffer.from(buffer));
|
||
console.log('✅ Apex format working!');
|
||
console.log(' File saved as: apex_test.pdf');
|
||
} else {
|
||
const error = await response.json();
|
||
console.log('❌ Apex format failed:', error.error);
|
||
}
|
||
} catch (error) {
|
||
console.log('❌ Apex format test failed:', error.message);
|
||
}
|
||
|
||
console.log('\n🎉 API testing completed!');
|
||
console.log('\n📁 Generated files:');
|
||
if (fs.existsSync('test_brochure.pdf')) {
|
||
console.log(' • test_brochure.pdf');
|
||
}
|
||
if (fs.existsSync('apex_test.pdf')) {
|
||
console.log(' • apex_test.pdf');
|
||
}
|
||
}
|
||
|
||
// Check if fetch is available (Node.js 18+)
|
||
if (typeof fetch === 'undefined') {
|
||
console.log('❌ This script requires Node.js 18+ or you need to install node-fetch');
|
||
console.log(' Install with: npm install node-fetch');
|
||
process.exit(1);
|
||
}
|
||
|
||
// Run tests
|
||
testAPI().catch(console.error);
|