| 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 | // La red a SoundCloud va bloqueada: el test solo mira si la app la PIDE. |
| 14 | // |
| 15 | // BASE=http://127.0.0.1:8898 node tests/mind-consent.mjs |
| 16 | import { chromium } from 'playwright'; |
| 17 | const BASE = process.env.BASE || 'http://localhost:8799'; |
| 18 | let fails = 0; const ok = (n, c, e = '') => { console.log((c ? '✅' : '❌') + ' ' + n + (e ? ' — ' + e : '')); if (!c) fails++; }; |
| 19 | |
| 20 | const b = await chromium.launch(); |
| 21 | const ctx = await b.newContext({ locale: 'es-ES' }); |
| 22 | await ctx.addInitScript(() => { |
| 23 | try { localStorage.setItem('elffusscode.model', 'rules'); } catch {} |
| 24 | window.__permiso = 0; |
| 25 | if (window.Notification) Notification.requestPermission = () => { window.__permiso++; return Promise.resolve('default'); }; |
| 26 | }); |
| 27 | const sc = []; |
| 28 | await ctx.route(/soundcloud/, r => { sc.push(r.request().url()); r.abort(); }); |
| 29 | const p = await ctx.newPage({ viewport: { width: 1400, height: 900 } }); |
| 30 | const errs = []; p.on('pageerror', e => errs.push(e.message.slice(0, 160))); |
| 31 | |
| 32 | await p.goto(BASE + '/manifest.json'); |
| 33 | await p.evaluate(async () => { |
| 34 | const root = await navigator.storage.getDirectory(); |
| 35 | const escribe = async (path, txt) => { |
| 36 | const parts = path.split('/'); const name = parts.pop(); let d = root; |
| 37 | for (const x of parts) d = await d.getDirectoryHandle(x, { create: true }); |
| 38 | const w = await (await d.getFileHandle(name, { create: true })).createWritable(); await w.write(txt); await w.close(); |
| 39 | }; |
| 40 | await escribe('src/app.js', 'export const a = 1;\n'); |
| 41 | await escribe('.elffuss/soul/2026-10-10-1200-prueba.md', '# Propuesta\n\nRevisa `src/app.js`.\n'); |
| 42 | }); |
| 43 | await p.goto(BASE + '/?test-opfs', { waitUntil: 'domcontentloaded' }); |
| 44 | await p.waitForFunction(() => window.monaco && document.querySelector('#tree [data-path]'), null, { timeout: 30000 }); |
| 45 | |
| 46 | // 1 · abrir la Mente |
| 47 | const esBoton = await p.evaluate(() => { const b = document.getElementById('act-mind'); return !!b && b.tagName === 'BUTTON' && !!b.getAttribute('aria-label'); }); |
| 48 | ok('1 · la elfa es un botón con nombre accesible', esBoton); |
| 49 | await p.click('#act-mind'); |
| 50 | await p.waitForSelector('#mind-overlay', { timeout: 45000 }); |
| 51 | await p.waitForTimeout(800); |
| 52 | const r1 = await p.evaluate(async () => ({ on: (await import('/js/ceo.js')).isEnabled(), permiso: window.__permiso, boton: document.getElementById('mind-playstop').textContent })); |
| 53 | ok('1 · abrir la Mente NO enciende el cerebro', !r1.on && /activar/.test(r1.boton), JSON.stringify(r1)); |
| 54 | ok('1 · …ni pide permiso de notificaciones', r1.permiso === 0, JSON.stringify(r1)); |
| 55 | ok('5 · la música no se carga sola (ninguna petición a SoundCloud)', sc.length === 0, sc.join(' ')); |
| 56 | |
| 57 | // 2 · modo Básico: no piensa |
| 58 | await p.click('#mind-playstop'); // encenderlo a propósito |
| 59 | await p.click('#mind-config'); |
| 60 | await p.click('#cfg-think-now'); |
| 61 | await p.waitForTimeout(300); |
| 62 | const nota = await p.evaluate(() => document.getElementById('cfg-note')?.textContent || ''); |
| 63 | ok('2 · «Pensar ahora» sin modelo lo dice en el panel', /Sin un modelo cargado/.test(nota), nota); |
| 64 | const r2 = await p.evaluate(async () => { |
| 65 | const ceo = await import('/js/ceo.js'); |
| 66 | const forzado = await ceo.forceCycle(); |
| 67 | const root = await navigator.storage.getDirectory(); |
| 68 | const soul = await (await root.getDirectoryHandle('.elffuss')).getDirectoryHandle('soul'); |
| 69 | const nombres = []; for await (const e of soul.values()) nombres.push(e.name); |
| 70 | return { forzado, nombres }; |
| 71 | }); |
| 72 | ok('2 · …y con el motor de reglas no se escribe ninguna propuesta', r2.forzado === false && r2.nombres.length === 1, JSON.stringify(r2)); |
| 73 | await p.click('#cfg-x'); |
| 74 | |
| 75 | // 3 · etiqueta de propuesta pulsable |
| 76 | await p.waitForSelector('.mind-node-label', { timeout: 10000 }); |
| 77 | const r3 = await p.evaluate(() => { |
| 78 | const lab = [...document.querySelectorAll('.mind-node-label')].find(l => l.style.display !== 'none'); |
| 79 | const r = lab.getBoundingClientRect(); |
| 80 | const enPunto = document.elementFromPoint(r.left + r.width / 2, r.top + r.height / 2); |
| 81 | return { boton: lab.tagName === 'BUTTON', recibeClic: enPunto === lab, aria: lab.getAttribute('aria-label') }; |
| 82 | }); |
| 83 | 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)); |
| 84 | await p.evaluate(() => [...document.querySelectorAll('.mind-node-label')].find(l => l.style.display !== 'none').click()); |
| 85 | await p.waitForSelector('#mind-panel.show'); |
| 86 | const enlace = await p.evaluate(() => [...document.querySelectorAll('#mind-panel .mp-body a')].map(a => a.textContent)); |
| 87 | ok('4 · las rutas de la propuesta son enlaces', enlace.includes('src/app.js'), JSON.stringify(enlace)); |
| 88 | |
| 89 | // 4 · Abrir en el editor: cierra la Mente y abre el fichero |
| 90 | await p.click('#mp-open'); |
| 91 | await p.waitForTimeout(400); |
| 92 | const r4 = await p.evaluate(() => ({ |
| 93 | mente: getComputedStyle(document.getElementById('mind-overlay')).display, |
| 94 | activa: document.querySelector('#tabs-bar .tab.active')?.dataset.path, |
| 95 | })); |
| 96 | ok('4 · «Abrir en el editor» cierra la Mente y abre la propuesta', r4.mente === 'none' && /prueba\.md$/.test(r4.activa || ''), JSON.stringify(r4)); |
| 97 | await p.click('#act-mind'); |
| 98 | await p.waitForTimeout(500); |
| 99 | await p.evaluate(() => [...document.querySelectorAll('.mind-node-label')].find(l => l.style.display !== 'none').click()); |
| 100 | await p.waitForSelector('#mind-panel.show'); |
| 101 | await p.locator('#mind-panel .mp-body a', { hasText: 'src/app.js' }).click(); |
| 102 | await p.waitForTimeout(400); |
| 103 | const r4b = await p.evaluate(() => ({ mente: getComputedStyle(document.getElementById('mind-overlay')).display, activa: document.querySelector('#tabs-bar .tab.active')?.dataset.path })); |
| 104 | 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)); |
| 105 | |
| 106 | // 5 · la música, solo si se pide, y se puede quitar |
| 107 | await p.click('#act-mind'); |
| 108 | await p.waitForTimeout(400); |
| 109 | await p.click('.mind-music-btn'); |
| 110 | await p.waitForTimeout(300); |
| 111 | const conMusica = await p.evaluate(() => !!document.getElementById('mind-sc')); |
| 112 | await p.click('.mind-music-btn'); |
| 113 | await p.waitForTimeout(200); |
| 114 | const sinMusica = await p.evaluate(() => !document.getElementById('mind-sc')); |
| 115 | ok('5 · «Poner música» carga el reproductor y «Quitar música» lo elimina', conMusica && sinMusica && sc.length > 0, `${conMusica} ${sinMusica} ${sc.length}`); |
| 116 | const leyenda = await p.evaluate(() => [...document.querySelectorAll('#mind-legend .ml-item')].every(x => x.tagName === 'BUTTON')); |
| 117 | ok('3 · la leyenda de perfiles son botones (teclado)', leyenda); |
| 118 | |
| 119 | console.log('\n errores JS:', errs[0] || 'ninguno'); |
| 120 | ok('sin errores de página', errs.length === 0, errs.join(' | ')); |
| 121 | console.log(fails ? `\n❌ ${fails} FALLO(S)` : '\n✅ LA MENTE PIDE PERMISO Y SE DEJA PULSAR'); |
| 122 | await b.close(); |
| 123 | process.exit(fails ? 1 : 0); |