| 1 | // Enlaces del chat a ficheros, y el botón de la propuesta que sobrevive. |
| 2 | // |
| 3 | // 1. `ruta:línea` en una respuesta es un enlace de verdad (<a>, con Tab y |
| 4 | // Enter) que abre el fichero EN esa línea. Antes era un <code> con un |
| 5 | // onclick: solo ratón y sin línea. |
| 6 | // 2. La ruta de una fila de herramienta («● code.read src/app.js») y cada |
| 7 | // acierto de una búsqueda («src/app.js:2: …») también se abren. |
| 8 | // 3. #/file/ruta:línea en la URL abre ese fichero al cargar. |
| 9 | // 4. «▶ Ejecutar esta propuesta» del informe del cerebro sigue ahí tras |
| 10 | // recargar (antes solo vivía en el DOM del momento), y su _cursiva_ se |
| 11 | // pinta como cursiva (salían los guiones bajos). |
| 12 | // |
| 13 | // BASE=http://127.0.0.1:8898 node tests/chatlinks.mjs |
| 14 | import { chromium } from 'playwright'; |
| 15 | const BASE = process.env.BASE || 'http://localhost:8799'; |
| 16 | let fails = 0; const ok = (n, c, e = '') => { console.log((c ? '✅' : '❌') + ' ' + n + (e ? ' — ' + e : '')); if (!c) fails++; }; |
| 17 | |
| 18 | const b = await chromium.launch(); |
| 19 | const ctx = await b.newContext({ locale: 'es-ES' }); |
| 20 | await ctx.addInitScript(() => { try { localStorage.setItem('elffusscode.model', 'rules'); } catch {} }); |
| 21 | const p = await ctx.newPage({ viewport: { width: 1500, height: 900 } }); |
| 22 | const errs = []; p.on('pageerror', e => errs.push(e.message.slice(0, 160))); |
| 23 | |
| 24 | await p.goto(BASE + '/manifest.json'); |
| 25 | await p.evaluate(async () => { |
| 26 | const root = await navigator.storage.getDirectory(); |
| 27 | const escribe = async (path, txt) => { |
| 28 | const parts = path.split('/'); const name = parts.pop(); let d = root; |
| 29 | for (const x of parts) d = await d.getDirectoryHandle(x, { create: true }); |
| 30 | const w = await (await d.getFileHandle(name, { create: true })).createWritable(); await w.write(txt); await w.close(); |
| 31 | }; |
| 32 | await escribe('src/app.js', 'linea 1\nlinea 2 con unaPalabraRara\nlinea 3\nlinea 4\n'); |
| 33 | }); |
| 34 | const arranca = async (hash = '') => { |
| 35 | await p.goto(BASE + '/?test-opfs' + hash, { waitUntil: 'domcontentloaded' }); |
| 36 | await p.waitForFunction(() => window.monaco && window.elffussClaw?.conv?.getActive?.(), null, { timeout: 30000 }); |
| 37 | await p.waitForTimeout(300); |
| 38 | }; |
| 39 | const posicion = () => p.evaluate(() => ({ activa: document.querySelector('#tabs-bar .tab.active')?.dataset.path, linea: window.monaco?.editor?.getEditors?.()[0]?.getPosition()?.lineNumber })); |
| 40 | const libre = () => p.waitForFunction(() => !window.elffussClaw.conv.isBusy(), null, { timeout: 10000 }); |
| 41 | |
| 42 | await arranca(); |
| 43 | |
| 44 | // 1 · ruta:línea en una respuesta |
| 45 | await p.evaluate(async () => { |
| 46 | const conv = window.elffussClaw.conv; |
| 47 | conv.setProvider({ chat: async () => 'Mira `src/app.js:3` y también `README.md`.' }); |
| 48 | window.elffussClaw.send('¿dónde está?'); |
| 49 | }); |
| 50 | await libre(); |
| 51 | await p.waitForTimeout(200); |
| 52 | const enlace = await p.evaluate(() => { const a = [...document.querySelectorAll('#chat-log a.file-link')].find(x => x.textContent === 'src/app.js:3'); return a ? { href: a.getAttribute('href'), tab: a.tabIndex } : null; }); |
| 53 | ok('1 · `src/app.js:3` es un enlace (<a href="#/file/…:3">)', enlace?.href === '#/file/src/app.js:3' && enlace.tab === 0, JSON.stringify(enlace)); |
| 54 | await p.locator('#chat-log a.file-link', { hasText: 'src/app.js:3' }).focus(); |
| 55 | await p.keyboard.press('Enter'); |
| 56 | await p.waitForTimeout(500); |
| 57 | ok('1 · Enter en el enlace abre el fichero en la línea 3', JSON.stringify(await posicion()) === '{"activa":"src/app.js","linea":3}', JSON.stringify(await posicion())); |
| 58 | |
| 59 | // 2 · fila de herramienta y aciertos de búsqueda (modo básico: reglas) |
| 60 | const reglas = await p.evaluate(async () => { const r = await import('/js/providers/rules.js'); window.elffussClaw.conv.setProvider(r); }); |
| 61 | await p.fill('#prompt', 'busca unaPalabraRara'); |
| 62 | await p.press('#prompt', 'Enter'); |
| 63 | await libre(); |
| 64 | await p.waitForTimeout(300); |
| 65 | const busqueda = await p.evaluate(() => [...document.querySelectorAll('#chat-log .tool-result pre a.file-link')].map(a => a.textContent)); |
| 66 | ok('2 · cada acierto de la búsqueda es un enlace fichero:línea', busqueda.includes('src/app.js:2'), JSON.stringify(busqueda)); |
| 67 | await p.evaluate(() => document.querySelector('#chat-log .tool-result')?.setAttribute('open', '')); |
| 68 | await p.locator('#chat-log .tool-result pre a.file-link', { hasText: 'src/app.js:2' }).first().click(); |
| 69 | await p.waitForTimeout(500); |
| 70 | ok('2 · …y abre el fichero en esa línea', JSON.stringify(await posicion()) === '{"activa":"src/app.js","linea":2}', JSON.stringify(await posicion())); |
| 71 | await p.fill('#prompt', 'lee src/app.js'); |
| 72 | await p.press('#prompt', 'Enter'); |
| 73 | await libre(); |
| 74 | await p.waitForTimeout(200); |
| 75 | const fila = await p.evaluate(() => [...document.querySelectorAll('#chat-log .msg.tool .targ a.file-link')].map(a => a.textContent)); |
| 76 | ok('2 · la ruta de la fila de herramienta es un enlace', fila.includes('src/app.js'), JSON.stringify(fila)); |
| 77 | |
| 78 | // 4 · informe del cerebro con su botón, también tras recargar |
| 79 | await p.evaluate(async () => { |
| 80 | const m = await import('/js/main.js'); |
| 81 | m.reportImprovements({ path: '.elffuss/soul/x.md', md: '# Propuesta\nhaz algo', proposals: [{ dept: 'Calidad', text: 'Añadir validación a la entrada del formulario.' }] }); |
| 82 | }); |
| 83 | await p.waitForTimeout(300); |
| 84 | const informe = await p.evaluate(() => { const ms = [...document.querySelectorAll('#chat-log .msg.assistant')]; const u = ms.at(-1); return { boton: !!u?.querySelector('.proposal-exec-btn'), cursiva: !!u?.querySelector('i'), crudo: /_Guardado/.test(u?.textContent || '') }; }); |
| 85 | ok('4 · el informe trae su botón «▶ Ejecutar esta propuesta»', informe.boton, JSON.stringify(informe)); |
| 86 | ok('4 · su _cursiva_ se pinta como cursiva (sin guiones bajos sueltos)', informe.cursiva && !informe.crudo, JSON.stringify(informe)); |
| 87 | await arranca(); |
| 88 | const trasRecargar = await p.evaluate(() => [...document.querySelectorAll('#chat-log .msg.assistant')].some(m => m.querySelector('.proposal-exec-btn') && /Calidad/.test(m.textContent))); |
| 89 | ok('4 · tras recargar, el botón sigue ahí', trasRecargar); |
| 90 | |
| 91 | // 3 · #/file/ruta:línea |
| 92 | await arranca('#/file/src/app.js:4'); |
| 93 | await p.waitForTimeout(600); |
| 94 | ok('3 · #/file/src/app.js:4 abre el fichero en la línea 4 al cargar', JSON.stringify(await posicion()) === '{"activa":"src/app.js","linea":4}', JSON.stringify(await posicion())); |
| 95 | |
| 96 | console.log('\n errores JS:', errs[0] || 'ninguno'); |
| 97 | ok('sin errores de página', errs.length === 0, errs.join(' | ')); |
| 98 | console.log(fails ? `\n❌ ${fails} FALLO(S)` : '\n✅ ENLACES DEL CHAT Y BOTÓN DE PROPUESTA OK'); |
| 99 | await b.close(); |
| 100 | process.exit(fails ? 1 : 0); |