| 1 | // 📨 Errores y feedback: opt-in (apagado por defecto — "nada sale de tu |
| 2 | // máquina" sigue siendo cierto salvo que el usuario lo active), captura de |
| 3 | // errores no capturados cuando está activo, y envío manual de feedback que |
| 4 | // funciona SIEMPRE (acción explícita) sin activar el automático de fondo. |
| 5 | import { chromium } from 'playwright'; |
| 6 | const BASE = process.env.BASE || 'http://localhost:8799'; |
| 7 | let fails = 0; const ok = (n, c, e = '') => { console.log((c ? '✅' : '❌') + ' ' + n + (e ? ' — ' + e : '')); if (!c) fails++; }; |
| 8 | |
| 9 | const b = await chromium.launch(); |
| 10 | const ctx = await b.newContext(); |
| 11 | await ctx.addInitScript(() => { try { localStorage.setItem('elffusscode.model', 'rules'); } catch {} }); |
| 12 | const p = await ctx.newPage(); |
| 13 | |
| 14 | // intercepta /proxy/report para no depender de un backend real |
| 15 | const reports = []; |
| 16 | await p.route('**/proxy/report', async route => { |
| 17 | reports.push(JSON.parse(route.request().postData() || '{}')); |
| 18 | await route.fulfill({ status: 200, contentType: 'application/json', body: '{"ok":true}' }); |
| 19 | }); |
| 20 | |
| 21 | await p.goto(BASE + '/?test-opfs', { waitUntil: 'domcontentloaded' }); |
| 22 | await p.waitForTimeout(1500); |
| 23 | |
| 24 | // 1) apagado por defecto — nada se manda sin acción explícita |
| 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 | // 2) panel de Ajustes: checkbox existe, refleja el estado, y lo cambia de verdad |
| 31 | await p.click('#btn-settings'); |
| 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-code') === '1')); |
| 38 | |
| 39 | // 3) con telemetría activa, un error SÍ se manda |
| 40 | await p.evaluate(async () => { const t = await import('/js/telemetry.js'); await t.reportError('fallo de prueba', { stack: 'en algún sitio' }); }); |
| 41 | await p.waitForTimeout(200); |
| 42 | ok('con telemetría activa, reportError() manda el informe', reports.some(r => r.kind === 'error' && r.message === 'fallo de prueba')); |
| 43 | ok('el informe lleva app, url y user-agent, nunca código', reports.some(r => r.app === 'elffuss-code' && r.url && r.userAgent)); |
| 44 | |
| 45 | // 4) errores globales no capturados también se mandan cuando está activo |
| 46 | await p.evaluate(() => { setTimeout(() => { throw new Error('crash-no-capturado-de-prueba'); }, 0); }); |
| 47 | await p.waitForTimeout(300); |
| 48 | ok('un error global no capturado también se reporta solo', reports.some(r => /crash-no-capturado-de-prueba/.test(r.message))); |
| 49 | |
| 50 | // 5) lo desactivamos y confirmamos que deja de mandar automáticos… |
| 51 | await p.locator('#tel-enabled').uncheck(); |
| 52 | await p.waitForTimeout(100); |
| 53 | const before = reports.length; |
| 54 | await p.evaluate(async () => { const t = await import('/js/telemetry.js'); await t.reportError('esto no debería salir ya'); }); |
| 55 | await p.waitForTimeout(200); |
| 56 | ok('al desactivarlo, reportError() vuelve a no mandar nada', reports.length === before); |
| 57 | |
| 58 | // …pero el feedback MANUAL sigue funcionando (acción explícita del usuario) |
| 59 | await p.fill('#tel-feedback', 'esto es un feedback manual de prueba'); |
| 60 | await p.click('#tel-send'); |
| 61 | await p.waitForTimeout(300); |
| 62 | 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))); |
| 63 | ok('tras mandar el feedback manual, el automático sigue apagado', await p.evaluate(async () => (await import('/js/telemetry.js')).isEnabled()) === false); |
| 64 | ok('el textarea se limpia tras enviar', await p.locator('#tel-feedback').inputValue() === ''); |
| 65 | |
| 66 | // 6) el informe no lleva la ruta de un fichero del proyecto (#/file/<ruta>:<línea>) |
| 67 | await p.evaluate(async () => { |
| 68 | location.hash = '#/file/src/clientes-secretos.js:2'; |
| 69 | await new Promise(r => setTimeout(r, 200)); |
| 70 | const t = await import('/js/telemetry.js'); |
| 71 | t.setEnabled(true); |
| 72 | await t.reportError('fallo con un fichero abierto'); |
| 73 | await t.sendFeedback('feedback con un fichero abierto'); |
| 74 | t.setEnabled(false); |
| 75 | }); |
| 76 | await p.waitForTimeout(300); |
| 77 | const conFichero = reports.filter(r => /con un fichero abierto/.test(r.message)); |
| 78 | ok('ni un error ni el feedback llevan la ruta del fichero (sin # ni búsqueda en url)', conFichero.length === 2 |
| 79 | && conFichero.every(r => r.url === new URL(BASE).origin + '/' && !/clientes-secretos|#|\?/.test(r.url)), JSON.stringify(conFichero.map(r => r.url))); |
| 80 | |
| 81 | console.log(fails ? `\n❌ ${fails} FALLO(S)` : '\n✅ TELEMETRÍA/FEEDBACK: opt-in real, nunca automático sin permiso, manual siempre disponible'); |
| 82 | await b.close(); |
| 83 | process.exit(fails ? 1 : 0); |