| 1 | // End-to-end del bug «ficheros vacíos»: un mensaje del modelo con VARIAS |
| 2 | // code.write debe escribir TODOS los ficheros con su contenido. Inyectamos un |
| 3 | // proveedor falso (sin modelo real) y comprobamos el contenido en OPFS. |
| 4 | import { chromium } from 'playwright'; |
| 5 | import { CHROME_ARGS } from './chrome-args.mjs'; |
| 6 | const BASE = process.env.BASE || 'http://localhost:8799'; |
| 7 | let fails = 0; |
| 8 | const ok = (n, c, e = '') => { console.log((c ? '✅' : '❌') + ' ' + n + (e ? ' — ' + e : '')); if (!c) fails++; }; |
| 9 | |
| 10 | const b = await chromium.launch({ args: ['--enable-unsafe-webgpu', ...CHROME_ARGS] }); |
| 11 | const ctx = await b.newContext(); |
| 12 | await ctx.addInitScript(() => { try { localStorage.setItem('elffusscode.model', 'rules'); } catch {} }); |
| 13 | const p = await ctx.newPage(); |
| 14 | await p.goto(BASE + '/?test-opfs', { waitUntil: 'domcontentloaded' }); |
| 15 | await p.waitForTimeout(2000); |
| 16 | |
| 17 | const events = await p.evaluate(async () => { |
| 18 | const { Agent } = await import('/js/agent.js'); |
| 19 | const msg = `He creado la estructura. Ahora los ficheros: |
| 20 | \`\`\`tool |
| 21 | {"tool":"code.write","args":{"path":"proj/index.html","content":"<!DOCTYPE html>\\n<html lang=\\"en\\"><body><h1>vLLM</h1></body></html>"}} |
| 22 | \`\`\` |
| 23 | El CSS: |
| 24 | \`\`\`tool |
| 25 | {"tool":"code.write","args":{"path":"proj/style.css","content":"body { margin: 0; font-family: system-ui; }"}} |
| 26 | \`\`\` |
| 27 | Y el script: |
| 28 | \`\`\`tool |
| 29 | {"tool":"code.write","args":{"path":"proj/script.js","content":"console.log('vLLM explainer');"}} |
| 30 | \`\`\` |
| 31 | Listo!`; |
| 32 | let n = 0; |
| 33 | const fake = { chat: async () => (n++ === 0 ? msg : 'Proyecto creado ✔') }; |
| 34 | const a = new Agent(fake); |
| 35 | const ev = []; |
| 36 | await a.handle('crea un mini proyecto web que explique vllm', e => ev.push(e.type + (e.tool ? ':' + e.tool : ''))); |
| 37 | return ev; |
| 38 | }); |
| 39 | const writes = events.filter(e => e === 'tool_result:code.write').length; |
| 40 | ok('se ejecutan las 3 code.write del mismo mensaje', writes === 3, `${writes} writes · eventos: ${events.join(',')}`); |
| 41 | |
| 42 | const files = await p.evaluate(async () => { |
| 43 | const o = await navigator.storage.getDirectory(); |
| 44 | const read = async (path) => { let d = o; const parts = path.split('/'); const name = parts.pop(); for (const x of parts) d = await d.getDirectoryHandle(x); return (await (await d.getFileHandle(name)).getFile()).text(); }; |
| 45 | return { idx: await read('proj/index.html').catch(() => ''), css: await read('proj/style.css').catch(() => ''), js: await read('proj/script.js').catch(() => '') }; |
| 46 | }); |
| 47 | ok('index.html NO está vacío', files.idx.includes('<h1>vLLM</h1>'), JSON.stringify(files.idx.slice(0, 40))); |
| 48 | ok('style.css NO está vacío (llaves intactas)', files.css.includes('margin: 0'), JSON.stringify(files.css.slice(0, 40))); |
| 49 | ok('script.js NO está vacío', files.js.includes('vLLM explainer'), JSON.stringify(files.js.slice(0, 40))); |
| 50 | |
| 51 | console.log(fails ? `\n❌ ${fails} FALLO(S)` : '\n✅ MULTI-WRITE OK — los ficheros se crean con contenido'); |
| 52 | await b.close(); |
| 53 | process.exit(fails ? 1 : 0); |