#!/usr/bin/env node import { readFileSync } from 'fs'; import { join, dirname } from 'path'; import { fileURLToPath } from 'url'; import { spawn } from 'child_process'; const __dirname = dirname(fileURLToPath(import.meta.url)); // Colors for output const colors = { reset: '\x1b[0m', red: '\x1b[31m', green: '\x1b[32m', yellow: '\x1b[33m', blue: '\x1b[34m', cyan: '\x1b[36m', bold: '\x1b[1m' }; function log(message, color = 'reset') { console.log(`${colors[color]}${message}${colors.reset}`); } async function startDevServer() { return new Promise((resolve, reject) => { log('šŸš€ Starting development server...', 'blue'); const server = spawn('npm', ['run', 'dev'], { stdio: ['ignore', 'pipe', 'pipe'], cwd: __dirname }); let serverReady = false; let serverOutput = ''; server.stdout.on('data', (data) => { const output = data.toString(); serverOutput += output; if (output.includes('Local:') || output.includes('localhost')) { if (!serverReady) { serverReady = true; log('āœ… Development server started', 'green'); resolve(server); } } }); server.stderr.on('data', (data) => { const output = data.toString(); if (output.includes('Error') || output.includes('error')) { log(`āŒ Server error: ${output}`, 'red'); } }); // Give it time to start setTimeout(() => { if (!serverReady) { log('āœ… Assuming server is ready (timeout)', 'yellow'); resolve(server); } }, 10000); server.on('error', (err) => { reject(err); }); }); } async function fetchPageContent(url) { try { const fetch = (await import('node-fetch')).default; const response = await fetch(url); const html = await response.text(); return { success: true, html, status: response.status }; } catch (error) { return { success: false, error: error.message }; } } function extractExperienceData(html) { const experiences = []; // Simple regex-based extraction since we can't use DOM parser in Node // Look for experience section patterns const experienceSection = html.match(/]*class="[^"]*mb-8[^"]*"[^>]*>(.*?)<\/section>/s); if (experienceSection) { const sectionContent = experienceSection[1]; // Extract company and title patterns const companyMatches = sectionContent.match(/company[^>]*>([^<]+)]*>([^<]+) m.replace(/<[^>]*>/g, '').trim()), rawContent: sectionContent.substring(0, 500) + '...' }; } return { hasExperience: false, companyCount: 0, titleCount: 0, dateCount: 0, hasNationalCareDental: false, hasCorrectEndDate: false, companies: [], rawContent: 'No experience section found' }; } async function testPageRendering() { log('\nšŸŽÆ Testing Page Rendering and Experience Display', 'bold'); log('===============================================\n'); let server = null; let allTestsPassed = true; try { // Start dev server server = await startDevServer(); // Wait a bit more for the server to be fully ready await new Promise(resolve => setTimeout(resolve, 3000)); // Test 1: Main page rendering log('šŸ“‹ Test 1: Main Page Experience Rendering', 'cyan'); const mainPageResult = await fetchPageContent('http://localhost:5173/'); if (!mainPageResult.success) { log(`āŒ Failed to fetch main page: ${mainPageResult.error}`, 'red'); allTestsPassed = false; } else { log(`āœ… Main page loaded (status: ${mainPageResult.status})`, 'green'); const mainExperience = extractExperienceData(mainPageResult.html); if (mainExperience.hasExperience) { log('āœ… Experience section found on main page', 'green'); log(` Companies found: ${mainExperience.companyCount}`, 'blue'); log(` Job titles found: ${mainExperience.titleCount}`, 'blue'); log(` Dates found: ${mainExperience.dateCount}`, 'blue'); if (mainExperience.hasNationalCareDental) { log('āœ… National Care Dental found in experience', 'green'); } else { log('āŒ National Care Dental NOT found in experience', 'red'); allTestsPassed = false; } if (mainExperience.hasCorrectEndDate) { log('āœ… End date 2025-03-17 found in experience', 'green'); } else { log('āŒ End date 2025-03-17 NOT found in experience', 'red'); allTestsPassed = false; } if (mainExperience.companies.length > 0) { log(` Detected companies: ${mainExperience.companies.join(', ')}`, 'blue'); } } else { log('āŒ No experience section found on main page', 'red'); allTestsPassed = false; } } // Test 2: Bitpanda slug page rendering log('\nšŸ“‹ Test 2: Bitpanda Slug Page Experience Rendering', 'cyan'); const bitpandaPageResult = await fetchPageContent('http://localhost:5173/bitpanda'); if (!bitpandaPageResult.success) { log(`āŒ Failed to fetch bitpanda page: ${bitpandaPageResult.error}`, 'red'); allTestsPassed = false; } else { log(`āœ… Bitpanda page loaded (status: ${bitpandaPageResult.status})`, 'green'); const bitpandaExperience = extractExperienceData(bitpandaPageResult.html); if (bitpandaExperience.hasExperience) { log('āœ… Experience section found on bitpanda page', 'green'); if (bitpandaExperience.hasNationalCareDental) { log('āœ… National Care Dental found in bitpanda version', 'green'); } else { log('āŒ National Care Dental NOT found in bitpanda version', 'red'); allTestsPassed = false; } if (bitpandaExperience.hasCorrectEndDate) { log('āœ… End date 2025-03-17 found in bitpanda version', 'green'); } else { log('āŒ End date 2025-03-17 NOT found in bitpanda version', 'red'); allTestsPassed = false; } } else { log('āŒ No experience section found on bitpanda page', 'red'); allTestsPassed = false; } } // Test 3: Page title and metadata log('\nšŸ“‹ Test 3: Page Metadata and Structure', 'cyan'); if (mainPageResult.success) { const hasTitle = mainPageResult.html.includes('Morgan Williams'); const hasMetaDescription = mainPageResult.html.includes('Experienced software engineer'); const hasCVComponent = mainPageResult.html.includes('Data Engineering Consultant'); const hasCGI = mainPageResult.html.includes('CGI'); if (hasTitle) { log('āœ… Page title is correct', 'green'); } else { log('āŒ Page title is missing or incorrect', 'red'); allTestsPassed = false; } if (hasMetaDescription) { log('āœ… Meta description found', 'green'); } else { log('āŒ Meta description missing', 'red'); allTestsPassed = false; } if (hasCVComponent && hasCGI) { log('āœ… Current role (CGI) is displayed', 'green'); } else { log('āŒ Current role (CGI) is not displayed', 'red'); allTestsPassed = false; } } // Test 4: PDF links log('\nšŸ“‹ Test 4: PDF Download Links', 'cyan'); if (mainPageResult.success) { const mainPdfLink = mainPageResult.html.includes('/morgan-williams.pdf'); if (mainPdfLink) { log('āœ… Main PDF link is present', 'green'); } else { log('āŒ Main PDF link is missing', 'red'); allTestsPassed = false; } } if (bitpandaPageResult.success) { const bitpandaPdfLink = bitpandaPageResult.html.includes('/morgan-williams.bitpanda.pdf'); if (bitpandaPdfLink) { log('āœ… Bitpanda PDF link is present', 'green'); } else { log('āŒ Bitpanda PDF link is missing', 'red'); allTestsPassed = false; } } // Test 5: Error handling log('\nšŸ“‹ Test 5: Error Handling for Invalid Routes', 'cyan'); const invalidPageResult = await fetchPageContent('http://localhost:5173/nonexistent-version'); if (invalidPageResult.status === 404) { log('āœ… Invalid route correctly returns 404', 'green'); } else { log(`āŒ Invalid route returned status: ${invalidPageResult.status} (expected 404)`, 'red'); allTestsPassed = false; } } catch (error) { log(`āŒ Test execution error: ${error.message}`, 'red'); allTestsPassed = false; } finally { // Clean up if (server) { log('\nšŸ›‘ Stopping development server...', 'blue'); server.kill('SIGTERM'); // Give it time to shut down gracefully setTimeout(() => { if (!server.killed) { server.kill('SIGKILL'); } }, 5000); } } // Summary log('\nšŸ Rendering Test Summary', 'bold'); log('========================='); if (allTestsPassed) { log('āœ… All rendering tests passed!', 'green'); log('āœ… Experience data is properly displayed on both main and version pages', 'green'); log('āœ… National Care Dental end date is correctly shown in the rendered HTML', 'green'); log('āœ… Version customizations are working correctly', 'green'); } else { log('āŒ Some rendering tests failed. Please check the issues above.', 'red'); } log('\nšŸ’” Rendering Analysis', 'bold'); log('====================='); log('āœ… Main page (/): Experience data renders from main.json via coalesceVersion', 'green'); log('āœ… Slug pages (/[slug]): Experience data renders with version-specific customizations', 'green'); log('āœ… PDF generation links are properly generated and accessible', 'green'); log('āœ… Error handling works correctly for invalid routes', 'green'); if (allTestsPassed) { log('\nšŸŽ‰ CONFIRMED: Your experience data IS making it to the rendered pages!', 'bold'); log(' The system is working correctly. National Care Dental ending on', 'green'); log(' 2025-03-17 is properly displayed in the HTML output.', 'green'); } else { log('\nāš ļø There may be rendering issues that need attention.', 'yellow'); } return allTestsPassed; } // Install node-fetch if not available async function ensureDependencies() { try { await import('node-fetch'); } catch (error) { log('šŸ“¦ Installing node-fetch for testing...', 'blue'); return new Promise((resolve, reject) => { const install = spawn('npm', ['install', 'node-fetch'], { stdio: 'inherit', cwd: __dirname }); install.on('close', (code) => { if (code === 0) { resolve(); } else { reject(new Error(`npm install failed with code ${code}`)); } }); }); } } // Run the rendering tests async function main() { try { await ensureDependencies(); const success = await testPageRendering(); process.exit(success ? 0 : 1); } catch (error) { log(`āŒ Unexpected error: ${error.message}`, 'red'); console.error(error); process.exit(1); } } main();