| 1 | // «Desconectar» el bridge aguanta una recarga. |
| 2 | // |
| 3 | // Ajustes dice «No se volverá a conectar solo hasta que pulses Conectar», pero |
| 4 | // la pausa vivía solo en memoria: al recargar, tryAutoConnect() volvía a |
| 5 | // conectar con el token guardado. Ahora la pausa se guarda y «Conectar» la quita. |
| 6 | // |
| 7 | // Sin bridge de verdad: se cuentan los WebSocket que la página intenta abrir |
| 8 | // (contra 127.0.0.1:8765 no contesta nadie, y no hace falta). |
| 9 | // |
| 10 | // BASE=http://127.0.0.1:8898 node tests/bridge-pause.mjs |
| 11 | import { chromium } from 'playwright'; |
| 12 | const BASE = process.env.BASE || 'http://localhost:8799'; |
| 13 | let fails = 0; const ok = (n, c, e = '') => { console.log((c ? '✅' : '❌') + ' ' + n + (e ? ' — ' + e : '')); if (!c) fails++; }; |
| 14 | |
| 15 | const b = await chromium.launch(); |
| 16 | const ctx = await b.newContext({ locale: 'es-ES' }); |
| 17 | await ctx.addInitScript(() => { |
| 18 | try { |
| 19 | localStorage.setItem('elffusscode.model', 'rules'); |
| 20 | if (!sessionStorage.getItem('sembrado')) { localStorage.setItem('elffusscode.bridgeToken', 'tok-de-prueba'); sessionStorage.setItem('sembrado', '1'); } |
| 21 | } catch {} |
| 22 | window.__ws = 0; |
| 23 | const W = window.WebSocket; |
| 24 | window.WebSocket = function (...a) { window.__ws++; return new W(...a); }; |
| 25 | window.WebSocket.prototype = W.prototype; |
| 26 | }); |
| 27 | const p = await ctx.newPage(); |
| 28 | const errs = []; p.on('pageerror', e => errs.push(e.message.slice(0, 160))); |
| 29 | const listo = () => p.waitForFunction(() => window.elffussClaw?.conv?.getActive?.(), null, { timeout: 15000 }); |
| 30 | |
| 31 | await p.goto(BASE + '/?test-opfs', { waitUntil: 'domcontentloaded' }); |
| 32 | await listo(); |
| 33 | await p.waitForTimeout(800); |
| 34 | ok('con un token guardado, al abrir intenta conectar solo', await p.evaluate(() => window.__ws) >= 1, String(await p.evaluate(() => window.__ws))); |
| 35 | |
| 36 | await p.evaluate(async () => (await import('/js/bridge.js')).disconnect()); |
| 37 | ok('«Desconectar» se guarda', await p.evaluate(() => localStorage.getItem('elffusscode.bridgePaused')) === '1'); |
| 38 | |
| 39 | await p.reload({ waitUntil: 'domcontentloaded' }); |
| 40 | await listo(); |
| 41 | await p.waitForTimeout(1500); |
| 42 | const tras = await p.evaluate(async () => ({ ws: window.__ws, conectado: (await import('/js/bridge.js')).isConnected(), auto: await (await import('/js/bridge.js')).tryAutoConnect() })); |
| 43 | ok('tras recargar NO vuelve a conectar solo (ni lo intenta)', tras.ws === 0 && !tras.conectado && tras.auto === false, JSON.stringify(tras)); |
| 44 | |
| 45 | await p.evaluate(async () => { await (await import('/js/bridge.js')).connect('tok-de-prueba').catch(() => {}); }); |
| 46 | ok('«Conectar» quita la pausa', await p.evaluate(() => localStorage.getItem('elffusscode.bridgePaused')) === null); |
| 47 | |
| 48 | ok('sin errores de página', errs.length === 0, errs.join(' | ')); |
| 49 | console.log(fails ? `\n❌ ${fails} FALLO(S)` : '\n✅ BRIDGE: «Desconectar» aguanta una recarga'); |
| 50 | await b.close(); |
| 51 | process.exit(fails ? 1 : 0); |