import type { Observations, Lighthouse } from '../core/types.js'; export async function coletarLighthouse(obs: Observations): Promise { try { const { launch } = await import('chrome-launcher'); const lighthouseMod = await import('lighthouse'); const lighthouse = (lighthouseMod.default ?? lighthouseMod) as unknown as ( url: string, flags: Record, config?: unknown, ) => Promise<{ lhr: { categories: Record } }>; const { chromium } = await import('playwright'); const chromePath = chromium.executablePath(); const chrome = await launch({ chromePath, chromeFlags: ['--headless=new', '--no-sandbox', '--disable-gpu'], }); try { const resultado = await lighthouse(obs.urlFinal || obs.urlSolicitada, { port: chrome.port, output: 'json', logLevel: 'silent', onlyCategories: ['performance', 'accessibility', 'seo', 'best-practices'], }); const cats = resultado.lhr.categories; const nota = (chave: string): number | undefined => { const s = cats[chave]?.score; return s == null ? undefined : Math.round(s * 100); }; const lh: Lighthouse = { performance: nota('performance'), acessibilidade: nota('accessibility'), seo: nota('seo'), boasPraticas: nota('best-practices'), }; obs.lighthouse = lh; } finally { await chrome.kill(); } } catch (erro) { obs.avisos.push(`Auditoria Lighthouse indisponível: ${(erro as Error).message}`); } }