fix: clear stale backend errors while reconnecting (#2616)

Co-authored-by: Steven Enamakel <enamakel@tinyhumans.ai>
This commit is contained in:
Shrinivas Biradar
2026-05-30 06:47:52 -07:00
committed by GitHub
co-authored by Steven Enamakel
parent 3556842337
commit 695bff458e
2 changed files with 24 additions and 1 deletions
@@ -39,6 +39,23 @@ describe('connectivitySlice', () => {
expect(state.lastError.backend).toBeUndefined();
});
it('setBackend clears the stale backend error when a reconnect begins (connecting)', () => {
// A prior disconnect leaves an error string in state…
let state = connectivityReducer(
undefined,
setBackend({ value: 'disconnected', error: 'transport close' })
);
expect(state.lastError.backend).toBe('transport close');
// …and the reconnect attempt ('connecting') must fully clear it rather than
// leave the key present-but-undefined, so the UI does not surface a stale
// disconnect message during the reconnection window.
state = connectivityReducer(state, setBackend({ value: 'connecting' }));
expect(state.backend).toBe('connecting');
expect(state.lastError.backend).toBeUndefined();
expect('backend' in state.lastError).toBe(false);
});
it('initial internet state is "offline" when navigator.onLine is false (line 33)', () => {
// Simulate the browser reporting no network at boot time.
const originalOnLine = Object.getOwnPropertyDescriptor(navigator, 'onLine');
+7 -1
View File
@@ -58,7 +58,13 @@ const slice = createSlice({
},
setBackend(state, action: PayloadAction<{ value: BackendState; error?: string }>) {
state.backend = action.payload.value;
if (action.payload.value === 'connected') {
if (action.payload.value === 'connected' || action.payload.value === 'connecting') {
// Clear the stale error on both successful connection and reconnect
// attempts. Previously only 'connected' deleted lastError.backend,
// which meant a prior disconnect error (e.g. "transport close") was
// left set to `undefined` — not deleted — during 'connecting'. UI
// components that read lastError.backend could still surface a stale
// message in the reconnect window even though the key appeared falsy.
delete state.lastError.backend;
} else {
state.lastError.backend = action.payload.error;