| 1 | // Chip ⚡ de velocidad (velocimetro.js). |
| 2 | // |
| 3 | // 1. Con el motor cargado DOS veces (montar() otra vez), el clic sigue |
| 4 | // abriendo el panel. Antes cada montar() añadía otro par de oyentes: el |
| 5 | // clic abría y cerraba a la vez y el panel no se abría nunca. |
| 6 | // 2. aria-expanded en el chip, y Escape cierra el panel devolviendo el foco. |
| 7 | // 3. Al pasar a un cerebro que no mide (Básico, ONNX, externo) el chip se |
| 8 | // oculta; antes seguía enseñando las cifras del motor. |
| 9 | // El «motor» es de mentira: un proveedor con alMedir/ultimaMedida. |
| 10 | // |
| 11 | // BASE=http://127.0.0.1:8898 node tests/speedchip.mjs |
| 12 | import { chromium } from 'playwright'; |
| 13 | const BASE = process.env.BASE || 'http://localhost:8799'; |
| 14 | let fails = 0; const ok = (n, c, e = '') => { console.log((c ? '✅' : '❌') + ' ' + n + (e ? ' — ' + e : '')); if (!c) fails++; }; |
| 15 | |
| 16 | const b = await chromium.launch(); |
| 17 | const ctx = await b.newContext({ locale: 'es-ES' }); |
| 18 | await ctx.addInitScript(() => { try { localStorage.setItem('elffusscode.model', 'rules'); } catch {} }); |
| 19 | const p = await ctx.newPage({ viewport: { width: 1400, height: 900 } }); |
| 20 | const errs = []; p.on('pageerror', e => errs.push(e.message.slice(0, 160))); |
| 21 | await p.goto(BASE + '/?test-opfs', { waitUntil: 'domcontentloaded' }); |
| 22 | await p.waitForFunction(() => window.elffussClaw?.conv?.getActive?.(), null, { timeout: 20000 }); |
| 23 | |
| 24 | await p.evaluate(async () => { |
| 25 | const vm = await import('/js/velocimetro.js'); |
| 26 | const medida = { prefillTokS: 240, genTokS: 21, ttftMs: 320, genTokens: 12, procesados: 300, enCurso: false }; |
| 27 | window.__motor = { alMedir(cb) { window.__motor.cb = cb; return () => { window.__motor.cb = null; }; }, ultimaMedida: () => medida }; |
| 28 | vm.montar(window.__motor); |
| 29 | vm.montar(window.__motor); // el motor cargado otra vez |
| 30 | }); |
| 31 | const estado = () => p.evaluate(() => ({ chip: !document.getElementById('speed-chip').hidden, panel: !document.getElementById('speed-panel').hidden, exp: document.getElementById('speed-chip').getAttribute('aria-expanded') })); |
| 32 | ok('el chip se ve con el motor cargado', (await estado()).chip); |
| 33 | await p.click('#speed-chip'); |
| 34 | await p.waitForTimeout(100); |
| 35 | let e = await estado(); |
| 36 | ok('1 · montado dos veces, el clic abre el panel', e.panel, JSON.stringify(e)); |
| 37 | ok('2 · aria-expanded refleja el panel abierto', e.exp === 'true', JSON.stringify(e)); |
| 38 | await p.keyboard.press('Escape'); |
| 39 | await p.waitForTimeout(100); |
| 40 | e = await estado(); |
| 41 | ok('2 · Escape lo cierra y el foco vuelve al chip', !e.panel && e.exp === 'false' && await p.evaluate(() => document.activeElement?.id === 'speed-chip'), JSON.stringify(e)); |
| 42 | await p.click('#speed-chip'); |
| 43 | await p.waitForTimeout(100); |
| 44 | ok('1 · y se vuelve a abrir', (await estado()).panel); |
| 45 | |
| 46 | // 3 · cambiar a Básico desde Ajustes |
| 47 | await p.click('#btn-settings'); |
| 48 | await p.waitForTimeout(300); |
| 49 | await p.locator('#settings-panel .model-card', { hasText: 'Básico' }).click(); |
| 50 | await p.waitForTimeout(400); |
| 51 | e = await estado(); |
| 52 | ok('3 · al pasar a Básico el chip y el panel se ocultan', !e.chip && !e.panel, JSON.stringify(e)); |
| 53 | ok('3 · …y se deja de escuchar al motor', await p.evaluate(() => window.__motor.cb === null)); |
| 54 | |
| 55 | console.log('\n errores JS:', errs[0] || 'ninguno'); |
| 56 | ok('sin errores de página', errs.length === 0, errs.join(' | ')); |
| 57 | console.log(fails ? `\n❌ ${fails} FALLO(S)` : '\n✅ CHIP DE VELOCIDAD OK'); |
| 58 | await b.close(); |
| 59 | process.exit(fails ? 1 : 0); |