| 1 | // Editor: guardar de verdad, no perder cambios al cerrar, y el árbol que no se |
| 2 | // pliega solo. |
| 3 | // |
| 4 | // 1. Cerrar una pestaña con cambios (●) pregunta: Cancelar la deja abierta, |
| 5 | // «No guardar» la cierra sin tocar el disco, «Guardar» escribe y cierra. |
| 6 | // Antes se cerraba y los cambios se perdían sin aviso. |
| 7 | // 2. File › Guardar y «Guardar» de la paleta guardan (antes lanzaban una |
| 8 | // acción que el Monaco suelto no tiene: no pasaba nada) y la paleta se |
| 9 | // cierra. |
| 10 | // 3. El atajo anunciado es el de esta máquina (⌘S en Mac, Ctrl+S si no). |
| 11 | // 4. Una escritura del agente repinta el árbol sin plegar lo que estaba |
| 12 | // abierto. |
| 13 | // 5. Renombrar mueve la pestaña abierta y lo dice; borrar cierra la pestaña; |
| 14 | // «Copiar ruta» lo dice. |
| 15 | // 6. El árbol se usa con el teclado (↓ y Enter) y Escape cierra su menú. |
| 16 | // 7. «Ir a símbolo» sin fichero abierto lo dice en vez de no hacer nada. |
| 17 | // |
| 18 | // BASE=http://127.0.0.1:8898 node tests/editor-save.mjs |
| 19 | import { chromium } from 'playwright'; |
| 20 | const BASE = process.env.BASE || 'http://localhost:8799'; |
| 21 | let fails = 0; const ok = (n, c, e = '') => { console.log((c ? '✅' : '❌') + ' ' + n + (e ? ' — ' + e : '')); if (!c) fails++; }; |
| 22 | |
| 23 | const b = await chromium.launch(); |
| 24 | const ctx = await b.newContext({ locale: 'es-ES' }); |
| 25 | await ctx.grantPermissions(['clipboard-read', 'clipboard-write'], { origin: BASE }); |
| 26 | await ctx.addInitScript(() => { try { localStorage.setItem('elffusscode.model', 'rules'); } catch {} }); |
| 27 | const p = await ctx.newPage({ viewport: { width: 1500, height: 900 } }); |
| 28 | const errs = []; p.on('pageerror', e => errs.push(e.message.slice(0, 160))); |
| 29 | |
| 30 | // proyecto sembrado ANTES de arrancar el IDE |
| 31 | await p.goto(BASE + '/manifest.json'); |
| 32 | await p.evaluate(async () => { |
| 33 | const root = await navigator.storage.getDirectory(); |
| 34 | const escribe = async (path, txt) => { |
| 35 | const parts = path.split('/'); const name = parts.pop(); let d = root; |
| 36 | for (const x of parts) d = await d.getDirectoryHandle(x, { create: true }); |
| 37 | const w = await (await d.getFileHandle(name, { create: true })).createWritable(); await w.write(txt); await w.close(); |
| 38 | }; |
| 39 | await escribe('README.md', '# Proyecto\n'); |
| 40 | await escribe('src/app.js', 'export const a = 1;\n'); |
| 41 | await escribe('src/util.js', 'export const u = 2;\n'); |
| 42 | await escribe('notes/todo.md', '- tarea\n'); |
| 43 | }); |
| 44 | await p.goto(BASE + '/?test-opfs', { waitUntil: 'domcontentloaded' }); |
| 45 | await p.waitForFunction(() => window.monaco && document.querySelector('#tree .file'), null, { timeout: 30000 }); |
| 46 | |
| 47 | const disco = (path) => p.evaluate(async (path) => { |
| 48 | const parts = path.split('/'); const name = parts.pop(); let d = await navigator.storage.getDirectory(); |
| 49 | try { for (const x of parts) d = await d.getDirectoryHandle(x); return await (await (await d.getFileHandle(name)).getFile()).text(); } |
| 50 | catch { return null; } |
| 51 | }, path); |
| 52 | const pestañas = () => p.evaluate(() => [...document.querySelectorAll('#tabs-bar .tab')].map(t => ({ path: t.dataset.path, dirty: !!t.querySelector('.dirty'), active: t.classList.contains('active') }))); |
| 53 | const estado = () => p.evaluate(() => document.getElementById('statusbar').textContent); |
| 54 | const abre = async (path) => { await p.evaluate((path) => window.elffussClaw.openFile(path), path); await p.waitForTimeout(200); }; |
| 55 | const escribeEnEditor = async (txt) => { |
| 56 | await p.click('#editor .view-lines'); |
| 57 | await p.keyboard.press(process.platform === 'darwin' ? 'Meta+End' : 'Control+End'); |
| 58 | await p.keyboard.type(txt); |
| 59 | await p.waitForTimeout(100); |
| 60 | }; |
| 61 | const cerrarActiva = () => p.locator('#tabs-bar .tab.active').locator('.tab-x, b').first().click(); |
| 62 | const dialogo = (val) => p.locator(`#dirty-dialog button[data-val="${val}"]`).click(); |
| 63 | |
| 64 | // 1 · cerrar con cambios |
| 65 | await abre('src/app.js'); |
| 66 | await escribeEnEditor('// cambio 1\n'); |
| 67 | let tb = await pestañas(); |
| 68 | ok('1 · la pestaña queda marcada con cambios (●)', tb.find(t => t.path === 'src/app.js')?.dirty, JSON.stringify(tb)); |
| 69 | await cerrarActiva(); |
| 70 | await p.waitForTimeout(150); |
| 71 | const hayDialogo = await p.locator('#dirty-dialog').isVisible().catch(() => false); |
| 72 | ok('1 · cerrar una pestaña con cambios pregunta', hayDialogo); |
| 73 | await dialogo('cancel'); |
| 74 | tb = await pestañas(); |
| 75 | ok('1 · Cancelar: la pestaña sigue abierta y con sus cambios', tb.some(t => t.path === 'src/app.js' && t.dirty), JSON.stringify(tb)); |
| 76 | await cerrarActiva(); |
| 77 | await dialogo('discard'); |
| 78 | await p.waitForTimeout(150); |
| 79 | tb = await pestañas(); |
| 80 | ok('1 · No guardar: se cierra y el disco no cambia', !tb.some(t => t.path === 'src/app.js') && (await disco('src/app.js')) === 'export const a = 1;\n', JSON.stringify(tb)); |
| 81 | await abre('src/app.js'); |
| 82 | await escribeEnEditor('// cambio 2\n'); |
| 83 | await cerrarActiva(); |
| 84 | await dialogo('save'); |
| 85 | await p.waitForTimeout(250); |
| 86 | tb = await pestañas(); |
| 87 | const tras = await disco('src/app.js'); |
| 88 | ok('1 · Guardar: escribe en disco y cierra', !tb.some(t => t.path === 'src/app.js') && tras.includes('// cambio 2'), JSON.stringify({ tb, tras })); |
| 89 | |
| 90 | // 2 · File › Guardar |
| 91 | await abre('README.md'); |
| 92 | await escribeEnEditor('linea nueva\n'); |
| 93 | await p.click('#menubar button[data-menu="file"]'); |
| 94 | const hint = await p.locator('#topmenu .menu-item', { hasText: 'Guardar' }).locator('span').textContent(); |
| 95 | await p.locator('#topmenu .menu-item', { hasText: 'Guardar' }).click(); |
| 96 | await p.waitForTimeout(250); |
| 97 | tb = await pestañas(); |
| 98 | ok('2 · File › Guardar escribe el fichero', (await disco('README.md')).includes('linea nueva')); |
| 99 | ok('2 · …la pestaña deja de estar marcada y la barra de estado lo dice', !tb.find(t => t.path === 'README.md')?.dirty && /guardado/.test(await estado()), JSON.stringify(tb) + ' · ' + await estado()); |
| 100 | const esMac = await p.evaluate(() => /Mac|iPhone|iPad/.test(navigator.platform || '')); |
| 101 | ok('3 · el atajo anunciado es el de esta máquina', hint === (esMac ? '⌘S' : 'Ctrl+S'), hint); |
| 102 | |
| 103 | // 2 · paleta › Guardar |
| 104 | await escribeEnEditor('otra linea\n'); |
| 105 | await p.keyboard.press(process.platform === 'darwin' ? 'Meta+Shift+P' : 'Control+Shift+P'); |
| 106 | await p.waitForSelector('#palette:not([hidden])'); |
| 107 | await p.fill('#pal-input', '>Guardar'); |
| 108 | await p.waitForTimeout(100); |
| 109 | await p.press('#pal-input', 'Enter'); |
| 110 | await p.waitForTimeout(250); |
| 111 | ok('2 · la paleta › Guardar escribe el fichero', (await disco('README.md')).includes('otra linea')); |
| 112 | ok('2 · …y la paleta se cierra', await p.locator('#palette').isHidden()); |
| 113 | |
| 114 | // 4 · el árbol no se pliega tras una escritura del agente |
| 115 | await p.locator('#tree summary', { hasText: 'src' }).click(); |
| 116 | await p.locator('#tree summary', { hasText: 'notes' }).click(); |
| 117 | await p.waitForTimeout(200); |
| 118 | await p.evaluate(async () => { const code = await import('/js/tools/code.js'); await code.write({ path: 'src/nuevo.js', content: 'export {}\n' }); }); |
| 119 | await p.waitForTimeout(400); |
| 120 | const abiertas = await p.evaluate(() => [...document.querySelectorAll('#tree details[open] > summary')].map(s => s.dataset.path)); |
| 121 | ok('4 · tras escribir el agente, src y notes siguen abiertas', abiertas.includes('src') && abiertas.includes('notes'), JSON.stringify(abiertas)); |
| 122 | ok('4 · …y el fichero nuevo aparece dentro', await p.locator('#tree .file[data-path="src/nuevo.js"]').count() === 1); |
| 123 | |
| 124 | // 5 · renombrar, copiar ruta y borrar |
| 125 | await abre('src/util.js'); |
| 126 | await p.evaluate(() => { window.prompt = () => 'util2.js'; window.confirm = () => true; }); |
| 127 | await p.locator('#tree .file[data-path="src/util.js"]').click({ button: 'right' }); |
| 128 | await p.locator('#tree-menu .tm-item', { hasText: 'Renombrar' }).click(); |
| 129 | await p.waitForTimeout(400); |
| 130 | tb = await pestañas(); |
| 131 | ok('5 · renombrar mueve la pestaña abierta a la ruta nueva', tb.some(t => t.path === 'src/util2.js') && !tb.some(t => t.path === 'src/util.js'), JSON.stringify(tb)); |
| 132 | ok('5 · …y lo dice en la barra de estado', /Renombrado/.test(await estado()), await estado()); |
| 133 | await p.locator('#tabs-bar .tab[data-path="src/util2.js"]').click({ button: 'right' }); |
| 134 | await p.locator('#tab-menu .tm-item', { hasText: 'Copiar ruta' }).click(); |
| 135 | await p.waitForTimeout(200); |
| 136 | ok('5 · «Copiar ruta» lo dice', /Ruta copiada|No pude copiar/.test(await estado()), await estado()); |
| 137 | await p.locator('#tree .file[data-path="src/util2.js"]').click({ button: 'right' }); |
| 138 | await p.locator('#tree-menu .tm-item', { hasText: 'Eliminar' }).click(); |
| 139 | await p.waitForTimeout(400); |
| 140 | tb = await pestañas(); |
| 141 | ok('5 · borrar el fichero cierra su pestaña', !tb.some(t => t.path === 'src/util2.js'), JSON.stringify(tb)); |
| 142 | |
| 143 | // 6 · teclado en el árbol y Escape en su menú |
| 144 | await p.locator('#tree .file[data-path="README.md"]').focus(); |
| 145 | await p.keyboard.press('ArrowUp'); |
| 146 | const enfocado = await p.evaluate(() => document.activeElement?.dataset?.path); |
| 147 | ok('6 · ↑ mueve el foco por las filas del árbol', !!enfocado && enfocado !== 'README.md', enfocado); |
| 148 | await p.locator('#tree .file[data-path="src/app.js"]').focus(); |
| 149 | await p.keyboard.press('Enter'); |
| 150 | await p.waitForTimeout(250); |
| 151 | ok('6 · Enter en una fila de fichero lo abre', (await pestañas()).some(t => t.path === 'src/app.js' && t.active)); |
| 152 | await p.keyboard.press('Shift+F10'); |
| 153 | await p.waitForTimeout(100); |
| 154 | const menuAbierto = await p.locator('#tree-menu').count(); |
| 155 | await p.keyboard.press('Escape'); |
| 156 | await p.waitForTimeout(100); |
| 157 | ok('6 · Shift+F10 abre el menú del árbol y Escape lo cierra', menuAbierto === 1 && await p.locator('#tree-menu').count() === 0); |
| 158 | |
| 159 | // 7 · Ir a símbolo sin fichero abierto |
| 160 | await p.evaluate(() => { for (const x of document.querySelectorAll('#tabs-bar .tab-x')) x.click(); }); |
| 161 | await p.waitForTimeout(300); |
| 162 | await p.click('#menubar button[data-menu="edit"]'); |
| 163 | await p.locator('#topmenu .menu-item', { hasText: 'Ir a símbolo' }).click(); |
| 164 | await p.waitForTimeout(150); |
| 165 | ok('7 · «Ir a símbolo» sin fichero abierto lo dice', /Abre un fichero primero/.test(await estado()), await estado()); |
| 166 | |
| 167 | console.log('\n errores JS:', errs[0] || 'ninguno'); |
| 168 | ok('sin errores de página', errs.length === 0, errs.join(' | ')); |
| 169 | console.log(fails ? `\n❌ ${fails} FALLO(S)` : '\n✅ EDITOR: GUARDAR, CERRAR CON CAMBIOS Y ÁRBOL OK'); |
| 170 | await b.close(); |
| 171 | process.exit(fails ? 1 : 0); |