| 1 | // Cuándo se carga solo el ONNX ligero (Qwen3.5-0.8B). |
| 2 | // |
| 3 | // Desde d526a74 SOLO con WebGPU: sin ella ningún dtype funciona en wasm, y |
| 4 | // ofrecerlo era bajar ~600 MB para acabar en «básico». Así que: |
| 5 | // · sin WebGPU y sin modelo guardado: no se carga nada, ni se ofrece; |
| 6 | // · sin WebGPU y con el id viejo «onnx» guardado: tampoco se baja nada; |
| 7 | // · con WebGPU (simulada, pequeña: no entra Gemma) sí se carga el ligero, |
| 8 | // con el id que ofrece el selector. Es el control positivo: sin él, el test |
| 9 | // pasaría igual si el ONNX hubiera dejado de cargarse en todas partes. |
| 10 | // |
| 11 | // transformers.js es de mentira (page.route): no se descarga ningún modelo. |
| 12 | // |
| 13 | // BASE=http://localhost:8799 node tests/onnx-autoload.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(task, id, opts) { |
| 23 | (window.__tf = window.__tf || []).push({ task, id, device: opts?.device, dtype: opts?.dtype }); |
| 24 | const g = async () => [{ generated_text: 'hola' }]; |
| 25 | g.tokenizer = {}; g.dispose = () => {}; |
| 26 | return g; |
| 27 | }`; |
| 28 | |
| 29 | const b = await chromium.launch(); |
| 30 | async function arranca(guardado, { gpu = false } = {}) { |
| 31 | const ctx = await b.newContext({ locale: 'es-ES' }); |
| 32 | await ctx.addInitScript(([g, conGPU]) => { |
| 33 | // con GPU: un adaptador pequeño (256 MB de buffer), donde no entra Gemma |
| 34 | const falsa = { requestAdapter: async () => ({ limits: { maxBufferSize: 2 ** 28 }, features: new Set(), info: {}, requestDevice: async () => { throw new Error('no hace falta'); } }) }; |
| 35 | try { Object.defineProperty(Navigator.prototype, 'gpu', { get() { return conGPU ? falsa : undefined; }, configurable: true }); } catch { /* */ } |
| 36 | try { if (g) localStorage.setItem('elffusscode.model', g); } catch { /* */ } |
| 37 | }, [guardado, gpu]); |
| 38 | const p = await ctx.newPage({ viewport: { width: 1400, height: 900 } }); |
| 39 | const errs = []; p.on('pageerror', e => errs.push(e.message.slice(0, 160))); |
| 40 | await p.route(/cdn\.jsdelivr\.net\/npm\/@huggingface\/transformers@4/, r => r.fulfill({ status: 200, contentType: 'text/javascript', body: FALSO_TF })); |
| 41 | await p.goto(BASE + '/?test-opfs', { waitUntil: 'domcontentloaded' }); |
| 42 | await p.waitForFunction(() => /dot on/.test(document.getElementById('model-dot')?.className || ''), null, { timeout: 20000 }).catch(() => {}); |
| 43 | const r = await p.evaluate(() => ({ |
| 44 | tf: window.__tf || [], guardado: localStorage.getItem('elffusscode.model'), |
| 45 | dot: document.getElementById('model-dot')?.className, nombre: document.getElementById('model-dot')?.getAttribute('aria-label'), |
| 46 | sel: document.getElementById('model-select')?.value, |
| 47 | opciones: [...document.querySelectorAll('#model-select option')].map(o => o.value), |
| 48 | })); |
| 49 | await ctx.close(); |
| 50 | return { ...r, errs }; |
| 51 | } |
| 52 | |
| 53 | const r1 = await arranca(null); |
| 54 | ok('sin WebGPU y sin modelo guardado no se baja ningún modelo', r1.tf.length === 0, JSON.stringify(r1.tf)); |
| 55 | ok('…el ONNX ligero ni se ofrece en el selector', !r1.opciones.includes('onnx:qwen3.5-0.8b'), JSON.stringify(r1.opciones)); |
| 56 | ok('…y el IDE queda en «básico», con el punto apagado', r1.sel === 'rules' && !/\bon\b/.test(r1.dot || ''), JSON.stringify({ sel: r1.sel, dot: r1.dot })); |
| 57 | |
| 58 | // Quien tenía guardado el id viejo ('onnx' a secas) tampoco se baja nada sin GPU. |
| 59 | const r2 = await arranca('onnx'); |
| 60 | ok('sin WebGPU, con el id viejo «onnx» guardado, tampoco se baja nada', r2.tf.length === 0 && r2.sel === 'rules', JSON.stringify({ tf: r2.tf.length, guardado: r2.guardado, sel: r2.sel })); |
| 61 | |
| 62 | // Con el modo Básico elegido no se carga nada (eso no cambia). |
| 63 | const r3 = await arranca('rules'); |
| 64 | ok('con «Básico» elegido no se carga ningún modelo', r3.tf.length === 0 && r3.guardado === 'rules', JSON.stringify({ tf: r3.tf.length, guardado: r3.guardado })); |
| 65 | |
| 66 | // ⊕ Control positivo: CON WebGPU (pequeña, donde no entra Gemma) el ligero sí |
| 67 | // se carga solo, con el id del selector. Sin esto, todo lo de arriba pasaría |
| 68 | // igual si el ONNX hubiera dejado de cargarse en todas partes. |
| 69 | const r4 = await arranca(null, { gpu: true }); |
| 70 | ok('⊕ con WebGPU y sin modelo guardado, se carga solo el ONNX ligero', r4.tf.length === 1 && r4.tf[0].id === 'onnx-community/Qwen3.5-0.8B-ONNX', JSON.stringify(r4.tf)); |
| 71 | ok('⊕ …y queda elegido con el id que ofrece el selector', r4.sel === 'onnx:qwen3.5-0.8b' && r4.opciones.includes(r4.sel), JSON.stringify({ sel: r4.sel, opciones: r4.opciones })); |
| 72 | |
| 73 | const errs = [...r1.errs, ...r2.errs, ...r3.errs, ...r4.errs]; |
| 74 | console.log('\n errores JS:', errs[0] || 'ninguno'); |
| 75 | ok('sin errores de página', errs.length === 0, errs.join(' | ')); |
| 76 | console.log(fails ? `\n❌ ${fails} FALLO(S)` : '\n✅ EL ONNX LIGERO SOLO SE CARGA CON WEBGPU'); |
| 77 | await b.close(); |
| 78 | process.exit(fails ? 1 : 0); |