Skip to content

Evenfire is an intelligence layer platform for running LLM agents that take real actions, on your infrastructure

TypeScript

fix: keep authenticated team authoritative

Josué9ed26d64989f
diff --git a/desktop-app/ui/src/hooks/__tests__/useAppController.teamContext.test.tsx b/desktop-app/ui/src/hooks/__tests__/useAppController.teamContext.test.tsx
new file mode 100644
index 000000000..d0c26fc19
--- /dev/null
+++ b/desktop-app/ui/src/hooks/__tests__/useAppController.teamContext.test.tsx
@@ -0,0 +1,61 @@
+// @vitest-environment jsdom
+import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest'
+import { act, waitFor } from '@testing-library/react'
+import {
+ HARNESS_ME,
+ 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
+
+describe('useAppController team context', () => {
+ let unmount: (() => void) | null = null
+
+ beforeEach(() => {
+ vi.clearAllMocks()
+ })
+
+ afterEach(() => {
+ unmount?.()
+ unmount = null
+ vi.restoreAllMocks()
+ uninstallMockClerum()
+ })
+
+ it('switches back when stale directory data disagrees with the authenticated team', async () => {
+ const { handle } = installAppControllerClerum({
+ teamDirectory: {
+ items: [],
+ currentTeamId: HARNESS_ME.teamId,
+ },
+ })
+ const app = renderAppController()
+ unmount = app.unmount
+
+ await waitFor(() => expect(app.result.current.booting).toBe(false))
+ await waitFor(() => expect(app.result.current.isAuthenticated).toBe(true))
+ await waitFor(() => expect(app.result.current.initialExperienceLoading).toBe(false))
+
+ handle.teamDirectory.mockClear()
+ handle.teamDirectory.mockRejectedValueOnce(new Error('directory refresh failed'))
+
+ await act(async () => {
+ await app.result.current.handleEnsureTeamContext({ teamId: 'team-2' })
+ })
+ expect(handle.switchTeam).toHaveBeenCalledWith('team-2')
+ expect(handle.teamDirectory).toHaveBeenCalledOnce()
+
+ handle.switchTeam.mockClear()
+ let switchedBack = false
+ await act(async () => {
+ switchedBack = await app.result.current.handleEnsureTeamContext({
+ teamId: HARNESS_ME.teamId,
+ })
+ })
+
+ expect(switchedBack).toBe(true)
+ expect(handle.switchTeam).toHaveBeenCalledWith(HARNESS_ME.teamId)
+ })
+})
diff --git a/desktop-app/ui/src/hooks/domain/__tests__/__fixtures__/appControllerHarness.tsx b/desktop-app/ui/src/hooks/domain/__tests__/__fixtures__/appControllerHarness.tsx
index af67f0992..0b1007986 100644
--- a/desktop-app/ui/src/hooks/domain/__tests__/__fixtures__/appControllerHarness.tsx
+++ b/desktop-app/ui/src/hooks/domain/__tests__/__fixtures__/appControllerHarness.tsx
@@ -51,9 +51,9 @@ function createDeferred<T>(): Deferred<T> {
return { promise, resolve }
}
-function createSessionState(authenticated: boolean) {
+function createSessionState(authenticated: boolean, me = HARNESS_ME) {
return authenticated
- ? { authenticated: true, me: HARNESS_ME }
+ ? { authenticated: true, me }
: { authenticated: false, me: null }
}
@@ -118,6 +118,7 @@ export interface AppControllerClerumHandle {
passwordLogin: Fn
getDesktopReleaseStatus: Fn
teamDirectory: Fn
+ switchTeam: Fn
refreshCatalog: Fn
listPending: Fn
/** Settles a dependency-health probe held by `delayHealth`. */
@@ -140,6 +141,7 @@ export function extendMockClerumForAppController(
const bridge = clerum as unknown as Record<string, unknown>
let authenticated = options.startAuthenticated ?? true
+ let sessionMe = { ...HARNESS_ME }
const teamDirectoryPayload = options.teamDirectory ?? {
items: [],
currentTeamId: HARNESS_ME.teamId,
@@ -154,11 +156,11 @@ export function extendMockClerumForAppController(
if (options.healthError) throw options.healthError
return options.delayHealth ? healthDeferred.promise : createHealth()
})
- const getSessionState = vi.fn(async () => createSessionState(authenticated))
+ const getSessionState = vi.fn(async () => createSessionState(authenticated, sessionMe))
const passwordLogin = vi.fn(async () => {
if (options.passwordLoginError) throw options.passwordLoginError
authenticated = true
- return createSessionState(true)
+ return createSessionState(true, sessionMe)
})
const getDesktopReleaseStatus = vi.fn(
async () => options.desktopReleaseStatus ?? DEFAULT_DESKTOP_RELEASE_STATUS
@@ -166,6 +168,14 @@ export function extendMockClerumForAppController(
const teamDirectory = vi.fn(async () =>
held() ? teamDirectoryDeferred.promise : teamDirectoryPayload
)
+ const switchTeam = vi.fn(async (teamId: string) => {
+ sessionMe = {
+ ...sessionMe,
+ teamId,
+ teamName: teamId === HARNESS_ME.teamId ? HARNESS_ME.teamName : teamId,
+ }
+ return createSessionState(true, sessionMe)
+ })
const refreshCatalog = vi.fn(async () =>
held() ? catalogDeferred.promise : createCatalog(agentNames)
)
@@ -201,7 +211,7 @@ export function extendMockClerumForAppController(
members: vi.fn(async () => []),
directory: teamDirectory,
initialDirectory: teamDirectory,
- switch: vi.fn(async () => createSessionState(true)),
+ switch: switchTeam,
},
access: {
refreshCatalog,
@@ -267,6 +277,7 @@ export function extendMockClerumForAppController(
passwordLogin,
getDesktopReleaseStatus,
teamDirectory,
+ switchTeam,
refreshCatalog,
listPending,
resolveHealth() {
diff --git a/desktop-app/ui/src/hooks/useAppController.ts b/desktop-app/ui/src/hooks/useAppController.ts
index 32574b52a..37b3b7aa1 100644
--- a/desktop-app/ui/src/hooks/useAppController.ts
+++ b/desktop-app/ui/src/hooks/useAppController.ts
@@ -154,7 +154,7 @@ export function useAppController() {
const contextsData = useContextsDataController()
const mcpServersData = useMcpServersDataController()
const teamsData = useTeamsDataController()
- const currentTeamId = teamsData.currentTeamId || auth.me?.teamId || ''
+ const currentTeamId = auth.me?.teamId || teamsData.currentTeamId || ''
const availableTeamIds = useMemo(() => teamsData.teams.map(team => team.id), [teamsData.teams])
const authenticatedPrincipalIdentity =
auth.isAuthenticated && auth.me