| 1 | // Elffuss Lab: que lo que se ve sea lo que corre. |
| 2 | // |
| 3 | // 1. Desde el IDE se llega al Lab (View › Elffuss Lab y la paleta); antes |
| 4 | // solo escribiendo la URL. |
| 5 | // 2. Cambiar de cerebro en la lista descarta el cargado: antes una corrida |
| 6 | // «con onnx» se ejecutaba con Reglas. «Lanzar» espera a que el cerebro |
| 7 | // elegido esté cargado cuando el banco lo necesita. |
| 8 | // 3. Cambiar de banco repinta el ranking de ese banco. |
| 9 | // 4. ■ a mitad: «parado (parcial)», no «✔ terminado/completado», y no |
| 10 | // entra en el ranking. |
| 11 | // 5. «Abrir el IDE con este cerebro» hace solo eso (antes prometía aplicar |
| 12 | // algoritmo y perillas que el IDE no lee). |
| 13 | // El cerebro «onnx» es de mentira (page.route sirve un providers/onnx.js propio). |
| 14 | // |
| 15 | // BASE=http://127.0.0.1:8898 node tests/lab-ux.mjs |
| 16 | import { chromium } from 'playwright'; |
| 17 | const BASE = process.env.BASE || 'http://localhost:8799'; |
| 18 | let fails = 0; const ok = (n, c, e = '') => { console.log((c ? '✅' : '❌') + ' ' + n + (e ? ' — ' + e : '')); if (!c) fails++; }; |
| 19 | |
| 20 | const FALSO = `export let name = 'falso'; export async function load() {} |
| 21 | export async function chat() { return 'hecho'; }`; |
| 22 | |
| 23 | const b = await chromium.launch(); |
| 24 | const ctx = await b.newContext({ locale: 'es-ES' }); |
| 25 | await ctx.addInitScript(() => { try { localStorage.setItem('elffusscode.model', 'rules'); } catch {} }); |
| 26 | const errs = []; |
| 27 | |
| 28 | // 1 · el IDE lleva al Lab |
| 29 | const ide = await ctx.newPage({ viewport: { width: 1400, height: 900 } }); |
| 30 | ide.on('pageerror', e => errs.push(e.message.slice(0, 160))); |
| 31 | await ide.goto(BASE + '/?test-opfs', { waitUntil: 'domcontentloaded' }); |
| 32 | await ide.waitForFunction(() => window.elffussClaw?.conv?.getActive?.(), null, { timeout: 15000 }); |
| 33 | await ide.evaluate(() => { window.__abiertas = []; window.open = (u) => { window.__abiertas.push(String(u)); return null; }; }); |
| 34 | await ide.click('#menubar button[data-menu="view"]'); |
| 35 | const enMenu = await ide.locator('#topmenu .menu-item', { hasText: 'Elffuss Lab' }).count(); |
| 36 | await ide.locator('#topmenu .menu-item', { hasText: 'Elffuss Lab' }).click().catch(() => {}); |
| 37 | await ide.keyboard.press(process.platform === 'darwin' ? 'Meta+Shift+P' : 'Control+Shift+P'); |
| 38 | await ide.fill('#pal-input', '>Lab'); |
| 39 | await ide.waitForTimeout(150); |
| 40 | await ide.press('#pal-input', 'Enter'); |
| 41 | const abiertas = await ide.evaluate(() => window.__abiertas); |
| 42 | ok('1 · View › Elffuss Lab abre el Lab', enMenu === 1 && abiertas[0] === 'lab.html', JSON.stringify(abiertas)); |
| 43 | ok('1 · …y también la paleta', abiertas[1] === 'lab.html', JSON.stringify(abiertas)); |
| 44 | await ide.close(); |
| 45 | |
| 46 | const p = await ctx.newPage({ viewport: { width: 1300, height: 900 } }); |
| 47 | p.on('pageerror', e => errs.push(e.message.slice(0, 160))); |
| 48 | await p.route('**/js/providers/onnx.js*', route => route.fulfill({ status: 200, contentType: 'text/javascript', body: FALSO })); |
| 49 | await p.goto(BASE + '/lab.html', { waitUntil: 'domcontentloaded' }); |
| 50 | await p.waitForTimeout(600); |
| 51 | const estado = () => p.evaluate(() => ({ |
| 52 | led: document.querySelector('#led-model').textContent.trim(), run: document.getElementById('btn-run').disabled, |
| 53 | hint: document.getElementById('run-hint').textContent, prog: document.getElementById('prog-txt').textContent, |
| 54 | rank: document.getElementById('rank-tag').textContent, log: document.getElementById('log').textContent, |
| 55 | ultima: document.getElementById('log').lastElementChild?.textContent || '', |
| 56 | })); |
| 57 | |
| 58 | // 2 · cargar Reglas y cambiar a onnx |
| 59 | await p.click('#btn-load'); // Reglas |
| 60 | await p.waitForFunction(() => /modelo: rules/.test(document.querySelector('#led-model').textContent), null, { timeout: 5000 }); |
| 61 | await p.locator('#brains label').nth(1).click(); // onnx |
| 62 | let e = await estado(); |
| 63 | ok('2 · cambiar de cerebro descarta el que estaba cargado', /sin cargar/.test(e.led), e.led); |
| 64 | await p.locator('#benches label').nth(2).click(); // juegos: necesita cerebro |
| 65 | e = await estado(); |
| 66 | ok('2 · con un banco que necesita cerebro, «Lanzar» espera a que se cargue', e.run && /Carga el cerebro «onnx:qwen3\.5-0\.8b»/.test(e.hint), JSON.stringify(e)); |
| 67 | await p.click('#btn-load'); |
| 68 | await p.waitForFunction(() => /modelo: onnx/.test(document.querySelector('#led-model').textContent), null, { timeout: 5000 }); |
| 69 | e = await estado(); |
| 70 | ok('2 · cargado el elegido, «Lanzar» se habilita', !e.run && !e.hint, JSON.stringify(e)); |
| 71 | |
| 72 | // 3 · el ranking sigue al banco |
| 73 | await p.locator('#benches label').nth(0).click(); // contexto |
| 74 | await p.click('#btn-run'); |
| 75 | await p.waitForFunction(() => /completado/.test(document.getElementById('prog-txt').textContent), null, { timeout: 15000 }); |
| 76 | await p.click('#btn-save'); |
| 77 | e = await estado(); |
| 78 | ok('3 · el ranking de «contexto» tiene la corrida guardada', /1 corridas · contexto/.test(e.rank), e.rank); |
| 79 | await p.locator('#benches label').nth(1).click(); // swe |
| 80 | e = await estado(); |
| 81 | ok('3 · al cambiar a «swe» el ranking es el de swe', /sin corridas de swe/.test(e.rank), e.rank); |
| 82 | |
| 83 | // 4 · ■ a mitad: parcial |
| 84 | await p.locator('#benches label').nth(0).click(); // contexto otra vez |
| 85 | await p.evaluate(() => { document.getElementById('btn-run').click(); document.getElementById('btn-stop').click(); }); |
| 86 | await p.waitForFunction(() => !document.getElementById('btn-stop').disabled === false && /parado/.test(document.getElementById('prog-txt').textContent), null, { timeout: 15000 }).catch(() => {}); |
| 87 | e = await estado(); |
| 88 | ok('4 · parado a mitad: dice «parado (parcial)», no «completado»', e.prog === 'parado (parcial)' && /parado: resultado PARCIAL/.test(e.ultima) && !/terminado/.test(e.ultima), e.prog + ' · ' + e.ultima.slice(0, 90)); |
| 89 | // Parcial no es comparable: «Guardar» no la mete en el ranking (su porcentaje |
| 90 | // competiría con el de corridas completas aunque fuera marcada). |
| 91 | const filasAntes = await p.evaluate(() => document.querySelectorAll('#rank-body tr').length); |
| 92 | await p.click('#btn-save'); |
| 93 | const r4 = await p.evaluate(() => ({ filas: document.querySelectorAll('#rank-body tr').length, ultima: document.getElementById('log').lastElementChild?.textContent || '' })); |
| 94 | ok('4 · la corrida parada no entra en el ranking, y se dice', r4.filas === filasAntes && /no entra en el ranking/.test(r4.ultima), JSON.stringify(r4)); |
| 95 | |
| 96 | // 5 · abrir el IDE con este cerebro |
| 97 | await p.evaluate(() => { window.open = () => null; localStorage.setItem('elffusscode.ctxopts', '{"MMR":false}'); }); |
| 98 | const etiqueta = await p.locator('#btn-launch').textContent(); |
| 99 | await p.click('#btn-launch'); |
| 100 | const r5 = await p.evaluate(() => ({ model: localStorage.getItem('elffusscode.model'), ctx: localStorage.getItem('elffusscode.ctxopts'), algo: localStorage.getItem('elffusscode.algo') })); |
| 101 | ok('5 · el botón dice lo que hace', /Abrir el IDE con este cerebro/.test(etiqueta), etiqueta); |
| 102 | ok('5 · guarda el cerebro y no deja claves que el IDE no lee', r5.model === 'onnx:qwen3.5-0.8b' && r5.ctx === null && r5.algo === null, JSON.stringify(r5)); |
| 103 | // …y ese id es uno que el IDE ofrece: el ligero era 'onnx' a secas, que el IDE |
| 104 | // ya no tiene, así que lo descartaba al arrancar (y se quedaba sin modelo). |
| 105 | { |
| 106 | const ide2 = await ctx.newPage({ viewport: { width: 1400, height: 900 } }); |
| 107 | await ide2.goto(BASE + '/?test-opfs', { waitUntil: 'domcontentloaded' }); |
| 108 | await ide2.waitForFunction(() => document.querySelectorAll('#model-select option').length > 0, null, { timeout: 15000 }); |
| 109 | const ids = await ide2.$$eval('#model-select option', os => os.map(o => o.value)); |
| 110 | ok('5 · el IDE ofrece ese mismo cerebro', ids.includes(r5.model), JSON.stringify({ lab: r5.model, ide: ids })); |
| 111 | await ide2.close(); |
| 112 | } |
| 113 | |
| 114 | console.log('\n errores JS:', errs[0] || 'ninguno'); |
| 115 | ok('sin errores de página', errs.length === 0, errs.join(' | ')); |
| 116 | console.log(fails ? `\n❌ ${fails} FALLO(S)` : '\n✅ LAB: LO QUE SE VE ES LO QUE CORRE'); |
| 117 | await b.close(); |
| 118 | process.exit(fails ? 1 : 0); |