Files
supabase/apps/studio/components/interfaces/Functions/EdgeFunctionOverview/EdgeFunctionRecentErrors.platform.test.tsx
T
7f7ebf527b feat(studio): use shared logs table for recent function errors (#50958)
The Overview's "Errors in the last 24h" section now uses the Logs tab's
queries (#50954) and the read-only `EmbeddedLogsTable` (#50953) instead
of its own SQL and table. It previews the latest five errors since the
last deploy, capped at 24h, shows "+N more" when there are more and
links to the rest. It's behind the `edgeFunctionsOverview` flag, like
the rest of the new Overview.

- Rows have no multi-select checkbox. Clicking one opens it in the Logs
tab with the same range and error filter, and "View logs" and "+N more"
open the filtered list.
- With the unified logs preview off, those links open the pre-unified
Invocations tab (for invocation rows) or Logs tab (for runtime rows),
using those tabs' own `its`/`ite`/`log` params.
- "Explain with AI" and "Copy summary" send the previewed errors to the
Assistant with the same prompt as selected logs on the Logs page. The
prompt also notes how many errors were left out.
- One count query, scoped to the function and unfiltered, gives both the
error count behind "+N more" and the invocations since the deploy, from
its level and log type facets. It replaces the hand-built invocation
count SQL, so the section runs two queries instead of three.
- The row query fetches only the five errors shown, through a new
`pageSize` option on `useUnifiedLogsInfiniteQuery`.
- The section uses the edge function preset from #50957, so it runs the
Logs tab's scoped queries and shares its saved columns.
- The custom error grouping and its SQL are removed. Net, the PR removes
about 390 lines.

### How to test

1. With `edgeFunctionsOverview` on, open a function that has recent
errors. You should see up to five rows without checkboxes, and "+N more"
when there are more.
2. Click a row with the unified logs preview on. It opens the Logs tab
with that log selected. Turn the preview off and click an invocation row
and a runtime row. They open Invocations and Logs respectively.
3. Use Explain with AI and Copy summary and check the prompt.
4. For a function with no errors since the last deploy, the section
shows the invocation count instead.

### Validation

- MSW component tests: preview rows and "+N more", no checkboxes, the
summary prompt, row links with the preview on and off, and the no-errors
state. They also check the row query asks for five rows and that a
single unfiltered count runs. `pageSize` and the facet count helper have
unit tests.
- Studio typecheck, lint ratchet, Prettier and knip pass on this layer.

### Stack

Merge in order:

1. #50953 — reusable logs table building blocks
2. #50954 — function-scoped unified logs queries
3. #50955 — slimmer function detail layout, header on Overview
4. #50957 — Invocations merged into a function-scoped Logs tab
5. #50958 — Overview errors in the shared logs table
6. #52281 — remove the legacy Overview

---------

Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>
Co-authored-by: Jordi Enric <jordi.err@gmail.com>
2026-10-27 16:47:51 +01:00

332 lines
13 KiB
TypeScript

import { QueryClient } from '@tanstack/react-query'
import { screen, waitFor } from '@testing-library/react'
import userEvent from '@testing-library/user-event'
import type { platformComponents } from 'api-types'
import { FeatureFlagContext, LOCAL_STORAGE_KEYS } from 'common'
import { http, HttpResponse } from 'msw'
import { createDynamicRouteParser } from 'next-router-mock/dynamic-routes'
import { afterAll, afterEach, beforeEach, describe, expect, it, vi } from 'vitest'
import { z } from 'zod'
import { EdgeFunctionRecentErrors } from './EdgeFunctionRecentErrors'
import { FeaturePreviewContextProvider } from '@/components/interfaces/App/FeaturePreview/FeaturePreviewContext'
import { miscKeys } from '@/data/misc/keys'
import { AiAssistantStateContext, createAiAssistantState } from '@/state/ai-assistant-state'
import { sidebarManagerState } from '@/state/sidebar-manager-state'
import { customRender } from '@/tests/lib/custom-render'
import { addAPIMock, mswServer, type APIErrorBody } from '@/tests/lib/msw'
type AnalyticsResponse = platformComponents['schemas']['AnalyticsResponse_Output']
vi.unmock('common')
const { default: routerMock } =
await vi.importMock<typeof import('next-router-mock')>('next/router')
const removeRouteParser = routerMock.useParser(
createDynamicRouteParser(['/project/[ref]/functions/[functionSlug]'])
)
afterAll(removeRouteParser)
const featureFlags = {
configcat: {
otelUnifiedLogs: true,
otelLegacyLogs: true,
pipelineCreationWizard: false,
platformWebhooks: false,
jitDbAccess: false,
marketplaceIntegrations: false,
explorer: false,
storageVersioningPrivateAlpha: false,
sqlEditorManualSaveForced: false,
},
posthog: {},
hasLoaded: true,
}
const OTEL_ENDPOINT = '/platform/projects/:ref/analytics/endpoints/logs.all.otel'
const errorRow = (index: number, logType = 'edge function runtime') => ({
id: `log-${index}`,
timestamp: new Date(Date.now() - index * 60_000).toISOString(),
log_type: logType,
status: null,
level: 'error',
pathname: null,
event_message: `TypeError: failure ${index}`,
method: null,
log_count: null,
logs: null,
auth_user: null,
metadata: { level: 'error' },
})
const mockLogs = ({
rows,
totalErrors,
invocationCount,
}: {
rows: ReturnType<typeof errorRow>[]
totalErrors: number
invocationCount: number
}) => {
const rowListSql: string[] = []
const countSql: string[] = []
addAPIMock({
method: 'post',
path: OTEL_ENDPOINT,
response: async ({ request }) => {
const { sql } = z.object({ sql: z.string() }).parse(await request.json())
if (sql.includes('-- unified logs: row list')) {
rowListSql.push(sql)
const limit = Number(sql.match(/LIMIT (\d+)$/)?.[1] ?? rows.length)
return HttpResponse.json<AnalyticsResponse>({ result: rows.slice(0, limit) })
}
if (sql.includes('-- unified logs: sidebar facet counts')) {
countSql.push(sql)
return HttpResponse.json<AnalyticsResponse>({
result: [
{ facet: 'total', value: 'all', count: totalErrors + invocationCount },
{ facet: 'level', value: 'error', count: totalErrors },
{ facet: 'log_type', value: 'edge function', count: invocationCount },
],
})
}
return HttpResponse.json<APIErrorBody>({ message: 'Unexpected SQL' }, { status: 400 })
},
})
return { rowListSql, countSql }
}
const renderErrors = () => {
addAPIMock({
method: 'get',
path: '/platform/projects/:ref',
response: {
cloud_provider: 'AWS',
db_host: 'db.default.supabase.co',
high_availability: false,
id: 1,
inserted_at: '2026-01-01T00:00:00Z',
integration_source: null,
is_branch_enabled: false,
is_physical_backups_enabled: false,
name: 'Test project',
organization_id: 1,
ref: 'default',
region: 'us-east-1',
restUrl: 'https://default.supabase.co',
status: 'ACTIVE_HEALTHY',
subscription_id: 'subscription-1',
updated_at: '2026-01-01T00:00:00Z',
connectionString: 'postgresql://postgres:password@localhost:5432/postgres',
},
})
const queryClient = new QueryClient({ defaultOptions: { queries: { retry: false } } })
queryClient.setQueryData(miscKeys.enabledFeaturesOverride(), { disabled_features: [] })
const aiState = createAiAssistantState()
const content = (functionId: string) => (
<FeatureFlagContext.Provider value={featureFlags}>
<FeaturePreviewContextProvider>
<AiAssistantStateContext.Provider value={aiState}>
<EdgeFunctionRecentErrors
functionId={functionId}
functionSlug="hello-world"
projectRef="default"
updatedAt={new Date(Date.now() - 60 * 60 * 1000).toISOString()}
/>
</AiAssistantStateContext.Provider>
</FeaturePreviewContextProvider>
</FeatureFlagContext.Provider>
)
const rendered = customRender(content('fn-1'), { queryClient })
return {
...rendered,
renderFunction: (functionId: string) => rendered.rerender(content(functionId)),
}
}
describe('EdgeFunctionRecentErrors', () => {
beforeEach(() => {
routerMock.setCurrentUrl('/project/default/functions/hello-world')
localStorage.setItem(LOCAL_STORAGE_KEYS.UI_PREVIEW_UNIFIED_LOGS, 'true')
})
afterEach(() => {
localStorage.removeItem(LOCAL_STORAGE_KEYS.UI_PREVIEW_UNIFIED_LOGS)
sidebarManagerState.unregisterSidebar('ai-assistant')
})
it("previews the function's latest errors and links the rest to the Logs tab", async () => {
const user = userEvent.setup()
const copy = vi.spyOn(navigator.clipboard, 'writeText')
const { rowListSql, countSql } = mockLogs({
rows: [0, 1, 2, 3, 4, 5].map((index) => errorRow(index)),
totalErrors: 8,
invocationCount: 20,
})
renderErrors()
expect(await screen.findByText('TypeError: failure 0')).toBeInTheDocument()
expect(screen.getByText('TypeError: failure 4')).toBeInTheDocument()
expect(screen.queryByRole('checkbox')).not.toBeInTheDocument()
expect(screen.queryByText('TypeError: failure 5')).not.toBeInTheDocument()
expect(await screen.findByRole('button', { name: '+3 more' })).toBeInTheDocument()
expect(screen.getByRole('button', { name: 'Explain with AI' })).toBeInTheDocument()
await user.click(screen.getByRole('button', { name: 'Copy summary' }))
const summary = copy.mock.calls[0][0]
expect(summary).toContain('I have 5 Supabase log entries from the **Edge Functions** service')
expect(summary).toContain('TypeError: failure 4')
expect(summary).not.toContain('TypeError: failure 5')
expect(summary).toContain('+3 more matching log entries not included.')
expect(rowListSql[0]).toContain(`log_attributes['function_id'] IN ('fn-1')`)
expect(rowListSql[0]).toContain(`IN ('error')`)
expect(rowListSql[0]).toMatch(/LIMIT 5$/)
expect(countSql).toHaveLength(1)
expect(countSql[0]).toContain(`log_attributes['function_id'] IN ('fn-1')`)
expect(countSql[0]).not.toContain(`IN ('error')`)
})
it('opens the clicked error in the Logs tab with the same filters', async () => {
mockLogs({ rows: [0, 1].map((index) => errorRow(index)), totalErrors: 2, invocationCount: 20 })
renderErrors()
await userEvent.click(await screen.findByText('TypeError: failure 1'))
await waitFor(() => expect(routerMock.query.id).toBe('log-1'))
const url = new URL(routerMock.asPath, 'http://localhost')
expect(url.pathname).toBe('/project/default/functions/hello-world/logs')
expect(url.searchParams.get('filter')).toBe('level:eq:error')
expect(url.searchParams.get('id')).toBe('log-1')
expect(url.searchParams.get('date')).toMatch(/^\d+-\d+$/)
expect(screen.queryByRole('button', { name: /more$/ })).not.toBeInTheDocument()
})
it('opens errors in the pre-unified Invocations or Logs tab when unified logs are off', async () => {
localStorage.setItem(LOCAL_STORAGE_KEYS.UI_PREVIEW_UNIFIED_LOGS, 'false')
mockLogs({
rows: [errorRow(0), errorRow(1, 'edge function')],
totalErrors: 2,
invocationCount: 20,
})
renderErrors()
await userEvent.click(await screen.findByText('TypeError: failure 0'))
const runtimeUrl = new URL(routerMock.asPath, 'http://localhost')
expect(runtimeUrl.pathname).toBe('/project/default/functions/hello-world/logs')
expect(runtimeUrl.searchParams.get('log')).toBe('log-0')
expect(runtimeUrl.searchParams.get('its')).toBeTruthy()
expect(runtimeUrl.searchParams.get('ite')).toBeTruthy()
await userEvent.click(screen.getByText('TypeError: failure 1'))
const invocationUrl = new URL(routerMock.asPath, 'http://localhost')
expect(invocationUrl.pathname).toBe('/project/default/functions/hello-world/invocations')
expect(invocationUrl.searchParams.get('log')).toBe('log-1')
})
it('summarizes invocations since the last deploy when there are no errors', async () => {
mockLogs({ rows: [], totalErrors: 0, invocationCount: 3 })
renderErrors()
expect(await screen.findByText('3 invocations')).toBeInTheDocument()
expect(screen.getByText(/since last deploy and no errors/)).toBeInTheDocument()
expect(screen.queryByRole('button', { name: 'Copy summary' })).not.toBeInTheDocument()
expect(screen.queryByRole('button', { name: 'Explain with AI' })).not.toBeInTheDocument()
})
it('shows a retrieval error without offering summaries of unavailable errors', async () => {
addAPIMock({
method: 'post',
path: OTEL_ENDPOINT,
response: () =>
HttpResponse.json<APIErrorBody>({ message: 'Function logs unavailable' }, { status: 500 }),
})
renderErrors()
expect(await screen.findByText(/Failed to retrieve logs/)).toBeInTheDocument()
expect(screen.queryByRole('button', { name: 'Copy summary' })).not.toBeInTheDocument()
expect(screen.queryByRole('button', { name: 'Explain with AI' })).not.toBeInTheDocument()
expect(screen.queryByText(/since last deploy and no errors/)).not.toBeInTheDocument()
})
it('clears the previous function errors and summaries while another function loads', async () => {
mockLogs({ rows: [errorRow(0)], totalErrors: 1, invocationCount: 20 })
const { renderFunction } = renderErrors()
expect(await screen.findByText('TypeError: failure 0')).toBeInTheDocument()
let releaseResponse: () => void = () => {}
const pendingResponse = new Promise<void>((resolve) => {
releaseResponse = resolve
})
addAPIMock({
method: 'post',
path: OTEL_ENDPOINT,
response: async ({ request }) => {
const { sql } = z.object({ sql: z.string() }).parse(await request.json())
if (sql.includes('sidebar facet counts'))
return HttpResponse.json<AnalyticsResponse>({ result: [] })
await pendingResponse
return HttpResponse.json<AnalyticsResponse>({ result: [errorRow(1)] })
},
})
try {
renderFunction('fn-2')
expect(screen.queryByText('TypeError: failure 0')).not.toBeInTheDocument()
expect(screen.queryByRole('button', { name: 'Copy summary' })).not.toBeInTheDocument()
expect(screen.queryByRole('button', { name: 'Explain with AI' })).not.toBeInTheDocument()
} finally {
releaseResponse()
}
expect(await screen.findByText('TypeError: failure 1')).toBeInTheDocument()
})
it('opens the Assistant with the same preview summary copied for external AI tools', async () => {
const user = userEvent.setup()
const copy = vi.spyOn(navigator.clipboard, 'writeText')
mockLogs({ rows: [errorRow(0)], totalErrors: 3, invocationCount: 20 })
const submittedMessages: Array<{ parts: Array<{ type: string; text?: string }> }> = []
mswServer.use(
http.post('/api/ai/sql/generate-v4', async ({ request }) => {
const { messages } = z
.object({
messages: z.array(
z.object({
parts: z.array(z.object({ type: z.string(), text: z.string().optional() })),
})
),
})
.parse(await request.json())
submittedMessages.push(...messages)
return new HttpResponse('data: [DONE]\n\n', {
headers: { 'content-type': 'text/event-stream', 'x-vercel-ai-ui-message-stream': 'v1' },
})
})
)
sidebarManagerState.registerSidebar('ai-assistant', () => null)
renderErrors()
await screen.findByText('TypeError: failure 0')
await user.click(screen.getByRole('button', { name: 'Copy summary' }))
await user.click(screen.getByRole('button', { name: 'Explain with AI' }))
expect(sidebarManagerState.activeSidebar?.id).toBe('ai-assistant')
await waitFor(() => {
expect(submittedMessages).toHaveLength(1)
expect(submittedMessages[0].parts).toEqual([{ type: 'text', text: copy.mock.calls[0][0] }])
})
})
})