Elffuss BraidHub

utopia-ia/elffuss-code public

main · 8.2 KiBRawPermalink
1// La landing: sin píldora fantasma y sin fallos mudos.
2//
3// 1. Sin descarga a medias, la píldora «Reanudar / Descartar» NO se ve. Antes
4// `.resume-clone { display:flex }` le ganaba al atributo hidden y salía
5// siempre, vacía y con dos botones sin manejador (también en producción).
6// 2. Con una descarga a medias sí sale, enlaza owner/repo al repositorio y
7// «Descartar» la quita.
8// 3. Clonar: URL vacía o no reconocida se dice ANTES de abrir el selector de
9// carpeta (antes se elegía carpeta y luego fallaba, o no pasaba nada).
10// 4. Abrir carpeta: cancelar el selector no es un error; cualquier otro fallo
11// se dice en la landing.
12// 5. Sin File System Access (Firefox, Safari): los botones que no pueden
13// funcionar se desactivan con el motivo, y se ofrece probar en la carpeta
14// privada del navegador: una carpeta propia, donde no se ven ni se tocan
15// los pesos de los modelos (que viven en la raíz de ese almacenamiento).
16// Nada sale a la red: el selector de carpetas va de mentira y nunca se llega a
17// listar un repo.
18//
19// BASE=http://127.0.0.1:8898 node tests/landing.mjs
20import { chromium } from 'playwright';
21const BASE = process.env.BASE || 'http://localhost:8799';
22let fails = 0; const ok = (n, c, e = '') => { console.log((c ? '✅' : '❌') + ' ' + n + (e ? ' — ' + e : '')); if (!c) fails++; };
23
24const b = await chromium.launch();
25const errs = [];
26const nueva = async ({ sinSelector = false } = {}) => {
27 const ctx = await b.newContext({ locale: 'es-ES' });
28 await ctx.addInitScript((sinSelector) => {
29 try { localStorage.setItem('elffusscode.model', 'rules'); } catch {}
30 if (sinSelector) { delete window.showDirectoryPicker; return; }
31 window.__picker = { llamadas: 0, error: 'AbortError' };
32 window.showDirectoryPicker = async () => { window.__picker.llamadas++; throw new DOMException('selector de prueba', window.__picker.error); };
33 }, sinSelector);
34 const p = await ctx.newPage({ viewport: { width: 1300, height: 850 } });
35 p.on('pageerror', e => errs.push(e.message.slice(0, 160)));
36 return { ctx, p };
37};
38const estado = (p) => p.evaluate(() => document.getElementById('clone-status').textContent);
39
40// 1 · píldora oculta sin descarga a medias
41let { ctx, p } = await nueva();
42await p.goto(BASE + '/', { waitUntil: 'domcontentloaded' });
43await p.waitForTimeout(1200);
44const pildora = await p.evaluate(() => {
45 const el = document.getElementById('resume-clone');
46 return { display: getComputedStyle(el).display, visible: !!(el.offsetWidth || el.offsetHeight) };
47});
48ok('1 · sin descarga a medias, la píldora Reanudar/Descartar no se ve', pildora.display === 'none' && !pildora.visible, JSON.stringify(pildora));
49ok('1 · …y sus botones no son paradas del tabulador', !(await p.locator('#resume-clone-btn').isVisible()) && !(await p.locator('#discard-clone-btn').isVisible()));
50
51// 3 · clonar: validar antes del selector
52await p.click('#clone-btn');
53await p.waitForTimeout(150);
54ok('3 · URL vacía: lo dice y no abre el selector', /Pega la URL/.test(await estado(p)) && await p.evaluate(() => window.__picker.llamadas) === 0, await estado(p));
55await p.fill('#clone-url', 'esto no es un repo');
56await p.press('#clone-url', 'Enter');
57await p.waitForTimeout(200);
58ok('3 · URL no reconocida: lo dice ANTES de abrir el selector', /URL no reconocida/.test(await estado(p)) && await p.evaluate(() => window.__picker.llamadas) === 0, await estado(p));
59await p.fill('#clone-url', 'https://github.com/octo/demo');
60await p.click('#clone-btn');
61await p.waitForTimeout(200);
62ok('3 · URL válida: abre el selector; cancelarlo no es un error', await p.evaluate(() => window.__picker.llamadas) === 1 && !/⚠️/.test(await estado(p)), await estado(p));
63
64// 4 · abrir carpeta: cancelar calla, otro fallo se dice
65await p.click('#open-project');
66await p.waitForTimeout(150);
67ok('4 · cancelar el selector de «Abrir carpeta» no muestra error', !/⚠️/.test(await estado(p)), await estado(p));
68await p.evaluate(() => { window.__picker.error = 'SecurityError'; });
69await p.click('#open-project');
70await p.waitForTimeout(150);
71ok('4 · cualquier otro fallo al abrir se dice en la landing', /No pude abrir la carpeta/.test(await estado(p)), await estado(p));
72
73// 2 · con descarga a medias: píldora visible, enlace y Descartar
74await p.evaluate(async () => {
75 const db = await import('/js/db.js');
76 const handle = await navigator.storage.getDirectory();
77 await db.set('kv', 'cloneJob', { url: 'https://github.com/octo/demo', host: 'github', owner: 'octo', repo: 'demo', branch: 'main', handle, files: [{ path: 'a.txt' }, { path: 'b.txt' }], done: ['a.txt'], skipped: 0 });
78});
79await p.reload({ waitUntil: 'domcontentloaded' });
80await p.waitForTimeout(1200);
81const banner = await p.evaluate(() => {
82 const el = document.getElementById('resume-clone');
83 const a = el.querySelector('.rc-text a');
84 return { visible: !!(el.offsetWidth || el.offsetHeight), texto: el.querySelector('.rc-text').textContent, href: a?.href, rel: a?.rel };
85});
86ok('2 · con una descarga a medias, la píldora sale con su progreso', banner.visible && /octo\/demo \(1\/2 ficheros\)/.test(banner.texto), JSON.stringify(banner));
87ok('2 · owner/repo enlaza al repositorio', banner.href === 'https://github.com/octo/demo' && /noopener/.test(banner.rel), JSON.stringify(banner));
88await p.click('#discard-clone-btn');
89await p.waitForTimeout(200);
90const trasDescartar = await p.evaluate(async () => {
91 const db = await import('/js/db.js');
92 return { visible: !!document.getElementById('resume-clone').offsetHeight, job: await db.get('kv', 'cloneJob') };
93});
94ok('2 · Descartar quita la píldora y olvida la descarga', !trasDescartar.visible && !trasDescartar.job, JSON.stringify(trasDescartar));
95await ctx.close();
96
97// 5 · navegador sin File System Access
98({ ctx, p } = await nueva({ sinSelector: true }));
99// pesos de modelo de mentira donde los guarda runtime/model-store.js: la raíz del OPFS
100await p.goto(BASE + '/manifest.json');
101await p.evaluate(async () => {
102 const dir = await (await navigator.storage.getDirectory()).getDirectoryHandle('elffuss-models', { create: true });
103 for (const n of ['abc123.bin', 'abc123.bin.done']) { const w = await (await dir.getFileHandle(n, { create: true })).createWritable(); await w.write('x'); await w.close(); }
104});
105await p.goto(BASE + '/', { waitUntil: 'domcontentloaded' });
106await p.waitForTimeout(1200);
107const sinFS = await p.evaluate(() => ({
108 abrir: document.getElementById('open-project').disabled, clonar: document.getElementById('clone-btn').disabled,
109 motivo: document.getElementById('clone-status').textContent, probar: !document.getElementById('try-browser').hidden,
110}));
111ok('5 · sin File System Access: abrir y clonar desactivados, con el motivo', sinFS.abrir && sinFS.clonar && /Chrome o Edge/.test(sinFS.motivo), JSON.stringify(sinFS));
112ok('5 · …y se ofrece probar en el navegador', sinFS.probar);
113await p.click('#try-browser');
114await p.waitForFunction(() => !document.getElementById('ide').hidden, null, { timeout: 30000 }).catch(() => {});
115ok('5 · «Probar en el navegador» abre el IDE', await p.evaluate(() => !document.getElementById('ide').hidden));
116await p.waitForTimeout(800);
117const visto = await p.evaluate(async () => ({
118 arbol: [...document.querySelectorAll('#tree [data-path]')].map(e => e.dataset.path),
119 lista: await (await import('/js/tools/code.js')).tree({ path: '' }).catch(e => 'error: ' + e.message),
120 ficheros: await (await import('/js/tools/code.js')).fileList().catch(e => 'error: ' + e.message),
121}));
122ok('5 · …en su propia carpeta: ni el explorador ni las herramientas ven los pesos de los modelos',
123 !visto.arbol.some(x => /elffuss-models/.test(x)) && !/elffuss-models/.test(JSON.stringify([visto.lista, visto.ficheros])), JSON.stringify(visto));
124ok('5 · …y los pesos siguen intactos', await p.evaluate(async () => {
125 try { await (await (await navigator.storage.getDirectory()).getDirectoryHandle('elffuss-models')).getFileHandle('abc123.bin'); return true; } catch { return false; }
126}));
127await ctx.close();
128
129console.log('\n errores JS:', errs[0] || 'ninguno');
130ok('sin errores de página', errs.length === 0, errs.join(' | '));
131console.log(fails ? `\n❌ ${fails} FALLO(S)` : '\n✅ LANDING SIN PÍLDORA FANTASMA NI FALLOS MUDOS');
132await b.close();
133process.exit(fails ? 1 : 0);