Elffuss BraidHub

utopia-ia/elffuss-code public

main · 6.4 KiBRawPermalink
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
14import { chromium } from 'playwright';
15const BASE = process.env.BASE || 'http://localhost:8799';
16let fails = 0; const ok = (n, c, e = '') => { console.log((c ? '✅' : '❌') + ' ' + n + (e ? ' — ' + e : '')); if (!c) fails++; };
17
18const b = await chromium.launch();
19const ctx = await b.newContext({ locale: 'es-ES' });
20await ctx.addInitScript(() => { try { localStorage.setItem('elffusscode.model', 'rules'); } catch {} });
21const p = await ctx.newPage({ viewport: { width: 1500, height: 900 } });
22const errs = []; p.on('pageerror', e => errs.push(e.message.slice(0, 160)));
23
24await p.goto(BASE + '/manifest.json');
25await 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});
34const 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};
39const posicion = () => p.evaluate(() => ({ activa: document.querySelector('#tabs-bar .tab.active')?.dataset.path, linea: window.monaco?.editor?.getEditors?.()[0]?.getPosition()?.lineNumber }));
40const libre = () => p.waitForFunction(() => !window.elffussClaw.conv.isBusy(), null, { timeout: 10000 });
41
42await arranca();
43
44// 1 · ruta:línea en una respuesta
45await 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});
50await libre();
51await p.waitForTimeout(200);
52const 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; });
53ok('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));
54await p.locator('#chat-log a.file-link', { hasText: 'src/app.js:3' }).focus();
55await p.keyboard.press('Enter');
56await p.waitForTimeout(500);
57ok('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)
60const reglas = await p.evaluate(async () => { const r = await import('/js/providers/rules.js'); window.elffussClaw.conv.setProvider(r); });
61await p.fill('#prompt', 'busca unaPalabraRara');
62await p.press('#prompt', 'Enter');
63await libre();
64await p.waitForTimeout(300);
65const busqueda = await p.evaluate(() => [...document.querySelectorAll('#chat-log .tool-result pre a.file-link')].map(a => a.textContent));
66ok('2 · cada acierto de la búsqueda es un enlace fichero:línea', busqueda.includes('src/app.js:2'), JSON.stringify(busqueda));
67await p.evaluate(() => document.querySelector('#chat-log .tool-result')?.setAttribute('open', ''));
68await p.locator('#chat-log .tool-result pre a.file-link', { hasText: 'src/app.js:2' }).first().click();
69await p.waitForTimeout(500);
70ok('2 · …y abre el fichero en esa línea', JSON.stringify(await posicion()) === '{"activa":"src/app.js","linea":2}', JSON.stringify(await posicion()));
71await p.fill('#prompt', 'lee src/app.js');
72await p.press('#prompt', 'Enter');
73await libre();
74await p.waitForTimeout(200);
75const fila = await p.evaluate(() => [...document.querySelectorAll('#chat-log .msg.tool .targ a.file-link')].map(a => a.textContent));
76ok('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
79await 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});
83await p.waitForTimeout(300);
84const 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 || '') }; });
85ok('4 · el informe trae su botón «▶ Ejecutar esta propuesta»', informe.boton, JSON.stringify(informe));
86ok('4 · su _cursiva_ se pinta como cursiva (sin guiones bajos sueltos)', informe.cursiva && !informe.crudo, JSON.stringify(informe));
87await arranca();
88const trasRecargar = await p.evaluate(() => [...document.querySelectorAll('#chat-log .msg.assistant')].some(m => m.querySelector('.proposal-exec-btn') && /Calidad/.test(m.textContent)));
89ok('4 · tras recargar, el botón sigue ahí', trasRecargar);
90
91// 3 · #/file/ruta:línea
92await arranca('#/file/src/app.js:4');
93await p.waitForTimeout(600);
94ok('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
96console.log('\n errores JS:', errs[0] || 'ninguno');
97ok('sin errores de página', errs.length === 0, errs.join(' | '));
98console.log(fails ? `\n❌ ${fails} FALLO(S)` : '\n✅ ENLACES DEL CHAT Y BOTÓN DE PROPUESTA OK');
99await b.close();
100process.exit(fails ? 1 : 0);