| 1 | // stop.mjs con el IDE abierto: el botón de parar del agente para de verdad |
| 2 | // también con un proyecto abierto en el editor (y, en el modo extensiones de |
| 3 | // run-modes.sh, con el recinto de extensiones corriendo al lado, que stop.mjs |
| 4 | // nunca llega a arrancar porque se queda en la portada). |
| 5 | // |
| 6 | // BASE=http://127.0.0.1:<port> node tests/stop_ide.mjs |
| 7 | import { chromium } from 'playwright'; |
| 8 | const BASE = process.env.BASE || 'http://localhost:8799'; |
| 9 | const b = await chromium.launch(); |
| 10 | const ctx = await b.newContext(); |
| 11 | // Modo Básico: sin esto, main.js se pone a cargar el modelo local por defecto |
| 12 | await ctx.addInitScript(() => { try { localStorage.setItem('elffusscode.model', 'rules'); } catch {} }); |
| 13 | const p = await ctx.newPage(); |
| 14 | await p.goto(BASE + '/', { waitUntil: 'domcontentloaded' }); |
| 15 | await p.evaluate(async () => { |
| 16 | const o = await navigator.storage.getDirectory(); |
| 17 | const f = await o.getFileHandle('app.js', { create: true }); |
| 18 | const s = await f.createWritable(); await s.write('function a() { return 1; }\n'); await s.close(); |
| 19 | }); |
| 20 | await p.goto(BASE + '/?test-opfs', { waitUntil: 'domcontentloaded' }); |
| 21 | await p.waitForFunction(() => !!window.monaco?.editor?.getEditors?.().length, null, { timeout: 60000 }); |
| 22 | await p.evaluate(async () => { await (await import('/js/ide.js')).openFile('app.js'); }); |
| 23 | const ide = await p.evaluate(() => !!document.querySelector('#editor .monaco-editor') && window.monaco.editor.getEditors()[0].getModel().getValue().includes('function a')); |
| 24 | const r = await p.evaluate(async () => { |
| 25 | const { Agent } = await import('/js/agent.js?v=' + Date.now()); |
| 26 | const eventos = []; |
| 27 | let llamadas = 0; |
| 28 | // proveedor que SIEMPRE pide una tool y respeta la señal (espera y comprueba) |
| 29 | const lento = { chat: async (h, s, cb, signal) => { |
| 30 | llamadas++; |
| 31 | for (let i = 0; i < 20; i++) { if (signal?.aborted) break; await new Promise(r => setTimeout(r, 50)); } |
| 32 | return '```tool\n{"tool":"code.tree","args":{}}\n```'; |
| 33 | }}; |
| 34 | const ag = new Agent(lento); |
| 35 | const run = ag.handle('haz algo largo', ev => eventos.push(ev.type)); |
| 36 | await new Promise(r => setTimeout(r, 300)); // deja arrancar |
| 37 | const llamadasAntes = llamadas; |
| 38 | ag.stop(); // ← PARAR a mitad |
| 39 | await run; // debe terminar pronto, no agotar MAX_STEPS |
| 40 | return { eventos: [...new Set(eventos)], llamadasAntes, llamadasTotal: llamadas, abortado: eventos.includes('aborted') }; |
| 41 | }); |
| 42 | console.log('parada del agente con el IDE abierto:'); |
| 43 | console.log(' eventos:', r.eventos.join(', ')); |
| 44 | console.log(' llamadas al modelo antes de parar:', r.llamadasAntes, '· total tras parar:', r.llamadasTotal); |
| 45 | const ok = ide && r.abortado && r.llamadasTotal <= r.llamadasAntes + 1; |
| 46 | console.log(ide ? '✅ el IDE está abierto con el proyecto' : '❌ el IDE no se abrió'); |
| 47 | console.log(ok ? '✅ para de verdad: emite «aborted» y no sigue el bucle' |
| 48 | : '❌ no paró bien'); |
| 49 | await b.close(); |
| 50 | process.exit(ok ? 0 : 1); |