Files
supabase/apps/studio/data/logs/unified-logs-infinite-query.test.tsx
973dfa1b97 feat(studio): scope unified logs queries to one edge function (#50954)
Adds an optional `scope` to the unified logs search: fixed filters every
query applies on top of the user's, which the user can't remove. Nothing
sets a scope in this PR, so the Logs page is unchanged. #50957 uses one
to narrow the function Logs tab to one edge function.

A scope uses the same columns and operators as the `filter` URL param,
so any view can narrow logs to one resource without new query code.
Columns the query builder doesn't know match a `log_attributes` key, so
one edge function's logs are:

```ts
{
  filters: [
    { column: 'log_type', operator: '=', value: 'edge function' },
    { column: 'log_type', operator: '=', value: 'edge function runtime' },
    { column: 'function_id', operator: '=', value: functionId },
  ],
}
```

- New `edge function runtime` log type, read from the `function_logs`
source. Its level comes from the console level (`error`/`fatal` count as
errors, `warn`/`warning` as warnings), and its details stay in metadata.
- **Every scan**: the scope's filters apply to rows, counts, the chart
and single-facet queries. Facet scans never drop them, unlike the user's
own filter on the facet's column. Filters on the same column and
operator combine like the URL's (`=` values match any), every group must
match, and a scope replaces the default postgres + edge log types.
- **Fails closed**: a scope condition that can't be built matches no
rows, instead of being dropped like a bad user filter.
- **ClickHouse only**: scoped searches always run on ClickHouse,
whatever `otelUnifiedLogs` says, including downloads and the log detail
panel. The BigQuery queries are unchanged from master and throw on a
scope rather than return rows from outside it.
- `getLogTypeVisibility` offers only the log types a scope fixes.
Otherwise it keeps the new log type out of the filters, since the
project-wide Logs page already shows runtime logs inside each
invocation's detail panel.
- `toQuerySearchParams` and `createUnifiedLogsSearch` attach the scope.
- `UnifiedLogs` works out which log types its queries may use once,
instead of in each of its queries, filters and facets.

### How to test

1. Open the project Logs page with the unified logs preview on. The log
type filter shouldn't offer "Edge Function runtime", and default results
should match master.
2. The scoped behavior is easiest to try on #50957.

### Validation

- Query builder tests: the scope on every count scan, the chart and
facet queries, requests for out-of-scope log types, different operators
on one column, the fail-closed case, BigQuery refusing a scope, and
unscoped queries staying the same.
- 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-26 16:52:15 +01:00

65 lines
2.2 KiB
TypeScript

import { waitFor } from '@testing-library/react'
import { platformComponents } from 'api-types'
import { FeatureFlagContext } from 'common'
import { HttpResponse } from 'msw'
import { expect, it } from 'vitest'
import { z } from 'zod'
import { useUnifiedLogsInfiniteQuery } from './unified-logs-infinite-query'
import { createUnifiedLogsSearch } from '@/components/interfaces/UnifiedLogs/UnifiedLogs.utils'
import { customRenderHook, CustomWrapper } from '@/tests/lib/custom-render'
import { addAPIMock } from '@/tests/lib/msw'
type AnalyticsResponse = platformComponents['schemas']['AnalyticsResponse_Output']
const requestSchema = z.object({ sql: z.string() })
it.each([
{
scenario: 'uses BigQuery for project-wide logs when the ClickHouse feature flag is off',
isScoped: false,
},
{
scenario: 'keeps function-scoped logs on ClickHouse when project-wide logs use BigQuery',
isScoped: true,
},
])('$scenario', async ({ isScoped }) => {
const queries: string[] = []
addAPIMock({
method: 'post',
path: isScoped
? '/platform/projects/:ref/analytics/endpoints/logs.all.otel'
: '/platform/projects/:ref/analytics/endpoints/logs.all',
response: async ({ request }) => {
queries.push(requestSchema.parse(await request.json()).sql)
return HttpResponse.json<AnalyticsResponse>({ result: [] })
},
})
const scope = isScoped
? {
filters: [{ column: 'function_id', operator: '=' as const, value: 'fn-123' }],
}
: undefined
const { result } = customRenderHook(
() =>
useUnifiedLogsInfiniteQuery({
projectRef: 'default',
search: createUnifiedLogsSearch({}, scope),
}),
{
wrapper: ({ children }) => (
<CustomWrapper>
<FeatureFlagContext.Provider
value={{ configcat: { otelUnifiedLogs: false }, posthog: {}, hasLoaded: true }}
>
{children}
</FeatureFlagContext.Provider>
</CustomWrapper>
),
}
)
await waitFor(() => expect(result.current.isFetching).toBe(false))
expect(result.current.isSuccess).toBe(true)
expect(queries).toHaveLength(1)
if (isScoped) expect(queries[0]).toContain("log_attributes['function_id'] IN ('fn-123')")
})