Files
supabase/apps/studio/components/interfaces/Functions/EdgeFunctionOverview/EdgeFunctionOverview.periods.platform.test.tsx
Jordi Enric 431a55435a fix(studio): keep the function overview range in logs FE-4973 (#53867)
## Problem

An Edge Function’s Overview can show an invocation from two hours ago.
Selecting **Open logs** or pressing **O** then **L** opens the
function’s Logs page with its default one-hour window, which hides that
invocation.

## Fix

Both actions now pass the start and end of the selected Overview window
to the function’s Logs page.

## How to test

1. Open an Edge Function with invocations in the last three hours and
select the three-hour interval on Overview.
2. Select **Open logs**. Check that Logs opens for the same function and
the date range spans exactly three hours. If an invocation is older than
one hour, confirm it appears in that range.
3. Return to Overview, select three hours again, and press **O** then
**L**. Check the same function and time range.

Related to [FE-4973](https://linear.app/supabase/issue/FE-4973).

## Verification

On the Studio staging preview, both **Open logs** and **O** then **L**
opened the same function with the exact Overview timestamps,
13:03:16.960 through 16:03:16.960 UTC. The results included a successful
invocation more than one hour before the window ended.

All 72 focused Overview and Logs navigation tests passed. Removing the
date range from either entry point independently failed its regression
assertion.
2026-12-01 13:03:23 +00:00

109 lines
4.7 KiB
TypeScript

import { QueryClient } from '@tanstack/react-query'
import { screen, waitFor } from '@testing-library/react'
import userEvent from '@testing-library/user-event'
import { FeatureFlagContext, LOCAL_STORAGE_KEYS } from 'common'
import { HttpResponse } from 'msw'
import { afterEach, expect, it, vi } from 'vitest'
import { FeaturePreviewContextProvider } from '@/components/interfaces/App/FeaturePreview/FeaturePreviewContext'
import { configKeys } from '@/data/config/keys'
import { customDomainKeys } from '@/data/custom-domains/keys'
import type { EdgeFunction } from '@/data/edge-functions/edge-function-query'
import { edgeFunctionsKeys } from '@/data/edge-functions/keys'
import { projectKeys } from '@/data/projects/keys'
import { subscriptionKeys } from '@/data/subscriptions/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'
afterEach(() => {
vi.useRealTimers()
localStorage.removeItem(LOCAL_STORAGE_KEYS.UI_PREVIEW_UNIFIED_LOGS)
})
it('loads the overview before feature flags and updates metrics with the selected period', async () => {
localStorage.setItem(LOCAL_STORAGE_KEYS.UI_PREVIEW_UNIFIED_LOGS, 'true')
vi.useFakeTimers({ toFake: ['Date'] })
vi.setSystemTime(new Date('2026-03-20T10:37:21Z'))
const user = userEvent.setup()
const queryClient = new QueryClient({
defaultOptions: { queries: { retry: false, staleTime: Infinity } },
})
queryClient.setQueryData(projectKeys.detail('default'), createMockProject({ ref: 'default' }))
queryClient.setQueryData(configKeys.settingsV2('default'), {
app_config: { endpoint: 'default.supabase.co' },
})
queryClient.setQueryData(subscriptionKeys.addons('default'), { selected_addons: [] })
queryClient.setQueryData(customDomainKeys.list('default'), {})
queryClient.setQueryData<EdgeFunction>(edgeFunctionsKeys.detail('default', 'send-email'), {
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(),
})
addAPIMock({
method: 'get',
path: '/platform/projects/:ref/analytics/endpoints/functions.combined-stats',
response: ({ request }) => {
const interval = new URL(request.url).searchParams.get('interval')
if (interval === '15min') return HttpResponse.json<{ result: never[] }>({ result: [] })
const isDaily = interval === '1day'
return HttpResponse.json<{
result: Array<{ timestamp: string; avg_execution_time: number }>
}>({
result: [
{ timestamp: '2026-03-20T10:35:00Z', avg_execution_time: isDaily ? 90 : 40 },
{ timestamp: '2026-03-20T10:36:00Z', avg_execution_time: isDaily ? 90 : 80 },
],
})
},
})
addAPIMock({
method: 'post',
path: '/platform/projects/:ref/analytics/endpoints/logs.all.otel',
response: () => HttpResponse.json<{ result: never[] }>({ result: [] }),
})
const { router } = await renderRoute(Route, {
routeId: '/project/$ref/functions/$functionSlug/',
url: '/project/default/functions/send-email',
renderOptions: {
wrapper: ({ children }) => (
<CustomWrapper queryClient={queryClient}>
<FeatureFlagContext.Provider value={{ configcat: {}, posthog: {}, hasLoaded: false }}>
<FeaturePreviewContextProvider>{children}</FeaturePreviewContextProvider>
</FeatureFlagContext.Provider>
</CustomWrapper>
),
},
})
expect(await screen.findByText('60ms')).toBeVisible()
await user.click(screen.getByRole('button', { name: '15 min' }))
expect((await screen.findAllByText('No data to show'))[0]).toBeVisible()
await user.click(screen.getByRole('button', { name: '1 day' }))
await waitFor(() =>
expect(screen.getByRole('button', { name: '1 day', pressed: true })).toBeVisible()
)
expect(await screen.findByText('90ms')).toBeVisible()
await user.click(screen.getByRole('button', { name: '3 hours' }))
const expectedRange = `${Date.parse('2026-03-20T07:37:21.000Z')}-${Date.parse('2026-03-20T10:37:21.000Z')}`
const openLogsLink = await screen.findByRole('link', { name: 'Open logs' })
expect
.soft(openLogsLink)
.toHaveAttribute('href', `/project/default/functions/send-email/logs?date=${expectedRange}`)
await user.keyboard('o')
await user.keyboard('l')
await waitFor(() =>
expect(router.state.location.pathname).toBe('/project/default/functions/send-email/logs')
)
expect(router.state.location.searchStr).toBe(`?date=${expectedRange}`)
})