| 1 | // «▶ Ejecutar» en una notificación con la app CERRADA: el IDE se abre con |
| 2 | // ?notif=execute&id=… (sw.js guardó la propuesta en Cache Storage con ese id) y |
| 3 | // la propuesta se manda en cuanto hay un modelo de verdad cargado. Antes la |
| 4 | // ventana nueva no recibía la propuesta: abría la Mente y no ejecutaba nada, |
| 5 | // sin decirlo. (La parte del service worker la prueba tests/sw-notif.mjs.) |
| 6 | // |
| 7 | // 1. Con un modelo que carga (transformers.js de mentira): la propuesta sale |
| 8 | // sola cuando está listo, y la guardada se borra. |
| 9 | // 2. En modo Básico no se gasta con el motor de reglas: se dice que espera a |
| 10 | // un modelo, y sale al elegir uno. |
| 11 | // 3. Si la propuesta ya no está: se dice y se abre la Mente. |
| 12 | // |
| 13 | // BASE=http://localhost:8799 node tests/notif-run.mjs |
| 14 | import { chromium } from 'playwright'; |
| 15 | const BASE = process.env.BASE || 'http://localhost:8799'; |
| 16 | let fails = 0; const ok = (n, c, e = '') => { console.log((c ? '✅' : '❌') + ' ' + n + (e ? ' — ' + e : '')); if (!c) fails++; }; |
| 17 | |
| 18 | const FALSO_TF = ` |
| 19 | export const env = {}; |
| 20 | export class TextStreamer { constructor(t, o) { this.o = o; } } |
| 21 | export class InterruptableStoppingCriteria { interrupt() {} } |
| 22 | export async function pipeline() { |
| 23 | const g = async (messages) => { (window.__recibido ||= []).push(messages.at(-1)?.content || ''); return [{ generated_text: 'hecho' }]; }; |
| 24 | g.tokenizer = {}; g.dispose = () => {}; |
| 25 | return g; |
| 26 | }`; |
| 27 | const MD = '# Propuestas de mejora — ciclo 7\n\n## Calidad\nAñadir pruebas a app.js'; |
| 28 | const b = await chromium.launch(); |
| 29 | const errs = []; |
| 30 | |
| 31 | async function abre({ modelo, sembrar = true, id = 'abc123def' }) { |
| 32 | const ctx = await b.newContext({ locale: 'es-ES' }); |
| 33 | await ctx.addInitScript(m => { |
| 34 | // WebGPU simulada y pequeña (no entra Gemma): desde d526a74 el ONNX ligero |
| 35 | // solo se ofrece con WebGPU, y este test va del flujo de la notificación, |
| 36 | // no de la GPU. transformers.js es de mentira: no se baja nada. |
| 37 | const falsa = { requestAdapter: async () => ({ limits: { maxBufferSize: 2 ** 28 }, features: new Set(), info: {}, requestDevice: async () => { throw new Error('no hace falta'); } }) }; |
| 38 | try { Object.defineProperty(Navigator.prototype, 'gpu', { get() { return falsa; }, configurable: true }); } catch { /* */ } |
| 39 | try { localStorage.setItem('elffusscode.model', m); } catch { /* */ } |
| 40 | }, modelo); |
| 41 | const p = await ctx.newPage({ viewport: { width: 1400, height: 900 } }); |
| 42 | p.on('pageerror', e => errs.push(e.message.slice(0, 160))); |
| 43 | await p.route(/cdn\.jsdelivr\.net\/npm\/@huggingface\/transformers@4/, r => r.fulfill({ status: 200, contentType: 'text/javascript', body: FALSO_TF })); |
| 44 | await p.goto(BASE + '/manifest.json'); |
| 45 | if (sembrar) await p.evaluate(async ({ id, md }) => { await (await caches.open('elffuss-notif')).put('./notif/' + id, new Response(md)); }, { id, md: MD }); |
| 46 | await p.goto(BASE + `/?test-opfs¬if=execute&id=${id}`, { waitUntil: 'domcontentloaded' }); |
| 47 | await p.waitForFunction(() => window.elffussClaw?.conv?.getActive?.(), null, { timeout: 20000 }); |
| 48 | return { ctx, p }; |
| 49 | } |
| 50 | const estado = p => p.evaluate(async () => ({ |
| 51 | url: location.search, |
| 52 | usuario: [...document.querySelectorAll('#chat-log .msg.user')].map(n => n.textContent.slice(0, 60)), |
| 53 | sistema: [...document.querySelectorAll('#chat-log .msg.sys')].map(n => n.textContent).join(' | '), |
| 54 | recibido: window.__recibido || [], |
| 55 | guardada: await (await caches.open('elffuss-notif')).match('./notif/abc123def').then(r => !!r), |
| 56 | mente: !!document.getElementById('mind-overlay') && getComputedStyle(document.getElementById('mind-overlay')).display !== 'none', |
| 57 | })); |
| 58 | |
| 59 | // 1 · con un modelo que carga |
| 60 | { |
| 61 | const { ctx, p } = await abre({ modelo: 'onnx:qwen3.5-0.8b' }); |
| 62 | await p.waitForFunction(() => (window.__recibido || []).length > 0, null, { timeout: 20000 }).catch(() => {}); |
| 63 | await p.waitForFunction(() => !window.elffussClaw.conv.isBusy(), null, { timeout: 10000 }).catch(() => {}); |
| 64 | const e = await estado(p); |
| 65 | ok('1 · la propuesta de la notificación se manda sola en cuanto el modelo está listo', e.usuario.some(u => /Implementa esta propuesta/.test(u)) && e.recibido.some(r => /Añadir pruebas a app\.js/.test(r)), JSON.stringify({ usuario: e.usuario, recibido: e.recibido.length })); |
| 66 | ok('1 · …una sola vez', e.usuario.filter(u => /Implementa esta propuesta/.test(u)).length === 1, JSON.stringify(e.usuario)); |
| 67 | ok('1 · …la guardada se borra y la URL queda limpia', !e.guardada && !/notif|id=/.test(e.url), JSON.stringify({ guardada: e.guardada, url: e.url })); |
| 68 | await ctx.close(); |
| 69 | } |
| 70 | |
| 71 | // 2 · modo Básico: espera a un modelo y no se gasta con las reglas |
| 72 | { |
| 73 | const { ctx, p } = await abre({ modelo: 'rules' }); |
| 74 | await p.waitForTimeout(800); |
| 75 | const e = await estado(p); |
| 76 | ok('2 · en modo Básico no se manda (se gastaría con el motor de reglas)', !e.usuario.some(u => /Implementa esta propuesta/.test(u)), JSON.stringify(e.usuario)); |
| 77 | ok('2 · …y se dice que espera a un modelo', /se ejecuta en cuanto haya un modelo cargado/.test(e.sistema), e.sistema.slice(0, 200)); |
| 78 | await p.click('#btn-settings'); |
| 79 | await p.locator('.model-card[data-model="onnx:qwen3.5-0.8b"]').click(); |
| 80 | await p.waitForFunction(() => (window.__recibido || []).length > 0, null, { timeout: 20000 }).catch(() => {}); |
| 81 | const e2 = await estado(p); |
| 82 | ok('2 · al elegir un modelo, sale', e2.recibido.some(r => /Añadir pruebas a app\.js/.test(r)), JSON.stringify(e2.recibido.length)); |
| 83 | await ctx.close(); |
| 84 | } |
| 85 | |
| 86 | // 3 · la propuesta ya no está |
| 87 | { |
| 88 | const { ctx, p } = await abre({ modelo: 'rules', sembrar: false }); |
| 89 | await p.waitForTimeout(800); |
| 90 | const e = await estado(p); |
| 91 | ok('3 · sin la propuesta guardada lo dice y abre la Mente', /No encuentro la propuesta/.test(e.sistema) && e.mente, JSON.stringify({ sistema: e.sistema.slice(0, 120), mente: e.mente })); |
| 92 | await ctx.close(); |
| 93 | } |
| 94 | |
| 95 | console.log('\n errores JS:', errs[0] || 'ninguno'); |
| 96 | ok('sin errores de página', errs.length === 0, errs.join(' | ')); |
| 97 | console.log(fails ? `\n❌ ${fails} FALLO(S)` : '\n✅ NOTIFICACIÓN «▶ Ejecutar» CON LA APP CERRADA: la propuesta llega y sale'); |
| 98 | await b.close(); |
| 99 | process.exit(fails ? 1 : 0); |