Elffuss BraidHub

utopia-ia/elffuss-code public

9828539d2fc0 · 2.3 KiBRawPermalink
1// Valida cachedModelBlob de litert.js: descarga 1 vez, cachea en Cache Storage
2// (persistente) y en la 2ª llamada devuelve el blob SIN red (offline). Usa un
3// fixture .litertlm determinista (no baja los GB reales).
4import { chromium } from 'playwright';
5import { writeFileSync, rmSync } from 'node:fs';
6import { fileURLToPath } from 'node:url';
7import { dirname, join } from 'node:path';
8const BASE = process.env.BASE || 'http://localhost:8799';
9const SIZE = 120000;
10let fails = 0; const ok = (n,c,e='')=>{console.log((c?'✅':'❌')+' '+n+(e?' — '+e:''));if(!c)fails++};
11const WEB = join(dirname(fileURLToPath(import.meta.url)), '..', 'web');
12const FIXP = join(WEB, '__litert_fixture.litertlm');
13writeFileSync(FIXP, Buffer.from(Array.from({length:SIZE},(_,i)=>i%256)));
14process.on('exit', ()=>{ try{rmSync(FIXP)}catch{} });
15const FIX = BASE + '/__litert_fixture.litertlm';
16
17const b = await chromium.launch({args:['--enable-unsafe-webgpu','--use-angle=metal']});
18const ctx = await b.newContext();
19const p = await ctx.newPage();
20await p.goto(BASE+'/', {waitUntil:'domcontentloaded'});
21
22const call = (u) => p.evaluate(async (u) => {
23 const m = await import('/js/providers/litert.js');
24 const msgs = [];
25 const r = await m.cachedModelBlob(u, s => msgs.push(s));
26 const blob = r instanceof Blob ? r : null;
27 return { isBlob: !!blob, size: blob ? blob.size : 0, firstMsg: msgs[0]||'', lastMsg: msgs.at(-1)||'', type: typeof r };
28}, u);
29
30// 1) primera vez: descarga + cachea, devuelve Blob del tamaño correcto
31let r = await call(FIX);
32ok('1ª vez → Blob del tamaño real (descarga + cachea)', r.isBlob && r.size === SIZE, `${r.type} size=${r.size} · ${r.lastMsg}`);
33
34// 2) está en Cache Storage
35const cached = await p.evaluate(async (u) => { const c = await caches.open('elffuss-models-v1'); return !!(await c.match(u)); }, FIX);
36ok('el modelo quedó en Cache Storage', cached);
37
38// 3) OFFLINE: 2ª llamada devuelve el blob desde caché sin red
39await ctx.setOffline(true);
40r = await call(FIX);
41ok('2ª vez OFFLINE → Blob desde caché (no re-descarga)', r.isBlob && r.size === SIZE && /cach[eé]/i.test(r.firstMsg), `${r.size} · ${r.firstMsg}`);
42await ctx.setOffline(false);
43
44console.log(fails?`\n❌ ${fails} FALLO(S)`:'\n✅ CACHÉ LiteRT (blob) OK — descarga 1 vez, sirve offline');
45await b.close(); process.exit(fails?1:0);