| 1 | // Verifica el bug real encontrado: el informe del cerebro CEO ahora se |
| 2 | // guarda en el historial de verdad (antes solo vivía en el DOM y |
| 3 | // desaparecía al cambiar de pestaña) + que el ciclo del CEO no deja |
| 4 | // "pumping"/"running" colgado para siempre (lo que bloquearía tanto al |
| 5 | // propio cerebro como al chat normal). |
| 6 | import { chromium } from 'playwright'; |
| 7 | const BASE = process.env.BASE || 'http://localhost:8799'; |
| 8 | let fails = 0; const ok = (n, c, e = '') => { console.log((c ? '✅' : '❌') + ' ' + n + (e ? ' — ' + e : '')); if (!c) fails++; }; |
| 9 | |
| 10 | const b = await chromium.launch(); |
| 11 | const ctx = await b.newContext(); |
| 12 | await ctx.addInitScript(() => { try { localStorage.setItem('elffusscode.model', 'rules'); } catch {} }); |
| 13 | const p = await ctx.newPage(); |
| 14 | const errors = []; |
| 15 | // El motor propio (web/js/engine) no se publica con este repo: donde no está, sus |
| 16 | // sondas dan 404, y eso no es un error de este flujo. Con extensiones de código |
| 17 | // encendidas, el recinto (otro sitio, /x2/v1/) comprueba al arrancar que su CSP |
| 18 | // le corta la red, y el navegador anota ese bloqueo como error de consola de su |
| 19 | // marco: tampoco es de este flujo. |
| 20 | p.on('console', m => { if (m.type() === 'error' && !/\/js\/engine\/|\/x2\/v1\//.test(m.location()?.url || '')) errors.push(m.text()); }); |
| 21 | p.on('pageerror', e => errors.push('PAGEERROR: ' + e.message)); |
| 22 | await p.goto(BASE + '/?test-opfs', { waitUntil: 'domcontentloaded' }); await p.waitForTimeout(1500); |
| 23 | |
| 24 | // fuerza un ciclo del CEO con un proveedor falso rápido — SIN pisar |
| 25 | // onEvent: main.js ya lo dejó enganchado a reportImprovements() al arrancar, |
| 26 | // que es justo lo que queremos probar (igual que hace mind_stars.mjs). |
| 27 | const cycleResult = await p.evaluate(async () => { |
| 28 | const ceo = await import('/js/ceo.js'); |
| 29 | ceo.init({ |
| 30 | provider: () => ({ chat: async (h, s, cb) => { cb('ok'); return 'Propuesta de prueba con contenido suficientemente largo para contar como sustanciosa de verdad.'; } }), |
| 31 | isBusy: () => false, |
| 32 | }); |
| 33 | const t0 = Date.now(); |
| 34 | while (!ceo.isThisTabLeader() && Date.now() - t0 < 5000) await new Promise(r => setTimeout(r, 100)); |
| 35 | const ran = await ceo.forceCycle(); |
| 36 | return { ran, runningAfter: ceo.isRunning() }; |
| 37 | }); |
| 38 | ok('el ciclo del CEO se ejecuta', cycleResult.ran, JSON.stringify(cycleResult)); |
| 39 | ok('tras terminar, running() vuelve a false (no se queda colgado)', cycleResult.runningAfter === false); |
| 40 | await p.waitForTimeout(400); |
| 41 | |
| 42 | const chatBefore = await p.locator('#chat-log').innerText(); |
| 43 | ok('la propuesta del CEO aparece en el chat', /💡.*revisé el proyecto/.test(chatBefore), chatBefore.slice(-200)); |
| 44 | |
| 45 | // cambia de pestaña (fuerza un renderActiveLog() completo) y vuelve |
| 46 | await p.click('#conv-tabs .tab-add'); |
| 47 | await p.waitForTimeout(200); |
| 48 | await p.click('#conv-tabs .tab:not(.active)'); |
| 49 | await p.waitForTimeout(200); |
| 50 | const chatAfterSwitch = await p.locator('#chat-log').innerText(); |
| 51 | ok('tras cambiar de pestaña, la propuesta del CEO SIGUE ahí (persistida de verdad)', /💡.*revisé el proyecto/.test(chatAfterSwitch), chatAfterSwitch.slice(-200)); |
| 52 | |
| 53 | // tras el ciclo del CEO, el chat normal SIGUE funcionando (no se ha "parado") |
| 54 | await p.fill('#prompt', 'sigue funcionando el chat?'); |
| 55 | await p.press('#prompt', 'Enter'); |
| 56 | await p.waitForTimeout(500); |
| 57 | const chatFinal = await p.locator('#chat-log').innerText(); |
| 58 | ok('el chat normal sigue respondiendo tras el ciclo del CEO', chatFinal.includes('sigue funcionando el chat?')); |
| 59 | ok('sin errores de consola en todo el flujo', errors.length === 0, errors.slice(0, 5).join(' | ')); |
| 60 | |
| 61 | console.log(fails ? `\n❌ ${fails} FALLO(S)` : '\n✅ CEO: SIN CUELGUES + PROPUESTA PERSISTIDA DE VERDAD'); |
| 62 | await b.close(); |
| 63 | process.exit(fails ? 1 : 0); |