| 1 | // Teléfono y ventanas estrechas. |
| 2 | // · A 390 px, en la vista: la página no se desplaza de lado (las pestañas se |
| 3 | // desplazan dentro) y «💬 Chat» se ve entero para volver. |
| 4 | // · En el chat, el botón de vista no tapa la fila de chips; su texto y su |
| 5 | // nombre van en el idioma de la página. La caja de progreso de una descarga |
| 6 | // no tapa la caja de texto. |
| 7 | // · La puerta de móvil sale en un teléfono, no en una ventana estrecha con |
| 8 | // ratón; si la descarga falla, la tarjeta se queda con el error y |
| 9 | // «Reintentar». |
| 10 | // · Los avisos no tapan ■, ⛏, / ni ⇄ (van encima, también de la caja de |
| 11 | // progreso): tocar ■ con un aviso a la vista para el turno. Un aviso tocado |
| 12 | // con el dedo se va solo igualmente (el toque no lo para para siempre). |
| 13 | // · ⛏ se llama «Hard Work» también a 390 px, donde solo se ve el pico. |
| 14 | // · Con un cerebro EXTERNO guardado no sale la puerta (no descarga nada) y el |
| 15 | // cerebro carga; si no carga, la puerta sale para el local, con su tamaño. |
| 16 | // |
| 17 | // BASE=http://localhost:8743 node tests/ux-movil.mjs |
| 18 | import { createServer } from 'node:http'; |
| 19 | import { navegador, contexto, pagina, ok, hasta, espera, fin, OUT } from './ux-comun.mjs'; |
| 20 | |
| 21 | const b = await navegador(); |
| 22 | const telefono = { viewport: { width: 390, height: 844 }, isMobile: true, hasTouch: true }; |
| 23 | const solapan = (a, c) => !(a.x + a.width <= c.x || c.x + c.width <= a.x || a.y + a.height <= c.y || c.y + c.height <= a.y); |
| 24 | |
| 25 | // Un cerebro de prueba (compatible con OpenAI) que escribe DESPACIO y apunta si |
| 26 | // el navegador le cortó la respuesta. |
| 27 | const peticiones = []; |
| 28 | const servidor = createServer((req, res) => { |
| 29 | const cors = { 'Access-Control-Allow-Origin': '*', 'Access-Control-Allow-Headers': 'content-type, authorization', 'Access-Control-Allow-Methods': 'POST, OPTIONS' }; |
| 30 | if (req.method === 'OPTIONS') { res.writeHead(204, cors); res.end(); return; } |
| 31 | req.resume(); |
| 32 | req.on('end', () => { |
| 33 | const r = { cortada: false, terminada: false, enviados: 0 }; |
| 34 | peticiones.push(r); |
| 35 | res.writeHead(200, { ...cors, 'Content-Type': 'text/event-stream', 'Cache-Control': 'no-cache' }); |
| 36 | const t = setInterval(() => { |
| 37 | if (r.enviados >= 2000) { clearInterval(t); res.write('data: [DONE]\n\n'); res.end(); r.terminada = true; return; } |
| 38 | r.enviados++; |
| 39 | res.write(`data: ${JSON.stringify({ choices: [{ delta: { content: 'palabra ' } }] })}\n\n`); |
| 40 | }, 40); |
| 41 | res.on('close', () => { clearInterval(t); if (!r.terminada) r.cortada = true; }); |
| 42 | }); |
| 43 | }); |
| 44 | await new Promise(r => servidor.listen(0, '127.0.0.1', r)); |
| 45 | const OLLAMA = JSON.stringify({ ollama: { enabled: true, baseURL: `http://localhost:${servidor.address().port}/v1`, model: 'falso', apiKey: 'ollama' } }); |
| 46 | |
| 47 | // ── avisos y botones del compositor en un teléfono ────────────────────────── |
| 48 | { |
| 49 | const { ctx } = await contexto(b, { ...telefono, model: 'ext:ollama', extraInit: { 'elffuss.providers': OLLAMA, 'elffuss.movil.ok': '1' } }); |
| 50 | const { p, errores } = await pagina(ctx); |
| 51 | ok('teléfono: el cerebro de prueba carga', await hasta(() => p.evaluate(() => document.getElementById('model-status').classList.contains('on')))); |
| 52 | await p.evaluate(async () => (await import('/js/ui.js')).toast('Modelo IA listo · externo', 60000)); |
| 53 | const tapados = () => p.evaluate(() => Object.fromEntries(['send', 'btn-hardwork', 'btn-slash', 'btn-flip', 'prompt'].map(id => { |
| 54 | const e = document.getElementById(id).getBoundingClientRect(); |
| 55 | const h = document.elementFromPoint(e.left + e.width / 2, e.top + e.height / 2); |
| 56 | return [id, !!h?.closest('#toasts')]; |
| 57 | }))); |
| 58 | const t1 = await tapados(); |
| 59 | ok('con un aviso a la vista, ni ■/➤, ni ⛏, ni /, ni ⇄, ni la caja de texto quedan debajo', !Object.values(t1).some(Boolean), JSON.stringify(t1)); |
| 60 | await p.evaluate(async () => (await import('/js/ui.js')).modelProgress('Descargando el cerebro · 42 %')); |
| 61 | await espera(100); |
| 62 | const cajas = await p.evaluate(() => { |
| 63 | const r = s => { const e = document.querySelector(s).getBoundingClientRect(); return { x: e.x, y: e.y, width: e.width, height: e.height }; }; |
| 64 | return { aviso: r('#toasts .toast'), progreso: r('#model-progress') }; |
| 65 | }); |
| 66 | ok('…y con la caja de progreso a la vista, el aviso va encima de ella, sin taparla', !solapan(cajas.aviso, cajas.progreso), JSON.stringify(cajas)); |
| 67 | await p.evaluate(async () => (await import('/js/ui.js')).modelProgress(null)); |
| 68 | // ■ con el aviso a la vista: el toque llega a ■ y el turno se para. |
| 69 | await p.fill('#prompt', 'escribe un poema muy largo'); |
| 70 | await p.press('#prompt', 'Enter'); |
| 71 | await hasta(() => peticiones.length > 0 && peticiones.at(-1).enviados > 3); |
| 72 | const caja = await p.locator('#send').boundingBox(); |
| 73 | await p.touchscreen.tap(caja.x + caja.width / 2, caja.y + caja.height / 2); |
| 74 | ok('tocar ■ con un aviso a la vista para el turno (el servidor ve la respuesta cortada)', await hasta(() => peticiones.at(-1).cortada), JSON.stringify(peticiones.at(-1))); |
| 75 | // Un aviso tocado con el dedo no se queda para siempre. |
| 76 | await p.evaluate(async () => { document.querySelectorAll('#toasts .toast').forEach(t => t.remove()); (await import('/js/ui.js')).toast('aviso corto', 1500); }); |
| 77 | const aviso = p.locator('#toasts .toast', { hasText: 'aviso corto' }); |
| 78 | const ct = await aviso.locator('.toast-txt').boundingBox(); |
| 79 | await p.touchscreen.tap(ct.x + 10, ct.y + ct.height / 2); |
| 80 | ok('un aviso tocado con el dedo se va solo igualmente', await hasta(async () => await aviso.count() === 0, { veces: 80, cada: 50 })); |
| 81 | // ⛏ a 390 px: solo se ve el pico, pero se llama «Hard Work». |
| 82 | const cdp = await ctx.newCDPSession(p); |
| 83 | const doc = await cdp.send('DOM.getDocument', {}); |
| 84 | const { nodeId } = await cdp.send('DOM.querySelector', { nodeId: doc.root.nodeId, selector: '#btn-hardwork' }); |
| 85 | const { nodes } = await cdp.send('Accessibility.getPartialAXTree', { nodeId, fetchRelatives: false }); |
| 86 | const nombre = nodes[0]?.name?.value || ''; |
| 87 | ok('a 390 px el botón ⛏ se llama «Hard Work» (no solo el pico)', /Hard Work/.test(nombre) |
| 88 | && await p.evaluate(() => document.querySelector('#btn-hardwork .hw-txt').getBoundingClientRect().width <= 1), nombre); |
| 89 | await cdp.detach(); |
| 90 | ok('sin errores de página (avisos en el teléfono)', errores.length === 0, errores.slice(0, 3).join(' | ')); |
| 91 | await ctx.close(); |
| 92 | } |
| 93 | |
| 94 | // ── 390 px: chat y vista ──────────────────────────────────────────────────── |
| 95 | { |
| 96 | const { ctx } = await contexto(b, telefono); |
| 97 | const { p, errores } = await pagina(ctx); |
| 98 | const flip = p.locator('#btn-flip'); |
| 99 | ok('en el chat, el botón dice «✳ Vista» y su nombre explica adónde lleva', (await flip.innerText()).trim() === '✳ Vista' |
| 100 | && /Ver lo que crea Elffuss/.test(await flip.getAttribute('aria-label'))); |
| 101 | const [bf, bc] = [await flip.boundingBox(), await p.locator('#chips').boundingBox()]; |
| 102 | ok('…y no tapa la fila de chips', !solapan(bf, bc), JSON.stringify({ bf, bc })); |
| 103 | // Durante una descarga, la caja de progreso no tapa la caja de texto ni el botón |
| 104 | // (antes, a 390 px, se montaba encima del compositor durante toda la descarga). |
| 105 | await p.evaluate(async () => (await import('/js/ui.js')).modelProgress('Descargando el cerebro · 42 %')); |
| 106 | const cajas = await p.evaluate(() => { |
| 107 | const r = id => { const e = document.getElementById(id).getBoundingClientRect(); return { x: e.x, y: e.y, width: e.width, height: e.height }; }; |
| 108 | const pr = document.getElementById('prompt').getBoundingClientRect(); |
| 109 | const arriba = document.elementFromPoint(pr.left + pr.width / 2, pr.top + pr.height / 2); |
| 110 | return { progreso: r('model-progress'), prompt: r('prompt'), composer: r('composer'), flip: r('btn-flip'), |
| 111 | visible: !document.getElementById('model-progress').hidden, enElPrompt: arriba?.id || arriba?.tagName }; |
| 112 | }); |
| 113 | ok('con una descarga en marcha, la caja de progreso se ve y no tapa el compositor ni el botón ⇄', cajas.visible |
| 114 | && !solapan(cajas.progreso, cajas.composer) && !solapan(cajas.progreso, cajas.flip) && cajas.enElPrompt === 'prompt', JSON.stringify(cajas)); |
| 115 | await p.evaluate(async () => (await import('/js/ui.js')).modelProgress(null)); |
| 116 | await flip.click(); |
| 117 | await espera(200); |
| 118 | const vista = await p.evaluate(() => { |
| 119 | const f = document.getElementById('btn-flip').getBoundingClientRect(); |
| 120 | return { |
| 121 | ancho: document.documentElement.scrollWidth, ventana: innerWidth, |
| 122 | viz: document.getElementById('viz').getBoundingClientRect().width, |
| 123 | tabsDesplazan: document.getElementById('tabs').scrollWidth > document.getElementById('tabs').clientWidth, |
| 124 | flip: { izq: f.left, der: f.right, texto: document.getElementById('btn-flip').textContent }, |
| 125 | }; |
| 126 | }); |
| 127 | ok('en la vista la página no se desplaza de lado', vista.ancho <= vista.ventana && vista.viz <= vista.ventana, JSON.stringify(vista)); |
| 128 | ok('…las pestañas se desplazan dentro de su fila', vista.tabsDesplazan); |
| 129 | ok('…y «💬 Chat» se ve entero para volver', vista.flip.izq >= 0 && vista.flip.der <= vista.ventana && /Chat/.test(vista.flip.texto), JSON.stringify(vista.flip)); |
| 130 | if (OUT) await p.screenshot({ path: OUT + '/ux-movil-vista.png' }); |
| 131 | await p.click('#tabs button[data-tab="ajustes"]'); |
| 132 | ok('la última pestaña (Ajustes) se puede alcanzar y abrir', await p.evaluate(() => document.getElementById('panel-ajustes').classList.contains('active'))); |
| 133 | ok('sin errores de página', errores.length === 0, errores.slice(0, 3).join(' | ')); |
| 134 | await ctx.close(); |
| 135 | } |
| 136 | { |
| 137 | const { ctx } = await contexto(b, { ...telefono, locale: 'en-US' }); |
| 138 | const { p } = await pagina(ctx); |
| 139 | ok('en inglés el botón dice «✳ View»', (await p.locator('#btn-flip').innerText()).trim() === '✳ View'); |
| 140 | await ctx.close(); |
| 141 | } |
| 142 | |
| 143 | // ── la puerta de móvil: teléfono sí, ventana estrecha con ratón no ────────── |
| 144 | const sinCerebroGuardado = { model: null }; |
| 145 | { |
| 146 | const { ctx } = await contexto(b, { ...sinCerebroGuardado, viewport: { width: 760, height: 900 } }); |
| 147 | const { p } = await pagina(ctx); |
| 148 | await espera(2500); |
| 149 | ok('ventana de 760 px con ratón: NO sale «En el móvil todavía no funciona»', await p.locator('.mobile-gate').count() === 0); |
| 150 | await ctx.close(); |
| 151 | } |
| 152 | { |
| 153 | const { ctx } = await contexto(b, { ...sinCerebroGuardado, ...telefono }); |
| 154 | const { p } = await pagina(ctx); |
| 155 | ok('en un teléfono sí sale la tarjeta', await hasta(async () => await p.locator('.mobile-gate').count() === 1)); |
| 156 | await p.locator('.mobile-gate button').click(); |
| 157 | // la descarga de verdad está cortada en las pruebas (Hugging Face bloqueado): falla |
| 158 | ok('si falla, la tarjeta se queda con el error y «Reintentar»', await hasta(async () => |
| 159 | /No ha cargado/.test(await p.locator('.mobile-gate').innerText().catch(() => '')) && |
| 160 | (await p.locator('.mobile-gate button').innerText()) === 'Reintentar', { veces: 400 })); |
| 161 | await ctx.close(); |
| 162 | } |
| 163 | |
| 164 | // ── con un cerebro externo guardado: sin puerta ───────────────────────────── |
| 165 | { |
| 166 | const { ctx } = await contexto(b, { ...telefono, model: 'ext:ollama', extraInit: { 'elffuss.providers': OLLAMA } }); |
| 167 | const { p, errores } = await pagina(ctx); |
| 168 | ok('teléfono con un cerebro externo guardado: carga sin la puerta de «descargar ~600 MB»', await hasta(() => p.evaluate(() => |
| 169 | document.getElementById('model-select').value === 'ext:ollama' && document.getElementById('model-status').classList.contains('on'))) |
| 170 | && await p.locator('.mobile-gate').count() === 0, await p.locator('.mobile-gate').innerText().catch(() => 'sin puerta')); |
| 171 | ok('…sin errores de página', errores.length === 0, errores.slice(0, 3).join(' | ')); |
| 172 | await ctx.close(); |
| 173 | } |
| 174 | { |
| 175 | // Un externo que no carga (OpenAI sin clave): la puerta sale para el local que |
| 176 | // viene detrás, con SU tamaño. |
| 177 | const { ctx } = await contexto(b, { ...telefono, model: 'ext:openai', |
| 178 | extraInit: { 'elffuss.providers': JSON.stringify({ openai: { enabled: true, baseURL: 'https://api.openai.com/v1', model: 'gpt-4o-mini', apiKey: '' } }) } }); |
| 179 | const { p } = await pagina(ctx); |
| 180 | const puerta = p.locator('.mobile-gate'); |
| 181 | ok('si el externo guardado no carga, la puerta sale para el local, con el tamaño de ese (~600 MB)', await hasta(async () => await puerta.count() === 1) |
| 182 | && /~600 MB/.test(await puerta.innerText()) && await p.evaluate(() => document.getElementById('model-select').value === 'rules'), |
| 183 | await puerta.innerText().catch(() => 'sin puerta')); |
| 184 | await ctx.close(); |
| 185 | } |
| 186 | |
| 187 | { |
| 188 | // Un Gemma guardado en un teléfono con WebGPU: la puerta dice SU tamaño |
| 189 | // (~2.8 GB), no el del ligero. |
| 190 | const { ctx } = await contexto(b, { ...telefono, model: 'litert:gemma-e4b' }); |
| 191 | await ctx.addInitScript(() => { |
| 192 | const adaptador = { limits: { maxBufferSize: 2 ** 32 }, features: new Set(), info: {}, async requestDevice() { throw new Error('sin GPU en la prueba'); } }; |
| 193 | if (navigator.gpu) { try { navigator.gpu.requestAdapter = async () => adaptador; } catch { /* — */ } } |
| 194 | else Object.defineProperty(navigator, 'gpu', { value: { requestAdapter: async () => adaptador }, configurable: true }); |
| 195 | }); |
| 196 | const { p } = await pagina(ctx); |
| 197 | const puerta = p.locator('.mobile-gate'); |
| 198 | ok('con un Gemma guardado, la puerta dice lo que de verdad se bajaría (~2.8 GB)', await hasta(async () => await puerta.count() === 1) |
| 199 | && /~2\.8 GB/.test(await puerta.innerText()) && !/600 MB/.test(await puerta.innerText()), await puerta.innerText().catch(() => 'sin puerta')); |
| 200 | await ctx.close(); |
| 201 | } |
| 202 | |
| 203 | servidor.close(); |
| 204 | await fin(b, 'MÓVIL: sin desbordes, «💬 Chat» a la vista, avisos que no tapan ■ y la puerta solo en teléfonos'); |