Files
supabase/apps/studio/data/logs/unified-log-attributes-query.test.ts
6612073428 feat(studio): add bounded request correlation queries (#50840)
Add tested query support for correlating logs by request and execution
IDs across API Gateway, Auth, Storage, and Edge Functions. Queries are
source-scoped, time-bounded, and limited to 200 rows, with at most one
additional lookup to follow a newly discovered execution ID.

This data-only layer introduces no UI or automatic requests. The
Timeline tab consumes its query options in #50836. Overview and request
correlation share a cached source-and-attributes lookup, including
concurrent requests. Cancelling a timeline preserves any shared overview
lookup. The new query is exercised through MSW without mocking React
Query hooks.

### How to test

This is a data-only PR. From the repository root, run:

```sh
pnpm --filter studio exec vitest run components/interfaces/UnifiedLogs/UnifiedLogs.queries.test.ts data/logs/unified-log-request-timeline-query.test.ts
```

The tests exercise source-scoped IDs, SQL escaping, time/row limits,
missing IDs, failed requests, the additional execution-ID lookup,
attribute-cache reuse in both navigation orders and concurrently, and
cancellation through MSW.

### Validation

- Synced with master; verified head
`a60bcf0565e0e3477779665660e871eec2edd2c8`.
- Focused unit/MSW baseline and shuffled seed 5084028: 121 tests across
seven files passed with retries disabled. Independent shuffled run, seed
5084029: 117 tests across six files passed.
- Correlation, shared lookup/cache, cancellation, and the 400-second
function boundary are covered. Behavioral mutation checks detect
shared-lookup cancellation and a shortened execution window.
- Matching studio-staging preview smoke check passed for existing
grouped overview attributes and complete copied Raw JSON metadata.
- All four unit-test shards, all four E2E shards, typecheck, formatting,
lint ratchet, knip, and Studio Docker build passed on this head.
- Independent review found no actionable issues; no unresolved review
threads.

### Stack

Based on the logs table building blocks stack, which merges first:
#50953 → #50954 → #50955 → #50957 → #50958 → #52281. Then merge in
order:

1. #50835 — grouped log overviews and raw JSON
2. #50840 — request correlation queries
3. #50836 — request timeline UI and folding
4. #50841 — shared Auth user details
5. #50837 — log user correlation and Auth activity

---------

Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>
Co-authored-by: Jordi Enric <jordi.err@gmail.com>
2026-10-28 14:41:49 +01:00

81 lines
2.9 KiB
TypeScript

import { QueryClient } from '@tanstack/react-query'
import type { platformComponents } from 'api-types'
import { HttpResponse } from 'msw'
import { describe, expect, it } from 'vitest'
import { unifiedLogAttributesQueryOptions } from './unified-log-attributes-query'
import { getLogAttributesQuery } from '@/components/interfaces/UnifiedLogs/UnifiedLogs.queries'
import { addAPIMock, type APIErrorBody } from '@/tests/lib/msw'
type LogsResponse = platformComponents['schemas']['AnalyticsResponse_Output']
const variables = {
projectRef: 'default',
logId: 'log-1',
source: 'edge_logs',
logTimestampMs: Date.parse('2026-01-01T10:00:00Z'),
}
const fetchAttributes = () =>
new QueryClient({ defaultOptions: { queries: { retry: false } } }).fetchQuery(
unifiedLogAttributesQueryOptions(variables)
)
describe('log attributes', () => {
it('escapes IDs and sources and limits the lookup to one row', () => {
const sql = getLogAttributesQuery({ logId: "log'1", source: "source'1" })
expect(sql).toContain("id = 'log''1' AND source = 'source''1'")
expect(sql).toContain('LIMIT 1')
})
it('fetches attributes within one minute of the log timestamp', async () => {
let body: unknown
addAPIMock({
method: 'post',
path: '/platform/projects/:ref/analytics/endpoints/logs.all.otel',
response: async ({ request }) => {
body = await request.json()
return HttpResponse.json<LogsResponse>({
result: [{ source: 'edge_logs', log_attributes: { 'request.method': 'GET' } }],
})
},
})
await expect(fetchAttributes()).resolves.toEqual({
source: 'edge_logs',
log_attributes: { 'request.method': 'GET' },
})
expect(body).toMatchObject({
iso_timestamp_start: '2026-01-01T09:59:00.000Z',
iso_timestamp_end: '2026-01-01T10:01:00.000Z',
})
})
it.each([{ result: [] }, { result: [{ source: 'edge_logs', log_attributes: null }] }])(
'preserves the source when attributes are absent: %j',
async ({ result }) => {
addAPIMock({
method: 'post',
path: '/platform/projects/:ref/analytics/endpoints/logs.all.otel',
response: () => HttpResponse.json<LogsResponse>({ result }),
})
await expect(fetchAttributes()).resolves.toEqual(result[0] ?? null)
}
)
it('rejects malformed rows', async () => {
addAPIMock({
method: 'post',
path: '/platform/projects/:ref/analytics/endpoints/logs.all.otel',
response: () => HttpResponse.json<LogsResponse>({ result: [{ source: 42 }] }),
})
await expect(fetchAttributes()).rejects.toThrow()
})
it('propagates failed requests', async () => {
addAPIMock({
method: 'post',
path: '/platform/projects/:ref/analytics/endpoints/logs.all.otel',
response: () => HttpResponse.json<APIErrorBody>({ message: 'Unavailable' }, { status: 500 }),
})
await expect(fetchAttributes()).rejects.toThrow('Unavailable')
})
})