#!/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);