Skip to content

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

TypeScript

fix(profile-ui): preserve access after refresh failures

Josuéc911cf693a14
diff --git a/profile-ui/app/components/__tests__/ProfileAccessProvider.refresh.test.tsx b/profile-ui/app/components/__tests__/ProfileAccessProvider.refresh.test.tsx
new file mode 100644
index 000000000..e357bdf13
--- /dev/null
+++ b/profile-ui/app/components/__tests__/ProfileAccessProvider.refresh.test.tsx
@@ -0,0 +1,207 @@
+import React from 'react'
+import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest'
+import { cleanup, fireEvent, render, screen, waitFor } from '@testing-library/react'
+import { ProfileAccessProvider, useProfileAccess } from '@components/ProfileAccessContext'
+import { resetProfileAccessCache } from '@lib/profileAccess'
+import type { ApprovalChannelTarget } from '@/app/types/approvalChannels'
+import type { ManageableTeam } from '@/app/types/profile'
+
+const authState = vi.hoisted(() => ({
+ isLoggedIn: true,
+ isLoading: false,
+ me: {
+ id: 'user-1',
+ email: '[email protected]',
+ name: 'Lead User',
+ role: 'member' as const,
+ },
+}))
+
+vi.mock('@components/AuthContext', () => ({
+ useAuth: () => ({
+ authState,
+ checkAuth: vi.fn(),
+ login: vi.fn(),
+ logout: vi.fn(),
+ }),
+}))
+
+function team(id: string): ManageableTeam {
+ return {
+ id,
+ name: id,
+ role: 'admin',
+ canAssignLeader: true,
+ }
+}
+
+function target(id: string): ApprovalChannelTarget {
+ return {
+ id,
+ medium: 'slack',
+ agentName: 'agent-1',
+ channelName: id,
+ channelNamespace: 'default',
+ botLabel: 'Slack App',
+ botUsername: null,
+ botDeepLink: null,
+ status: 'ready',
+ }
+}
+
+function jsonResponse(items: unknown[]): Response {
+ return new Response(JSON.stringify({ items }), {
+ headers: { 'Content-Type': 'application/json' },
+ status: 200,
+ })
+}
+
+function errorResponse(): Response {
+ return new Response(JSON.stringify({ message: 'temporary failure' }), {
+ headers: { 'Content-Type': 'application/json' },
+ status: 500,
+ statusText: 'Internal Server Error',
+ })
+}
+
+function AccessProbe() {
+ const {
+ approvalTargets,
+ approvalTargetsError,
+ canManageMembers,
+ manageableTeams,
+ manageableTeamsError,
+ refreshApprovalTargets,
+ refreshManageableTeams,
+ } = useProfileAccess()
+
+ return (
+ <div>
+ <div data-testid="member-access">
+ {canManageMembers ? 'members-visible' : 'members-hidden'}
+ </div>
+ <div data-testid="teams">{manageableTeams.map(item => item.id).join(',') || 'no-teams'}</div>
+ <div data-testid="team-error">{manageableTeamsError ? 'team-error' : 'team-ok'}</div>
+ <div data-testid="targets">
+ {approvalTargets.map(item => item.id).join(',') || 'no-targets'}
+ </div>
+ <div data-testid="target-error">{approvalTargetsError ? 'target-error' : 'target-ok'}</div>
+ <button
+ type="button"
+ onClick={() => void refreshManageableTeams({ force: true }).catch(() => {})}
+ >
+ Refresh teams
+ </button>
+ <button
+ type="button"
+ onClick={() => void refreshApprovalTargets({ force: true }).catch(() => {})}
+ >
+ Refresh targets
+ </button>
+ </div>
+ )
+}
+
+function renderProvider() {
+ return render(
+ <ProfileAccessProvider>
+ <AccessProbe />
+ </ProfileAccessProvider>
+ )
+}
+
+let originalFetch: typeof fetch
+let teamsResult: ManageableTeam[] | 'error'
+let targetsResult: ApprovalChannelTarget[] | 'error'
+
+beforeEach(() => {
+ originalFetch = globalThis.fetch
+ resetProfileAccessCache()
+ authState.isLoggedIn = true
+ authState.isLoading = false
+ authState.me = {
+ id: 'user-1',
+ email: '[email protected]',
+ name: 'Lead User',
+ role: 'member',
+ }
+ teamsResult = [team('team-1')]
+ targetsResult = [target('target-1')]
+ globalThis.fetch = (async input => {
+ const url = String(input)
+ if (url.includes('/members/manageable-teams')) {
+ return teamsResult === 'error' ? errorResponse() : jsonResponse(teamsResult)
+ }
+ if (url.includes('/workflow-approval-mediums/targets')) {
+ return targetsResult === 'error' ? errorResponse() : jsonResponse(targetsResult)
+ }
+ throw new Error(`unexpected request: ${url}`)
+ }) as typeof fetch
+})
+
+afterEach(() => {
+ cleanup()
+ resetProfileAccessCache()
+ globalThis.fetch = originalFetch
+})
+
+describe('ProfileAccessProvider refresh errors', () => {
+ it('preserves known access after transient refresh failures', async () => {
+ renderProvider()
+
+ await waitFor(() => expect(screen.getByTestId('teams')).toHaveTextContent('team-1'))
+ await waitFor(() => expect(screen.getByTestId('targets')).toHaveTextContent('target-1'))
+ expect(screen.getByTestId('member-access')).toHaveTextContent('members-visible')
+
+ teamsResult = 'error'
+ fireEvent.click(screen.getByRole('button', { name: 'Refresh teams' }))
+ await waitFor(() => expect(screen.getByTestId('team-error')).toHaveTextContent('team-error'))
+ expect(screen.getByTestId('teams')).toHaveTextContent('team-1')
+ expect(screen.getByTestId('member-access')).toHaveTextContent('members-visible')
+
+ targetsResult = 'error'
+ fireEvent.click(screen.getByRole('button', { name: 'Refresh targets' }))
+ await waitFor(() =>
+ expect(screen.getByTestId('target-error')).toHaveTextContent('target-error')
+ )
+ expect(screen.getByTestId('targets')).toHaveTextContent('target-1')
+
+ teamsResult = [team('team-2')]
+ targetsResult = [target('target-2')]
+ fireEvent.click(screen.getByRole('button', { name: 'Refresh teams' }))
+ fireEvent.click(screen.getByRole('button', { name: 'Refresh targets' }))
+
+ await waitFor(() => expect(screen.getByTestId('teams')).toHaveTextContent('team-2'))
+ await waitFor(() => expect(screen.getByTestId('targets')).toHaveTextContent('target-2'))
+ expect(screen.getByTestId('team-error')).toHaveTextContent('team-ok')
+ expect(screen.getByTestId('target-error')).toHaveTextContent('target-ok')
+ })
+
+ it('does not expose preserved access after a user id change', async () => {
+ const view = renderProvider()
+ await waitFor(() => expect(screen.getByTestId('teams')).toHaveTextContent('team-1'))
+
+ teamsResult = 'error'
+ fireEvent.click(screen.getByRole('button', { name: 'Refresh teams' }))
+ await waitFor(() => expect(screen.getByTestId('team-error')).toHaveTextContent('team-error'))
+ expect(screen.getByTestId('teams')).toHaveTextContent('team-1')
+
+ authState.me = {
+ id: 'user-2',
+ email: '[email protected]',
+ name: 'Member User',
+ role: 'member',
+ }
+ teamsResult = []
+ targetsResult = []
+ view.rerender(
+ <ProfileAccessProvider>
+ <AccessProbe />
+ </ProfileAccessProvider>
+ )
+
+ expect(screen.getByTestId('teams')).toHaveTextContent('no-teams')
+ expect(screen.getByTestId('member-access')).toHaveTextContent('members-hidden')
+ expect(screen.getByTestId('targets')).toHaveTextContent('no-targets')
+ })
+})
diff --git a/profile-ui/lib/profileAccessState.ts b/profile-ui/lib/profileAccessState.ts
index c451200ec..bb4a40f23 100644
--- a/profile-ui/lib/profileAccessState.ts
+++ b/profile-ui/lib/profileAccessState.ts
@@ -46,7 +46,6 @@ export function profileAccessStateAfterManageableTeamsError(
): ProfileAccessDataState {
return {
...previous,
- manageableTeams: [],
manageableTeamsError: true,
manageableTeamsLoading: false,
}
@@ -57,7 +56,6 @@ export function profileAccessStateAfterApprovalTargetsError(
): ProfileAccessDataState {
return {
...previous,
- approvalTargets: [],
approvalTargetsError: true,
approvalTargetsLoading: false,
}
diff --git a/profile-ui/package-lock.json b/profile-ui/package-lock.json
index 1eb798411..1719b1104 100644
--- a/profile-ui/package-lock.json
+++ b/profile-ui/package-lock.json
@@ -1,12 +1,12 @@
{
"name": "clerum-profile-ui",
- "version": "0.1.109",
+ "version": "0.1.110",
"lockfileVersion": 3,
"requires": true,
"packages": {
"": {
"name": "clerum-profile-ui",
- "version": "0.1.109",
+ "version": "0.1.110",
"dependencies": {
"next": "16.2.11",
"react": "^18.3.1",
diff --git a/profile-ui/package.json b/profile-ui/package.json
index f73652280..e719ae0c8 100644
--- a/profile-ui/package.json
+++ b/profile-ui/package.json
@@ -1,6 +1,6 @@
{
"name": "clerum-profile-ui",
- "version": "0.1.109",
+ "version": "0.1.110",
"private": true,
"scripts": {
"dev": "next dev",
diff --git a/profile-ui/test/profileAccess.test.ts b/profile-ui/test/profileAccess.test.ts
index 75480a3e4..70d5020d8 100644
--- a/profile-ui/test/profileAccess.test.ts
+++ b/profile-ui/test/profileAccess.test.ts
@@ -215,7 +215,29 @@ test('profile access authorization derives from role or manageable-team access',
assert.equal(canManageMembersForAccess(null, []), false)
})
-test('profile access errors clear protected data and remain fail-closed', () => {
+test('profile access errors without prior data remain fail-closed', () => {
+ const previous = {
+ approvalTargets: [],
+ approvalTargetsError: false,
+ approvalTargetsLoading: true,
+ manageableTeams: [],
+ manageableTeamsError: false,
+ manageableTeamsLoading: true,
+ }
+
+ assert.deepEqual(profileAccessStateAfterManageableTeamsError(previous), {
+ ...previous,
+ manageableTeamsError: true,
+ manageableTeamsLoading: false,
+ })
+ assert.deepEqual(profileAccessStateAfterApprovalTargetsError(previous), {
+ ...previous,
+ approvalTargetsError: true,
+ approvalTargetsLoading: false,
+ })
+})
+
+test('profile access refresh errors preserve known access data', () => {
const previous = {
approvalTargets: [target('target-1')],
approvalTargetsError: false,
@@ -227,14 +249,38 @@ test('profile access errors clear protected data and remain fail-closed', () =>
assert.deepEqual(profileAccessStateAfterManageableTeamsError(previous), {
...previous,
- manageableTeams: [],
manageableTeamsError: true,
manageableTeamsLoading: false,
})
+ assert.equal(
+ canManageMembersForAccess(
+ 'member',
+ profileAccessStateAfterManageableTeamsError(previous).manageableTeams
+ ),
+ true
+ )
assert.deepEqual(profileAccessStateAfterApprovalTargetsError(previous), {
...previous,
- approvalTargets: [],
approvalTargetsError: true,
approvalTargetsLoading: false,
})
})
+
+test('profile access user changes clear previously preserved error-state access', () => {
+ const previous = {
+ approvalTargets: [target('target-1')],
+ approvalTargetsError: false,
+ approvalTargetsLoading: false,
+ manageableTeams: [team('team-1')],
+ manageableTeamsError: false,
+ manageableTeamsLoading: false,
+ }
+ const errored = profileAccessStateAfterManageableTeamsError(previous)
+
+ assert.deepEqual(errored.manageableTeams, [team('team-1')])
+ const nextUser = profileAccessStateForUser('user-2')
+ assert.deepEqual(nextUser.manageableTeams, [])
+ assert.equal(nextUser.manageableTeamsLoading, true)
+ assert.deepEqual(nextUser.approvalTargets, [])
+ assert.equal(nextUser.approvalTargetsLoading, true)
+})