| 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. |
| 6 | import { chromium } from 'playwright'; |
| 7 | import { CHROME_ARGS } from './chrome-args.mjs'; |
| 8 | const OUT = (process.env.SCRATCH || '/tmp/elffuss-test'); |
| 9 | const BASE = process.env.BASE || 'http://localhost:8799'; |
| 10 | let fails = 0; |
| 11 | const ok = (n, c, e = '') => { console.log((c ? '✅' : '❌') + ' ' + n + (e ? ' — ' + e : '')); if (!c) fails++; }; |
| 12 | |
| 13 | const b = await chromium.launch({ args: ['--enable-unsafe-webgpu', ...CHROME_ARGS] }); |
| 14 | const ctx = await b.newContext(); |
| 15 | await ctx.addInitScript(() => { try { localStorage.setItem('elffusscode.model', 'rules'); } catch {} }); |
| 16 | const p = await ctx.newPage({ viewport: { width: 1500, height: 900 } }); |
| 17 | p.on('pageerror', e => { if (!/allow-same-origin/.test(e.message)) console.log(' pageerror:', e.message.slice(0, 160)); }); |
| 18 | |
| 19 | // sembrar proyecto en OPFS |
| 20 | await p.goto(BASE + '/', { waitUntil: 'domcontentloaded' }); |
| 21 | await 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 | }); |
| 30 | await p.goto(BASE + '/?test-opfs', { waitUntil: 'domcontentloaded' }); |
| 31 | await p.waitForTimeout(2500); |
| 32 | |
| 33 | // ---- Motor del shell (mismo singleton que la app) ---- |
| 34 | const 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 | |
| 40 | ok('ls lista los ficheros reales del proyecto', /README\.md/.test(await sh('ls')) && /src\//.test(await sh('ls')), await sh('ls')); |
| 41 | ok('cat lee contenido real', (await sh('cat package.json')).includes('"name": "demo"')); |
| 42 | ok('cd + pwd navegan el árbol', (await sh('cd src')) !== undefined && (await sh('pwd')) === '/src'); |
| 43 | ok('ls dentro de src tras cd', /app\.js/.test(await sh('ls')) && /utils\.js/.test(await sh('ls'))); |
| 44 | await sh('cd ..'); |
| 45 | ok('grep recursivo encuentra el símbolo', /utils\.js/.test(await sh('grep -n greet src')), (await sh('grep -n greet src')).replace(/\n/g, ' | ')); |
| 46 | ok('tubería cat | grep', (await sh('cat src/utils.js | grep TODO')).includes('TODO')); |
| 47 | ok('mkdir + echo> escriben en disco', (await sh('mkdir tests && echo hola-terminal > tests/nota.txt'), (await sh('cat tests/nota.txt')).trim()) === 'hola-terminal'); |
| 48 | ok('find -name localiza por patrón', /utils\.js/.test(await sh('find src -name *.js'))); |
| 49 | ok('wc -l cuenta líneas', (await sh('wc -l src/utils.js')).trim() === '2', await sh('wc -l src/utils.js')); |
| 50 | ok('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) |
| 53 | await p.waitForTimeout(400); |
| 54 | ok('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) ---- |
| 57 | const agentOut = await p.evaluate(async () => { |
| 58 | const t = await import('/js/tools/index.js'); |
| 59 | return t.runTool('terminal.run', { command: 'ls src' }); |
| 60 | }); |
| 61 | ok('la elfa puede ejecutar comandos (tool terminal.run)', /app\.js/.test(agentOut), String(agentOut).slice(0, 60)); |
| 62 | |
| 63 | // ---- UI de xterm ---- |
| 64 | await p.click('#act-term'); |
| 65 | await p.waitForTimeout(1500); |
| 66 | ok('el panel del terminal se abre', !(await p.locator('#terminal-panel').getAttribute('hidden').catch(() => 'x'))); |
| 67 | ok('xterm se monta (canvas/rows presentes)', await p.locator('#terminal-host .xterm').count() > 0); |
| 68 | // teclear un comando en el terminal real |
| 69 | await p.locator('#terminal-host').click(); |
| 70 | await p.locator('#terminal-host .xterm-helper-textarea').focus().catch(() => {}); |
| 71 | await p.waitForTimeout(200); |
| 72 | await p.keyboard.type('ls'); |
| 73 | await p.keyboard.press('Enter'); |
| 74 | await p.waitForTimeout(700); |
| 75 | const rows = await p.locator('#terminal-host .xterm-rows').innerText().catch(() => ''); |
| 76 | ok('teclear «ls» en la UI muestra los ficheros', /README\.md/.test(rows) || /package\.json/.test(rows), rows.replace(/\n+/g, ' | ').slice(0, 120)); |
| 77 | await p.screenshot({ path: OUT + '/terminal.png' }); |
| 78 | console.log('captura → terminal.png'); |
| 79 | |
| 80 | console.log(fails ? `\n❌ ${fails} FALLO(S)` : '\n✅ TERMINAL OK — shell real + xterm + tool de la elfa'); |
| 81 | await b.close(); |
| 82 | process.exit(fails ? 1 : 0); |