const http = require('http'); const fs = require('fs'); const path = require('path'); const { chromium } = require('playwright'); const ROOT = path.resolve(__dirname, '..'); // Find available chromium executable or default to Playwright's cached browser function findChromiumPath() { const candidates = [ '/home/alteixeira20/.cache/ms-playwright/chromium_headless_shell-1248/chrome-headless-shell-linux64/chrome-headless-shell', '/home/alteixeira20/.cache/ms-playwright/chromium-1248/chrome-linux/chrome', ]; for (const c of candidates) { if (fs.existsSync(c)) return c; } return undefined; } async function run() { const server = http.createServer((req, res) => { const url = new URL(req.url, 'http://127.0.0.1'); if (url.pathname.startsWith('/api/email/read/')) { const uid = url.pathname.replace('/api/email/read/', ''); if (uid === 'fail_uid' || uid === 'net_fail') { res.writeHead(500, { 'Content-Type': 'application/json' }); res.end(JSON.stringify({ error: 'Mailbox disconnected' })); return; } if (uid === 'crash_network') { req.destroy(); return; } const markSeen = url.searchParams.get('mark_seen'); if (markSeen !== 'false') { res.writeHead(400, { 'Content-Type': 'application/json' }); res.end(JSON.stringify({ error: 'mark_seen must be false' })); return; } let body_html = ''; if (uid === 'img_test') { body_html = [ '
', '

Email with diverse images

', '', 'Company Brand Logo', '', '', '', '', '', '', '', '', '
' ].join('\n'); } else if (uid === 'long_msg') { body_html = `

${'A'.repeat(5000)}

`; } else { body_html = `

Standard message body for ${uid}

`; } res.writeHead(200, { 'Content-Type': 'application/json' }); res.end(JSON.stringify({ uid: uid, subject: `Subject ${uid}`, from_name: 'Sender', from_address: 'sender@example.com', to: 'user@example.com', date: '2026-10-20', date_display: 'Oct 20, 2026', body: `Plain text for ${uid}`, body_html: body_html, })); return; } let filePath = path.join(ROOT, url.pathname); if (fs.existsSync(filePath) && fs.statSync(filePath).isFile()) { const ext = path.extname(filePath); const mime = ext === '.js' ? 'application/javascript' : ext === '.html' ? 'text/html' : ext === '.css' ? 'text/css' : 'text/plain'; res.writeHead(200, { 'Content-Type': mime }); res.end(fs.readFileSync(filePath)); return; } res.writeHead(404); res.end('Not found'); }); await new Promise(r => server.listen(0, '127.0.0.1', r)); const port = server.address().port; const origin = `http://127.0.0.1:${port}`; const launchOpts = { headless: true }; const chromePath = findChromiumPath(); if (chromePath) launchOpts.executablePath = chromePath; const browser = await chromium.launch(launchOpts); const context = await browser.newContext(); const page = await context.newPage(); const results = {}; try { await page.goto(`${origin}/static/index.html`); await page.evaluate(() => { if (!document.getElementById('email-lib-bulk-actions')) { const d = document.createElement('div'); d.id = 'email-lib-bulk-actions'; document.body.appendChild(d); } if (!document.getElementById('email-lib-selected-count')) { const d = document.createElement('div'); d.id = 'email-lib-selected-count'; document.body.appendChild(d); } }); // TEST 1: Full Print Image Sanitization and Replacement const printImageResult = await page.evaluate(async (base) => { window.API_BASE = base; const mod = await import(`${base}/static/js/emailLibrary/index.js`); const stateMod = await import(`${base}/static/js/emailLibrary/state.js`); stateMod.state._selectedUids = new Set(['img_test']); stateMod.state._libEmails = [{ uid: 'img_test', subject: 'Image Test', folder: 'INBOX' }]; let iframePrintInvoked = false; let printDocHtml = ''; let afterPrintTriggered = false; let hasPlaceholderElement = false; const observer = new MutationObserver(() => { const frame = document.getElementById('email-browser-print-frame'); if (frame && frame.contentWindow && !frame._patched) { frame._patched = true; frame.contentWindow.print = function() { iframePrintInvoked = true; printDocHtml = frame.contentDocument ? frame.contentDocument.documentElement.outerHTML : ''; const body = frame.contentDocument ? frame.contentDocument.body : null; hasPlaceholderElement = !!body?.querySelector('.email-inline-image-placeholder'); frame.contentWindow.dispatchEvent(new Event('afterprint')); afterPrintTriggered = true; }; } }); observer.observe(document.body, { childList: true }); await mod._exportSelectedEmailPdf(); observer.disconnect(); const frameAfter = document.getElementById('email-browser-print-frame'); return { iframePrintInvoked, afterPrintTriggered, frameRemovedAfterPrint: frameAfter === null, hasLogoNotice: printDocHtml.includes('[Image: Company Brand Logo]'), hasTrackerNotice: printDocHtml.includes('[Image: Tracking Pixel]'), hasNoAltNotice: printDocHtml.includes('[Image omitted]'), hasPlaceholderElement, hasRemoteImageBlockedText: printDocHtml.includes('Remote image blocked'), hasDownloadButton: printDocHtml.includes('email-inline-image-download-btn'), hasInteractiveActionBtn: printDocHtml.includes('btn-action'), hasXSS: printDocHtml.includes('