| 1 | // code.edit invocado por el AGENTE de verdad (tool-calling real, no import directo) |
| 2 | import { chromium } from 'playwright'; |
| 3 | const BASE = process.env.BASE || 'http://localhost:8799'; |
| 4 | let fails = 0; const ok = (n, c, e = '') => { console.log((c ? '✅' : '❌') + ' ' + n + (e ? ' — ' + e : '')); if (!c) fails++; }; |
| 5 | |
| 6 | const b = await chromium.launch(); |
| 7 | const ctx = await b.newContext(); |
| 8 | await ctx.addInitScript(() => { try { localStorage.setItem('elffusscode.model', 'rules'); } catch {} }); |
| 9 | const p = await ctx.newPage(); |
| 10 | await p.goto(BASE + '/', { waitUntil: 'domcontentloaded' }); |
| 11 | await p.evaluate(async () => { |
| 12 | const o = await navigator.storage.getDirectory(); |
| 13 | const f = await o.getFileHandle('config.py', { create: true }); |
| 14 | const s = await f.createWritable(); |
| 15 | await s.write('DEBUG = False\nMAX_RETRIES = 3\n'); |
| 16 | await s.close(); |
| 17 | }); |
| 18 | await p.goto(BASE + '/?test-opfs', { waitUntil: 'domcontentloaded' }); await p.waitForTimeout(1500); |
| 19 | |
| 20 | await p.evaluate(() => { |
| 21 | window.elffussClaw.conv.getActive().agent.provider = { |
| 22 | chat: async (h, s, cb) => { |
| 23 | const res = h.find(m => m.role === 'user' && m.content.startsWith('[resultado code.edit]')); |
| 24 | if (!res) return '```tool\n{"tool":"code.edit","args":{"path":"config.py","search":"DEBUG = False","replace":"DEBUG = True"}}\n```'; |
| 25 | return 'Hecho: ' + res.content; |
| 26 | }, |
| 27 | }; |
| 28 | }); |
| 29 | await p.fill('#prompt', 'activa el modo debug en config.py'); |
| 30 | await p.press('#prompt', 'Enter'); |
| 31 | await p.waitForTimeout(800); |
| 32 | |
| 33 | const content = await p.evaluate(async () => (await import('/js/tools/code.js')).read({ path: 'config.py' })); |
| 34 | ok('el agente aplicó la edición parcial de verdad vía tool-calling', content.includes('DEBUG = True')); |
| 35 | ok('el resto del fichero se conservó intacto', content.includes('MAX_RETRIES = 3')); |
| 36 | const chatLog = await p.locator('#chat-log').innerText(); |
| 37 | ok('el chat muestra la tool-call code.edit', /code\.edit/.test(chatLog)); |
| 38 | |
| 39 | console.log(fails ? `\n❌ ${fails} FALLO(S)` : '\n✅ code.edit vía tool-calling real del agente OK'); |
| 40 | await b.close(); |
| 41 | process.exit(fails ? 1 : 0); |