| 1 | // El copiloto (?copilot=1) con un Translator de mentira: una página servida en |
| 2 | // https://translator.elffuss.com por intercepción (nada llega a producción) que |
| 3 | // abre Claw y le manda la conversación como lo hace el de verdad. |
| 4 | // · Avisa de que instala una skill en tus Skills (con «Ver»). |
| 5 | // · Lo que se oye en la llamada sale como transcripción (quién habla), no como |
| 6 | // un mensaje tuyo, y en el chat solo va el consejo (no CONSEJO/ACCION/DATOS |
| 7 | // crudos ni el texto de ayuda de Básico). |
| 8 | // · «Me llamo Ana Ruiz», con mayúscula, rellena el nombre. |
| 9 | // · «⬇ Exportar a Excel (CSV)» descarga de verdad dentro del visor. |
| 10 | // · Una app abierta a mano desde Apps no la tapa el tablero a los 5 s. |
| 11 | // |
| 12 | // BASE=http://localhost:8743 node tests/ux-copiloto.mjs |
| 13 | import { navegador, contexto, ok, hasta, espera, fin, BASE, OUT } from './ux-comun.mjs'; |
| 14 | |
| 15 | const TRANSLATOR = 'https://translator.elffuss.com'; |
| 16 | const STUB = '<!doctype html><html><body>stub translator<script>window.__msgs=[];addEventListener("message",e=>window.__msgs.push({origin:e.origin,data:e.data}));</script></body></html>'; |
| 17 | const b = await navegador(); |
| 18 | const { ctx } = await contexto(b, { |
| 19 | grants: ['apps'], |
| 20 | rutas: async (url, route) => { |
| 21 | if (!url.startsWith(TRANSLATOR + '/')) return false; |
| 22 | await route.fulfill({ status: 200, contentType: 'text/html', body: STUB }); |
| 23 | return true; |
| 24 | }, |
| 25 | }); |
| 26 | const stub = await ctx.newPage(); |
| 27 | await stub.goto(TRANSLATOR + '/copiloto-de-prueba.html'); |
| 28 | const [claw] = await Promise.all([ |
| 29 | ctx.waitForEvent('page'), |
| 30 | stub.evaluate(u => { window.__claw = window.open(u, 'claw'); }, BASE + '/?copilot=1&skill=ficha'), |
| 31 | ]); |
| 32 | const errores = []; |
| 33 | claw.on('pageerror', e => { if (!/allow-same-origin/.test(e.message)) errores.push(e.message); }); |
| 34 | await claw.waitForFunction(() => !!window.elffuss?.agent, null, { timeout: 15000 }); |
| 35 | ok('Claw avisa al Translator de que está listo', await hasta(() => stub.evaluate(() => window.__msgs.some(m => m.data?.kind === 'copilot-ready')))); |
| 36 | const aClaw = msg => stub.evaluate(([m, o]) => window.__claw.postMessage(m, o), [msg, new URL(BASE).origin]); |
| 37 | |
| 38 | await aClaw({ kind: 'copilot-init', skill: 'ficha', sellerLangName: 'español' }); |
| 39 | ok('el tablero «Ficha de cliente» se monta en el visor', await hasta(() => claw.evaluate(() => |
| 40 | (document.getElementById('appframe').srcdoc || '').includes('Ficha de cliente')))); |
| 41 | const aviso = claw.locator('#toasts .toast', { hasText: 'Ficha de cliente' }); |
| 42 | ok('dice que ha instalado la skill en tus Skills, con «Ver»', await hasta(async () => await aviso.count() === 1) |
| 43 | && (await aviso.locator('.toast-accion').innerText()) === 'Ver'); |
| 44 | |
| 45 | await aClaw({ kind: 'transcript', speaker: 'Hablante 2', text: 'Me llamo Ana Ruiz y mi teléfono es 600123456' }); |
| 46 | ok('la línea de la llamada sale como transcripción (con quién habla), no como mensaje tuyo', await hasta(async () => |
| 47 | await claw.locator('#log .msg.transcript', { hasText: 'Hablante 2' }).count() === 1) |
| 48 | && await claw.locator('#log .msg.user', { hasText: 'Ana Ruiz' }).count() === 0); |
| 49 | ok('en el chat va solo el consejo (💡), sin el contrato crudo ni la ayuda de Básico', await hasta(async () => { |
| 50 | const t = await claw.locator('#log').innerText(); |
| 51 | return /💡/.test(t) && !/CONSEJO:|ACCION:|DATOS:/.test(t) && !/Estoy en modo básico/.test(t); |
| 52 | }), (await claw.locator('#log').innerText()).slice(-300)); |
| 53 | const tablero = () => claw.frames().find(f => f !== claw.mainFrame() && f.url() === 'about:srcdoc'); |
| 54 | ok('«Me llamo Ana Ruiz» (con mayúscula) rellena el nombre, y el teléfono también', await hasta(async () => { |
| 55 | const f = tablero(); if (!f) return false; |
| 56 | const t = await f.locator('#slots').innerText().catch(() => ''); |
| 57 | return /Ana Ruiz/.test(t) && /600123456/.test(t); |
| 58 | }), await tablero()?.locator('#slots').innerText().catch(() => '?')); |
| 59 | if (OUT) await claw.screenshot({ path: OUT + '/ux-copiloto.png' }); |
| 60 | |
| 61 | const [descarga] = await Promise.all([ |
| 62 | claw.waitForEvent('download', { timeout: 10000 }).catch(() => null), |
| 63 | tablero().locator('#exp').click(), |
| 64 | ]); |
| 65 | ok('«Exportar a Excel (CSV)» descarga dentro del visor', !!descarga && /\.csv$/.test(descarga.suggestedFilename()), descarga?.suggestedFilename()); |
| 66 | |
| 67 | // ── una app abierta a mano no la tapa el tablero ──────────────────────────── |
| 68 | await claw.evaluate(async () => { |
| 69 | await (await import('/js/db.js')).set('apps', 'reloj', { name: 'reloj', html: '<!doctype html><title>reloj</title><h1>RELOJ-DE-PRUEBA</h1>', created: Date.now() }); |
| 70 | }); |
| 71 | await claw.click('#tabs button[data-tab="apps"]'); |
| 72 | await claw.locator('#panel-apps .card', { hasText: 'reloj' }).locator('button', { hasText: 'Abrir' }).click(); |
| 73 | ok('Apps › Abrir abre la app', await hasta(() => claw.evaluate(() => /RELOJ-DE-PRUEBA/.test(document.getElementById('appframe').srcdoc)))); |
| 74 | await espera(6500); |
| 75 | ok('…y sigue a la vista pasado el vigilante del tablero (5 s)', await claw.evaluate(() => /RELOJ-DE-PRUEBA/.test(document.getElementById('appframe').srcdoc))); |
| 76 | ok('…y el modelo sabe que es la app abierta', await claw.evaluate(async () => (await import('/js/tools/apps.js')).currentApp()) === 'reloj'); |
| 77 | |
| 78 | ok('sin errores de página', errores.length === 0, errores.slice(0, 3).join(' | ')); |
| 79 | await fin(b, 'COPILOTO: transcripción aparte, solo el consejo, nombre con mayúscula, CSV que descarga, Abrir que se respeta'); |