| 1 | // 📨 Errores y feedback: opt-in (apagado por defecto), captura de errores no |
| 2 | // capturados cuando está activo, y envío manual de feedback que funciona |
| 3 | // SIEMPRE (acción explícita) sin activar el automático de fondo. |
| 4 | import { chromium } from 'playwright'; |
| 5 | const BASE = process.env.BASE || 'http://localhost:8801'; |
| 6 | let fails = 0; const ok = (n, c, e = '') => { console.log((c ? '✅' : '❌') + ' ' + n + (e ? ' — ' + e : '')); if (!c) fails++; }; |
| 7 | |
| 8 | const b = await chromium.launch(); |
| 9 | // En español: la prueba busca el botón «Enviar» del feedback. Sin locale el |
| 10 | // navegador va en inglés, la página también («Send»), y la prueba se caía ahí |
| 11 | // sin llegar a sus dos últimas comprobaciones. Y en Básico: no necesita modelo. |
| 12 | const ctx = await b.newContext({ locale: 'es-ES' }); |
| 13 | await ctx.addInitScript(() => { try { localStorage.setItem('elffuss.welcomed', '1'); localStorage.setItem('elffuss.model', 'rules'); } catch {} }); |
| 14 | const p = await ctx.newPage(); |
| 15 | |
| 16 | const reports = []; |
| 17 | await p.route('**/proxy/report', async route => { |
| 18 | reports.push(JSON.parse(route.request().postData() || '{}')); |
| 19 | await route.fulfill({ status: 200, contentType: 'application/json', body: '{"ok":true}' }); |
| 20 | }); |
| 21 | |
| 22 | await p.goto(BASE + '/', { waitUntil: 'domcontentloaded' }); |
| 23 | await p.waitForTimeout(1500); |
| 24 | |
| 25 | ok('telemetría apagada por defecto', await p.evaluate(async () => (await import('/js/telemetry.js')).isEnabled()) === false); |
| 26 | await p.evaluate(async () => { const t = await import('/js/telemetry.js'); await t.reportError('no debería salir'); }); |
| 27 | await p.waitForTimeout(200); |
| 28 | ok('con telemetría apagada, reportError() no manda nada', reports.length === 0); |
| 29 | |
| 30 | // abre Ajustes (pestaña /modelo) |
| 31 | await p.evaluate(async () => (await import('/js/ui.js')).selectTab('ajustes')); |
| 32 | await p.waitForTimeout(300); |
| 33 | ok('el checkbox de errores/feedback existe y empieza desmarcado', await p.locator('#tel-enabled').isChecked() === false); |
| 34 | await p.locator('#tel-enabled').check(); |
| 35 | await p.waitForTimeout(100); |
| 36 | ok('marcarlo activa telemetry.isEnabled()', await p.evaluate(async () => (await import('/js/telemetry.js')).isEnabled()) === true); |
| 37 | ok('persiste en localStorage', await p.evaluate(() => localStorage.getItem('elffuss.telemetry.elffuss-claw') === '1')); |
| 38 | |
| 39 | await p.evaluate(async () => { const t = await import('/js/telemetry.js'); await t.reportError('fallo de prueba', { stack: 'en algún sitio' }); }); |
| 40 | await p.waitForTimeout(200); |
| 41 | ok('con telemetría activa, reportError() manda el informe', reports.some(r => r.kind === 'error' && r.message === 'fallo de prueba')); |
| 42 | ok('el informe lleva app=elffuss-claw, url y user-agent', reports.some(r => r.app === 'elffuss-claw' && r.url && r.userAgent)); |
| 43 | |
| 44 | await p.evaluate(() => { setTimeout(() => { throw new Error('crash-no-capturado-de-prueba'); }, 0); }); |
| 45 | await p.waitForTimeout(300); |
| 46 | ok('un error global no capturado también se reporta solo', reports.some(r => /crash-no-capturado-de-prueba/.test(r.message))); |
| 47 | |
| 48 | await p.locator('#tel-enabled').uncheck(); |
| 49 | await p.waitForTimeout(100); |
| 50 | const before = reports.length; |
| 51 | await p.evaluate(async () => { const t = await import('/js/telemetry.js'); await t.reportError('esto no debería salir ya'); }); |
| 52 | await p.waitForTimeout(200); |
| 53 | ok('al desactivarlo, reportError() vuelve a no mandar nada', reports.length === before); |
| 54 | |
| 55 | await p.fill('#tel-feedback', 'esto es un feedback manual de prueba'); |
| 56 | await p.locator('.card', { has: p.locator('#tel-feedback') }).locator('button', { hasText: 'Enviar' }).click(); |
| 57 | await p.waitForTimeout(300); |
| 58 | ok('el feedback manual SÍ se manda aunque el automático esté apagado', reports.some(r => r.kind === 'feedback' && /feedback manual de prueba/.test(r.message))); |
| 59 | ok('tras mandar el feedback manual, el automático sigue apagado', await p.evaluate(async () => (await import('/js/telemetry.js')).isEnabled()) === false); |
| 60 | |
| 61 | console.log(fails ? `\n❌ ${fails} FALLO(S)` : '\n✅ TELEMETRÍA/FEEDBACK (Claw): opt-in real, nunca automático sin permiso, manual siempre disponible'); |
| 62 | await b.close(); |
| 63 | process.exit(fails ? 1 : 0); |