| 1 | // Bug real visto en producción: `rm mejoras-*.md` decía «no existe» aunque |
| 2 | // había decenas de ficheros coincidentes, porque rm buscaba el nombre LITERAL |
| 3 | // con el asterisco en vez de expandir el comodín. La elfa concluía (mal) que |
| 4 | // los ficheros no existían y no los borraba. |
| 5 | import { chromium } from 'playwright'; |
| 6 | const BASE = process.env.BASE || 'http://localhost:8799'; |
| 7 | let fails = 0; const ok = (n, c, e = '') => { console.log((c ? '✅' : '❌') + ' ' + n + (e ? ' — ' + e : '')); if (!c) fails++; }; |
| 8 | |
| 9 | const b = await chromium.launch(); |
| 10 | const ctx = await b.newContext(); await ctx.addInitScript(() => { try { localStorage.setItem('elffusscode.model', 'rules'); } catch {} }); |
| 11 | const p = await ctx.newPage(); |
| 12 | await p.goto(BASE + '/', { waitUntil: 'domcontentloaded' }); |
| 13 | await p.evaluate(async () => { |
| 14 | const o = await navigator.storage.getDirectory(); |
| 15 | const dir = await o.getDirectoryHandle('.elffuss', { create: true }); |
| 16 | const w = async (n, t) => { const f = await dir.getFileHandle(n, { create: true }); const s = await f.createWritable(); await s.write(t); await s.close(); }; |
| 17 | for (let i = 1; i <= 5; i++) await w(`mejoras-${String(i).padStart(3, '0')}.md`, '# propuesta ' + i); |
| 18 | await w('archivo.md', '# no debe borrarse'); // no matchea el patrón |
| 19 | await w('README.md', '# tampoco'); |
| 20 | }); |
| 21 | await p.goto(BASE + '/?test-opfs', { waitUntil: 'domcontentloaded' }); await p.waitForTimeout(1500); |
| 22 | |
| 23 | const sh = cmd => p.evaluate(async (c) => { const s = await import('/js/shell.js'); const raw = await s.exec(c); return s.stripAnsi(raw); }, cmd); |
| 24 | |
| 25 | const before = await sh('ls .elffuss'); |
| 26 | ok('los 5 ficheros mejoras-*.md existen antes de borrar', (before.match(/mejoras-/g) || []).length === 5, before); |
| 27 | |
| 28 | const out = await sh('rm .elffuss/mejoras-*.md'); |
| 29 | ok('rm con comodín NO da "no existe" (el bug real)', !/no existe/.test(out), out); |
| 30 | ok('rm con comodín confirma cuántos borró', /Eliminados 5/.test(out), out); |
| 31 | |
| 32 | const after = await sh('ls .elffuss'); |
| 33 | ok('los 5 mejoras-*.md YA NO están', !/mejoras-/.test(after), after); |
| 34 | ok('archivo.md (no coincide el patrón) SIGUE ahí', /archivo\.md/.test(after), after); |
| 35 | ok('README.md (no coincide el patrón) SIGUE ahí', /README\.md/.test(after), after); |
| 36 | |
| 37 | // rm sin comodín (exacto) sigue funcionando igual que antes |
| 38 | await sh('echo x > .elffuss/solo.txt'); |
| 39 | const outExact = await sh('rm .elffuss/solo.txt'); |
| 40 | ok('rm exacto (sin comodín) sigue funcionando', !/no existe/.test(outExact), outExact); |
| 41 | ok('rm exacto de verdad borró el fichero', !/solo\.txt/.test(await sh('ls .elffuss'))); |
| 42 | |
| 43 | // glob sin coincidencias → error claro (no un "éxito" silencioso) |
| 44 | let errCaught = ''; |
| 45 | try { await p.evaluate(() => { const shp = import('/js/shell.js'); return shp.then(s => s.exec('rm .elffuss/no-existe-*.md')); }); } |
| 46 | catch { /* el evaluate puede tragarse el rechazo si no hacemos throw */ } |
| 47 | const errMsg = await p.evaluate(async () => { const s = await import('/js/shell.js'); try { await s.exec('rm .elffuss/no-existe-*.md'); return null; } catch (e) { return e.message; } }); |
| 48 | ok('glob sin coincidencias da error claro (no silencioso)', /sin coincidencias/.test(errMsg || ''), errMsg); |
| 49 | |
| 50 | console.log(fails ? `\n❌ ${fails} FALLO(S)` : '\n✅ RM CON COMODINES OK — el bug real queda arreglado'); |
| 51 | await b.close(); |
| 52 | process.exit(fails ? 1 : 0); |