| 1 | // Elffuss Lab: el botón ■ del banco de juegos PARA de verdad. |
| 2 | // |
| 3 | // Antes el ■ solo ponía una bandera que los bancos miraban ENTRE casos, y el de |
| 4 | // juegos no tiene casos: generaba el juego entero (varias inferencias) y lo |
| 5 | // puntuaba jugándolo, pulsaras lo que pulsaras. Ahora la parada entra en la |
| 6 | // inferencia en curso (4º argumento de chat()) y se comprueba antes de cada fase. |
| 7 | // |
| 8 | // El cerebro es de mentira: page.route sirve un providers/onnx.js propio, lento |
| 9 | // y que respeta la señal, así que no se descarga ni se ejecuta ningún modelo. |
| 10 | // |
| 11 | // BASE=http://localhost:8799 node tests/labstop.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 FALSO = ` |
| 17 | export let name = 'lento (prueba)'; |
| 18 | export async function load() {} |
| 19 | const st = (window.__labProv = { llamadas: 0, abortadas: 0, trasParar: 0, terminadas: 0 }); |
| 20 | export async function chat(h, s, cb = () => {}, signal) { |
| 21 | st.llamadas++; if (window.__labParado) st.trasParar++; |
| 22 | for (let i = 0; i < 100; i++) { |
| 23 | if (signal?.aborted) { st.abortadas++; throw new DOMException('Parado', 'AbortError'); } |
| 24 | cb('·'); await new Promise(r => setTimeout(r, 30)); |
| 25 | } |
| 26 | st.terminadas++; |
| 27 | return '<!doctype html><html><body><canvas width="80" height="80"></canvas><script>const c=document.querySelector("canvas").getContext("2d");(function f(){c.fillRect(Math.random()*80,Math.random()*80,2,2);requestAnimationFrame(f)})()<\\/script></body></html>'; |
| 28 | } |
| 29 | `; |
| 30 | |
| 31 | const b = await chromium.launch(); |
| 32 | const ctx = await b.newContext({ locale: 'es-ES' }); |
| 33 | const p = await ctx.newPage({ viewport: { width: 1300, height: 900 } }); |
| 34 | const errs = []; p.on('pageerror', e => errs.push(e.message.slice(0, 160))); |
| 35 | await p.route('**/js/providers/onnx.js*', route => route.fulfill({ status: 200, contentType: 'text/javascript', body: FALSO })); |
| 36 | await p.goto(BASE + '/lab.html', { waitUntil: 'domcontentloaded' }); |
| 37 | await p.waitForTimeout(800); |
| 38 | |
| 39 | // cerebro «onnx» (el falso) y banco «juegos» |
| 40 | await p.locator('#brains label').nth(1).click(); |
| 41 | await p.click('#btn-load'); |
| 42 | await p.waitForFunction(() => /modelo: onnx/.test(document.querySelector('#led-model')?.textContent || ''), null, { timeout: 5000 }).catch(() => {}); |
| 43 | await p.locator('#benches label').nth(2).click(); |
| 44 | await p.click('#btn-run'); |
| 45 | await p.waitForFunction(() => (window.__labProv?.llamadas || 0) >= 1, null, { timeout: 5000 }).catch(() => {}); |
| 46 | await p.waitForTimeout(300); // generando el juego |
| 47 | await p.evaluate(() => { window.__labParado = true; }); |
| 48 | await p.click('#btn-stop'); |
| 49 | const termino = await p.waitForFunction(() => !document.getElementById('btn-run').disabled, null, { timeout: 15000 }).then(() => true).catch(() => false); |
| 50 | await p.waitForTimeout(300); |
| 51 | const r = await p.evaluate(() => ({ |
| 52 | st: window.__labProv, |
| 53 | arena: !document.getElementById('arena-panel').hidden, |
| 54 | prog: document.getElementById('prog-txt').textContent, |
| 55 | log: document.getElementById('log').textContent, |
| 56 | })); |
| 57 | ok('la corrida estaba generando con el cerebro de prueba', r.st?.llamadas >= 1, `${r.st?.llamadas} llamadas`); |
| 58 | ok('■ corta la inferencia en curso (el modelo recibe la parada)', r.st?.abortadas === 1 && r.st?.terminadas === 0, JSON.stringify(r.st)); |
| 59 | ok(' …y no se lanza ninguna inferencia más', r.st?.trasParar === 0, `${r.st?.trasParar} tras parar`); |
| 60 | ok('la corrida termina (▶ vuelve a estar disponible)', termino); |
| 61 | ok('no llega a jugar el juego (la arena no se abre)', !r.arena); |
| 62 | ok('el progreso dice «abortado» y el registro «parado»', r.prog === 'abortado' && /parado/.test(r.log), `${r.prog} · …${r.log.slice(-60)}`); |
| 63 | |
| 64 | console.log('\n errores JS:', errs[0] || 'ninguno'); |
| 65 | console.log(fails ? `\n❌ ${fails} FALLO(S)` : '\n✅ LAB OK — el ■ para el banco de juegos de verdad'); |
| 66 | await b.close(); |
| 67 | process.exit(fails ? 1 : 0); |