| 1 | // Enter mientras la elfa trabaja ENCOLA el mensaje; solo el botón ■ para. |
| 2 | // |
| 3 | // Antes Enter con la elfa trabajando paraba el trabajo en curso y el texto se |
| 4 | // quedaba sin enviar en la caja, aunque conversations.js acepta y encola |
| 5 | // mensajes. Ahora: |
| 6 | // 1. Enter con texto mientras trabaja: no para, la caja se vacía y el mensaje |
| 7 | // aparece «en cola» (⏳) al final del chat. |
| 8 | // 2. La respuesta del turno en curso se pinta ANTES del mensaje encolado. |
| 9 | // 3. Cuando le toca, deja de estar en cola y el modelo lo recibe. |
| 10 | // 4. El botón ■ sí para, y lo que esperaba en cola se marca descartado. |
| 11 | // 5. Con OTRA conversación generando, lo que mandas aquí espera (⏳) también |
| 12 | // el primero, y cada burbuja deja de estar en cola cuando empieza SU |
| 13 | // turno: antes la marca se decidía por conversación y se le quitaba a la |
| 14 | // primera en cola, y la respuesta al primero salía debajo del segundo. |
| 15 | // |
| 16 | // BASE=http://127.0.0.1:8898 node tests/enterqueue.mjs |
| 17 | import { chromium } from 'playwright'; |
| 18 | const BASE = process.env.BASE || 'http://localhost:8799'; |
| 19 | let fails = 0; const ok = (n, c, e = '') => { console.log((c ? '✅' : '❌') + ' ' + n + (e ? ' — ' + e : '')); if (!c) fails++; }; |
| 20 | |
| 21 | const b = await chromium.launch(); |
| 22 | const ctx = await b.newContext({ locale: 'es-ES' }); |
| 23 | await ctx.addInitScript(() => { try { localStorage.setItem('elffusscode.model', 'rules'); } catch {} }); |
| 24 | const p = await ctx.newPage({ viewport: { width: 1400, height: 900 } }); |
| 25 | const errs = []; p.on('pageerror', e => errs.push(e.message.slice(0, 160))); |
| 26 | await p.goto(BASE + '/?test-opfs', { waitUntil: 'domcontentloaded' }); |
| 27 | await p.waitForFunction(() => window.elffussClaw?.conv?.getActive?.(), null, { timeout: 15000 }); |
| 28 | |
| 29 | // proveedor lento que apunta qué recibe y cuántas veces lo paran |
| 30 | await p.evaluate(() => { |
| 31 | const dormir = ms => new Promise(r => setTimeout(r, ms)); |
| 32 | window.__prov = { llamadas: [], abortadas: 0, async chat(h, s, cb, signal) { |
| 33 | const q = [...h].reverse().find(m => m.role === 'user')?.content || ''; |
| 34 | this.llamadas.push(q); |
| 35 | const n = this.pasosPara?.(q) ?? 40; |
| 36 | for (let i = 0; i < n; i++) { |
| 37 | if (signal?.aborted) { this.abortadas++; throw new DOMException('Parado', 'AbortError'); } |
| 38 | cb('·'); await dormir(25); |
| 39 | } |
| 40 | return 'respuesta a ' + q; |
| 41 | } }; |
| 42 | window.elffussClaw.conv.setProvider(window.__prov); |
| 43 | }); |
| 44 | const estado = () => p.evaluate(() => ({ |
| 45 | busy: window.elffussClaw.conv.isBusy(), |
| 46 | caja: document.getElementById('prompt').value, |
| 47 | llamadas: window.__prov.llamadas.slice(), abortadas: window.__prov.abortadas, |
| 48 | enCola: [...document.querySelectorAll('#chat-log > .msg.user.queued')].map(n => n.textContent), |
| 49 | descartados: [...document.querySelectorAll('#chat-log > .msg.user.cancelled')].map(n => n.textContent), |
| 50 | orden: [...document.querySelectorAll('#chat-log > .msg')].map(n => (n.classList.contains('user') ? 'U:' : n.classList.contains('assistant') ? 'A:' : '?:') + n.textContent.trim().slice(0, 30)), |
| 51 | })); |
| 52 | |
| 53 | await p.fill('#prompt', 'primero'); |
| 54 | await p.press('#prompt', 'Enter'); |
| 55 | await p.waitForFunction(() => window.__prov.llamadas.length === 1, null, { timeout: 5000 }); |
| 56 | await p.fill('#prompt', 'segundo'); |
| 57 | await p.press('#prompt', 'Enter'); |
| 58 | await p.waitForTimeout(150); |
| 59 | const e1 = await estado(); |
| 60 | ok('1 · Enter con la elfa trabajando NO para el turno en curso', e1.busy && e1.abortadas === 0, JSON.stringify(e1)); |
| 61 | ok('1 · la caja se vacía (el mensaje salió)', e1.caja === '', JSON.stringify(e1.caja)); |
| 62 | ok('1 · el mensaje aparece en cola (⏳)', e1.enCola.length === 1 && e1.enCola[0] === 'segundo', JSON.stringify(e1.enCola)); |
| 63 | |
| 64 | await p.waitForFunction(() => window.__prov.llamadas.length === 2, null, { timeout: 8000 }); |
| 65 | await p.waitForTimeout(100); |
| 66 | const e2 = await estado(); |
| 67 | ok('3 · cuando le toca, el modelo recibe el mensaje encolado', e2.llamadas[1] === 'segundo', JSON.stringify(e2.llamadas)); |
| 68 | ok('3 · …y deja de estar en cola', e2.enCola.length === 0, JSON.stringify(e2.enCola)); |
| 69 | const iResp = e2.orden.findIndex(x => x.startsWith('A:respuesta a primero')); |
| 70 | const iSeg = e2.orden.findIndex(x => x === 'U:segundo'); |
| 71 | ok('2 · la respuesta al primero va ANTES del segundo mensaje', iResp >= 0 && iSeg > iResp, JSON.stringify(e2.orden)); |
| 72 | await p.waitForFunction(() => !window.elffussClaw.conv.isBusy(), null, { timeout: 8000 }); |
| 73 | |
| 74 | // 4 · ■ para, y lo encolado queda descartado |
| 75 | await p.fill('#prompt', 'tercero'); |
| 76 | await p.press('#prompt', 'Enter'); |
| 77 | await p.waitForFunction(() => window.__prov.llamadas.length === 3, null, { timeout: 5000 }); |
| 78 | await p.fill('#prompt', 'cuarto'); |
| 79 | await p.press('#prompt', 'Enter'); |
| 80 | await p.waitForTimeout(100); |
| 81 | await p.click('#btn-send'); |
| 82 | await p.waitForFunction(() => !window.elffussClaw.conv.isBusy(), null, { timeout: 8000 }); |
| 83 | const e4 = await estado(); |
| 84 | ok('4 · el botón ■ sí para el turno', e4.abortadas === 1, JSON.stringify(e4)); |
| 85 | ok('4 · lo que esperaba en cola no se manda y se marca descartado', !e4.llamadas.includes('cuarto') && e4.descartados.includes('cuarto') && e4.enCola.length === 0, JSON.stringify(e4)); |
| 86 | ok('4 · «(Parado.)» queda encima del descartado', e4.orden.indexOf('A:(Parado.)') >= 0 && e4.orden.indexOf('A:(Parado.)') < e4.orden.indexOf('U:cuarto'), JSON.stringify(e4.orden)); |
| 87 | |
| 88 | // 5 · otra conversación tiene el modelo |
| 89 | await p.evaluate(async () => { |
| 90 | const conv = window.elffussClaw.conv; |
| 91 | window.__prov.pasosPara = q => q === 'B larga' ? 120 : 30; // B genera un buen rato |
| 92 | const A = conv.create(); |
| 93 | const B = conv.create(); |
| 94 | conv.switchTo(A.id); // en pantalla, A |
| 95 | conv.send(B.id, 'B larga'); |
| 96 | }); |
| 97 | await p.waitForFunction(() => window.__prov.llamadas.includes('B larga'), null, { timeout: 5000 }); |
| 98 | await p.fill('#prompt', 'A-uno'); |
| 99 | await p.press('#prompt', 'Enter'); |
| 100 | await p.fill('#prompt', 'A-dos'); |
| 101 | await p.press('#prompt', 'Enter'); |
| 102 | await p.waitForTimeout(150); |
| 103 | const pensando = () => p.evaluate(() => document.querySelectorAll('#chat-log > .msg.thinking').length); |
| 104 | const e5a = { ...(await estado()), pensando: await pensando() }; |
| 105 | ok('5 · con B generando, el PRIMER mensaje de A también espera (⏳), y el segundo', JSON.stringify(e5a.enCola) === '["A-uno","A-dos"]' && e5a.pensando === 0, JSON.stringify({ enCola: e5a.enCola, pensando: e5a.pensando })); |
| 106 | await p.waitForFunction(() => window.__prov.llamadas.includes('A-uno'), null, { timeout: 10000 }); |
| 107 | await p.waitForTimeout(100); |
| 108 | const e5b = await estado(); |
| 109 | const ordenA = o => o.filter(x => /^U:A-|^A:respuesta a A-|^\?:/.test(x)).map(x => x.startsWith('?:') ? 'pensando' : x); |
| 110 | ok('5 · al empezar A-uno solo él deja de estar en cola; A-dos sigue esperando', JSON.stringify(e5b.enCola) === '["A-dos"]', JSON.stringify(e5b.enCola)); |
| 111 | ok('5 · …y «pensando» queda entre los dos', JSON.stringify(ordenA(e5b.orden)) === '["U:A-uno","pensando","U:A-dos"]', JSON.stringify(ordenA(e5b.orden))); |
| 112 | await p.waitForFunction(() => !window.elffussClaw.conv.isBusy(), null, { timeout: 15000 }); |
| 113 | const e5c = await estado(); |
| 114 | ok('5 · cada respuesta queda debajo de SU mensaje', JSON.stringify(ordenA(e5c.orden)) === '["U:A-uno","A:respuesta a A-uno","U:A-dos","A:respuesta a A-dos"]', JSON.stringify(ordenA(e5c.orden))); |
| 115 | |
| 116 | console.log('\n errores JS:', errs[0] || 'ninguno'); |
| 117 | ok('sin errores de página', errs.length === 0, errs.join(' | ')); |
| 118 | console.log(fails ? `\n❌ ${fails} FALLO(S)` : '\n✅ ENTER ENCOLA, ■ PARA'); |
| 119 | await b.close(); |
| 120 | process.exit(fails ? 1 : 0); |