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