Elffuss BraidHub

utopia-ia/elffuss-code public

main · 3.3 KiBRawPermalink
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).
5import { chromium } from 'playwright';
6const BASE = process.env.BASE || 'http://localhost:8799';
7let fails = 0; const ok = (n, c, e = '') => { console.log((c ? '✅' : '❌') + ' ' + n + (e ? ' — ' + e : '')); if (!c) fails++; };
8
9const b = await chromium.launch();
10const ctx = await b.newContext({ locale: 'es-ES' }); await ctx.addInitScript(() => { try { localStorage.setItem('elffusscode.model', 'rules'); } catch {} });
11const p = await ctx.newPage();
12await p.goto(BASE + '/?test-opfs', { waitUntil: 'domcontentloaded' }); await p.waitForTimeout(1500);
13
14// ── 1) unidad pura: humanizeStreamPreview nunca deja pasar JSON crudo ──────
15const 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});
32ok('NUNCA se muestra el JSON crudo, en ningún punto del streaming', !trace.sawRawJson);
33ok('mientras el nombre de la tool aún no es legible, muestra el genérico', !!trace.mid, trace.mid);
34ok('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 ──
37const 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});
49ok('en el DOM real, nunca aparece el JSON crudo tampoco', !domTrace.anyRawJson);
50ok('el DOM real termina mostrando la frase humana', domTrace.finalText === '⟐ leyendo drone_hacker/config.py…', domTrace.finalText);
51
52console.log(fails ? `\n❌ ${fails} FALLO(S)` : '\n✅ STREAMING HUMANIZADO OK — el JSON crudo ya no se ve');
53await b.close();
54process.exit(fails ? 1 : 0);