| 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 |
| 20 | import { chromium } from 'playwright'; |
| 21 | const BASE = process.env.BASE || 'http://localhost:8799'; |
| 22 | let fails = 0; const ok = (n, c, e = '') => { console.log((c ? '✅' : '❌') + ' ' + n + (e ? ' — ' + e : '')); if (!c) fails++; }; |
| 23 | |
| 24 | const b = await chromium.launch(); |
| 25 | const errs = []; |
| 26 | const 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 | }; |
| 38 | const estado = (p) => p.evaluate(() => document.getElementById('clone-status').textContent); |
| 39 | |
| 40 | // 1 · píldora oculta sin descarga a medias |
| 41 | let { ctx, p } = await nueva(); |
| 42 | await p.goto(BASE + '/', { waitUntil: 'domcontentloaded' }); |
| 43 | await p.waitForTimeout(1200); |
| 44 | const pildora = await p.evaluate(() => { |
| 45 | const el = document.getElementById('resume-clone'); |
| 46 | return { display: getComputedStyle(el).display, visible: !!(el.offsetWidth || el.offsetHeight) }; |
| 47 | }); |
| 48 | ok('1 · sin descarga a medias, la píldora Reanudar/Descartar no se ve', pildora.display === 'none' && !pildora.visible, JSON.stringify(pildora)); |
| 49 | ok('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 |
| 52 | await p.click('#clone-btn'); |
| 53 | await p.waitForTimeout(150); |
| 54 | ok('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)); |
| 55 | await p.fill('#clone-url', 'esto no es un repo'); |
| 56 | await p.press('#clone-url', 'Enter'); |
| 57 | await p.waitForTimeout(200); |
| 58 | ok('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)); |
| 59 | await p.fill('#clone-url', 'https://github.com/octo/demo'); |
| 60 | await p.click('#clone-btn'); |
| 61 | await p.waitForTimeout(200); |
| 62 | ok('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 |
| 65 | await p.click('#open-project'); |
| 66 | await p.waitForTimeout(150); |
| 67 | ok('4 · cancelar el selector de «Abrir carpeta» no muestra error', !/⚠️/.test(await estado(p)), await estado(p)); |
| 68 | await p.evaluate(() => { window.__picker.error = 'SecurityError'; }); |
| 69 | await p.click('#open-project'); |
| 70 | await p.waitForTimeout(150); |
| 71 | ok('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 |
| 74 | await 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 | }); |
| 79 | await p.reload({ waitUntil: 'domcontentloaded' }); |
| 80 | await p.waitForTimeout(1200); |
| 81 | const 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 | }); |
| 86 | ok('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)); |
| 87 | ok('2 · owner/repo enlaza al repositorio', banner.href === 'https://github.com/octo/demo' && /noopener/.test(banner.rel), JSON.stringify(banner)); |
| 88 | await p.click('#discard-clone-btn'); |
| 89 | await p.waitForTimeout(200); |
| 90 | const 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 | }); |
| 94 | ok('2 · Descartar quita la píldora y olvida la descarga', !trasDescartar.visible && !trasDescartar.job, JSON.stringify(trasDescartar)); |
| 95 | await 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 |
| 100 | await p.goto(BASE + '/manifest.json'); |
| 101 | await 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 | }); |
| 105 | await p.goto(BASE + '/', { waitUntil: 'domcontentloaded' }); |
| 106 | await p.waitForTimeout(1200); |
| 107 | const 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 | })); |
| 111 | ok('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)); |
| 112 | ok('5 · …y se ofrece probar en el navegador', sinFS.probar); |
| 113 | await p.click('#try-browser'); |
| 114 | await p.waitForFunction(() => !document.getElementById('ide').hidden, null, { timeout: 30000 }).catch(() => {}); |
| 115 | ok('5 · «Probar en el navegador» abre el IDE', await p.evaluate(() => !document.getElementById('ide').hidden)); |
| 116 | await p.waitForTimeout(800); |
| 117 | const 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 | })); |
| 122 | ok('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)); |
| 124 | ok('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 | })); |
| 127 | await ctx.close(); |
| 128 | |
| 129 | console.log('\n errores JS:', errs[0] || 'ninguno'); |
| 130 | ok('sin errores de página', errs.length === 0, errs.join(' | ')); |
| 131 | console.log(fails ? `\n❌ ${fails} FALLO(S)` : '\n✅ LANDING SIN PÍLDORA FANTASMA NI FALLOS MUDOS'); |
| 132 | await b.close(); |
| 133 | process.exit(fails ? 1 : 0); |