Elffuss BraidHub

utopia-ia/elffuss-code public

main · 7.7 KiBRawPermalink
1// Elffuss Lab: que lo que se ve sea lo que corre.
2//
3// 1. Desde el IDE se llega al Lab (View › Elffuss Lab y la paleta); antes
4// solo escribiendo la URL.
5// 2. Cambiar de cerebro en la lista descarta el cargado: antes una corrida
6// «con onnx» se ejecutaba con Reglas. «Lanzar» espera a que el cerebro
7// elegido esté cargado cuando el banco lo necesita.
8// 3. Cambiar de banco repinta el ranking de ese banco.
9// 4. ■ a mitad: «parado (parcial)», no «✔ terminado/completado», y no
10// entra en el ranking.
11// 5. «Abrir el IDE con este cerebro» hace solo eso (antes prometía aplicar
12// algoritmo y perillas que el IDE no lee).
13// El cerebro «onnx» es de mentira (page.route sirve un providers/onnx.js propio).
14//
15// BASE=http://127.0.0.1:8898 node tests/lab-ux.mjs
16import { chromium } from 'playwright';
17const BASE = process.env.BASE || 'http://localhost:8799';
18let fails = 0; const ok = (n, c, e = '') => { console.log((c ? '✅' : '❌') + ' ' + n + (e ? ' — ' + e : '')); if (!c) fails++; };
19
20const FALSO = `export let name = 'falso'; export async function load() {}
21export async function chat() { return 'hecho'; }`;
22
23const b = await chromium.launch();
24const ctx = await b.newContext({ locale: 'es-ES' });
25await ctx.addInitScript(() => { try { localStorage.setItem('elffusscode.model', 'rules'); } catch {} });
26const errs = [];
27
28// 1 · el IDE lleva al Lab
29const ide = await ctx.newPage({ viewport: { width: 1400, height: 900 } });
30ide.on('pageerror', e => errs.push(e.message.slice(0, 160)));
31await ide.goto(BASE + '/?test-opfs', { waitUntil: 'domcontentloaded' });
32await ide.waitForFunction(() => window.elffussClaw?.conv?.getActive?.(), null, { timeout: 15000 });
33await ide.evaluate(() => { window.__abiertas = []; window.open = (u) => { window.__abiertas.push(String(u)); return null; }; });
34await ide.click('#menubar button[data-menu="view"]');
35const enMenu = await ide.locator('#topmenu .menu-item', { hasText: 'Elffuss Lab' }).count();
36await ide.locator('#topmenu .menu-item', { hasText: 'Elffuss Lab' }).click().catch(() => {});
37await ide.keyboard.press(process.platform === 'darwin' ? 'Meta+Shift+P' : 'Control+Shift+P');
38await ide.fill('#pal-input', '>Lab');
39await ide.waitForTimeout(150);
40await ide.press('#pal-input', 'Enter');
41const abiertas = await ide.evaluate(() => window.__abiertas);
42ok('1 · View › Elffuss Lab abre el Lab', enMenu === 1 && abiertas[0] === 'lab.html', JSON.stringify(abiertas));
43ok('1 · …y también la paleta', abiertas[1] === 'lab.html', JSON.stringify(abiertas));
44await ide.close();
45
46const p = await ctx.newPage({ viewport: { width: 1300, height: 900 } });
47p.on('pageerror', e => errs.push(e.message.slice(0, 160)));
48await p.route('**/js/providers/onnx.js*', route => route.fulfill({ status: 200, contentType: 'text/javascript', body: FALSO }));
49await p.goto(BASE + '/lab.html', { waitUntil: 'domcontentloaded' });
50await p.waitForTimeout(600);
51const estado = () => p.evaluate(() => ({
52 led: document.querySelector('#led-model').textContent.trim(), run: document.getElementById('btn-run').disabled,
53 hint: document.getElementById('run-hint').textContent, prog: document.getElementById('prog-txt').textContent,
54 rank: document.getElementById('rank-tag').textContent, log: document.getElementById('log').textContent,
55 ultima: document.getElementById('log').lastElementChild?.textContent || '',
56}));
57
58// 2 · cargar Reglas y cambiar a onnx
59await p.click('#btn-load'); // Reglas
60await p.waitForFunction(() => /modelo: rules/.test(document.querySelector('#led-model').textContent), null, { timeout: 5000 });
61await p.locator('#brains label').nth(1).click(); // onnx
62let e = await estado();
63ok('2 · cambiar de cerebro descarta el que estaba cargado', /sin cargar/.test(e.led), e.led);
64await p.locator('#benches label').nth(2).click(); // juegos: necesita cerebro
65e = await estado();
66ok('2 · con un banco que necesita cerebro, «Lanzar» espera a que se cargue', e.run && /Carga el cerebro «onnx:qwen3\.5-0\.8b»/.test(e.hint), JSON.stringify(e));
67await p.click('#btn-load');
68await p.waitForFunction(() => /modelo: onnx/.test(document.querySelector('#led-model').textContent), null, { timeout: 5000 });
69e = await estado();
70ok('2 · cargado el elegido, «Lanzar» se habilita', !e.run && !e.hint, JSON.stringify(e));
71
72// 3 · el ranking sigue al banco
73await p.locator('#benches label').nth(0).click(); // contexto
74await p.click('#btn-run');
75await p.waitForFunction(() => /completado/.test(document.getElementById('prog-txt').textContent), null, { timeout: 15000 });
76await p.click('#btn-save');
77e = await estado();
78ok('3 · el ranking de «contexto» tiene la corrida guardada', /1 corridas · contexto/.test(e.rank), e.rank);
79await p.locator('#benches label').nth(1).click(); // swe
80e = await estado();
81ok('3 · al cambiar a «swe» el ranking es el de swe', /sin corridas de swe/.test(e.rank), e.rank);
82
83// 4 · ■ a mitad: parcial
84await p.locator('#benches label').nth(0).click(); // contexto otra vez
85await p.evaluate(() => { document.getElementById('btn-run').click(); document.getElementById('btn-stop').click(); });
86await p.waitForFunction(() => !document.getElementById('btn-stop').disabled === false && /parado/.test(document.getElementById('prog-txt').textContent), null, { timeout: 15000 }).catch(() => {});
87e = await estado();
88ok('4 · parado a mitad: dice «parado (parcial)», no «completado»', e.prog === 'parado (parcial)' && /parado: resultado PARCIAL/.test(e.ultima) && !/terminado/.test(e.ultima), e.prog + ' · ' + e.ultima.slice(0, 90));
89// Parcial no es comparable: «Guardar» no la mete en el ranking (su porcentaje
90// competiría con el de corridas completas aunque fuera marcada).
91const filasAntes = await p.evaluate(() => document.querySelectorAll('#rank-body tr').length);
92await p.click('#btn-save');
93const r4 = await p.evaluate(() => ({ filas: document.querySelectorAll('#rank-body tr').length, ultima: document.getElementById('log').lastElementChild?.textContent || '' }));
94ok('4 · la corrida parada no entra en el ranking, y se dice', r4.filas === filasAntes && /no entra en el ranking/.test(r4.ultima), JSON.stringify(r4));
95
96// 5 · abrir el IDE con este cerebro
97await p.evaluate(() => { window.open = () => null; localStorage.setItem('elffusscode.ctxopts', '{"MMR":false}'); });
98const etiqueta = await p.locator('#btn-launch').textContent();
99await p.click('#btn-launch');
100const r5 = await p.evaluate(() => ({ model: localStorage.getItem('elffusscode.model'), ctx: localStorage.getItem('elffusscode.ctxopts'), algo: localStorage.getItem('elffusscode.algo') }));
101ok('5 · el botón dice lo que hace', /Abrir el IDE con este cerebro/.test(etiqueta), etiqueta);
102ok('5 · guarda el cerebro y no deja claves que el IDE no lee', r5.model === 'onnx:qwen3.5-0.8b' && r5.ctx === null && r5.algo === null, JSON.stringify(r5));
103// …y ese id es uno que el IDE ofrece: el ligero era 'onnx' a secas, que el IDE
104// ya no tiene, así que lo descartaba al arrancar (y se quedaba sin modelo).
105{
106 const ide2 = await ctx.newPage({ viewport: { width: 1400, height: 900 } });
107 await ide2.goto(BASE + '/?test-opfs', { waitUntil: 'domcontentloaded' });
108 await ide2.waitForFunction(() => document.querySelectorAll('#model-select option').length > 0, null, { timeout: 15000 });
109 const ids = await ide2.$$eval('#model-select option', os => os.map(o => o.value));
110 ok('5 · el IDE ofrece ese mismo cerebro', ids.includes(r5.model), JSON.stringify({ lab: r5.model, ide: ids }));
111 await ide2.close();
112}
113
114console.log('\n errores JS:', errs[0] || 'ninguno');
115ok('sin errores de página', errs.length === 0, errs.join(' | '));
116console.log(fails ? `\n❌ ${fails} FALLO(S)` : '\n✅ LAB: LO QUE SE VE ES LO QUE CORRE');
117await b.close();
118process.exit(fails ? 1 : 0);