Elffuss BraidHub

utopia-ia/elffuss-code public

main · 3.2 KiBRawPermalink
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.
5import { chromium } from 'playwright';
6const BASE = process.env.BASE || 'http://localhost:8799';
7let fails = 0; const ok = (n, c, e = '') => { console.log((c ? '✅' : '❌') + ' ' + n + (e ? ' — ' + e : '')); if (!c) fails++; };
8
9const b = await chromium.launch();
10const ctx = await b.newContext(); await ctx.addInitScript(() => { try { localStorage.setItem('elffusscode.model', 'rules'); } catch {} });
11const p = await ctx.newPage();
12await p.goto(BASE + '/', { waitUntil: 'domcontentloaded' });
13await 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});
21await p.goto(BASE + '/?test-opfs', { waitUntil: 'domcontentloaded' }); await p.waitForTimeout(1500);
22
23const 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
25const before = await sh('ls .elffuss');
26ok('los 5 ficheros mejoras-*.md existen antes de borrar', (before.match(/mejoras-/g) || []).length === 5, before);
27
28const out = await sh('rm .elffuss/mejoras-*.md');
29ok('rm con comodín NO da "no existe" (el bug real)', !/no existe/.test(out), out);
30ok('rm con comodín confirma cuántos borró', /Eliminados 5/.test(out), out);
31
32const after = await sh('ls .elffuss');
33ok('los 5 mejoras-*.md YA NO están', !/mejoras-/.test(after), after);
34ok('archivo.md (no coincide el patrón) SIGUE ahí', /archivo\.md/.test(after), after);
35ok('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
38await sh('echo x > .elffuss/solo.txt');
39const outExact = await sh('rm .elffuss/solo.txt');
40ok('rm exacto (sin comodín) sigue funcionando', !/no existe/.test(outExact), outExact);
41ok('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)
44let errCaught = '';
45try { await p.evaluate(() => { const shp = import('/js/shell.js'); return shp.then(s => s.exec('rm .elffuss/no-existe-*.md')); }); }
46catch { /* el evaluate puede tragarse el rechazo si no hacemos throw */ }
47const 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; } });
48ok('glob sin coincidencias da error claro (no silencioso)', /sin coincidencias/.test(errMsg || ''), errMsg);
49
50console.log(fails ? `\n❌ ${fails} FALLO(S)` : '\n✅ RM CON COMODINES OK — el bug real queda arreglado');
51await b.close();
52process.exit(fails ? 1 : 0);