| 1 | // Bug real reportado: el historial de la Mente («≡ historial») vivía SOLO en |
| 2 | // memoria/DOM — al recargar la página (cerrar pestaña, refrescar) se perdía |
| 3 | // todo. Ahora se persiste en IndexedDB y sobrevive a una recarga REAL. |
| 4 | import { chromium } from 'playwright'; |
| 5 | import { CHROME_ARGS } from './chrome-args.mjs'; |
| 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({ args: ['--enable-unsafe-webgpu', ...CHROME_ARGS, '--autoplay-policy=no-user-gesture-required'] }); |
| 10 | const ctx = await b.newContext(); await ctx.addInitScript(() => { try { localStorage.setItem('elffusscode.model', 'rules'); } catch {} }); |
| 11 | const p = await ctx.newPage({ viewport: { width: 1300, height: 850 } }); |
| 12 | await p.goto(BASE + '/?test-opfs', { waitUntil: 'domcontentloaded' }); await p.waitForTimeout(1500); |
| 13 | |
| 14 | await p.locator('#activity img').click(); await p.waitForTimeout(1000); |
| 15 | await p.evaluate(async () => { |
| 16 | const m = await import('/js/mind.js'); |
| 17 | m.pushThought('ceo', { type: 'cycle', n: 1, text: 'Ciclo de prueba antes de recargar la página.' }); |
| 18 | m.pushThought('arq', { type: 'open', name: 'Arquitectura' }); |
| 19 | m.pushThought('arq', { type: 'token', text: 'esta línea debe sobrevivir a la recarga de verdad.\n' }); |
| 20 | }); |
| 21 | await p.waitForTimeout(700); // que el guardado (debounce 400ms) llegue a disco |
| 22 | const before = await p.evaluate(() => document.getElementById('mind-log-body').innerText); |
| 23 | ok('el historial se ve ANTES de recargar', /debe sobrevivir/.test(before), before.slice(0, 60)); |
| 24 | |
| 25 | // RECARGA REAL de la página (no solo cerrar/abrir la Mente en la misma sesión) |
| 26 | await p.goto(BASE + '/?test-opfs', { waitUntil: 'domcontentloaded' }); await p.waitForTimeout(1500); |
| 27 | await p.locator('#activity img').click(); |
| 28 | // espera a que la Mente pinte su historial (con un tope), no un tiempo fijo: |
| 29 | // con extensiones de código arrancando a la vez la página tarda más en pintarlo |
| 30 | let after = ''; |
| 31 | for (let i = 0; i < 50 && !/debe sobrevivir/.test(after); i++) { await p.waitForTimeout(200); after = await p.evaluate(() => document.getElementById('mind-log-body')?.innerText || ''); } |
| 32 | ok('el historial SOBREVIVE a una recarga real de la página', /debe sobrevivir/.test(after), after.slice(0, 80)); |
| 33 | ok('también se conserva el ciclo/evento del CEO', /Ciclo de prueba/.test(after)); |
| 34 | |
| 35 | console.log(fails ? `\n❌ ${fails} FALLO(S)` : '\n✅ HISTORIAL PERSISTENTE OK — sobrevive a recargar la página'); |
| 36 | await b.close(); |
| 37 | process.exit(fails ? 1 : 0); |