Elffuss BraidHub

utopia-ia/elffuss-code public

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