diff --git a/apps/desktop/src/store/profile.test.ts b/apps/desktop/src/store/profile.test.ts index a4c6d75e3d..e6825de85a 100644 --- a/apps/desktop/src/store/profile.test.ts +++ b/apps/desktop/src/store/profile.test.ts @@ -9,7 +9,7 @@ import type { ProfileInfo } from '@/types/hermes' const ensureGatewayForProfile = vi.fn(async () => undefined) const ensureGatewayForAgent = vi.fn(async () => undefined) const openGatewayForProfile = vi.fn(async (_profile: string) => undefined) -const $gateway = atom({ id: 'live-socket' }) +const $gateway = atom({ id: 'live-socket', connectionState: 'open' }) const resetStarmapGraph = vi.fn() vi.mock('@/store/gateway', () => ({ $gateway, ensureGatewayForAgent, ensureGatewayForProfile, openGatewayForProfile })) @@ -55,7 +55,7 @@ beforeEach(() => { getConnection.mockReset() ensureGatewayForProfile.mockClear() openGatewayForProfile.mockClear() - $gateway.set({ id: 'live-socket' }) + $gateway.set({ id: 'live-socket', connectionState: 'open' }) $activeGatewayProfile.set('default') $connection.set(localConn()) $profiles.set([]) @@ -115,6 +115,17 @@ describe('ensureGatewayProfile → $connection sync (#46651)', () => { expect(ensureGatewayForProfile).not.toHaveBeenCalled() expect($connection.get()?.mode).toBe('remote') }) + + it('reconnects when the target profile is active but its gateway socket is closed', async () => { + $activeGatewayProfile.set('vps-remote') + $connection.set(remoteConn()) + $gateway.set({ connectionState: 'closed' }) + getConnection.mockResolvedValue(remoteConn()) + + await ensureGatewayProfile('vps-remote') + + expect(ensureGatewayForProfile).toHaveBeenCalledWith('vps-remote') + }) }) describe('profile-scoped cache invalidation', () => { diff --git a/apps/desktop/src/store/profile.ts b/apps/desktop/src/store/profile.ts index 09ab31619f..1a5431687a 100644 --- a/apps/desktop/src/store/profile.ts +++ b/apps/desktop/src/store/profile.ts @@ -491,7 +491,7 @@ export async function ensureGatewayProfile(profile: string | null | undefined): const target = normalizeProfileKey(profile) - if (normalizeProfileKey($activeGatewayProfile.get()) === target && $gateway.get()) { + if (normalizeProfileKey($activeGatewayProfile.get()) === target && $gateway.get()?.connectionState === 'open') { return } @@ -503,7 +503,7 @@ export async function ensureGatewayProfile(profile: string | null | undefined): await gatewaySwitch.catch(() => undefined) } - if (normalizeProfileKey($activeGatewayProfile.get()) === target && $gateway.get()) { + if (normalizeProfileKey($activeGatewayProfile.get()) === target && $gateway.get()?.connectionState === 'open') { return }