Skip to content

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

TypeScript

test(control-ui): cover auth cache invalidation

Josué5280548556c7
diff --git a/control-ui/components/__tests__/AuthContext.test.tsx b/control-ui/components/__tests__/AuthContext.test.tsx
index 02590e586..d447fdd1f 100644
--- a/control-ui/components/__tests__/AuthContext.test.tsx
+++ b/control-ui/components/__tests__/AuthContext.test.tsx
@@ -2,6 +2,15 @@ import React from 'react'
import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest'
import { cleanup, fireEvent, render, screen, waitFor } from '@testing-library/react'
import { apiGet } from '../../lib/api'
+import {
+ PublishScopeProvider,
+ resetPublishScopeCache,
+ usePublishScope,
+} from '../../lib/hooks/usePublishScope'
+import {
+ __resetRegistryCapabilityCacheForTests,
+ useRegistryCapability,
+} from '../../lib/hooks/useRegistryCapability'
import { AuthProvider, useAuth } from '../AuthContext'
import { ToastProvider } from '../Toast'
@@ -47,11 +56,39 @@ function CheckAuthButton() {
)
}
+function LoginButton() {
+ const { login } = useAuth()
+ return (
+ <button type="button" onClick={() => void login('admin-b', 'password')}>
+ Log in
+ </button>
+ )
+}
+
+function AuthUserProbe() {
+ const { authState } = useAuth()
+ return <div data-testid="auth-user">{authState.username || 'signed-out'}</div>
+}
+
function AuthProbe() {
useAuth()
return <div>Auth mounted</div>
}
+function PublishScopeProbe() {
+ const { loading, scope } = usePublishScope()
+ return <div data-testid="publish-scope">{loading ? 'loading' : (scope?.orgName ?? 'none')}</div>
+}
+
+function RegistryCapabilityProbe() {
+ const { capability, loading } = useRegistryCapability()
+ return (
+ <div data-testid="registry-capability">
+ {loading ? 'loading' : (capability?.orgName ?? 'none')}
+ </div>
+ )
+}
+
function LogoutButton() {
const { logout } = useAuth()
return (
@@ -63,6 +100,8 @@ function LogoutButton() {
afterEach(() => {
cleanup()
+ resetPublishScopeCache()
+ __resetRegistryCapabilityCacheForTests()
window.localStorage.clear()
vi.unstubAllGlobals()
})
@@ -70,6 +109,8 @@ afterEach(() => {
describe('AuthProvider session expiry handling', () => {
beforeEach(() => {
vi.clearAllMocks()
+ resetPublishScopeCache()
+ __resetRegistryCapabilityCacheForTests()
window.history.pushState({}, '', '/')
})
@@ -169,6 +210,165 @@ describe('AuthProvider session expiry handling', () => {
expect(replaceMock).toHaveBeenCalledTimes(1)
})
+ it('clears publish-scope and registry-capability caches after login', async () => {
+ let registryIdentity = 'Org A'
+ const requests: string[] = []
+ const fetchMock = vi.fn(async (input: RequestInfo | URL, init?: RequestInit) => {
+ const url = String(input)
+ const method = init?.method || 'GET'
+ requests.push(`${method} ${url}`)
+ if (url.includes('/api/v1/admin/registry/publish-scope')) {
+ return response(200, {
+ scope: registryIdentity === 'Org A' ? '@org-a' : '@org-b',
+ curator: false,
+ orgName: registryIdentity,
+ })
+ }
+ if (url.includes('/api/v1/admin/registry/connect')) {
+ return response(200, {
+ state: 'connected',
+ org: registryIdentity,
+ authEnabled: true,
+ })
+ }
+ if (url.includes('/api/v1/admin/auth/me')) {
+ return response(401, { error: 'expired' })
+ }
+ if (url.includes('/api/v1/admin/auth/login')) {
+ return response(200, {
+ me: { id: 'admin', username: 'admin-b', email: '[email protected]' },
+ })
+ }
+ throw new Error(`unexpected request: ${method} ${url}`)
+ })
+ vi.stubGlobal('fetch', fetchMock)
+
+ const publishA = render(
+ <PublishScopeProvider cacheKey="admin">
+ <PublishScopeProbe />
+ </PublishScopeProvider>
+ )
+ await waitFor(() => expect(screen.getByTestId('publish-scope')).toHaveTextContent('Org A'))
+ publishA.unmount()
+
+ const capabilityA = render(<RegistryCapabilityProbe />)
+ await waitFor(() =>
+ expect(screen.getByTestId('registry-capability')).toHaveTextContent('Org A')
+ )
+ capabilityA.unmount()
+
+ registryIdentity = 'Org B'
+ const auth = render(
+ <ToastProvider>
+ <AuthProvider>
+ <LoginButton />
+ <AuthUserProbe />
+ </AuthProvider>
+ </ToastProvider>
+ )
+ await waitFor(() =>
+ expect(fetchMock).toHaveBeenCalledWith(expect.stringContaining('/auth/me'), expect.anything())
+ )
+ fireEvent.click(screen.getByRole('button', { name: /log in/i }))
+ await waitFor(() => expect(screen.getByTestId('auth-user')).toHaveTextContent('admin-b'))
+ auth.unmount()
+
+ render(
+ <PublishScopeProvider cacheKey="admin">
+ <PublishScopeProbe />
+ </PublishScopeProvider>
+ )
+ await waitFor(() => expect(screen.getByTestId('publish-scope')).toHaveTextContent('Org B'))
+ cleanup()
+
+ render(<RegistryCapabilityProbe />)
+ await waitFor(() =>
+ expect(screen.getByTestId('registry-capability')).toHaveTextContent('Org B')
+ )
+ expect(
+ requests.filter(request => request.includes('/api/v1/admin/registry/publish-scope'))
+ ).toHaveLength(4)
+ })
+
+ it('clears publish-scope and registry-capability caches after session expiration', async () => {
+ let registryIdentity = 'Org A'
+ const requests: string[] = []
+ const fetchMock = vi.fn(async (input: RequestInfo | URL, init?: RequestInit) => {
+ const url = String(input)
+ const method = init?.method || 'GET'
+ requests.push(`${method} ${url}`)
+ if (url.includes('/api/v1/admin/registry/publish-scope')) {
+ return response(200, {
+ scope: registryIdentity === 'Org A' ? '@org-a' : '@org-b',
+ curator: false,
+ orgName: registryIdentity,
+ })
+ }
+ if (url.includes('/api/v1/admin/registry/connect')) {
+ return response(200, {
+ state: 'connected',
+ org: registryIdentity,
+ authEnabled: true,
+ })
+ }
+ if (url.includes('/api/v1/admin/auth/me')) {
+ return response(200, { me: { id: 'admin', username: 'admin-a' } })
+ }
+ if (url.includes('/api/v1/admin/hosts')) {
+ return response(401, { error: 'expired' })
+ }
+ throw new Error(`unexpected request: ${method} ${url}`)
+ })
+ vi.stubGlobal('fetch', fetchMock)
+
+ const publishA = render(
+ <PublishScopeProvider cacheKey="admin">
+ <PublishScopeProbe />
+ </PublishScopeProvider>
+ )
+ await waitFor(() => expect(screen.getByTestId('publish-scope')).toHaveTextContent('Org A'))
+ publishA.unmount()
+
+ const capabilityA = render(<RegistryCapabilityProbe />)
+ await waitFor(() =>
+ expect(screen.getByTestId('registry-capability')).toHaveTextContent('Org A')
+ )
+ capabilityA.unmount()
+
+ render(
+ <ToastProvider>
+ <AuthProvider>
+ <ExpireSessionButton />
+ </AuthProvider>
+ </ToastProvider>
+ )
+ await waitFor(() =>
+ expect(fetchMock).toHaveBeenCalledWith(expect.stringContaining('/auth/me'), expect.anything())
+ )
+ fireEvent.click(screen.getByRole('button', { name: /expire session/i }))
+ await waitFor(() =>
+ expect(screen.getAllByText('Session expired. Please sign in again.')).toHaveLength(1)
+ )
+ cleanup()
+
+ registryIdentity = 'Org B'
+ render(
+ <PublishScopeProvider cacheKey="admin">
+ <PublishScopeProbe />
+ </PublishScopeProvider>
+ )
+ await waitFor(() => expect(screen.getByTestId('publish-scope')).toHaveTextContent('Org B'))
+ cleanup()
+
+ render(<RegistryCapabilityProbe />)
+ await waitFor(() =>
+ expect(screen.getByTestId('registry-capability')).toHaveTextContent('Org B')
+ )
+ expect(
+ requests.filter(request => request.includes('/api/v1/admin/registry/publish-scope'))
+ ).toHaveLength(4)
+ })
+
it('clears legacy storage without a toast when no cookie session exists on mount', async () => {
window.history.pushState({}, '', '/contexts/context1')
window.localStorage.setItem('controlUiAdminToken', 'expired-token')
diff --git a/control-ui/package-lock.json b/control-ui/package-lock.json
index cc90c2d02..40bb70a3c 100644
--- a/control-ui/package-lock.json
+++ b/control-ui/package-lock.json
@@ -1,12 +1,12 @@
{
"name": "clerum-control-ui",
- "version": "0.1.379",
+ "version": "0.1.380",
"lockfileVersion": 3,
"requires": true,
"packages": {
"": {
"name": "clerum-control-ui",
- "version": "0.1.379",
+ "version": "0.1.380",
"dependencies": {
"@clerum/llm-providers": "file:../packages/llm-providers",
"@clerum/workflow-recipe-capability-policy": "file:../packages/workflow-recipe-capability-policy",
diff --git a/control-ui/package.json b/control-ui/package.json
index beca2d0af..e7be76a82 100644
--- a/control-ui/package.json
+++ b/control-ui/package.json
@@ -1,6 +1,6 @@
{
"name": "clerum-control-ui",
- "version": "0.1.379",
+ "version": "0.1.380",
"private": true,
"scripts": {
"dev": "NEXT_IGNORE_INCORRECT_LOCKFILE=1 next dev",