import { QueryClient } from '@tanstack/react-query' import { waitFor } from '@testing-library/react' import { FeatureFlagContext } from 'common' import { HttpResponse } from 'msw' import { createDynamicRouteParser } from 'next-router-mock/dynamic-routes' import { useRouter } from 'next/router' import type { PropsWithChildren } from 'react' import { afterAll, expect, it, vi } from 'vitest' import type { EdgeFunction } from '@/data/edge-functions/edge-function-query' import { projectKeys } from '@/data/projects/keys' import { Route } from '@/routes/project/$ref/functions/$functionSlug/index' import { createMockProject } from '@/tests/helpers' import { CustomWrapper } from '@/tests/lib/custom-render' import { addAPIMock } from '@/tests/lib/msw' import { renderRoute } from '@/tests/lib/route-render' vi.unmock('common') const { default: routerMock } = await vi.importMock('next/router') const removeRouteParser = routerMock.useParser( createDynamicRouteParser([ '/project/[ref]/functions/[functionSlug]', '/project/[ref]/functions/[functionSlug]/details', ]) ) afterAll(removeRouteParser) // The overview redirects through the next/router mock, which doesn't drive the // TanStack route, so unmount it once the mock leaves the overview URL. const UntilRedirected = ({ children }: PropsWithChildren) => { const { asPath } = useRouter() return asPath === '/project/default/functions/send-email' ? children : null } it('redirects the self-hosted overview to function details before feature flags load', async () => { routerMock.setCurrentUrl('/project/default/functions/send-email') const queryClient = new QueryClient({ defaultOptions: { queries: { retry: false } } }) queryClient.setQueryData(projectKeys.detail('default'), createMockProject({ ref: 'default' })) addAPIMock({ method: 'get', path: '/v1/projects/:ref/functions/:function_slug', response: () => HttpResponse.json({ id: 'function-id', name: 'Send email', slug: 'send-email', status: 'ACTIVE', version: 2, created_at: new Date('2026-03-20T08:00:00Z').valueOf(), updated_at: new Date('2026-03-20T09:00:00Z').valueOf(), }), }) await renderRoute(Route, { routeId: '/project/$ref/functions/$functionSlug/', url: '/project/default/functions/send-email', renderOptions: { wrapper: ({ children }) => ( {children} ), }, }) await waitFor(() => expect(routerMock.asPath).toBe('/project/default/functions/send-email/details') ) })