Elffuss BraidHub

utopia-ia/elffuss-code public

main · 10.6 KiBRawPermalink
1// La Mente: abrirla no enciende nada, y lo que se ve se puede pulsar.
2//
3// 1. Clic en la elfa: abre la Mente y NADA más. Antes el primer clic encendía
4// el cerebro autónomo en silencio y pedía permiso de notificaciones.
5// 2. Sin modelo (modo Básico) el cerebro no piensa: «Pensar ahora» lo dice en
6// el panel y no se escribe nada en la carpeta-alma. Antes el motor de
7// reglas generaba «4 mejoras» con el texto de ayuda dentro del proyecto.
8// 3. Las etiquetas de propuesta se pueden pulsar (antes pointer-events:none:
9// el clic se iba al lienzo) y son botones.
10// 4. «Abrir en el editor» cierra la Mente antes (antes abría DETRÁS) y las
11// rutas de la propuesta abren su fichero.
12// 5. La música no se carga sola: hay que pedirla, y se puede quitar.
13// 6. Con el teclado es modal: el foco entra, Tab no sale, el IDE de detrás queda
14// inerte, Escape devuelve el foco a la elfa y «Abrir en el editor» lo deja
15// en el editor.
16// La red a SoundCloud va bloqueada: el test solo mira si la app la PIDE.
17//
18// BASE=http://127.0.0.1:8898 node tests/mind-consent.mjs
19import { chromium } from 'playwright';
20const BASE = process.env.BASE || 'http://localhost:8799';
21let fails = 0; const ok = (n, c, e = '') => { console.log((c ? '✅' : '❌') + ' ' + n + (e ? ' — ' + e : '')); if (!c) fails++; };
22
23const b = await chromium.launch();
24const ctx = await b.newContext({ locale: 'es-ES' });
25await ctx.addInitScript(() => {
26 try { localStorage.setItem('elffusscode.model', 'rules'); } catch {}
27 window.__permiso = 0;
28 if (window.Notification) Notification.requestPermission = () => { window.__permiso++; return Promise.resolve('default'); };
29});
30const sc = [];
31await ctx.route(/soundcloud/, r => { sc.push(r.request().url()); r.abort(); });
32const p = await ctx.newPage({ viewport: { width: 1400, height: 900 } });
33const errs = []; p.on('pageerror', e => errs.push(e.message.slice(0, 160)));
34
35await p.goto(BASE + '/manifest.json');
36await p.evaluate(async () => {
37 const root = await navigator.storage.getDirectory();
38 const escribe = async (path, txt) => {
39 const parts = path.split('/'); const name = parts.pop(); let d = root;
40 for (const x of parts) d = await d.getDirectoryHandle(x, { create: true });
41 const w = await (await d.getFileHandle(name, { create: true })).createWritable(); await w.write(txt); await w.close();
42 };
43 await escribe('src/app.js', 'export const a = 1;\n');
44 await escribe('.elffuss/soul/2026-10-10-1200-prueba.md', '# Propuesta\n\nRevisa `src/app.js`.\n');
45});
46await p.goto(BASE + '/?test-opfs', { waitUntil: 'domcontentloaded' });
47await p.waitForFunction(() => window.monaco && document.querySelector('#tree [data-path]'), null, { timeout: 30000 });
48
49// 1 · abrir la Mente
50const esBoton = await p.evaluate(() => { const b = document.getElementById('act-mind'); return !!b && b.tagName === 'BUTTON' && !!b.getAttribute('aria-label'); });
51ok('1 · la elfa es un botón con nombre accesible', esBoton);
52await p.click('#act-mind');
53await p.waitForSelector('#mind-overlay', { timeout: 45000 });
54await p.waitForTimeout(800);
55const r1 = await p.evaluate(async () => ({ on: (await import('/js/ceo.js')).isEnabled(), permiso: window.__permiso, boton: document.getElementById('mind-playstop').textContent }));
56ok('1 · abrir la Mente NO enciende el cerebro', !r1.on && /activar/.test(r1.boton), JSON.stringify(r1));
57ok('1 · …ni pide permiso de notificaciones', r1.permiso === 0, JSON.stringify(r1));
58ok('5 · la música no se carga sola (ninguna petición a SoundCloud)', sc.length === 0, sc.join(' '));
59
60// 2 · modo Básico: no piensa
61await p.click('#mind-playstop'); // encenderlo a propósito
62await p.click('#mind-config');
63await p.click('#cfg-think-now');
64await p.waitForTimeout(300);
65const nota = await p.evaluate(() => document.getElementById('cfg-note')?.textContent || '');
66ok('2 · «Pensar ahora» sin modelo lo dice en el panel', /Sin un modelo cargado/.test(nota), nota);
67const r2 = await p.evaluate(async () => {
68 const ceo = await import('/js/ceo.js');
69 const forzado = await ceo.forceCycle();
70 const root = await navigator.storage.getDirectory();
71 const soul = await (await root.getDirectoryHandle('.elffuss')).getDirectoryHandle('soul');
72 const nombres = []; for await (const e of soul.values()) nombres.push(e.name);
73 return { forzado, nombres };
74});
75ok('2 · …y con el motor de reglas no se escribe ninguna propuesta', r2.forzado === false && r2.nombres.length === 1, JSON.stringify(r2));
76await p.click('#cfg-x');
77
78// 2 · «Reprogramar» no enciende el cerebro: ni en Básico ni si lo pausaste
79await p.click('#mind-playstop'); // pausarlo a propósito
80const pausado = await p.evaluate(async () => (await import('/js/ceo.js')).isEnabled());
81await p.click('#mind-config');
82await p.click('#cfg-save');
83const r2b = await p.evaluate(async () => ({ on: (await import('/js/ceo.js')).isEnabled(), boton: document.getElementById('mind-playstop').textContent }));
84ok('2 · «Reprogramar» no enciende un cerebro pausado (y el botón no cambia)', !pausado && !r2b.on && !/pausar/i.test(r2b.boton), JSON.stringify({ pausado, ...r2b }));
85
86// 3 · etiqueta de propuesta pulsable
87await p.waitForSelector('.mind-node-label', { timeout: 10000 });
88const r3 = await p.evaluate(() => {
89 const lab = [...document.querySelectorAll('.mind-node-label')].find(l => l.style.display !== 'none');
90 const r = lab.getBoundingClientRect();
91 const enPunto = document.elementFromPoint(r.left + r.width / 2, r.top + r.height / 2);
92 return { boton: lab.tagName === 'BUTTON', recibeClic: enPunto === lab, aria: lab.getAttribute('aria-label') };
93});
94ok('3 · la etiqueta es un botón y recibe el clic (no el lienzo)', r3.boton && r3.recibeClic && /Abrir la propuesta/.test(r3.aria || ''), JSON.stringify(r3));
95await p.evaluate(() => [...document.querySelectorAll('.mind-node-label')].find(l => l.style.display !== 'none').click());
96await p.waitForSelector('#mind-panel.show');
97const enlace = await p.evaluate(() => [...document.querySelectorAll('#mind-panel .mp-body a')].map(a => a.textContent));
98ok('4 · las rutas de la propuesta son enlaces', enlace.includes('src/app.js'), JSON.stringify(enlace));
99
100// 4 · Abrir en el editor: cierra la Mente y abre el fichero
101await p.click('#mp-open');
102await p.waitForTimeout(400);
103const r4 = await p.evaluate(() => ({
104 mente: getComputedStyle(document.getElementById('mind-overlay')).display,
105 activa: document.querySelector('#tabs-bar .tab.active')?.dataset.path,
106}));
107ok('4 · «Abrir en el editor» cierra la Mente y abre la propuesta', r4.mente === 'none' && /prueba\.md$/.test(r4.activa || ''), JSON.stringify(r4));
108await p.click('#act-mind');
109await p.waitForTimeout(500);
110await p.evaluate(() => [...document.querySelectorAll('.mind-node-label')].find(l => l.style.display !== 'none').click());
111await p.waitForSelector('#mind-panel.show');
112await p.locator('#mind-panel .mp-body a', { hasText: 'src/app.js' }).click();
113await p.waitForTimeout(400);
114const r4b = await p.evaluate(() => ({ mente: getComputedStyle(document.getElementById('mind-overlay')).display, activa: document.querySelector('#tabs-bar .tab.active')?.dataset.path }));
115ok('4 · una ruta de la propuesta cierra la Mente y abre ese fichero', r4b.mente === 'none' && r4b.activa === 'src/app.js', JSON.stringify(r4b));
116
117// 5 · la música, solo si se pide, y se puede quitar
118await p.click('#act-mind');
119await p.waitForTimeout(400);
120await p.click('.mind-music-btn');
121await p.waitForTimeout(300);
122const conMusica = await p.evaluate(() => !!document.getElementById('mind-sc'));
123await p.click('.mind-music-btn');
124await p.waitForTimeout(200);
125const sinMusica = await p.evaluate(() => !document.getElementById('mind-sc'));
126ok('5 · «Poner música» carga el reproductor y «Quitar música» lo elimina', conMusica && sinMusica && sc.length > 0, `${conMusica} ${sinMusica} ${sc.length}`);
127const leyenda = await p.evaluate(() => [...document.querySelectorAll('#mind-legend .ml-item')].every(x => x.tagName === 'BUTTON'));
128ok('3 · la leyenda de perfiles son botones (teclado)', leyenda);
129
130// 6 · con el teclado la Mente es modal de verdad (antes Tab seguía DETRÁS, en el
131// árbol, la pestaña y Monaco, y cada Tab sangraba el fichero abierto)
132await p.keyboard.press('Escape');
133const textoApp = () => p.evaluate(() => window.monaco.editor.getModels().find(m => /src\/app\.js$/.test(m.uri.path))?.getValue());
134const antes6 = await textoApp();
135await p.focus('#act-mind');
136await p.keyboard.press('Enter');
137await p.waitForFunction(() => getComputedStyle(document.getElementById('mind-overlay')).display !== 'none');
138const r6 = await p.evaluate(() => ({ foco: document.activeElement?.id, ideInerte: document.getElementById('ide').inert }));
139ok('6 · con Enter el foco entra en la Mente (✕ salir) y el IDE de detrás queda inerte', r6.foco === 'mind-close' && r6.ideInerte, JSON.stringify(r6));
140const visitados = [];
141for (let i = 0; i < 10; i++) {
142 await p.keyboard.press('Tab');
143 visitados.push(await p.evaluate(() => {
144 const a = document.activeElement;
145 return a?.closest('#mind-overlay') ? 'mente' : a === document.body || !a ? 'body' : (a.id || a.className || a.tagName);
146 }));
147}
148ok('6 · Tab no sale de la Mente (nunca llega al árbol, las pestañas ni el editor)', visitados.every(v => v === 'mente' || v === 'body'), JSON.stringify(visitados));
149ok('6 · …y el fichero abierto detrás no cambia', await textoApp() === antes6);
150await p.keyboard.press('Escape');
151const r6c = await p.evaluate(() => ({ cerrada: getComputedStyle(document.getElementById('mind-overlay')).display === 'none', foco: document.activeElement?.id, ideInerte: document.getElementById('ide').inert }));
152ok('6 · Escape cierra, el IDE vuelve a estar vivo y el foco vuelve a la elfa', r6c.cerrada && !r6c.ideInerte && r6c.foco === 'act-mind', JSON.stringify(r6c));
153await p.keyboard.press('Enter');
154await p.waitForFunction(() => getComputedStyle(document.getElementById('mind-overlay')).display !== 'none');
155await p.evaluate(() => [...document.querySelectorAll('.mind-node-label')].find(l => l.style.display !== 'none').click());
156await p.waitForSelector('#mind-panel.show');
157await p.click('#mp-open');
158ok('6 · «Abrir en el editor» deja el foco EN el editor', await p.waitForFunction(() =>
159 getComputedStyle(document.getElementById('mind-overlay')).display === 'none' && !!document.activeElement?.closest('.monaco-editor'), null, { timeout: 5000 }).then(() => true, () => false),
160 await p.evaluate(() => document.activeElement?.className || document.activeElement?.tagName));
161
162console.log('\n errores JS:', errs[0] || 'ninguno');
163ok('sin errores de página', errs.length === 0, errs.join(' | '));
164console.log(fails ? `\n❌ ${fails} FALLO(S)` : '\n✅ LA MENTE PIDE PERMISO Y SE DEJA PULSAR');
165await b.close();
166process.exit(fails ? 1 : 0);