| 1 | // Bug real visto por el usuario: la burbuja «Elffuss escribe» enseñaba el JSON |
| 2 | // crudo del tool-call según iba llegando en streaming (```tool\n{"tool":...). |
| 3 | // Debe detectar la cabecera y mostrar una frase humana en su lugar, NUNCA el |
| 4 | // JSON — reproduce el caso exacto de la captura (prosa → code.read). |
| 5 | import { chromium } from 'playwright'; |
| 6 | const BASE = process.env.BASE || 'http://localhost:8799'; |
| 7 | let fails = 0; const ok = (n, c, e = '') => { console.log((c ? '✅' : '❌') + ' ' + n + (e ? ' — ' + e : '')); if (!c) fails++; }; |
| 8 | |
| 9 | const b = await chromium.launch(); |
| 10 | const ctx = await b.newContext({ locale: 'es-ES' }); await ctx.addInitScript(() => { try { localStorage.setItem('elffusscode.model', 'rules'); } catch {} }); |
| 11 | const p = await ctx.newPage(); |
| 12 | await p.goto(BASE + '/?test-opfs', { waitUntil: 'domcontentloaded' }); await p.waitForTimeout(1500); |
| 13 | |
| 14 | // ── 1) unidad pura: humanizeStreamPreview nunca deja pasar JSON crudo ────── |
| 15 | const trace = await p.evaluate(async () => { |
| 16 | const { humanizeStreamPreview } = await import('/js/humanize.js'); |
| 17 | // el mensaje real de la captura: prosa que termina en una frase cortada y |
| 18 | // LUEGO el fence de tool-call — streameado carácter a carácter, como de verdad |
| 19 | const full = 'The project structure is now complete for a basic drone hacking simulation, using the `Drone.inject_payload` and `Drone.take_over` methods.\n\n' + |
| 20 | '```tool\n{"tool": "code.read", "args": {"path": "drone_hacker/config.py"}}\n```'; |
| 21 | let buf = '', sawRawJson = false; |
| 22 | const previews = []; |
| 23 | for (const ch of full) { |
| 24 | buf += ch; |
| 25 | const preview = humanizeStreamPreview(buf); |
| 26 | previews.push(preview); |
| 27 | const shown = preview ? '⟐ ' + preview : buf.slice(-200); |
| 28 | if (/\{"tool"|```tool|"path"\s*:/.test(shown)) sawRawJson = true; |
| 29 | } |
| 30 | return { sawRawJson, last: previews.at(-1), mid: previews.find(x => x === 'preparando una acción…') }; |
| 31 | }); |
| 32 | ok('NUNCA se muestra el JSON crudo, en ningún punto del streaming', !trace.sawRawJson); |
| 33 | ok('mientras el nombre de la tool aún no es legible, muestra el genérico', !!trace.mid, trace.mid); |
| 34 | ok('al final, la frase humana correcta («leyendo drone_hacker/config.py…»)', trace.last === 'leyendo drone_hacker/config.py…', trace.last); |
| 35 | |
| 36 | // ── 2) integración DOM real: thinkingBubble().tick() con el mismo streaming ── |
| 37 | const domTrace = await p.evaluate(async () => { |
| 38 | const m = await import('/js/main.js'); |
| 39 | const th = m.thinkingBubble(); |
| 40 | const full = 'The project structure is now complete for a basic drone hacking simulation, using the `Drone.inject_payload` and `Drone.take_over` methods.\n\n' + |
| 41 | '```tool\n{"tool": "code.read", "args": {"path": "drone_hacker/config.py"}}\n```'; |
| 42 | const seen = []; |
| 43 | for (const ch of full) { th.tick(ch); seen.push(document.querySelector('.msg.thinking .gen').textContent); } |
| 44 | const finalText = seen.at(-1); |
| 45 | const anyRawJson = seen.some(s => /\{"tool"|```tool/.test(s)); |
| 46 | th.remove(); |
| 47 | return { finalText, anyRawJson }; |
| 48 | }); |
| 49 | ok('en el DOM real, nunca aparece el JSON crudo tampoco', !domTrace.anyRawJson); |
| 50 | ok('el DOM real termina mostrando la frase humana', domTrace.finalText === '⟐ leyendo drone_hacker/config.py…', domTrace.finalText); |
| 51 | |
| 52 | console.log(fails ? `\n❌ ${fails} FALLO(S)` : '\n✅ STREAMING HUMANIZADO OK — el JSON crudo ya no se ve'); |
| 53 | await b.close(); |
| 54 | process.exit(fails ? 1 : 0); |