Elffuss BraidHub

utopia-ia/elffuss-code public

Bridge: "Disconnect" is remembered across a reload

"Disconnect" says it will not reconnect by itself until you press
Connect, but the pause lived only in memory: a reload ran tryAutoConnect()
with the saved token and reconnected. The pause is now stored
(elffusscode.bridgePaused), honoured by tryAutoConnect() and the
background detection, and cleared by connect(), which is the Connect
button.

bridge-pause.mjs (no real bridge; it counts the WebSockets the page
opens): after Disconnect and a reload the page does not even try to
connect, and Connect clears the pause. Fails on the previous tree.
Changeset5d9d7359c26adfb2480330393265b21508a0a2f95f153757bcaef62059e51cbf
AuthorKikoCis <kikocisneros@gmail.com> · 2026-10-10 19:28 +02:00
CommitterKikoCis <kikocisneros@gmail.com> · 2026-10-11 14:15 +02:00
Parentscc9d7422202d

Browse the files at this changeset

2 files changed

added tests/bridge-pause.mjs +51 −0
@@ -0,0 +1,51 @@
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);
modified web/js/bridge.js +8 −2
@@ -13,7 +13,13 @@
1313 const onStatus = (s) => { for (const f of oyentes) { try { f(s); } catch { /* */ } } };
1414 // «Desconectar» a propósito: la autodetección no vuelve a conectar sola hasta
1515 // que se pulse «Conectar» otra vez (antes reconectaba a los pocos segundos).
16+// Se GUARDA: solo en memoria, al recargar tryAutoConnect() volvía a conectar con
17+// el token guardado, y el aviso decía «no se volverá a conectar solo hasta que
18+// pulses Conectar».
19+const CLAVE_PAUSA = 'elffusscode.bridgePaused';
1620 let pausado = false;
21+try { pausado = localStorage.getItem(CLAVE_PAUSA) === '1'; } catch { /* */ }
22+const guardaPausa = v => { try { if (v) localStorage.setItem(CLAVE_PAUSA, '1'); else localStorage.removeItem(CLAVE_PAUSA); } catch { /* */ } };
1723 let savedFolder = '';
1824 try { savedFolder = localStorage.getItem('elffusscode.bridgeFolder') || ''; } catch { /* */ }
1925 let savedToken = '';
@@ -40,7 +46,7 @@
4046 }
4147
4248 export function connect(token) {
43- pausado = false;
49+ pausado = false; guardaPausa(false);
4450 return new Promise((resolve, reject) => {
4551 let sock;
4652 try { sock = ws = new WebSocket('ws://' + HOST + '/ws'); }
@@ -79,7 +85,7 @@
7985 }
8086
8187 export function disconnect() {
82- pausado = true;
88+ pausado = true; guardaPausa(true);
8389 try { ws?.close(); } catch { /* */ }
8490 connected = false; authed = false;
8591 onStatus('disconnected');