import http from 'http'; // Helper to fetch HTML content from the local server function fetchHtml(url) { return new Promise((resolve, reject) => { http.get(url, (res) => { const { statusCode } = res; if (statusCode !== 200) { reject(new Error(`Failed to fetch page. Status Code: ${statusCode}`)); return; } res.setEncoding('utf8'); let rawData = ''; res.on('data', (chunk) => { rawData += chunk; }); res.on('end', () => { resolve(rawData); }); }).on('error', (e) => { reject(new Error(`Error fetching URL: ${e.message}`)); }); }); } // Custom parser to extract tags, attributes, and text function parseHtml(html) { const tags = []; const tagRegExp = /<([a-z0-9-]+)(?:\s+([^>]*))?>/gi; const attrRegExp = /([a-z0-9-]+)(?:=(?:"([^"]*)"|'([^']*)'|([^>\s]+)))?/gi; let match; while ((match = tagRegExp.exec(html)) !== null) { const tagName = match[1].toLowerCase(); const attrString = match[2] || ''; const attrs = {}; let attrMatch; while ((attrMatch = attrRegExp.exec(attrString)) !== null) { const attrName = attrMatch[1].toLowerCase(); const attrValue = attrMatch[2] || attrMatch[3] || attrMatch[4] || ''; attrs[attrName] = attrValue; } // Attempt to extract text content after this tag until next tag const startIndex = match.index + match[0].length; const nextTagIndex = html.indexOf('<', startIndex); const textContent = nextTagIndex > startIndex ? html.slice(startIndex, nextTagIndex).trim() : ''; tags.push({ name: tagName, attrs, text: textContent, outerHtml: match[0], index: match.index }); } return tags; } async function runAudit() { const url = process.env.AUDIT_URL || 'http://localhost:3001'; console.log(`\n======================================================`); console.log(`š HUBAPI AUTOMATED STATIC CODE & ACCESSIBILITY AUDIT`); console.log(`======================================================`); console.log(`š” Connecting to local server: ${url}...`); let html; try { html = await fetchHtml(url); console.log(`ā Connection established successfully. Pages HTML loaded (${html.length} bytes).`); } catch (error) { console.error(`ā Connection failed: ${error.message}`); console.log(`š” Ensure Next.js dev server is running on http://localhost:3000`); process.exit(1); } const tags = parseHtml(html); let passed = 0; let failed = 0; const warnings = []; const successes = []; // 1. HTML lang attribute const htmlTag = tags.find(t => t.name === 'html'); if (htmlTag && htmlTag.attrs.lang) { successes.push(`SEO/a11y: tag specifies valid lang attribute ("${htmlTag.attrs.lang}")`); passed++; } else { warnings.push(`a11y: tag does not specify a "lang" attribute (critical for screen readers).`); failed++; } // 2. Page Title const titleTag = tags.find(t => t.name === 'title'); if (titleTag && titleTag.text) { successes.push(`SEO: Title tag found: "${titleTag.text}"`); passed++; } else { warnings.push(`SEO: Page