| 1 | // Bus de actividad: cada llamada de la elfa se anuncia (inicio y fin) para que la |
| 2 | // vista de ciudad encienda el fichero que toca y muestre el registro. |
| 3 | // |
| 4 | // Controles negativos incluidos: si el banco no se ha visto fallar, no prueba nada. |
| 5 | import { chromium } from 'playwright'; |
| 6 | const BASE = process.env.BASE || 'http://localhost:8799'; |
| 7 | let fails = 0; |
| 8 | 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 | p.on('pageerror', e => { console.log('❌ pageerror: ' + e.message); fails++; }); |
| 15 | await p.goto(BASE + '/?test-opfs', { waitUntil: 'domcontentloaded' }); |
| 16 | await p.waitForTimeout(1500); |
| 17 | |
| 18 | await p.evaluate(async () => { |
| 19 | const root = await navigator.storage.getDirectory(); |
| 20 | const d = await root.getDirectoryHandle('src', { create: true }); |
| 21 | const fh = await d.getFileHandle('app.js', { create: true }); |
| 22 | const w = await fh.createWritable(); await w.write('export const hola = 1;\n'); await w.close(); |
| 23 | }); |
| 24 | |
| 25 | const eventos = await p.evaluate(async () => { |
| 26 | const act = await import('/js/actividad.js'); |
| 27 | const tools = await import('/js/tools/index.js'); |
| 28 | const recogidos = []; |
| 29 | const quitar = act.escuchar(e => recogidos.push(e)); |
| 30 | // Un oyente que LANZA no debe tumbar la herramienta. |
| 31 | const quitarMalo = act.escuchar(() => { throw new Error('oyente roto'); }); |
| 32 | let leido = null, error = null; |
| 33 | try { leido = await tools.runTool('code.read', { path: 'src/app.js' }); } catch (e) { error = String(e.message); } |
| 34 | let fallo = null; |
| 35 | try { await tools.runTool('code.read', { path: 'src/no_existe_zz.js' }); } catch (e) { fallo = String(e.message); } |
| 36 | quitarMalo(); quitar(); |
| 37 | return { recogidos, leido: String(leido || '').slice(0, 60), error, fallo }; |
| 38 | }); |
| 39 | |
| 40 | const inicio = eventos.recogidos.find(e => e.fase === 'inicio' && e.herramienta === 'code.read' && e.ruta === 'src/app.js'); |
| 41 | const fin = eventos.recogidos.find(e => e.fase === 'fin' && e.herramienta === 'code.read' && e.ok === true); |
| 42 | ok('la lectura anuncia INICIO con la ruta y el modo «lee»', !!inicio && inicio.modo === 'lee', JSON.stringify(inicio)); |
| 43 | ok('la lectura anuncia FIN con ok:true y un resumen', !!fin && typeof fin.resumen === 'string' && fin.resumen.length > 0, JSON.stringify(fin)); |
| 44 | ok('la herramienta devolvió el contenido pese a un oyente roto', |
| 45 | eventos.leido.includes('hola') || eventos.leido.includes('export'), eventos.leido); |
| 46 | ok('sin error inesperado en la lectura buena', eventos.error === null, String(eventos.error)); |
| 47 | |
| 48 | // Un fallo también se anuncia (ok:false) y SIGUE lanzando. |
| 49 | const finFallo = eventos.recogidos.find(e => e.fase === 'fin' && e.ok === false && e.ruta === 'src/no_existe_zz.js'); |
| 50 | ok('un fallo se anuncia con ok:false', !!finFallo, JSON.stringify(finFallo)); |
| 51 | ok('y el error llega igual al que llama', !!eventos.fallo && eventos.fallo.includes('no existe'), String(eventos.fallo)); |
| 52 | |
| 53 | // Escritura: modo «escribe», no «lee». |
| 54 | const escrito = await p.evaluate(async () => { |
| 55 | const act = await import('/js/actividad.js'); |
| 56 | const t = await import('/js/tools/index.js'); |
| 57 | const recogidos = []; const q = act.escuchar(e => recogidos.push(e)); |
| 58 | try { await t.runTool('code.write', { path: 'src/nuevo.js', content: 'x' }); } catch {} |
| 59 | q(); |
| 60 | return recogidos; |
| 61 | }); |
| 62 | const inicioW = escrito.find(e => e.fase === 'inicio' && e.herramienta === 'code.write'); |
| 63 | ok('la escritura anuncia el modo «escribe»', !!inicioW && inicioW.modo === 'escribe', JSON.stringify(inicioW)); |
| 64 | |
| 65 | // Herramienta sin fichero (terminal.run): se anuncia pero sin ruta. |
| 66 | const sinFichero = await p.evaluate(async () => { |
| 67 | const act = await import('/js/actividad.js'); |
| 68 | const t = await import('/js/tools/index.js'); |
| 69 | const recogidos = []; const q = act.escuchar(e => recogidos.push(e)); |
| 70 | try { await t.runTool('terminal.run', { command: 'ls' }); } catch {} |
| 71 | q(); |
| 72 | return recogidos.find(e => e.fase === 'inicio'); |
| 73 | }); |
| 74 | ok('terminal.run se anuncia sin ruta (no toca un fichero concreto)', |
| 75 | !!sinFichero && sinFichero.ruta === undefined && sinFichero.args.command === 'ls', JSON.stringify(sinFichero)); |
| 76 | |
| 77 | // Control negativo: sin suscriptor no hay eventos, y tocar un fichero sin oyentes no falla. |
| 78 | const silencio = await p.evaluate(async () => { |
| 79 | const t = await import('/js/tools/index.js'); |
| 80 | try { await t.runTool('code.read', { path: 'src/app.js' }); return 'ok'; } catch (e) { return 'FALLA: ' + e.message; } |
| 81 | }); |
| 82 | ok('sin oyentes, runTool funciona igual', silencio === 'ok', silencio); |
| 83 | |
| 84 | await b.close(); |
| 85 | console.log(fails ? `\n${fails} fallo(s)` : '\nTodo en verde'); |
| 86 | process.exit(fails ? 1 : 0); |