Elffuss BraidHub

utopia-ia/elffuss-code public

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