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