import { afterEach, describe, expect, it, vi } from 'vitest'; import { fireEvent, render, screen, waitFor } from '@testing-library/react'; import { getMatrixToRoom, setMatrixToBase } from '$plugins/matrix-to'; import { ClientConfigLoader, FALLBACK_CLIENT_CONFIG } from './ClientConfigLoader'; const successfulResponse = (config: unknown) => ({ ok: true, status: 200, statusText: 'OK', json: vi.fn<() => Promise>().mockResolvedValue(config), }); afterEach(() => { setMatrixToBase(); vi.unstubAllGlobals(); }); describe('ClientConfigLoader', () => { it('does not render config-dependent children while loading', () => { vi.stubGlobal( 'fetch', vi.fn(() => new Promise(() => undefined)) ); render( Loading}> {() => Configured app} ); expect(screen.getByText('Loading')).toBeInTheDocument(); expect(screen.queryByText('Configured app')).not.toBeInTheDocument(); }); it('loads the config before rendering children', async () => { vi.stubGlobal( 'fetch', vi.fn().mockResolvedValue(successfulResponse({ allowCustomHomeservers: true })) ); render( {(config) => {String(config.allowCustomHomeservers)}} ); expect(await screen.findByText('true')).toBeInTheDocument(); }); it('reports non-success responses and retries', async () => { const fetchMock = vi .fn() .mockResolvedValueOnce({ ok: false, status: 503, statusText: 'Service Unavailable', json: vi.fn<() => Promise>(), } as unknown as Response) .mockResolvedValueOnce( successfulResponse({ homeserverList: ['example.org'] }) as unknown as Response ); vi.stubGlobal('fetch', fetchMock); render( ( )} > {(config) => {config.homeserverList?.[0]}} ); fireEvent.click(await screen.findByRole('button', { name: /503 Service Unavailable/ })); expect(await screen.findByText('example.org')).toBeInTheDocument(); expect(fetchMock).toHaveBeenCalledTimes(2); }); it('rejects non-object JSON', async () => { vi.stubGlobal('fetch', vi.fn().mockResolvedValue(successfulResponse([]))); render( {String(error)}}> {() => Configured app} ); expect(await screen.findByText(/must contain a JSON object/)).toBeInTheDocument(); expect(screen.queryByText('Configured app')).not.toBeInTheDocument(); }); it('uses deliberate defaults only after continuing', async () => { vi.stubGlobal('fetch', vi.fn().mockRejectedValue(new Error('offline'))); render( ( )} > {(config) => {config.homeserverList?.join(',')}} ); fireEvent.click(await screen.findByRole('button', { name: 'Continue' })); expect(screen.getByText(FALLBACK_CLIENT_CONFIG.homeserverList?.[0] ?? '')).toBeInTheDocument(); }); it('applies matrix.to configuration before rendering links', async () => { vi.stubGlobal( 'fetch', vi.fn().mockResolvedValue(successfulResponse({ matrixToBaseUrl: 'https://custom.example/' })) ); render( {(config) => { setMatrixToBase(config.matrixToBaseUrl); return {getMatrixToRoom('!room:example.com')}; }} ); await waitFor(() => expect(screen.getByText('https://custom.example/#/!room:example.com')).toBeInTheDocument() ); }); });