| 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 |
| 19 | import { chromium } from 'playwright'; |
| 20 | const BASE = process.env.BASE || 'http://localhost:8799'; |
| 21 | let fails = 0; const ok = (n, c, e = '') => { console.log((c ? '✅' : '❌') + ' ' + n + (e ? ' — ' + e : '')); if (!c) fails++; }; |
| 22 | |
| 23 | const b = await chromium.launch(); |
| 24 | const ctx = await b.newContext({ locale: 'es-ES' }); |
| 25 | await 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 | }); |
| 30 | const sc = []; |
| 31 | await ctx.route(/soundcloud/, r => { sc.push(r.request().url()); r.abort(); }); |
| 32 | const p = await ctx.newPage({ viewport: { width: 1400, height: 900 } }); |
| 33 | const errs = []; p.on('pageerror', e => errs.push(e.message.slice(0, 160))); |
| 34 | |
| 35 | await p.goto(BASE + '/manifest.json'); |
| 36 | await 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 | }); |
| 46 | await p.goto(BASE + '/?test-opfs', { waitUntil: 'domcontentloaded' }); |
| 47 | await p.waitForFunction(() => window.monaco && document.querySelector('#tree [data-path]'), null, { timeout: 30000 }); |
| 48 | |
| 49 | // 1 · abrir la Mente |
| 50 | const esBoton = await p.evaluate(() => { const b = document.getElementById('act-mind'); return !!b && b.tagName === 'BUTTON' && !!b.getAttribute('aria-label'); }); |
| 51 | ok('1 · la elfa es un botón con nombre accesible', esBoton); |
| 52 | await p.click('#act-mind'); |
| 53 | await p.waitForSelector('#mind-overlay', { timeout: 45000 }); |
| 54 | await p.waitForTimeout(800); |
| 55 | const r1 = await p.evaluate(async () => ({ on: (await import('/js/ceo.js')).isEnabled(), permiso: window.__permiso, boton: document.getElementById('mind-playstop').textContent })); |
| 56 | ok('1 · abrir la Mente NO enciende el cerebro', !r1.on && /activar/.test(r1.boton), JSON.stringify(r1)); |
| 57 | ok('1 · …ni pide permiso de notificaciones', r1.permiso === 0, JSON.stringify(r1)); |
| 58 | ok('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 |
| 61 | await p.click('#mind-playstop'); // encenderlo a propósito |
| 62 | await p.click('#mind-config'); |
| 63 | await p.click('#cfg-think-now'); |
| 64 | await p.waitForTimeout(300); |
| 65 | const nota = await p.evaluate(() => document.getElementById('cfg-note')?.textContent || ''); |
| 66 | ok('2 · «Pensar ahora» sin modelo lo dice en el panel', /Sin un modelo cargado/.test(nota), nota); |
| 67 | const 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 | }); |
| 75 | ok('2 · …y con el motor de reglas no se escribe ninguna propuesta', r2.forzado === false && r2.nombres.length === 1, JSON.stringify(r2)); |
| 76 | await p.click('#cfg-x'); |
| 77 | |
| 78 | // 2 · «Reprogramar» no enciende el cerebro: ni en Básico ni si lo pausaste |
| 79 | await p.click('#mind-playstop'); // pausarlo a propósito |
| 80 | const pausado = await p.evaluate(async () => (await import('/js/ceo.js')).isEnabled()); |
| 81 | await p.click('#mind-config'); |
| 82 | await p.click('#cfg-save'); |
| 83 | const r2b = await p.evaluate(async () => ({ on: (await import('/js/ceo.js')).isEnabled(), boton: document.getElementById('mind-playstop').textContent })); |
| 84 | ok('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 |
| 87 | await p.waitForSelector('.mind-node-label', { timeout: 10000 }); |
| 88 | const 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 | }); |
| 94 | ok('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)); |
| 95 | await p.evaluate(() => [...document.querySelectorAll('.mind-node-label')].find(l => l.style.display !== 'none').click()); |
| 96 | await p.waitForSelector('#mind-panel.show'); |
| 97 | const enlace = await p.evaluate(() => [...document.querySelectorAll('#mind-panel .mp-body a')].map(a => a.textContent)); |
| 98 | ok('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 |
| 101 | await p.click('#mp-open'); |
| 102 | await p.waitForTimeout(400); |
| 103 | const r4 = await p.evaluate(() => ({ |
| 104 | mente: getComputedStyle(document.getElementById('mind-overlay')).display, |
| 105 | activa: document.querySelector('#tabs-bar .tab.active')?.dataset.path, |
| 106 | })); |
| 107 | ok('4 · «Abrir en el editor» cierra la Mente y abre la propuesta', r4.mente === 'none' && /prueba\.md$/.test(r4.activa || ''), JSON.stringify(r4)); |
| 108 | await p.click('#act-mind'); |
| 109 | await p.waitForTimeout(500); |
| 110 | await p.evaluate(() => [...document.querySelectorAll('.mind-node-label')].find(l => l.style.display !== 'none').click()); |
| 111 | await p.waitForSelector('#mind-panel.show'); |
| 112 | await p.locator('#mind-panel .mp-body a', { hasText: 'src/app.js' }).click(); |
| 113 | await p.waitForTimeout(400); |
| 114 | const r4b = await p.evaluate(() => ({ mente: getComputedStyle(document.getElementById('mind-overlay')).display, activa: document.querySelector('#tabs-bar .tab.active')?.dataset.path })); |
| 115 | ok('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 |
| 118 | await p.click('#act-mind'); |
| 119 | await p.waitForTimeout(400); |
| 120 | await p.click('.mind-music-btn'); |
| 121 | await p.waitForTimeout(300); |
| 122 | const conMusica = await p.evaluate(() => !!document.getElementById('mind-sc')); |
| 123 | await p.click('.mind-music-btn'); |
| 124 | await p.waitForTimeout(200); |
| 125 | const sinMusica = await p.evaluate(() => !document.getElementById('mind-sc')); |
| 126 | ok('5 · «Poner música» carga el reproductor y «Quitar música» lo elimina', conMusica && sinMusica && sc.length > 0, `${conMusica} ${sinMusica} ${sc.length}`); |
| 127 | const leyenda = await p.evaluate(() => [...document.querySelectorAll('#mind-legend .ml-item')].every(x => x.tagName === 'BUTTON')); |
| 128 | ok('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) |
| 132 | await p.keyboard.press('Escape'); |
| 133 | const textoApp = () => p.evaluate(() => window.monaco.editor.getModels().find(m => /src\/app\.js$/.test(m.uri.path))?.getValue()); |
| 134 | const antes6 = await textoApp(); |
| 135 | await p.focus('#act-mind'); |
| 136 | await p.keyboard.press('Enter'); |
| 137 | await p.waitForFunction(() => getComputedStyle(document.getElementById('mind-overlay')).display !== 'none'); |
| 138 | const r6 = await p.evaluate(() => ({ foco: document.activeElement?.id, ideInerte: document.getElementById('ide').inert })); |
| 139 | ok('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)); |
| 140 | const visitados = []; |
| 141 | for (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 | } |
| 148 | ok('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)); |
| 149 | ok('6 · …y el fichero abierto detrás no cambia', await textoApp() === antes6); |
| 150 | await p.keyboard.press('Escape'); |
| 151 | const r6c = await p.evaluate(() => ({ cerrada: getComputedStyle(document.getElementById('mind-overlay')).display === 'none', foco: document.activeElement?.id, ideInerte: document.getElementById('ide').inert })); |
| 152 | ok('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)); |
| 153 | await p.keyboard.press('Enter'); |
| 154 | await p.waitForFunction(() => getComputedStyle(document.getElementById('mind-overlay')).display !== 'none'); |
| 155 | await p.evaluate(() => [...document.querySelectorAll('.mind-node-label')].find(l => l.style.display !== 'none').click()); |
| 156 | await p.waitForSelector('#mind-panel.show'); |
| 157 | await p.click('#mp-open'); |
| 158 | ok('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 | |
| 162 | console.log('\n errores JS:', errs[0] || 'ninguno'); |
| 163 | ok('sin errores de página', errs.length === 0, errs.join(' | ')); |
| 164 | console.log(fails ? `\n❌ ${fails} FALLO(S)` : '\n✅ LA MENTE PIDE PERMISO Y SE DEJA PULSAR'); |
| 165 | await b.close(); |
| 166 | process.exit(fails ? 1 : 0); |