import { QueryClient, QueryClientProvider, useQuery } from '@tanstack/react-query'
import { renderHook, waitFor } from '@testing-library/react'
import { describe, expect, it } from 'vitest'
// Mock i18next
vi.mock('react-i18next', () => ({
useTranslation: () => ({
t: (key: string) => key,
i18n: { language: 'en' },
}),
}))
const createWrapper = () => {
const queryClient = new QueryClient({
defaultOptions: {
queries: { retry: false },
mutations: { retry: false },
},
})
return ({ children }: { children: React.ReactNode }) => (
<QueryClientProvider client={queryClient}>{children}</QueryClientProvider>
)
}
describe('useAgentGroups hook patterns', () => {
it('useAgentGroups fetches list', async () => {
const { result } = renderHook(
() => {
return useQuery({
queryKey: ['agent-groups'],
queryFn: async () => {
// Simulated mock response - actual API would be intercepted by MSW
return { items: [], total: 0, page: 1, limit: 100 }
},
})
},
{ wrapper: createWrapper() },
)
await waitFor(() => expect(result.current.isSuccess).toBe(true))
expect(result.current.data).toBeDefined()
})
it('calculates group progress correctly', () => {
// Simulate group progress calculation
const totalAgents = 10
const completedAgents = 5
const progress = (completedAgents / totalAgents) * 100
expect(progress).toBe(50)
})
it('handles empty group list', () => {
const groups: unknown[] = []
expect(groups.length).toBe(0)
})
it('calculates group status correctly', () => {
interface GroupAgent {
status: 'Running' | 'Idle' | 'Completed' | 'Failed'
}
const agents: GroupAgent[] = [
{ status: 'Running' },
{ status: 'Running' },
{ status: 'Completed' },
{ status: 'Completed' },
]
const allCompleted = agents.every((a) => a.status === 'Completed' || a.status === 'Failed')
const anyRunning = agents.some((a) => a.status === 'Running')
expect(allCompleted).toBe(false)
expect(anyRunning).toBe(true)
// All completed scenario
const allDoneAgents: GroupAgent[] = [{ status: 'Completed' }, { status: 'Completed' }]
const allDone = allDoneAgents.every((a) => a.status === 'Completed' || a.status === 'Failed')
expect(allDone).toBe(true)
})
})