Elffuss BraidHub

utopia-ia/elffuss-code public

main · 5.1 KiBRawPermalink
1// Prueba de integración del TERMINAL de Elffuss Code.
2// Siembra un proyecto en OPFS, comprueba el motor del shell (ls/cat/grep/mkdir/
3// echo>/pipe/&&) contra los ficheros REALES, y la UI de xterm (teclear → salida).
4// El shell importado en page.evaluate es el MISMO singleton que usa la app
5// (los módulos ES se cachean por realm) → comparte el proyecto abierto.
6import { chromium } from 'playwright';
7import { CHROME_ARGS } from './chrome-args.mjs';
8const OUT = (process.env.SCRATCH || '/tmp/elffuss-test');
9const BASE = process.env.BASE || 'http://localhost:8799';
10let fails = 0;
11const ok = (n, c, e = '') => { console.log((c ? '✅' : '❌') + ' ' + n + (e ? ' — ' + e : '')); if (!c) fails++; };
12
13const b = await chromium.launch({ args: ['--enable-unsafe-webgpu', ...CHROME_ARGS] });
14const ctx = await b.newContext();
15await ctx.addInitScript(() => { try { localStorage.setItem('elffusscode.model', 'rules'); } catch {} });
16const p = await ctx.newPage({ viewport: { width: 1500, height: 900 } });
17p.on('pageerror', e => { if (!/allow-same-origin/.test(e.message)) console.log(' pageerror:', e.message.slice(0, 160)); });
18
19// sembrar proyecto en OPFS
20await p.goto(BASE + '/', { waitUntil: 'domcontentloaded' });
21await p.evaluate(async () => {
22 const opfs = await navigator.storage.getDirectory();
23 const w = async (dir, name, txt) => { const fh = await dir.getFileHandle(name, { create: true }); const s = await fh.createWritable(); await s.write(txt); await s.close(); };
24 const src = await opfs.getDirectoryHandle('src', { create: true });
25 await w(opfs, 'README.md', '# demo\nProyecto de prueba para el terminal.');
26 await w(opfs, 'package.json', '{ "name": "demo", "version": "1.0.0" }');
27 await w(src, 'app.js', "import { greet } from './utils.js';\nexport function app(){ return greet('mundo'); }");
28 await w(src, 'utils.js', "export const greet = (d) => `hola ${d}`;\nexport const TODO = 'refactor';");
29});
30await p.goto(BASE + '/?test-opfs', { waitUntil: 'domcontentloaded' });
31await p.waitForTimeout(2500);
32
33// ---- Motor del shell (mismo singleton que la app) ----
34const sh = async (cmd) => p.evaluate(async (c) => {
35 const s = await import('/js/shell.js');
36 const raw = await s.exec(c);
37 return s.stripAnsi(raw);
38}, cmd);
39
40ok('ls lista los ficheros reales del proyecto', /README\.md/.test(await sh('ls')) && /src\//.test(await sh('ls')), await sh('ls'));
41ok('cat lee contenido real', (await sh('cat package.json')).includes('"name": "demo"'));
42ok('cd + pwd navegan el árbol', (await sh('cd src')) !== undefined && (await sh('pwd')) === '/src');
43ok('ls dentro de src tras cd', /app\.js/.test(await sh('ls')) && /utils\.js/.test(await sh('ls')));
44await sh('cd ..');
45ok('grep recursivo encuentra el símbolo', /utils\.js/.test(await sh('grep -n greet src')), (await sh('grep -n greet src')).replace(/\n/g, ' | '));
46ok('tubería cat | grep', (await sh('cat src/utils.js | grep TODO')).includes('TODO'));
47ok('mkdir + echo> escriben en disco', (await sh('mkdir tests && echo hola-terminal > tests/nota.txt'), (await sh('cat tests/nota.txt')).trim()) === 'hola-terminal');
48ok('find -name localiza por patrón', /utils\.js/.test(await sh('find src -name *.js')));
49ok('wc -l cuenta líneas', (await sh('wc -l src/utils.js')).trim() === '2', await sh('wc -l src/utils.js'));
50ok('runtime real avisa con honestidad (npm)', /Bridge local/i.test(await sh('npm install').catch(e => e.message || '')) || /Bridge local/i.test(await p.evaluate(async () => { const s = await import('/js/shell.js'); try { await s.exec('npm install'); return ''; } catch (e) { return e.message; } })));
51
52// el fichero creado por el shell aparece en el árbol del explorador (fsChange)
53await p.waitForTimeout(400);
54ok('el árbol del IDE refleja el fichero creado por el shell', await p.locator('#tree').getByText('tests', { exact: false }).count() > 0);
55
56// ---- Tool de la elfa (terminal.run) ----
57const agentOut = await p.evaluate(async () => {
58 const t = await import('/js/tools/index.js');
59 return t.runTool('terminal.run', { command: 'ls src' });
60});
61ok('la elfa puede ejecutar comandos (tool terminal.run)', /app\.js/.test(agentOut), String(agentOut).slice(0, 60));
62
63// ---- UI de xterm ----
64await p.click('#act-term');
65await p.waitForTimeout(1500);
66ok('el panel del terminal se abre', !(await p.locator('#terminal-panel').getAttribute('hidden').catch(() => 'x')));
67ok('xterm se monta (canvas/rows presentes)', await p.locator('#terminal-host .xterm').count() > 0);
68// teclear un comando en el terminal real
69await p.locator('#terminal-host').click();
70await p.locator('#terminal-host .xterm-helper-textarea').focus().catch(() => {});
71await p.waitForTimeout(200);
72await p.keyboard.type('ls');
73await p.keyboard.press('Enter');
74await p.waitForTimeout(700);
75const rows = await p.locator('#terminal-host .xterm-rows').innerText().catch(() => '');
76ok('teclear «ls» en la UI muestra los ficheros', /README\.md/.test(rows) || /package\.json/.test(rows), rows.replace(/\n+/g, ' | ').slice(0, 120));
77await p.screenshot({ path: OUT + '/terminal.png' });
78console.log('captura → terminal.png');
79
80console.log(fails ? `\n❌ ${fails} FALLO(S)` : '\n✅ TERMINAL OK — shell real + xterm + tool de la elfa');
81await b.close();
82process.exit(fails ? 1 : 0);