| 1 | // Menú contextual de pestañas (botón derecho) + Vista previa de markdown. |
| 2 | import { chromium } from 'playwright'; |
| 3 | const OUT = (process.env.SCRATCH || '/tmp/elffuss-test'); |
| 4 | const BASE = process.env.BASE || 'http://localhost:8799'; |
| 5 | let fails = 0; const ok = (n, c, e = '') => { console.log((c ? '✅' : '❌') + ' ' + n + (e ? ' — ' + e : '')); if (!c) fails++; }; |
| 6 | |
| 7 | const b = await chromium.launch(); |
| 8 | const ctx = await b.newContext({ locale: 'es-ES' }); // los menús hablan el idioma del navegador; este test mira los textos en español |
| 9 | await ctx.addInitScript(() => { try { localStorage.setItem('elffusscode.model', 'rules'); } catch {} }); |
| 10 | const p = await ctx.newPage(); |
| 11 | await p.goto(BASE + '/', { waitUntil: 'domcontentloaded' }); |
| 12 | await p.evaluate(async () => { |
| 13 | const o = await navigator.storage.getDirectory(); |
| 14 | 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(); }; |
| 15 | await w('README.md', '# Titulo\n\nUn parrafo con **negrita**.'); |
| 16 | await w('app.js', 'export const app = 1;'); |
| 17 | }); |
| 18 | await p.goto(BASE + '/?test-opfs', { waitUntil: 'domcontentloaded' }); await p.waitForTimeout(1500); |
| 19 | |
| 20 | await p.evaluate(async () => (await import('/js/ide.js')).openFile('README.md')); |
| 21 | await p.evaluate(async () => (await import('/js/ide.js')).openFile('app.js')); |
| 22 | await p.waitForTimeout(300); |
| 23 | ok('2 pestañas abiertas', await p.locator('#tabs-bar .tab').count() === 2); |
| 24 | |
| 25 | // clic derecho en la pestaña README.md abre el menú custom (no el nativo) |
| 26 | const readmeTab = p.locator('#tabs-bar .tab', { hasText: 'README.md' }); |
| 27 | await readmeTab.click({ button: 'right' }); |
| 28 | await p.waitForTimeout(150); |
| 29 | ok('menú contextual de pestaña visible', await p.locator('#tab-menu').count() > 0); |
| 30 | const items = await p.locator('#tab-menu .tm-item').allInnerTexts(); |
| 31 | ok('incluye Cerrar / Cerrar otras / Cerrar todas / Copiar ruta', ['Cerrar', 'Cerrar otras', 'Cerrar todas', 'Copiar ruta'].every(x => items.includes(x)), items.join(' · ')); |
| 32 | ok('README.md (markdown) ofrece Vista previa', items.includes('Vista previa'), items.join(' · ')); |
| 33 | |
| 34 | // Vista previa: renderiza markdown en vez de mostrar el código fuente |
| 35 | await p.click('#tab-menu .tm-item:has-text("Vista previa")'); |
| 36 | await p.waitForTimeout(200); |
| 37 | ok('el editor Monaco se oculta en Vista previa', await p.locator('#editor').evaluate(el => getComputedStyle(el).display === 'none')); |
| 38 | const previewHtml = await p.locator('#md-preview-pane').innerHTML(); |
| 39 | ok('el markdown se renderiza FORMATEADO (mismo motor que chat/Mente: título → <h4>, negrita → <b>)', /<h4/.test(previewHtml) && /<b>/.test(previewHtml), previewHtml.slice(0, 80)); |
| 40 | ok('la pestaña en Vista previa se marca en cursiva', await readmeTab.evaluate(el => el.classList.contains('tab-preview'))); |
| 41 | |
| 42 | // clic derecho de nuevo → ahora ofrece volver a "Ver código fuente" |
| 43 | await readmeTab.click({ button: 'right' }); |
| 44 | await p.waitForTimeout(150); |
| 45 | const items2 = await p.locator('#tab-menu .tm-item').allInnerTexts(); |
| 46 | ok('toggle: ahora ofrece "Ver código fuente"', items2.includes('Ver código fuente'), items2.join(' · ')); |
| 47 | await p.click('#tab-menu .tm-item:has-text("Ver código fuente")'); |
| 48 | await p.waitForTimeout(200); |
| 49 | ok('vuelve a mostrar Monaco (código fuente)', await p.locator('#editor').evaluate(el => getComputedStyle(el).display !== 'none')); |
| 50 | |
| 51 | // app.js (no markdown) NO ofrece Vista previa |
| 52 | const appTab = p.locator('#tabs-bar .tab', { hasText: 'app.js' }); |
| 53 | await appTab.click({ button: 'right' }); |
| 54 | await p.waitForTimeout(150); |
| 55 | const items3 = await p.locator('#tab-menu .tm-item').allInnerTexts(); |
| 56 | ok('app.js (no markdown) NO ofrece Vista previa', !items3.includes('Vista previa'), items3.join(' · ')); |
| 57 | await p.keyboard.press('Escape'); await p.mouse.click(700, 400); await p.waitForTimeout(100); |
| 58 | |
| 59 | // Cerrar otras |
| 60 | await readmeTab.click({ button: 'right' }); |
| 61 | await p.waitForTimeout(150); |
| 62 | await p.click('#tab-menu .tm-item:has-text("Cerrar otras")'); |
| 63 | await p.waitForTimeout(200); |
| 64 | ok('«Cerrar otras» deja solo la pestaña activa', await p.locator('#tabs-bar .tab').count() === 1 && await p.locator('#tabs-bar .tab', { hasText: 'README.md' }).count() === 1); |
| 65 | |
| 66 | await p.screenshot({ path: OUT + '/tabmenu.png' }); |
| 67 | console.log('captura → tabmenu.png'); |
| 68 | console.log(fails ? `\n❌ ${fails} FALLO(S)` : '\n✅ MENÚ DE PESTAÑAS + VISTA PREVIA MARKDOWN OK'); |
| 69 | await b.close(); |
| 70 | process.exit(fails ? 1 : 0); |