| 1 | // Vista previa HTML: los <link rel=stylesheet href=...> y <script src=...> |
| 2 | // LOCALES del proyecto se incrustan de verdad (antes salían sin CSS: un |
| 3 | // iframe con srcdoc no tiene base URL y esas rutas relativas nunca resuelven). |
| 4 | import { chromium } from 'playwright'; |
| 5 | const OUT = (process.env.SCRATCH || '/tmp/elffuss-test'); |
| 6 | const BASE = process.env.BASE || 'http://localhost:8799'; |
| 7 | let fails = 0; const ok = (n, c, e = '') => { console.log((c ? '✅' : '❌') + ' ' + n + (e ? ' — ' + e : '')); if (!c) fails++; }; |
| 8 | |
| 9 | const b = await chromium.launch(); |
| 10 | const ctx = await b.newContext({ locale: 'es-ES' }); // los menús hablan el idioma del navegador; este test mira los textos en español |
| 11 | await ctx.addInitScript(() => { try { localStorage.setItem('elffusscode.model', 'rules'); } catch {} }); |
| 12 | const p = await ctx.newPage(); |
| 13 | await p.goto(BASE + '/', { waitUntil: 'domcontentloaded' }); |
| 14 | await p.evaluate(async () => { |
| 15 | const o = await navigator.storage.getDirectory(); |
| 16 | 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(); }; |
| 17 | await w('style.css', 'body { background: rgb(17, 34, 51); } #marca { color: rgb(255, 0, 0); font-size: 40px; }'); |
| 18 | await w('page.html', '<!doctype html><html><head><link rel="stylesheet" href="style.css"></head><body><h1 id="marca">hola</h1></body></html>'); |
| 19 | }); |
| 20 | await p.goto(BASE + '/?test-opfs', { waitUntil: 'domcontentloaded' }); await p.waitForTimeout(1500); |
| 21 | |
| 22 | await p.evaluate(async () => (await import('/js/ide.js')).openFile('page.html')); |
| 23 | await p.waitForTimeout(300); |
| 24 | // abre vista previa desde el menú de la pestaña |
| 25 | await p.locator('#tabs-bar .tab', { hasText: 'page.html' }).click({ button: 'right' }); |
| 26 | await p.waitForTimeout(150); |
| 27 | await p.click('#tab-menu .tm-item:has-text("Vista previa")'); |
| 28 | await p.waitForTimeout(500); |
| 29 | |
| 30 | const frame = p.frameLocator('#md-preview-pane iframe'); |
| 31 | const bg = await frame.locator('body').evaluate(el => getComputedStyle(el).backgroundColor); |
| 32 | const color = await frame.locator('#marca').evaluate(el => getComputedStyle(el).color); |
| 33 | const fontSize = await frame.locator('#marca').evaluate(el => getComputedStyle(el).fontSize); |
| 34 | ok('el CSS externo (link href) se aplica de verdad — background', bg === 'rgb(17, 34, 51)', bg); |
| 35 | ok('el CSS externo se aplica de verdad — color del texto', color === 'rgb(255, 0, 0)', color); |
| 36 | ok('el CSS externo se aplica de verdad — font-size', fontSize === '40px', fontSize); |
| 37 | |
| 38 | await p.screenshot({ path: OUT + '/htmlpreview_css.png' }); |
| 39 | console.log(fails ? `\n❌ ${fails} FALLO(S)` : '\n✅ VISTA PREVIA HTML: CSS EXTERNO SE APLICA DE VERDAD'); |
| 40 | await b.close(); |
| 41 | process.exit(fails ? 1 : 0); |