Elffuss BraidHub

utopia-ia/elffuss-claw public

main · 4.4 KiBRawPermalink
1// Utilidades comunes de los tests de UX (tests/ux-*.mjs). Sin modelo ni GPU:
2// modo básico, determinista. Las peticiones a terceros se cortan aquí para que
3// ningún test dependa de la red ni toque producción; three.js (la Mente) es lo
4// único que se deja pasar, y solo si un test abre la Mente.
5//
6// python3 server/serve.py 8743 & BASE=http://localhost:8743 node tests/ux-<algo>.mjs
7import { chromium } from 'playwright';
8
9export const BASE = process.env.BASE || 'http://localhost:8743';
10export const OUT = process.env.SCRATCH || '';
11
12let fails = 0;
13export const ok = (n, c, e = '') => { console.log((c ? '✅' : '❌') + ' ' + n + (e ? ' — ' + e : '')); if (!c) fails++; };
14export const fallos = () => fails;
15export const espera = ms => new Promise(r => setTimeout(r, ms));
16export async function hasta(cond, { veces = 200, cada = 50 } = {}) {
17 for (let i = 0; i < veces; i++) { if (await cond()) return true; await espera(cada); }
18 return false;
19}
20
21// Lo que NO puede salir de la máquina en un test: el modelo grande, el broker,
22// SoundCloud, Hugging Face, GitHub (salvo que el test lo stubee), producción.
23const CORTAR = [
24 /^https:\/\/([a-z0-9-]+\.)?soundcloud\.com\//,
25 /^https:\/\/([a-z0-9-]+\.)?huggingface\.co\//,
26 /^https:\/\/([a-z0-9-]+\.)?hf\.co\//,
27 /^https:\/\/(m\d+|models)\.elffuss\.(com|utopiaia\.com)\//,
28 /^https:\/\/([a-z0-9-]+\.)?elffuss\.com\//,
29 /^https:\/\/([a-z0-9-]+\.)?openverse\.org\//,
30 /^https:\/\/([a-z0-9-]+\.)?duckduckgo\.com\//,
31 /^https:\/\/cdn\.sheetjs\.com\//,
32 /^https:\/\/cdn\.jsdelivr\.net\/npm\/@huggingface\//,
33 /^https:\/\/cdn\.jsdelivr\.net\/npm\/@mlc-ai\//,
34 /^https:\/\/cdn\.jsdelivr\.net\/npm\/@litert/,
35 /^https:\/\/([a-z0-9-]+\.)?googleapis\.com\//,
36 /^https:\/\/(api\.github\.com|raw\.githubusercontent\.com)\//,
37];
38
39export async function navegador() { return chromium.launch(); }
40
41// Contexto con Claw en modo básico, ya «bienvenido» y con los permisos que se
42// pidan. `rutas` permite stubear una URL externa: (url, route) → true si la
43// atendió.
44export async function contexto(b, { locale = 'es-ES', viewport = { width: 1400, height: 900 }, welcomed = true,
45 model = 'rules', grants = null, rutas = null, isMobile = false, hasTouch = false, extraInit = null } = {}) {
46 const ctx = await b.newContext({ locale, viewport, acceptDownloads: true, isMobile, hasTouch });
47 const fuera = [];
48 await ctx.route('**/*', async route => {
49 const url = route.request().url();
50 if (url.startsWith(BASE) || url.startsWith('data:') || url.startsWith('blob:')) return route.continue();
51 if (rutas && await rutas(url, route)) return;
52 fuera.push(route.request().method() + ' ' + url.slice(0, 140));
53 if (CORTAR.some(rx => rx.test(url))) return route.abort();
54 return route.continue();
55 });
56 await ctx.addInitScript(({ welcomed, model, grants, extraInit }) => {
57 try {
58 if (welcomed) localStorage.setItem('elffuss.welcomed', '1');
59 if (model) localStorage.setItem('elffuss.model', model);
60 if (grants) localStorage.setItem('elffuss.grants', JSON.stringify(grants));
61 if (extraInit) for (const [k, v] of Object.entries(extraInit)) localStorage.setItem(k, v);
62 } catch { /* */ }
63 }, { welcomed, model, grants, extraInit });
64 return { ctx, fuera };
65}
66
67export async function pagina(ctx, ruta = '/') {
68 const p = await ctx.newPage();
69 const errores = [];
70 p.on('pageerror', e => { if (!/allow-same-origin/.test(e.message)) errores.push(e.message); });
71 p.on('dialog', d => { p.__dialogos = (p.__dialogos || []).concat(d.message()); (p.__aceptar === false ? d.dismiss() : d.accept()).catch(() => {}); });
72 await p.goto(BASE + ruta, { waitUntil: 'domcontentloaded' });
73 await p.waitForFunction(() => !!window.elffuss?.agent, null, { timeout: 15000 });
74 return { p, errores };
75}
76
77export const kv = (p, key) => p.evaluate(async k => (await import('/js/db.js')).get('kv', k), key);
78export const avisos = p => p.$$eval('#toasts .toast .toast-txt', ts => ts.map(t => t.textContent));
79export async function escribir(p, texto) { await p.fill('#prompt', texto); await p.press('#prompt', 'Enter'); }
80export const reposo = p => hasta(() => p.evaluate(() => !document.getElementById('send').classList.contains('is-stop')
81 && !document.querySelector('#log .msg.thinking')));
82
83export function fin(b, titulo) {
84 console.log(fails ? `\n❌ ${fails} FALLO(S)` : `\n✅ ${titulo}`);
85 return b.close().then(() => process.exit(fails ? 1 : 0));
86}