Elffuss BraidHub

utopia-ia/elffuss-code public

main · 6.0 KiBRawPermalink
1// Conversaciones: varias pestañas en paralelo + Historial (reemplaza el
2// viejo botón "Nueva/eliminar" que borraba todo sin dejar rastro).
3import { chromium } from 'playwright';
4const OUT = (process.env.SCRATCH || '/tmp/elffuss-test');
5const BASE = process.env.BASE || 'http://localhost:8799';
6let fails = 0; const ok = (n, c, e = '') => { console.log((c ? '✅' : '❌') + ' ' + n + (e ? ' — ' + e : '')); if (!c) fails++; };
7
8const b = await chromium.launch();
9const ctx = await b.newContext();
10await 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
14ok('arranca con exactamente 1 pestaña', await p.locator('#conv-tabs .tab').count() === 1);
15
16// tab 1: manda un mensaje
17await p.fill('#prompt', 'mensaje uno');
18await p.press('#prompt', 'Enter');
19await p.waitForTimeout(400);
20ok('mensaje uno aparece en el chat', (await p.locator('#chat-log').innerText()).includes('mensaje uno'));
21
22// + nueva pestaña
23await p.click('#conv-tabs .tab-add');
24await p.waitForTimeout(200);
25ok('ahora hay 2 pestañas', await p.locator('#conv-tabs .tab').count() === 2);
26ok('la pestaña nueva está vacía (no ve el mensaje de la 1)', !(await p.locator('#chat-log').innerText()).includes('mensaje uno'));
27
28// tab 2: manda otro mensaje
29await p.fill('#prompt', 'mensaje dos');
30await p.press('#prompt', 'Enter');
31await p.waitForTimeout(400);
32ok('mensaje dos aparece en la pestaña 2', (await p.locator('#chat-log').innerText()).includes('mensaje dos'));
33
34// volver a la pestaña 1: debe ver SOLO su propio mensaje
35await p.click('#conv-tabs .tab:not(.active)');
36await p.waitForTimeout(200);
37const log1 = await p.locator('#chat-log').innerText();
38ok('al volver a la pestaña 1, ve «mensaje uno»', log1.includes('mensaje uno'));
39ok('la pestaña 1 NO ve «mensaje dos» (historiales independientes)', !log1.includes('mensaje dos'));
40
41// volver a la 2: debe ver SOLO su mensaje
42await p.click('#conv-tabs .tab:not(.active)');
43await p.waitForTimeout(200);
44const log2 = await p.locator('#chat-log').innerText();
45ok('al volver a la pestaña 2, ve «mensaje dos»', log2.includes('mensaje dos'));
46ok('la pestaña 2 NO ve «mensaje uno»', !log2.includes('mensaje uno'));
47
48// ---- procesamiento en paralelo: mandar en una mientras la otra piensa ----
49await p.evaluate(() => {
50 window.elffussClaw.conv.setProvider({
51 chat: async (h, s, onToken) => { await new Promise(r => setTimeout(r, 1500)); onToken('lista'); return 'lista'; },
52 });
53});
54// estamos en la pestaña 2; mandamos ahí y sin esperar cambiamos a la 1 y mandamos también
55await p.fill('#prompt', 'lenta dos');
56await p.press('#prompt', 'Enter');
57await p.waitForTimeout(150);
58await p.click('#conv-tabs .tab:not(.active)'); // a la 1
59await p.waitForTimeout(150);
60ok('la pestaña 2 (en 2º plano, procesando) se marca ocupada', await p.locator('#conv-tabs .tab.tab-busy').count() >= 1);
61await p.fill('#prompt', 'lenta uno');
62await p.press('#prompt', 'Enter');
63await p.waitForTimeout(200);
64ok('la pestaña activa (1) también entra en cola sin bloquearse por la 2', (await p.locator('#chat-log').innerText()).includes('lenta uno'));
65await p.waitForTimeout(3500); // suficiente para que el cerrojo de inferencia procese ambas
66const log1b = await p.locator('#chat-log').innerText();
67ok('la pestaña 1 recibió su respuesta real («lista»)', /lista/.test(log1b));
68await p.click('#conv-tabs .tab:not(.active)');
69await p.waitForTimeout(200);
70const log2b = await p.locator('#chat-log').innerText();
71ok('la pestaña 2 (que procesaba en 2º plano) también recibió su respuesta', /lista/.test(log2b));
72ok('ninguna pestaña se queda "ocupada" para siempre', await p.locator('#conv-tabs .tab.tab-busy').count() === 0);
73
74// ---- cerrar pestaña NO borra: sigue en el Historial ----
75const tabCountBeforeClose = await p.locator('#conv-tabs .tab').count();
76const activeTitleBeforeClose = await p.locator('#conv-tabs .tab.active .tab-name').innerText();
77await p.locator('#conv-tabs .tab.active .tab-x').click(); // su ✕ es un botón .tab-x
78await p.waitForTimeout(200);
79ok('cerrar la pestaña activa la quita de la vista', await p.locator('#conv-tabs .tab').count() === tabCountBeforeClose - 1);
80
81await p.click('#btn-history');
82await p.waitForTimeout(200);
83const hpRows = await p.locator('.hp-row .hp-title').allInnerTexts();
84ok('el Historial lista AMBAS conversaciones (la cerrada no se perdió)', hpRows.length === 2, hpRows.join(' | '));
85
86// reabrir la que se cerró (identificada por título, no la que ya está abierta)
87await p.click(`.hp-row:has-text("${activeTitleBeforeClose}")`);
88await p.waitForTimeout(300);
89ok('reabrir desde el Historial la vuelve a poner como pestaña', await p.locator('#conv-tabs .tab').count() === tabCountBeforeClose);
90
91// eliminar de verdad desde el Historial (la OTRA conversación, no la activa)
92await p.click('#btn-history');
93await p.waitForTimeout(200);
94await p.evaluate(() => window.confirm = () => true); // acepta el confirm() nativo
95await p.click(`.hp-row:not(:has-text("${activeTitleBeforeClose}")) .hp-del`);
96await p.waitForTimeout(300);
97const hpRowsAfter = await p.locator('.hp-row .hp-title').count();
98ok('eliminar desde el Historial SÍ borra de verdad', hpRowsAfter === 1);
99
100await p.screenshot({ path: OUT + '/conv_tabs.png' });
101
102// ---- persistencia real tras recargar ----
103await p.click('body'); // cierra paneles abiertos
104await p.goto(BASE + '/?test-opfs', { waitUntil: 'domcontentloaded' }); await p.waitForTimeout(1500);
105const tabsAfterReload = await p.locator('#conv-tabs .tab').count();
106ok('tras recargar, las pestañas abiertas se restauran', tabsAfterReload >= 1, String(tabsAfterReload));
107const logAfterReload = await p.locator('#chat-log').innerText();
108ok('el contenido de la conversación activa sobrevive a recargar', /mensaje (uno|dos)|lenta (uno|dos)/.test(logAfterReload), logAfterReload.slice(0, 80));
109
110console.log(fails ? `\n❌ ${fails} FALLO(S)` : '\n✅ CONVERSACIONES EN PARALELO + HISTORIAL OK');
111await b.close();
112process.exit(fails ? 1 : 0);