Evenfire is an intelligence layer platform for running LLM agents that take real actions, on your infrastructure
TypeScript
test(desktop-app): verify environment switch confirmation flow (R12-M7)
Josué805c209fa4f2
diff --git a/desktop-app/ui/src/__tests__/App.deepLinks.test.tsx b/desktop-app/ui/src/__tests__/App.deepLinks.test.tsxindex c68d2da12..2b04fdf3b 100644--- a/desktop-app/ui/src/__tests__/App.deepLinks.test.tsx+++ b/desktop-app/ui/src/__tests__/App.deepLinks.test.tsx@@ -48,6 +48,8 @@ const confirmDialogHarness = vi.hoisted(() => ({props: null as null | {title: stringbody?: ReactNode+ cancelLabel?: string+ confirmLabel?: stringonCancel: () => voidonConfirm: () => void},@@ -400,6 +402,31 @@ describe('App deep-link orchestration', () => {expect(dialogBody).not.toContain('https://rpc.example.test')})+ it('wires environment switch confirmation actions to the controller', () => {+ const cancelSwitch = vi.fn()+ const confirmSwitch = vi.fn()+ currentController = makeController({+ initialExperienceLoading: false,+ pendingDesktopEnvironmentSwitchConfirmation: {+ activeEnvironmentName: 'Current environment',+ activeExternalRestApiBaseUrl: 'https://current-api.example.test/api/v1',+ targetEnvironmentName: 'Target environment',+ targetExternalRestApiBaseUrl: 'https://target-api.example.test/api/v1',+ },+ handleCancelDesktopEnvironmentSwitchConfirmation: cancelSwitch,+ handleConfirmDesktopEnvironmentSwitchConfirmation: confirmSwitch,+ } as Partial<AppController>)++ render(<App />)++ expect(confirmDialogHarness.props?.title).toBe('Switch desktop environment?')+ expect(confirmDialogHarness.props?.confirmLabel).toBe('Sign out and switch')+ act(() => confirmDialogHarness.props?.onCancel())+ expect(cancelSwitch).toHaveBeenCalledOnce()+ act(() => confirmDialogHarness.props?.onConfirm())+ expect(confirmSwitch).toHaveBeenCalledOnce()+ })+beforeEach(() => {vi.clearAllMocks()confirmDialogHarness.props = nulldiff --git a/desktop-app/ui/src/hooks/__tests__/useAppController.desktopEnvironmentHandoff.test.tsx b/desktop-app/ui/src/hooks/__tests__/useAppController.desktopEnvironmentHandoff.test.tsxnew file mode 100644index 000000000..deea08fd5--- /dev/null+++ b/desktop-app/ui/src/hooks/__tests__/useAppController.desktopEnvironmentHandoff.test.tsx@@ -0,0 +1,115 @@+// @vitest-environment jsdom+import { afterEach, describe, expect, it, vi } from 'vitest'+import { act, waitFor } from '@testing-library/react'+import type { DesktopRuntimeConfigState } from '../../../../src/types'+import {+ installAppControllerClerum,+ renderAppController,+} from '../domain/__tests__/__fixtures__/appControllerHarness'+import { uninstallMockClerum } from '../domain/__tests__/__fixtures__/mockClerum'++;(globalThis as { IS_REACT_ACT_ENVIRONMENT?: boolean }).IS_REACT_ACT_ENVIRONMENT = true++function runtimeConfigState(): DesktopRuntimeConfigState {+ const current = {+ appName: 'Current environment',+ externalRestApiBaseUrl: 'https://current-api.example.test/api/v1',+ rpcProxyBaseUrl: 'https://current-rpc.example.test',+ }+ const target = {+ appName: 'Target environment',+ externalRestApiBaseUrl: 'https://target-api.example.test/api/v1',+ rpcProxyBaseUrl: 'https://target-rpc.example.test',+ }+ return {+ configured: true,+ isLocalhost: false,+ selectorVisible: true,+ activeOptionId: 'current-profile',+ currentConfig: current,+ envKey: 'current-environment',+ storagePath: '/harness/runtime-configs',+ options: [+ {+ id: 'current-profile',+ label: 'Current environment',+ source: 'file',+ configPath: '/harness/current.json',+ ...current,+ },+ {+ id: 'target-profile',+ label: 'Target environment',+ source: 'file',+ configPath: '/harness/target.json',+ ...target,+ },+ ],+ }+}++describe('useAppController desktop environment handoff', () => {+ let unmount: (() => void) | null = null++ afterEach(() => {+ unmount?.()+ unmount = null+ vi.restoreAllMocks()+ uninstallMockClerum()+ })++ async function openSwitchConfirmation() {+ const { clerum, handle } = installAppControllerClerum({+ runtimeConfigState: runtimeConfigState(),+ })+ const app = renderAppController()+ unmount = app.unmount+ await waitFor(() => expect(app.result.current.booting).toBe(false))+ await waitFor(() => expect(app.result.current.initialExperienceLoading).toBe(false))+ await waitFor(() => expect(handle.onDesktopEnvironmentSetup).toHaveBeenCalledOnce())++ let handoff!: Promise<void>+ await act(async () => {+ handoff = handle.emitDesktopEnvironmentSetup({+ appName: 'Target environment',+ externalRestApiBaseUrl: 'https://target-api.example.test/api/v1',+ })+ })+ await waitFor(() =>+ expect(app.result.current.pendingDesktopEnvironmentSwitchConfirmation).toMatchObject({+ targetEnvironmentName: 'Target environment',+ targetExternalRestApiBaseUrl: 'https://target-api.example.test/api/v1',+ })+ )+ return { app, clerum, handle, handoff }+ }++ it('keeps the signed-in session and skips selection when the user cancels', async () => {+ const { app, handle, handoff } = await openSwitchConfirmation()++ await act(async () => app.result.current.handleCancelDesktopEnvironmentSwitchConfirmation())+ await act(async () => handoff)++ expect(handle.logout).not.toHaveBeenCalled()+ expect(handle.selectRuntimeConfigForHandoff).not.toHaveBeenCalled()+ expect(app.result.current.isAuthenticated).toBe(true)+ expect(app.result.current.pendingDesktopEnvironmentSwitchConfirmation).toBeNull()+ })++ it('signs out before selecting the saved environment with the logout generation', async () => {+ const { app, clerum, handle, handoff } = await openSwitchConfirmation()++ await act(async () => app.result.current.handleConfirmDesktopEnvironmentSwitchConfirmation())+ await act(async () => handoff)++ expect(handle.logout).toHaveBeenCalledOnce()+ expect(handle.selectRuntimeConfigForHandoff).toHaveBeenCalledWith('target-profile', 1)+ expect(handle.logout.mock.invocationCallOrder[0]).toBeLessThan(+ handle.selectRuntimeConfigForHandoff.mock.invocationCallOrder[0]+ )+ expect(app.result.current.isAuthenticated).toBe(false)+ await expect(clerum.auth.getRuntimeConfigState()).resolves.toMatchObject({+ activeOptionId: 'target-profile',+ })+ })+})diff --git a/desktop-app/ui/src/hooks/domain/__tests__/__fixtures__/appControllerHarness.tsx b/desktop-app/ui/src/hooks/domain/__tests__/__fixtures__/appControllerHarness.tsxindex 2e3fa8863..27f378717 100644--- a/desktop-app/ui/src/hooks/domain/__tests__/__fixtures__/appControllerHarness.tsx+++ b/desktop-app/ui/src/hooks/domain/__tests__/__fixtures__/appControllerHarness.tsx@@ -3,6 +3,7 @@ import { vi } from 'vitest'import { AgentTaskTrackerProvider } from '@contexts/AgentTaskTrackerContext'import { QueryClient, QueryClientProvider } from '@tanstack/react-query'import { renderHook } from '@testing-library/react'+import type { DesktopRuntimeConfigState } from '../../../../../../src/types'import { useAppController } from '../../../useAppController'import { type MockClerum, installMockClerum } from './mockClerum'@@ -110,12 +111,22 @@ export interface AppControllerClerumOptions {teamDirectory?: { items: unknown[]; currentTeamId: string }/** Overrides on the authenticated `me` the session state reports. */me?: Partial<typeof HARNESS_ME>+ /** Runtime profiles exposed to the desktop-environment handoff flow. */+ runtimeConfigState?: DesktopRuntimeConfigState}export interface AppControllerClerumHandle {getDependenciesHealth: FngetSessionState: FnpasswordLogin: Fn+ logout: Fn+ getSessionGeneration: Fn+ selectRuntimeConfigForHandoff: Fn+ onDesktopEnvironmentSetup: Fn+ emitDesktopEnvironmentSetup: (payload: {+ externalRestApiBaseUrl: string+ appName?: string+ }) => Promise<void>getDesktopReleaseStatus: FnteamDirectory: FnswitchTeam: Fn@@ -148,6 +159,17 @@ export function extendMockClerumForAppController(const bridge = clerum as unknown as Record<string, unknown>let authenticated = options.startAuthenticated ?? true+ let sessionGeneration = 0+ let runtimeConfigState: DesktopRuntimeConfigState = options.runtimeConfigState ?? {+ configured: true,+ isLocalhost: false,+ selectorVisible: true,+ activeOptionId: null,+ currentConfig: undefined,+ envKey: 'harness-environment',+ storagePath: '/harness/runtime-configs',+ options: [],+ }let sessionMe = { ...HARNESS_ME, ...options.me }const teamDirectoryPayload = options.teamDirectory ?? {items: [],@@ -169,6 +191,31 @@ export function extendMockClerumForAppController(authenticated = truereturn createSessionState(true, sessionMe)})+ const getSessionGeneration = vi.fn(async () => sessionGeneration)+ const selectRuntimeConfigForHandoff = vi.fn(+ async (optionId: string, expectedGeneration: number) => {+ if (expectedGeneration !== sessionGeneration) throw new Error('stale_session_generation')+ const selected = runtimeConfigState.options.find(option => option.id === optionId)+ if (!selected) throw new Error('runtime configuration not found')+ runtimeConfigState = {+ ...runtimeConfigState,+ activeOptionId: selected.id,+ currentConfig: {+ externalRestApiBaseUrl: selected.externalRestApiBaseUrl,+ rpcProxyBaseUrl: selected.rpcProxyBaseUrl,+ appName: selected.appName,+ },+ envKey: `harness-${selected.id}`,+ }+ sessionGeneration += 1+ return { runtimeConfigState, sessionGeneration }+ }+ )+ const logout = vi.fn(async () => {+ authenticated = false+ sessionGeneration += 1+ return { ok: true as const, sessionGeneration }+ })const getDesktopReleaseStatus = vi.fn(async () => options.desktopReleaseStatus ?? DEFAULT_DESKTOP_RELEASE_STATUS)@@ -199,6 +246,22 @@ export function extendMockClerumForAppController(}})+ let desktopEnvironmentSetupListener:+ | ((payload: { externalRestApiBaseUrl: string; appName?: string }) => void | Promise<void>)+ | null = null+ const onDesktopEnvironmentSetup = vi.fn(+ (+ callback: (payload: {+ externalRestApiBaseUrl: string+ appName?: string+ }) => void | Promise<void>+ ) => {+ desktopEnvironmentSetupListener = callback+ return () => {+ if (desktopEnvironmentSetupListener === callback) desktopEnvironmentSetupListener = null+ }+ }+ )Object.assign(bridge.rpc as object, {listServers: vi.fn(async () => ({ servers: [] })),@@ -217,18 +280,15 @@ export function extendMockClerumForAppController(auth: {getDependenciesHealth,getSessionState,- getRuntimeConfigState: vi.fn(async () => ({- activeProfileId: null,- configured: true,- isPackaged: false,- options: [],- })),+ getRuntimeConfigState: vi.fn(async () => runtimeConfigState),+ getSessionGeneration,+ selectRuntimeConfigForHandoff,getDesktopReleaseStatus,openDesktopRelease: vi.fn(async () => undefined),passwordLogin,- logout: vi.fn(async () => undefined),+ logout,onDesktopSetupToken: vi.fn(() => () => undefined),- onDesktopEnvironmentSetup: vi.fn(() => () => undefined),+ onDesktopEnvironmentSetup,onExternalLogout: vi.fn(() => () => undefined),},team: {@@ -300,6 +360,13 @@ export function extendMockClerumForAppController(getDependenciesHealth,getSessionState,passwordLogin,+ logout,+ getSessionGeneration,+ selectRuntimeConfigForHandoff,+ onDesktopEnvironmentSetup,+ async emitDesktopEnvironmentSetup(payload) {+ await desktopEnvironmentSetupListener?.(payload)+ },getDesktopReleaseStatus,teamDirectory,switchTeam,