| 1 | // ⛏ Hard Work como un turno MÁS de la conversación: se para con ■ y se guarda. |
| 2 | // |
| 3 | // Antes runHardWork corría por fuera de la cola: el botón nunca pasaba a ■ (no |
| 4 | // había forma de pararlo), la conversación no constaba como ocupada y la |
| 5 | // pregunta y la respuesta vivían solo en el DOM, así que desaparecían al |
| 6 | // cambiar de pestaña o recargar. Y al apagarlo dejaba un placeholder fijo en |
| 7 | // español. |
| 8 | // |
| 9 | // Proveedor de mentira (sin modelo): contesta a las sub-llamadas del map y del |
| 10 | // reduce, lento, y cuenta las llamadas para ver que tras ■ no sale ninguna más. |
| 11 | // |
| 12 | // BASE=http://127.0.0.1:8898 node tests/hardwork-queue.mjs |
| 13 | import { chromium } from 'playwright'; |
| 14 | const BASE = process.env.BASE || 'http://localhost:8799'; |
| 15 | let fails = 0; const ok = (n, c, e = '') => { console.log((c ? '✅' : '❌') + ' ' + n + (e ? ' — ' + e : '')); if (!c) fails++; }; |
| 16 | |
| 17 | const b = await chromium.launch(); |
| 18 | const ctx = await b.newContext({ locale: 'es-ES' }); |
| 19 | await ctx.addInitScript(() => { try { localStorage.setItem('elffusscode.model', 'rules'); } catch {} }); |
| 20 | const p = await ctx.newPage({ viewport: { width: 1400, height: 900 } }); |
| 21 | const errs = []; p.on('pageerror', e => errs.push(e.message.slice(0, 160))); |
| 22 | |
| 23 | const arranca = async () => { |
| 24 | await p.goto(BASE + '/?test-opfs', { waitUntil: 'domcontentloaded' }); |
| 25 | await p.waitForFunction(() => window.elffussClaw?.conv?.getActive?.(), null, { timeout: 15000 }); |
| 26 | }; |
| 27 | // proyecto sembrado en OPFS (?test-opfs lo abre como carpeta del proyecto) |
| 28 | const siembra = (kb) => p.evaluate(async (kb) => { |
| 29 | const root = await navigator.storage.getDirectory(); |
| 30 | const escribe = async (name, txt) => { const w = await (await root.getFileHandle(name, { create: true })).createWritable(); await w.write(txt); await w.close(); }; |
| 31 | const relleno = n => Array.from({ length: n }, (_, i) => `function f${i}() { return ${i}; } // línea de relleno`).join('\n'); |
| 32 | await escribe('app.js', relleno(kb * 20)); |
| 33 | await escribe('util.js', relleno(kb * 20)); |
| 34 | await escribe('README.md', '# Proyecto de prueba\nUsa app.js y util.js.'); |
| 35 | }, kb); |
| 36 | const ponProveedor = (pausa) => p.evaluate((pausa) => { |
| 37 | const dormir = ms => new Promise(r => setTimeout(r, ms)); |
| 38 | window.__hw = { llamadas: 0, trasParar: 0, parado: false, abortadas: 0 }; |
| 39 | window.elffussClaw.conv.setProvider({ name: 'hw-falso', async chat(h, sys, cb, signal) { |
| 40 | window.__hw.llamadas++; |
| 41 | if (window.__hw.parado) window.__hw.trasParar++; |
| 42 | for (let i = 0; i < 10; i++) { |
| 43 | if (signal?.aborted) { window.__hw.abortadas++; throw new DOMException('Parado', 'AbortError'); } |
| 44 | await dormir(pausa / 10); |
| 45 | } |
| 46 | return /sintetizador/.test(sys) ? 'respuesta final de prueba' : 'hallazgo en este trozo'; |
| 47 | } }); |
| 48 | }, pausa); |
| 49 | const hardWork = async (encender) => { |
| 50 | const on = await p.evaluate(() => document.getElementById('btn-hardwork').classList.contains('on')); |
| 51 | if (on === encender) return; |
| 52 | if (await p.locator('#modos-menu').isHidden()) await p.click('#btn-modos'); |
| 53 | await p.click('#btn-hardwork'); |
| 54 | }; |
| 55 | |
| 56 | await arranca(); |
| 57 | await siembra(2); |
| 58 | await ponProveedor(60); |
| 59 | await hardWork(true); |
| 60 | await p.fill('#prompt', '¿qué hace este proyecto?'); |
| 61 | await p.press('#prompt', 'Enter'); |
| 62 | await p.waitForFunction(() => document.getElementById('btn-send').classList.contains('is-stop'), null, { timeout: 5000 }).catch(() => {}); |
| 63 | const durante = await p.evaluate(() => ({ stop: document.getElementById('btn-send').classList.contains('is-stop'), busy: window.elffussClaw.conv.isBusy() })); |
| 64 | ok('1 · mientras trabaja, el botón es ■ y la conversación está ocupada', durante.stop && durante.busy, JSON.stringify(durante)); |
| 65 | await p.waitForFunction(() => !window.elffussClaw.conv.isBusy(), null, { timeout: 30000 }); |
| 66 | const fin = await p.evaluate(() => { |
| 67 | const h = window.elffussClaw.conv.getActive().agent.history; |
| 68 | return { log: document.getElementById('chat-log').innerText, ultimos: h.slice(-2).map(m => m.role + ':' + m.content.slice(0, 60)), id: window.elffussClaw.conv.getActive().id }; |
| 69 | }); |
| 70 | ok('1 · la respuesta aparece en el chat', fin.log.includes('respuesta final de prueba') && fin.log.includes('Hard Work (RLM)'), fin.log.slice(-200)); |
| 71 | ok('1 · pregunta y respuesta quedan en el historial de la conversación', fin.ultimos[0] === 'user:¿qué hace este proyecto?' && fin.ultimos[1]?.startsWith('assistant:respuesta final de prueba'), JSON.stringify(fin.ultimos)); |
| 72 | |
| 73 | // 2 · sobrevive a recargar |
| 74 | await arranca(); |
| 75 | const tras = await p.evaluate(() => ({ id: window.elffussClaw.conv.getActive().id, log: document.getElementById('chat-log').innerText })); |
| 76 | ok('2 · tras recargar, la pregunta y la respuesta siguen en la conversación', tras.id === fin.id && tras.log.includes('¿qué hace este proyecto?') && tras.log.includes('respuesta final de prueba'), tras.log.slice(0, 300)); |
| 77 | |
| 78 | // 3 · ■ lo para: no sale ninguna sub-llamada más y no se apunta respuesta |
| 79 | await siembra(12); |
| 80 | await ponProveedor(400); |
| 81 | await hardWork(true); |
| 82 | await p.fill('#prompt', 'audita todo el proyecto'); |
| 83 | await p.press('#prompt', 'Enter'); |
| 84 | await p.waitForFunction(() => window.__hw.llamadas >= 1, null, { timeout: 8000 }); |
| 85 | await p.waitForTimeout(150); |
| 86 | await p.evaluate(() => { window.__hw.parado = true; }); |
| 87 | await p.click('#btn-send'); |
| 88 | await p.waitForFunction(() => !window.elffussClaw.conv.isBusy(), null, { timeout: 8000 }).catch(() => {}); |
| 89 | await p.waitForTimeout(600); |
| 90 | const r3 = await p.evaluate(() => { |
| 91 | const h = window.elffussClaw.conv.getActive().agent.history; |
| 92 | return { busy: window.elffussClaw.conv.isBusy(), hw: window.__hw, ultimo: h.at(-1)?.role + ':' + h.at(-1)?.content.slice(0, 40), log: document.getElementById('chat-log').innerText.slice(-120) }; |
| 93 | }); |
| 94 | ok('3 · ■ para el Hard Work (deja de estar ocupado)', !r3.busy, JSON.stringify(r3)); |
| 95 | ok('3 · la sub-llamada en curso recibe la parada y no sale ninguna más', r3.hw.abortadas >= 1 && r3.hw.trasParar === 0, JSON.stringify(r3.hw)); |
| 96 | ok('3 · se ve «(Parado.)» y no se apunta respuesta', r3.log.includes('(Parado.)') && r3.ultimo === 'user:audita todo el proyecto', JSON.stringify(r3)); |
| 97 | |
| 98 | // 4 · al apagarlo vuelve el placeholder de la interfaz |
| 99 | await hardWork(false); |
| 100 | const ph = await p.evaluate(() => document.getElementById('prompt').placeholder); |
| 101 | ok('4 · al apagar Hard Work vuelve el placeholder normal', ph === 'Escribe aquí… (@ adjunta un archivo)', ph); |
| 102 | |
| 103 | console.log('\n errores JS:', errs[0] || 'ninguno'); |
| 104 | ok('sin errores de página', errs.length === 0, errs.join(' | ')); |
| 105 | console.log(fails ? `\n❌ ${fails} FALLO(S)` : '\n✅ HARD WORK EN LA COLA: SE PARA Y SE GUARDA'); |
| 106 | await b.close(); |
| 107 | process.exit(fails ? 1 : 0); |