Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 14 additions & 0 deletions apps/desktop/src/app/gateway/hooks/use-gateway-boot.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import { act, cleanup, render } from '@testing-library/react'
import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest'

import { $desktopBoot } from '@/store/boot'
import { $activeGatewayProfile } from '@/store/profile'
import { $gatewayState } from '@/store/session'

import { useGatewayBoot } from './use-gateway-boot'
Expand Down Expand Up @@ -122,6 +123,7 @@ beforeEach(() => {
FakeWebSocket.instances = []
;(globalThis as { WebSocket: unknown }).WebSocket = FakeWebSocket
;(window as { hermesDesktop?: unknown }).hermesDesktop = fakeDesktop()
$activeGatewayProfile.set('default')
$gatewayState.set('idle')
$desktopBoot.set({
error: null,
Expand Down Expand Up @@ -224,6 +226,18 @@ describe('useGatewayBoot remote reconnect loop (real hook, fake socket)', () =>
expect(FakeWebSocket.instances.length).toBeGreaterThan(1)
})

it('seeds the primary gateway profile from the desktop preference before connecting', async () => {
const desktop = fakeDesktop()
desktop.profile.get = vi.fn(async () => ({ profile: 'coder' }))
;(window as { hermesDesktop?: unknown }).hermesDesktop = desktop

render(<Harness />)

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This assertion runs after flushAsync(), so it can pass even if the profile is adopted after getConnection() has already started. Please defer getConnection() and assert the seeded profile before resolving it to lock down the boot-ordering contract.

await flushAsync()

expect($activeGatewayProfile.get()).toBe('coder')
expect(desktop.profile.get).toHaveBeenCalled()
})

it('FIX: after the prolonged drop the hook raises a recoverable boot error (the escape hatch)', async () => {
render(<Harness />)
await flushAsync()
Expand Down
20 changes: 16 additions & 4 deletions apps/desktop/src/app/gateway/hooks/use-gateway-boot.ts
Original file line number Diff line number Diff line change
Expand Up @@ -222,6 +222,20 @@ export function useGatewayBoot({
// Secondary (background-profile) sockets funnel into the same handler.
configureGatewayRegistry({ onEvent: event => callbacksRef.current.handleGatewayEvent(event) })

const seedPrimaryProfileFromPreference = async () => {
try {
const pref = await desktop.profile?.get?.()
const profileKey = normalizeProfileKey(pref?.profile)

$activeGatewayProfile.set(profileKey)
setPrimaryGateway(gateway, profileKey)

return profileKey
} catch {
return null
}
}

const offState = gateway.onState(st => {
// Mirror to the composer only while the primary is the active profile —
// a background secondary reconnect mustn't flip the foreground state.
Expand Down Expand Up @@ -315,6 +329,7 @@ export function useGatewayBoot({

async function boot() {
try {
await seedPrimaryProfileFromPreference()
const conn = await desktop.getConnection()

if (cancelled) {
Expand Down Expand Up @@ -343,10 +358,7 @@ export function useGatewayBoot({
// same-profile resumes are no-op swaps and any reconnect targets the
// right backend. Best-effort: a missing preference means "default".
try {
const pref = await desktop.profile?.get?.()
const profileKey = (pref?.profile ?? '').trim() || 'default'
$activeGatewayProfile.set(profileKey)
setPrimaryGateway(gateway, profileKey)
const profileKey = (await seedPrimaryProfileFromPreference()) || 'default'
void ensureGatewayForProfile(profileKey)
} catch {
$activeGatewayProfile.set('default')
Expand Down
Loading