| 1 | // Lo adjuntado con @ se ve como fichas encima de la caja, cada una con su ×. |
| 2 | // Antes el adjunto era solo texto dentro del mensaje: para saber qué iba |
| 3 | // adjunto había que leerlo, y para quitarlo, encontrarlo y borrarlo a mano. |
| 4 | // |
| 5 | // BASE=http://localhost:8799 node tests/attach-chips.mjs |
| 6 | import { chromium } from 'playwright'; |
| 7 | const BASE = process.env.BASE || 'http://localhost:8799'; |
| 8 | let fails = 0; const ok = (n, c, e = '') => { console.log((c ? '✅' : '❌') + ' ' + n + (e ? ' — ' + e : '')); if (!c) fails++; }; |
| 9 | |
| 10 | const b = await chromium.launch(); |
| 11 | const ctx = await b.newContext({ locale: 'es-ES' }); |
| 12 | await ctx.addInitScript(() => { try { localStorage.setItem('elffusscode.model', 'rules'); } catch {} }); |
| 13 | const p = await ctx.newPage({ viewport: { width: 1400, height: 900 } }); |
| 14 | const errs = []; p.on('pageerror', e => errs.push(e.message.slice(0, 160))); |
| 15 | await p.goto(BASE + '/manifest.json'); |
| 16 | await p.evaluate(async () => { |
| 17 | const o = await navigator.storage.getDirectory(); |
| 18 | const w = async (path, txt) => { const parts = path.split('/'); const name = parts.pop(); let d = o; for (const x of parts) d = await d.getDirectoryHandle(x, { create: true }); const f = await d.getFileHandle(name, { create: true }); const s = await f.createWritable(); await s.write(txt); await s.close(); }; |
| 19 | await w('README.md', '# demo'); |
| 20 | await w('src/app.js', 'export const app = 1;'); |
| 21 | }); |
| 22 | await p.goto(BASE + '/?test-opfs', { waitUntil: 'domcontentloaded' }); |
| 23 | await p.waitForFunction(() => window.elffussClaw?.conv?.getActive?.(), null, { timeout: 20000 }); |
| 24 | const fichas = () => p.evaluate(() => ({ |
| 25 | oculto: document.getElementById('attach-chips').hidden, |
| 26 | fichas: [...document.querySelectorAll('#attach-chips .attach-chip')].map(c => c.firstChild.textContent), |
| 27 | quitar: [...document.querySelectorAll('#attach-chips .attach-chip button')].map(b => b.getAttribute('aria-label')), |
| 28 | texto: document.getElementById('prompt').value, |
| 29 | })); |
| 30 | const elige = async (frag, ruta) => { |
| 31 | await p.focus('#prompt'); |
| 32 | await p.keyboard.type(frag); |
| 33 | await p.locator('#menu .menu-item', { hasText: '@' + ruta }).first().click(); |
| 34 | }; |
| 35 | |
| 36 | ok('sin adjuntos no hay fichas', (await fichas()).oculto); |
| 37 | await p.focus('#prompt'); await p.keyboard.type('mira '); |
| 38 | await elige('@app', 'src/app.js'); |
| 39 | let f = await fichas(); |
| 40 | ok('adjuntar con @ pone una ficha con la ruta', !f.oculto && JSON.stringify(f.fichas) === '["@src/app.js"]' && /@src\/app\.js/.test(f.texto), JSON.stringify(f)); |
| 41 | ok(' …y su × dice qué quita', f.quitar[0] === 'Quitar @src/app.js del mensaje', JSON.stringify(f.quitar)); |
| 42 | await p.keyboard.type('y '); |
| 43 | await elige('@READ', 'README.md'); |
| 44 | f = await fichas(); |
| 45 | ok('dos adjuntos, dos fichas', JSON.stringify(f.fichas) === '["@src/app.js","@README.md"]', JSON.stringify(f)); |
| 46 | await p.locator('#attach-chips .attach-chip button').first().click(); |
| 47 | f = await fichas(); |
| 48 | ok('× quita la ficha Y la @ruta del mensaje', JSON.stringify(f.fichas) === '["@README.md"]' && !/@src\/app\.js/.test(f.texto) && /@README\.md/.test(f.texto), JSON.stringify(f)); |
| 49 | ok(' …y el foco vuelve a la caja', await p.evaluate(() => document.activeElement?.id) === 'prompt'); |
| 50 | // borrar la @ruta a mano también se lleva su ficha |
| 51 | await p.evaluate(() => { const t = document.getElementById('prompt'); t.value = t.value.replace('@README.md', ''); t.dispatchEvent(new Event('input')); }); |
| 52 | f = await fichas(); |
| 53 | ok('borrar la @ruta a mano también quita su ficha', f.oculto && f.fichas.length === 0, JSON.stringify(f)); |
| 54 | // al enviar, las fichas se van con el mensaje |
| 55 | await elige('@app', 'src/app.js'); |
| 56 | await p.keyboard.press('Enter'); |
| 57 | await p.waitForTimeout(400); |
| 58 | f = await fichas(); |
| 59 | ok('al enviar, las fichas se van con el mensaje', f.oculto && f.texto === '', JSON.stringify(f)); |
| 60 | |
| 61 | console.log('\n errores JS:', errs[0] || 'ninguno'); |
| 62 | ok('sin errores de página', errs.length === 0, errs.join(' | ')); |
| 63 | console.log(fails ? `\n❌ ${fails} FALLO(S)` : '\n✅ ADJUNTOS COMO FICHAS QUE SE QUITAN'); |
| 64 | await b.close(); |
| 65 | process.exit(fails ? 1 : 0); |