| 1 | // Vista previa HTML (como la de markdown) + disponible también desde el |
| 2 | // menú contextual del explorador de ficheros (no solo el de la pestaña). |
| 3 | import { chromium } from 'playwright'; |
| 4 | const OUT = (process.env.SCRATCH || '/tmp/elffuss-test'); |
| 5 | const BASE = process.env.BASE || 'http://localhost:8799'; |
| 6 | let fails = 0; const ok = (n, c, e = '') => { console.log((c ? '✅' : '❌') + ' ' + n + (e ? ' — ' + e : '')); if (!c) fails++; }; |
| 7 | |
| 8 | const b = await chromium.launch(); |
| 9 | const ctx = await b.newContext({ locale: 'es-ES' }); // los menús hablan el idioma del navegador; este test mira los textos en español |
| 10 | await ctx.addInitScript(() => { try { localStorage.setItem('elffusscode.model', 'rules'); } catch {} }); |
| 11 | const p = await ctx.newPage(); |
| 12 | await p.goto(BASE + '/', { waitUntil: 'domcontentloaded' }); |
| 13 | await p.evaluate(async () => { |
| 14 | const o = await navigator.storage.getDirectory(); |
| 15 | const w = async (n, t) => { const f = await o.getFileHandle(n, { create: true }); const s = await f.createWritable(); await s.write(t); await s.close(); }; |
| 16 | await w('page.html', '<!doctype html><html><body><h1 id="marca">hola desde el HTML real</h1></body></html>'); |
| 17 | }); |
| 18 | await p.goto(BASE + '/?test-opfs', { waitUntil: 'domcontentloaded' }); await p.waitForTimeout(1500); |
| 19 | |
| 20 | // desde el EXPLORADOR (árbol), no desde una pestaña ya abierta |
| 21 | const fileRow = p.locator('#tree .file', { hasText: 'page.html' }); |
| 22 | await fileRow.click({ button: 'right' }); |
| 23 | await p.waitForTimeout(150); |
| 24 | ok('el árbol ofrece "Vista previa" para un .html', (await p.locator('#tree-menu .tm-item').allInnerTexts()).includes('Vista previa')); |
| 25 | await p.click('#tree-menu .tm-item:has-text("Vista previa")'); |
| 26 | await p.waitForTimeout(300); |
| 27 | |
| 28 | ok('se abrió como pestaña y quedó en Vista previa', await p.locator('#tabs-bar .tab.tab-preview').count() === 1); |
| 29 | ok('el editor Monaco se oculta', await p.locator('#editor').evaluate(el => getComputedStyle(el).display === 'none')); |
| 30 | const frameCount = await p.locator('#md-preview-pane iframe').count(); |
| 31 | ok('se muestra un iframe real (no HTML escapado como texto)', frameCount === 1); |
| 32 | const frame = p.frameLocator('#md-preview-pane iframe'); |
| 33 | ok('el HTML se renderiza DE VERDAD (el <h1> real existe en el DOM del iframe)', await frame.locator('#marca').count() === 1); |
| 34 | ok('el texto del h1 real aparece', (await frame.locator('#marca').innerText()) === 'hola desde el HTML real'); |
| 35 | |
| 36 | await p.screenshot({ path: OUT + '/htmlpreview.png' }); |
| 37 | console.log('captura → htmlpreview.png'); |
| 38 | console.log(fails ? `\n❌ ${fails} FALLO(S)` : '\n✅ VISTA PREVIA HTML (desde el explorador) OK'); |
| 39 | await b.close(); |
| 40 | process.exit(fails ? 1 : 0); |