import { QueryClientProvider } from '@tanstack/react-query' import { renderHook, act } from '@testing-library/react-native' import React, { Suspense } from 'react' import { buildTestQueryClient } from '../../__utils__/query_client' import { FALLBACK_ALLOWED_FILE_EXTENSIONS, FALLBACK_ALLOWED_MIME_TYPES, FALLBACK_MAX_ATTACHMENTS_PER_MESSAGE, FALLBACK_MAX_FILE_SIZE_IN_BYTES, } from '../../hooks/attachments/fallback_chat_configuration' import * as useApiClientModule from '../../hooks/use_api_client' import { useChatConfiguration } from '../../hooks/use_chat_configuration' const createWrapper = () => { const queryClient = buildTestQueryClient() return ({ children }: { children: React.ReactNode }) => ( {children} ) } const waitForQuery = async () => { await act(async () => { await Promise.resolve() await Promise.resolve() await Promise.resolve() }) } const mockApiClient = (getImpl: () => Promise) => { jest.spyOn(useApiClientModule, 'useApiClient').mockReturnValue({ chat: { get: jest.fn(getImpl) }, } as any) } describe('useChatConfiguration', () => { afterEach(() => { jest.restoreAllMocks() }) it('returns server-provided values when the API succeeds', async () => { mockApiClient(() => Promise.resolve({ data: { type: 'ChatConfiguration', id: 'current', allowedFileExtensions: ['.pdf', '.jpg'], allowedMimeTypes: ['application/pdf', 'image/jpeg'], maxFileSizeInBytes: 1000, maxAttachmentsPerMessage: 3, }, links: {}, meta: {}, }) ) const { result } = renderHook(() => useChatConfiguration(), { wrapper: createWrapper() }) await waitForQuery() expect(result.current).toEqual({ allowedFileExtensions: ['.pdf', '.jpg'], allowedMimeTypes: ['application/pdf', 'image/jpeg'], maxFileSizeInBytes: 1000, maxAttachmentsPerMessage: 3, }) }) it('falls back to FALLBACK_ALLOWED_MIME_TYPES when the server omits the field', async () => { // During server-side rollout the field may be absent from the response. mockApiClient(() => Promise.resolve({ data: { type: 'ChatConfiguration', id: 'current', allowedFileExtensions: ['.pdf'], maxFileSizeInBytes: 1000, maxAttachmentsPerMessage: 3, }, links: {}, meta: {}, }) ) const { result } = renderHook(() => useChatConfiguration(), { wrapper: createWrapper() }) await waitForQuery() expect(result.current.allowedMimeTypes).toEqual(FALLBACK_ALLOWED_MIME_TYPES) }) it('returns fallback values when the API rejects', async () => { mockApiClient(() => Promise.reject(new Error('boom'))) const { result } = renderHook(() => useChatConfiguration(), { wrapper: createWrapper() }) await waitForQuery() expect(result.current).toEqual({ allowedFileExtensions: FALLBACK_ALLOWED_FILE_EXTENSIONS, allowedMimeTypes: FALLBACK_ALLOWED_MIME_TYPES, maxFileSizeInBytes: FALLBACK_MAX_FILE_SIZE_IN_BYTES, maxAttachmentsPerMessage: FALLBACK_MAX_ATTACHMENTS_PER_MESSAGE, }) }) })