| 1 | // Vista previa HTML: el iframe va con sandbox="allow-scripts" a propósito |
| 2 | // (SIN allow-same-origin — aislado del proyecto real, ver ide.js applyView). |
| 3 | // Pero eso hace que `localStorage` LANCE al referenciarla — y como suele ser |
| 4 | // de las primeras líneas de un script, el fichero entero se queda sin |
| 5 | // ejecutar, sin aviso (los botones no hacen nada). Se incrusta un shim en |
| 6 | // memoria SOLO cuando hace falta, sin tocar el sandbox real. |
| 7 | import { chromium } from 'playwright'; |
| 8 | const OUT = (process.env.SCRATCH || '/tmp/elffuss-test'); |
| 9 | const BASE = process.env.BASE || 'http://localhost:8799'; |
| 10 | let fails = 0; const ok = (n, c, e = '') => { console.log((c ? '✅' : '❌') + ' ' + n + (e ? ' — ' + e : '')); if (!c) fails++; }; |
| 11 | |
| 12 | const b = await chromium.launch(); |
| 13 | const ctx = await b.newContext({ locale: 'es-ES' }); // los menús hablan el idioma del navegador; este test mira los textos en español |
| 14 | await ctx.addInitScript(() => { try { localStorage.setItem('elffusscode.model', 'rules'); } catch {} }); |
| 15 | const p = await ctx.newPage(); |
| 16 | const pageErrors = []; |
| 17 | p.on('pageerror', e => pageErrors.push(e.message)); |
| 18 | await p.goto(BASE + '/', { waitUntil: 'domcontentloaded' }); |
| 19 | await p.evaluate(async () => { |
| 20 | const o = await navigator.storage.getDirectory(); |
| 21 | 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(); }; |
| 22 | await w('app.js', |
| 23 | "const tasks = JSON.parse(localStorage.getItem('tasks')) || [];\n" + |
| 24 | "document.getElementById('add').addEventListener('click', () => {\n" + |
| 25 | " tasks.push(document.getElementById('nueva').value);\n" + |
| 26 | " localStorage.setItem('tasks', JSON.stringify(tasks));\n" + |
| 27 | " const li = document.createElement('li'); li.textContent = tasks[tasks.length - 1];\n" + |
| 28 | " document.getElementById('lista').appendChild(li);\n" + |
| 29 | "});\n"); |
| 30 | await w('page.html', '<!doctype html><html><head></head><body>' + |
| 31 | '<input id="nueva" value="tarea real"><button id="add">Add</button><ul id="lista"></ul>' + |
| 32 | '<script src="app.js"></script></body></html>'); |
| 33 | }); |
| 34 | await p.goto(BASE + '/?test-opfs', { waitUntil: 'domcontentloaded' }); await p.waitForTimeout(1500); |
| 35 | |
| 36 | await p.evaluate(async () => (await import('/js/ide.js')).openFile('page.html')); |
| 37 | await p.waitForTimeout(300); |
| 38 | await p.locator('#tabs-bar .tab', { hasText: 'page.html' }).click({ button: 'right' }); |
| 39 | await p.waitForTimeout(150); |
| 40 | await p.click('#tab-menu .tm-item:has-text("Vista previa")'); |
| 41 | await p.waitForTimeout(500); |
| 42 | |
| 43 | ok('sin excepciones de página al cargar (localStorage no revienta el script)', pageErrors.length === 0, pageErrors.join(' | ')); |
| 44 | |
| 45 | const frame = p.frameLocator('#md-preview-pane iframe'); |
| 46 | await frame.locator('#add').click(); |
| 47 | await p.waitForTimeout(200); |
| 48 | const liText = await frame.locator('#lista').innerText().catch(() => ''); |
| 49 | ok('el click SÍ ejecuta el script (antes moría en la 1ª línea, sin avisar)', /tarea real/.test(liText), liText); |
| 50 | |
| 51 | const stored = await frame.locator('body').evaluate(() => localStorage.getItem('tasks')).catch(e => 'ERR:' + e.message); |
| 52 | ok('localStorage.getItem funciona dentro del iframe (shim en memoria)', /tarea real/.test(stored || ''), stored); |
| 53 | |
| 54 | // el shim NUNCA debe tocar el localStorage REAL de la página (aislamiento intacto) |
| 55 | const realStorage = await p.evaluate(() => localStorage.getItem('tasks')); |
| 56 | ok('el localStorage REAL de la IDE no se toca (aislamiento del proyecto real intacto)', realStorage === null, String(realStorage)); |
| 57 | |
| 58 | await p.screenshot({ path: OUT + '/previewstorage.png' }); |
| 59 | console.log(fails ? `\n❌ ${fails} FALLO(S)` : '\n✅ VISTA PREVIA: localStorage no revienta el script (shim aislado) OK'); |
| 60 | await b.close(); |
| 61 | process.exit(fails ? 1 : 0); |