Files
supabase/apps/studio/components/interfaces/Functions/EdgeFunctionOverview/EdgeFunctionOverview.utils.test.ts
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

329 lines
11 KiB
TypeScript

import dayjs from 'dayjs'
import { describe, expect, it } from 'vitest'
import {
EDGE_FUNCTION_CHART_INTERVALS,
formatChartTimestamp,
formatMetric,
formatRate,
formatReferenceDelta,
getChartEmptyStateCopy,
getChartTimeRangeLabels,
getEdgeFunctionLogsScope,
getEdgeFunctionLogsUrl,
getExecutionMetrics,
getInvocationChartData,
getInvocationChartNavigationUrl,
getInvocationTotals,
getInvocationUpdateAnnotation,
getLegacyEdgeFunctionLogsUrl,
getMemoryTooltipDetail,
getRollingTimeRange,
getUsageMetrics,
toEdgeFunctionChartData,
type EdgeFunctionChartRawDatum,
} from './EdgeFunctionOverview.utils'
import { getUnifiedLogsQuery } from '@/components/interfaces/UnifiedLogs/UnifiedLogs.queries'
import { createUnifiedLogsSearch } from '@/components/interfaces/UnifiedLogs/UnifiedLogs.utils'
describe('Edge function overview chart summaries', () => {
it('counts every invocation status and groups redirects and client errors as warnings', () => {
const chartData = toEdgeFunctionChartData([
{
timestamp: '2026-03-20T10:00:00.000Z',
success_count: 10,
redirect_count: 2,
client_err_count: 1,
server_err_count: 3,
},
{
timestamp: '2026-03-20T10:15:00.000Z',
success_count: '4',
redirect_count: '1',
client_err_count: '0',
server_err_count: '2',
},
] satisfies EdgeFunctionChartRawDatum[])
const data = getInvocationChartData(chartData)
expect(data).toEqual([
{
timestamp: '2026-03-20T10:00:00.000Z',
ok_count: 10,
warning_count: 3,
error_count: 3,
},
{
timestamp: '2026-03-20T10:15:00.000Z',
ok_count: 4,
warning_count: 1,
error_count: 2,
},
])
expect(chartData[1]).toEqual({
timestamp: '2026-03-20T10:15:00.000Z',
success_count: 4,
redirect_count: 1,
client_err_count: 0,
server_err_count: 2,
avg_execution_time: 0,
max_execution_time: 0,
avg_cpu_time_used: 0,
max_cpu_time_used: 0,
avg_memory_used: 0,
avg_heap_memory_used: 0,
avg_external_memory_used: 0,
})
expect(getInvocationTotals(data)).toEqual({
totalInvocationCount: 23,
totalWarningCount: 4,
totalErrorCount: 5,
})
})
it('labels the first and last chart timestamps for the selected period', () => {
expect(
getChartTimeRangeLabels(
[{ timestamp: '2026-03-20T10:00:00.000Z' }, { timestamp: '2026-03-20T11:00:00.000Z' }],
'MMM D, h:mma'
)
).toEqual({
start: dayjs('2026-03-20T10:00:00.000Z').format('MMM D, h:mma'),
end: dayjs('2026-03-20T11:00:00.000Z').format('MMM D, h:mma'),
})
expect(getChartTimeRangeLabels([], 'MMM D')).toBeUndefined()
expect(formatChartTimestamp('2026-03-20T10:00:00.000Z', 'MMM D, h:mma')).toBe(
dayjs('2026-03-20T10:00:00.000Z').format('MMM D, h:mma')
)
})
it('summarizes execution time, CPU time, and memory across the returned invocation buckets', () => {
const stats = [
{
timestamp: '2026-03-20T10:00:00.000Z',
success_count: 0,
redirect_count: 0,
client_err_count: 0,
server_err_count: 0,
avg_execution_time: 10,
max_execution_time: 18,
avg_cpu_time_used: 5,
max_cpu_time_used: 8,
avg_memory_used: 100,
avg_heap_memory_used: 75,
avg_external_memory_used: 25,
},
{
timestamp: '2026-03-20T10:15:00.000Z',
success_count: 0,
redirect_count: 0,
client_err_count: 0,
server_err_count: 0,
avg_execution_time: 30,
max_execution_time: 45,
avg_cpu_time_used: 15,
max_cpu_time_used: 20,
avg_memory_used: 200,
avg_heap_memory_used: 150,
avg_external_memory_used: 50,
},
]
expect(getExecutionMetrics(stats)).toEqual({
averageExecutionTime: 20,
maxExecutionTime: 45,
})
expect(getUsageMetrics(stats)).toEqual({
averageCpuTime: 10,
maxCpuTime: 20,
averageMemoryUsage: 150,
totalHeapMemory: 225,
totalExternalMemory: 75,
totalMemoryByType: 300,
})
})
it.each([
['an ISO string', '2026-03-20T10:16:30.000Z'],
['a numeric', new Date('2026-03-20T10:16:30.000Z').valueOf()],
])(
'marks the nearest invocation bucket to a deployment recorded with %s timestamp',
(_, updatedAt) => {
const annotation = getInvocationUpdateAnnotation({
updatedAt,
invocationChartData: [
{ timestamp: '2026-03-20T10:00:00.000Z', ok_count: 3, warning_count: 0, error_count: 0 },
{ timestamp: '2026-03-20T10:15:00.000Z', ok_count: 5, warning_count: 1, error_count: 1 },
{ timestamp: '2026-03-20T10:30:00.000Z', ok_count: 2, warning_count: 0, error_count: 0 },
],
windowStart: new Date('2026-03-20T09:45:00.000Z'),
windowEnd: new Date('2026-03-20T10:45:00.000Z'),
})
expect(annotation?.timestamp).toBe('2026-03-20T10:15:00.000Z')
expect(annotation?.position).toBeCloseTo(50)
expect(annotation?.updatedAt.toISOString()).toBe('2026-03-20T10:16:30.000Z')
}
)
it('hides deployments outside the selected chart period', () => {
const annotation = getInvocationUpdateAnnotation({
updatedAt: '2026-03-20T11:05:00.000Z',
invocationChartData: [
{ timestamp: '2026-03-20T10:00:00.000Z', ok_count: 3, warning_count: 0, error_count: 0 },
],
windowStart: new Date('2026-03-20T09:45:00.000Z'),
windowEnd: new Date('2026-03-20T10:45:00.000Z'),
})
expect(annotation).toBeUndefined()
})
it('hides the deployment marker when its timestamp cannot be read', () => {
const annotation = getInvocationUpdateAnnotation({
updatedAt: 'invalid',
invocationChartData: [
{ timestamp: '2026-03-20T10:00:00.000Z', ok_count: 3, warning_count: 0, error_count: 0 },
],
windowStart: new Date('2026-03-20T09:45:00.000Z'),
windowEnd: new Date('2026-03-20T10:45:00.000Z'),
})
expect(annotation).toBeUndefined()
})
it.each([
{
updatedAt: undefined,
invocationChartData: [
{ timestamp: '2026-03-20T10:00:00.000Z', ok_count: 3, warning_count: 0, error_count: 0 },
],
},
{ updatedAt: '2026-03-20T10:00:00.000Z', invocationChartData: [] },
])(
'hides the deployment marker until its timestamp and invocation data are available',
({ updatedAt, invocationChartData }) => {
const annotation = getInvocationUpdateAnnotation({
updatedAt,
invocationChartData,
windowStart: new Date('2026-03-20T09:45:00.000Z'),
windowEnd: new Date('2026-03-20T10:45:00.000Z'),
})
expect(annotation).toBeUndefined()
}
)
it.each([
{ period: '1hr', start: '2026-03-20T09:37:00.000Z' },
{ period: '1day', start: '2026-03-19T10:37:00.000Z' },
])('opens the full past $period through the current minute', ({ period, start }) => {
const interval = EDGE_FUNCTION_CHART_INTERVALS.find((item) => item.key === period)
if (!interval) throw new Error(`Unknown function chart period: ${period}`)
const [rollingStart, rollingEnd] = getRollingTimeRange(
interval,
new Date('2026-03-20T10:37:00.000Z')
)
expect(rollingStart.toISOString()).toBe(start)
expect(rollingEnd.toISOString()).toBe('2026-03-20T10:37:00.000Z')
})
it.each([
{
isUnifiedLogsEnabled: true,
expected: `/project/project-ref/functions/function-slug/logs?date=${Date.parse('2026-03-20T10:27:30.000Z')}-${Date.parse('2026-03-20T10:32:30.000Z')}`,
},
{
isUnifiedLogsEnabled: false,
expected:
'/project/project-ref/functions/function-slug/invocations?its=2026-03-20T10%3A27%3A30.000Z&ite=2026-03-20T10%3A32%3A30.000Z',
},
])(
'builds a focused URL from the clicked bar (unified logs: $isUnifiedLogsEnabled)',
({ isUnifiedLogsEnabled, expected }) => {
const url = getInvocationChartNavigationUrl({
projectRef: 'project-ref',
functionSlug: 'function-slug',
isUnifiedLogsEnabled,
rangeStart: '2026-03-20T10:00:00.000Z',
rangeEnd: '2026-03-20T11:00:00.000Z',
clickedTimestamp: '2026-03-20T10:30:00.000Z',
})
expect(url).toBe(expected)
}
)
it('links to the unified function logs tab with an optional range and selected log', () => {
const base = { projectRef: 'project-ref', functionSlug: 'function-slug' }
expect(getEdgeFunctionLogsUrl(base)).toBe('/project/project-ref/functions/function-slug/logs')
expect(
getEdgeFunctionLogsUrl({
...base,
start: '2026-03-20T10:00:00.000Z',
end: '2026-03-20T11:00:00.000Z',
filters: ['level:eq:error'],
logId: 'log-id',
})
).toBe(
`/project/project-ref/functions/function-slug/logs?filter=level:eq:error&date=${Date.parse('2026-03-20T10:00:00.000Z')}-${Date.parse('2026-03-20T11:00:00.000Z')}&id=log-id`
)
})
it("scopes unified logs to the function's invocations and runtime logs", () => {
const sql = getUnifiedLogsQuery(createUnifiedLogsSearch({}, getEdgeFunctionLogsScope('fn-1')))
const where = sql.split(/\bWHERE\b/)[1] ?? ''
expect(where).toContain(
`((source = 'function_edge_logs') OR (source = 'function_logs')) AND log_attributes['function_id'] IN ('fn-1')`
)
expect(where).not.toContain(`source = 'postgres_logs'`)
})
it('links to the pre-unified Invocations and Logs tabs with their own params', () => {
const base = { projectRef: 'project-ref', functionSlug: 'function-slug' }
expect(getLegacyEdgeFunctionLogsUrl({ ...base, tab: 'logs' })).toBe(
'/project/project-ref/functions/function-slug/logs'
)
expect(
getLegacyEdgeFunctionLogsUrl({
...base,
tab: 'invocations',
start: '2026-03-20T10:00:00.000Z',
end: '2026-03-20T11:00:00.000Z',
logId: 'log-id',
})
).toBe(
'/project/project-ref/functions/function-slug/invocations?its=2026-03-20T10%3A00%3A00.000Z&ite=2026-03-20T11%3A00%3A00.000Z&log=log-id'
)
})
it('shows metric units, invocation percentages, and differences from the average', () => {
expect(formatMetric(12.34, 'MB')).toBe('12.3MB')
expect(formatMetric(1234, 'ms')).toBe('1,234ms')
expect(formatRate(1, 4)).toBe('25%')
expect(formatReferenceDelta(110, 100)).toBe('10% above average')
expect(formatReferenceDelta(90, 100)).toBe('10% below average')
expect(formatReferenceDelta(100, 100)).toBe('At average')
})
it('explains empty charts, query failures, and heap and external memory usage', () => {
expect(getChartEmptyStateCopy('invocations', false, 'boom')).toEqual({
title: 'No data to show',
description: undefined,
})
expect(getChartEmptyStateCopy('CPU time', true, 'Request failed')).toEqual({
title: 'Unable to load CPU time',
description: 'Request failed',
})
expect(getMemoryTooltipDetail(12.34, 5.67)).toBe('Heap 12.3MB • External 5.7MB')
})
})