Elffuss BraidHub

utopia-ia/elffuss-code public

main · 4.6 KiBRawPermalink
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.
5import { chromium } from 'playwright';
6const BASE = process.env.BASE || 'http://localhost:8799';
7let fails = 0;
8const ok = (n, c, e = '') => { console.log((c ? '✅' : '❌') + ' ' + n + (e ? ' — ' + e : '')); if (!c) fails++; };
9
10const b = await chromium.launch();
11const ctx = await b.newContext();
12await ctx.addInitScript(() => { try { localStorage.setItem('elffusscode.model', 'rules'); } catch {} });
13const p = await ctx.newPage();
14p.on('pageerror', e => { console.log('❌ pageerror: ' + e.message); fails++; });
15await p.goto(BASE + '/?test-opfs', { waitUntil: 'domcontentloaded' });
16await p.waitForTimeout(1500);
17
18await 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
25const 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
40const inicio = eventos.recogidos.find(e => e.fase === 'inicio' && e.herramienta === 'code.read' && e.ruta === 'src/app.js');
41const fin = eventos.recogidos.find(e => e.fase === 'fin' && e.herramienta === 'code.read' && e.ok === true);
42ok('la lectura anuncia INICIO con la ruta y el modo «lee»', !!inicio && inicio.modo === 'lee', JSON.stringify(inicio));
43ok('la lectura anuncia FIN con ok:true y un resumen', !!fin && typeof fin.resumen === 'string' && fin.resumen.length > 0, JSON.stringify(fin));
44ok('la herramienta devolvió el contenido pese a un oyente roto',
45 eventos.leido.includes('hola') || eventos.leido.includes('export'), eventos.leido);
46ok('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.
49const finFallo = eventos.recogidos.find(e => e.fase === 'fin' && e.ok === false && e.ruta === 'src/no_existe_zz.js');
50ok('un fallo se anuncia con ok:false', !!finFallo, JSON.stringify(finFallo));
51ok('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».
54const 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});
62const inicioW = escrito.find(e => e.fase === 'inicio' && e.herramienta === 'code.write');
63ok('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.
66const 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});
74ok('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.
78const 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});
82ok('sin oyentes, runTool funciona igual', silencio === 'ok', silencio);
83
84await b.close();
85console.log(fails ? `\n${fails} fallo(s)` : '\nTodo en verde');
86process.exit(fails ? 1 : 0);