mirror of
https://github.com/supabase/supabase.git
synced 2026-12-04 07:29:16 +00:00
517 lines
17 KiB
TypeScript
517 lines
17 KiB
TypeScript
import { describe, expect, it } from 'vitest'
|
|
|
|
import type { UnifiedLogsScope } from './UnifiedLogs.types'
|
|
import {
|
|
buildUnifiedLogsUrl,
|
|
createUnifiedLogsSearch,
|
|
gateLogTypeFilters,
|
|
gateLogTypeOptions,
|
|
getComputeLogsAvailability,
|
|
getEventMessageDisplay,
|
|
getFacetValueCount,
|
|
getLogRowClassName,
|
|
getLogTypeVisibility,
|
|
getRawLogData,
|
|
getUniqueLogRows,
|
|
isOfferedColumnFilter,
|
|
isOfferedFilter,
|
|
parseMultigresEventMessage,
|
|
toQuerySearchParams,
|
|
wrapIlikePattern,
|
|
} from './UnifiedLogs.utils'
|
|
|
|
describe('buildUnifiedLogsUrl', () => {
|
|
const parse = (url: string) => {
|
|
const [path, query] = url.split('?')
|
|
return { path, params: new URLSearchParams(query) }
|
|
}
|
|
|
|
it('targets the project logs route with a log_type filter', () => {
|
|
const { path, params } = parse(buildUnifiedLogsUrl({ projectRef: 'abc', logType: 'postgres' }))
|
|
expect(path).toBe('/project/abc/logs')
|
|
expect(params.get('filter')).toBe('log_type:eq:postgres')
|
|
expect(params.has('date')).toBe(false)
|
|
})
|
|
|
|
it('preserves multi-word log types once decoded', () => {
|
|
const { params } = parse(buildUnifiedLogsUrl({ projectRef: 'abc', logType: 'edge function' }))
|
|
expect(params.get('filter')).toBe('log_type:eq:edge function')
|
|
})
|
|
|
|
it.each([
|
|
['Date objects', new Date('2026-05-08T00:00:00.000Z'), new Date('2026-05-08T01:00:00.000Z')],
|
|
['ISO strings', '2026-05-08T00:00:00.000Z', '2026-05-08T01:00:00.000Z'],
|
|
])('adds the date range as an epoch-ms pair from %s', (_, start, end) => {
|
|
const { params } = parse(
|
|
buildUnifiedLogsUrl({ projectRef: 'abc', logType: 'auth', start, end })
|
|
)
|
|
expect(params.get('date')).toBe(`${new Date(start).valueOf()}-${new Date(end).valueOf()}`)
|
|
})
|
|
|
|
it('adds the user filter and leaves out params that are not set', () => {
|
|
expect(buildUnifiedLogsUrl({ projectRef: 'abc' })).toBe('/project/abc/logs')
|
|
const { params } = parse(buildUnifiedLogsUrl({ projectRef: 'abc', user: 'user@example.com' }))
|
|
expect(params.get('user')).toBe('user@example.com')
|
|
expect(params.has('filter')).toBe(false)
|
|
})
|
|
|
|
it('omits the date range when only one bound is provided', () => {
|
|
const { params } = parse(
|
|
buildUnifiedLogsUrl({ projectRef: 'abc', logType: 'storage', start: new Date() })
|
|
)
|
|
expect(params.has('date')).toBe(false)
|
|
})
|
|
|
|
it('opens a log when given its id', () => {
|
|
const { params } = parse(buildUnifiedLogsUrl({ projectRef: 'abc', user: 'u', logId: 'log-1' }))
|
|
expect(params.get('id')).toBe('log-1')
|
|
expect(params.get('user')).toBe('u')
|
|
})
|
|
})
|
|
|
|
describe('parseMultigresEventMessage', () => {
|
|
it('extracts the msg field from a stringified JSON payload', () => {
|
|
const value = JSON.stringify({
|
|
time: '2026-07-03T09:42:12.344925698Z',
|
|
level: 'INFO',
|
|
msg: 'user pool capacity updated',
|
|
user: 'supabase_admin',
|
|
})
|
|
expect(parseMultigresEventMessage(value)).toBe('user pool capacity updated')
|
|
})
|
|
|
|
it('returns the raw string when it is not JSON', () => {
|
|
expect(parseMultigresEventMessage('plain text message')).toBe('plain text message')
|
|
})
|
|
|
|
it('returns the raw string when msg is missing or empty', () => {
|
|
expect(parseMultigresEventMessage(JSON.stringify({ level: 'INFO' }))).toBe('{"level":"INFO"}')
|
|
expect(parseMultigresEventMessage(JSON.stringify({ msg: ' ' }))).toBe('{"msg":" "}')
|
|
})
|
|
|
|
it('passes empty values through unchanged', () => {
|
|
expect(parseMultigresEventMessage(undefined)).toBeUndefined()
|
|
expect(parseMultigresEventMessage('')).toBe('')
|
|
})
|
|
})
|
|
|
|
describe('getEventMessageDisplay', () => {
|
|
it('parses multigres rows into their msg field and capitalizes them', () => {
|
|
const value = JSON.stringify({ level: 'INFO', msg: 'Configuring synchronous replication' })
|
|
expect(getEventMessageDisplay('multigres', value)).toEqual({
|
|
message: 'Configuring synchronous replication',
|
|
capitalize: true,
|
|
})
|
|
})
|
|
|
|
it('leaves non-parsed log types untouched and uncapitalized', () => {
|
|
expect(getEventMessageDisplay('postgres', 'relation does not exist')).toEqual({
|
|
message: 'relation does not exist',
|
|
capitalize: false,
|
|
})
|
|
})
|
|
})
|
|
|
|
describe('getRawLogData', () => {
|
|
it('returns only the real Compute payload fields', () => {
|
|
const row = {
|
|
event_message: 'Error: Dynamic require of "path" is not supported',
|
|
id: '51a29911-9293-4616-8984-743cc548b629',
|
|
metadata: {
|
|
cw_event_id: '39883203917805946105278943454814281535421893832620638214',
|
|
launch_id: '1788424715503435269',
|
|
log_group: '/aws/lambda-microvms/workers/cxkpapyhaaywrtudnqpl/api',
|
|
log_stream: 'launch-1788424715503435269',
|
|
subservice: 'worker_guest_logs',
|
|
worker: 'api',
|
|
},
|
|
project: 'cxkpapyhaaywrtudnqpl',
|
|
timestamp: 1788424716876000,
|
|
log_type: 'compute' as const,
|
|
status: null,
|
|
level: null,
|
|
method: null,
|
|
pathname: null,
|
|
auth_user: null,
|
|
date: new Date(1788424716876),
|
|
}
|
|
|
|
expect(getRawLogData(row)).toEqual({
|
|
id: '51a29911-9293-4616-8984-743cc548b629',
|
|
timestamp: 1788424716876000,
|
|
event_message: 'Error: Dynamic require of "path" is not supported',
|
|
metadata: row.metadata,
|
|
})
|
|
})
|
|
|
|
it('returns non-Compute rows unchanged', () => {
|
|
const row = {
|
|
id: 'edge-log',
|
|
timestamp: 1788424716876000,
|
|
log_type: 'edge' as const,
|
|
status: 200,
|
|
method: 'GET' as const,
|
|
pathname: '/rest/v1',
|
|
level: 'success' as const,
|
|
date: new Date(1788424716876),
|
|
}
|
|
|
|
expect(getRawLogData(row)).toBe(row)
|
|
})
|
|
})
|
|
|
|
describe('gateLogTypeOptions', () => {
|
|
const fields = [
|
|
{ value: 'date' },
|
|
{
|
|
value: 'log_type',
|
|
options: [
|
|
{ label: 'Postgres', value: 'postgres' },
|
|
{ label: 'Multigres', value: 'multigres' },
|
|
{ label: 'Compute', value: 'compute' },
|
|
],
|
|
},
|
|
]
|
|
|
|
it('drops log_type options whose flags are disabled', () => {
|
|
const gated = gateLogTypeOptions(fields, { multigres: false, compute: false })
|
|
const logType = gated.find((field) => field.value === 'log_type')
|
|
expect(logType?.options?.map((option) => option.value)).toEqual(['postgres'])
|
|
})
|
|
|
|
it('keeps independently enabled log types', () => {
|
|
const gated = gateLogTypeOptions(fields, { multigres: false, compute: true })
|
|
const logType = gated.find((field) => field.value === 'log_type')
|
|
expect(logType?.options?.map((option) => option.value)).toEqual(['postgres', 'compute'])
|
|
})
|
|
|
|
it('returns the original fields when every gated log type is enabled', () => {
|
|
const gated = gateLogTypeOptions(fields, { multigres: true, compute: true })
|
|
expect(gated).toBe(fields)
|
|
})
|
|
|
|
it('leaves non log_type fields untouched', () => {
|
|
const gated = gateLogTypeOptions(fields, { compute: false })
|
|
expect(gated.find((field) => field.value === 'date')).toEqual({ value: 'date' })
|
|
})
|
|
})
|
|
|
|
describe('gateLogTypeFilters', () => {
|
|
it('removes disabled log types from equality and inequality filters', () => {
|
|
expect(
|
|
gateLogTypeFilters(
|
|
['log_type:eq:compute', 'log_type:neq:multigres', 'log_type:eq:postgres', 'method:eq:GET'],
|
|
{ compute: false, multigres: false }
|
|
)
|
|
).toEqual(['log_type:eq:postgres', 'method:eq:GET'])
|
|
})
|
|
|
|
it('keeps enabled log types and unrelated filters unchanged', () => {
|
|
const filters = ['log_type:eq:compute', 'method:eq:GET']
|
|
expect(gateLogTypeFilters(filters, { compute: true })).toBe(filters)
|
|
})
|
|
|
|
it('preserves absent filter values', () => {
|
|
expect(gateLogTypeFilters(undefined, { compute: false })).toBeUndefined()
|
|
expect(gateLogTypeFilters(null, { compute: false })).toBeNull()
|
|
})
|
|
})
|
|
|
|
const FUNCTION_SCOPE: UnifiedLogsScope = {
|
|
filters: [
|
|
{ column: 'log_type', operator: '=', value: 'edge function' },
|
|
{ column: 'log_type', operator: '=', value: 'edge function runtime' },
|
|
{ column: 'function_id', operator: '=', value: 'fn-123' },
|
|
],
|
|
}
|
|
|
|
describe('getFacetValueCount', () => {
|
|
const facets = {
|
|
level: {
|
|
total: 7,
|
|
rows: [
|
|
{ value: 'error', total: 3 },
|
|
{ value: 'success', total: 4 },
|
|
],
|
|
},
|
|
}
|
|
|
|
it("reads one value's count from a facet", () => {
|
|
expect(getFacetValueCount(facets, 'level', 'error')).toBe(3)
|
|
})
|
|
|
|
it('counts zero for values and facets the query found nothing for', () => {
|
|
expect(getFacetValueCount(facets, 'level', 'warning')).toBe(0)
|
|
expect(getFacetValueCount(facets, 'log_type', 'edge function')).toBe(0)
|
|
expect(getFacetValueCount(undefined, 'level', 'error')).toBe(0)
|
|
})
|
|
})
|
|
|
|
describe('isOfferedFilter', () => {
|
|
it('offers every filter when the view lists none', () => {
|
|
expect(isOfferedFilter('user')).toBe(true)
|
|
expect(isOfferedFilter('pathname')).toBe(true)
|
|
})
|
|
|
|
it('offers only the listed filters', () => {
|
|
const offeredFilters = ['date', 'level']
|
|
expect(isOfferedFilter('level', offeredFilters)).toBe(true)
|
|
expect(isOfferedFilter('user', offeredFilters)).toBe(false)
|
|
expect(isOfferedFilter('pathname', offeredFilters)).toBe(false)
|
|
})
|
|
})
|
|
|
|
describe('getLogTypeVisibility', () => {
|
|
it('hides edge function runtime logs from the project-wide view and keeps other gates', () => {
|
|
expect(getLogTypeVisibility({ compute: false })).toEqual({
|
|
compute: false,
|
|
'edge function runtime': false,
|
|
})
|
|
})
|
|
|
|
it('only shows the edge function log types in a function-scoped view', () => {
|
|
const visibility = getLogTypeVisibility({ compute: true }, FUNCTION_SCOPE)
|
|
const visibleLogTypes = Object.entries(visibility)
|
|
.filter(([, isVisible]) => isVisible)
|
|
.map(([logType]) => logType)
|
|
|
|
expect(visibleLogTypes).toEqual(['edge function', 'edge function runtime'])
|
|
expect(visibility.compute).toBe(false)
|
|
expect(visibility.postgres).toBe(false)
|
|
})
|
|
|
|
it('treats a scope that fixes no log types like the project-wide view', () => {
|
|
const attributeScope: UnifiedLogsScope = {
|
|
filters: [{ column: 'function_id', operator: '=', value: 'fn-123' }],
|
|
}
|
|
expect(getLogTypeVisibility({ compute: false }, attributeScope)).toEqual({
|
|
compute: false,
|
|
'edge function runtime': false,
|
|
})
|
|
})
|
|
|
|
it('drops out-of-scope log type filters from a function-scoped view', () => {
|
|
expect(
|
|
gateLogTypeFilters(
|
|
['log_type:eq:postgres', 'log_type:eq:edge function runtime', 'level:eq:error'],
|
|
getLogTypeVisibility({}, FUNCTION_SCOPE)
|
|
)
|
|
).toEqual(['log_type:eq:edge function runtime', 'level:eq:error'])
|
|
})
|
|
})
|
|
|
|
describe('createUnifiedLogsSearch', () => {
|
|
it('preserves fixed function filters for log previews', () => {
|
|
const search = createUnifiedLogsSearch({}, FUNCTION_SCOPE)
|
|
expect(search.scope).toEqual(FUNCTION_SCOPE)
|
|
})
|
|
|
|
it.each([
|
|
{
|
|
scenario: 'a missing filter value',
|
|
payload: '{"filters":[{"column":"function_id","operator":"="}]}',
|
|
},
|
|
{
|
|
scenario: 'an empty filter column',
|
|
payload: '{"filters":[{"column":"","operator":"=","value":""}]}',
|
|
},
|
|
])('rejects a fixed scope with $scenario instead of opening an unscoped view', ({ payload }) => {
|
|
const scope = JSON.parse(payload)
|
|
expect(() => createUnifiedLogsSearch({}, scope)).toThrow(
|
|
'Invalid unified logs search parameters'
|
|
)
|
|
})
|
|
|
|
it('does not restrict project-wide searches to a resource', () => {
|
|
expect(createUnifiedLogsSearch({})).not.toHaveProperty('scope')
|
|
})
|
|
|
|
it('keeps preview filters while excluding view state from query keys', () => {
|
|
const search = createUnifiedLogsSearch({
|
|
filter: ['level:eq:error'],
|
|
id: 'log-1',
|
|
live: true,
|
|
})
|
|
|
|
expect(search.filter).toEqual(['level:eq:error'])
|
|
expect(search.size).toBe(40)
|
|
expect(search).not.toHaveProperty('id')
|
|
expect(search).not.toHaveProperty('live')
|
|
expect(search).not.toHaveProperty('date')
|
|
})
|
|
|
|
it.each([undefined, {}, { size: undefined }])(
|
|
'keeps the default preview size when optional search settings are absent: %j',
|
|
(params) => {
|
|
expect(createUnifiedLogsSearch(params).size).toBe(40)
|
|
}
|
|
)
|
|
})
|
|
|
|
describe('toQuerySearchParams', () => {
|
|
it('preserves supplied filters, date ranges and explicit query settings', () => {
|
|
const date = [new Date('2026-10-01T00:00:00Z'), new Date('2026-10-01T01:00:00Z')]
|
|
const search = toQuerySearchParams({
|
|
filter: ['log_type:eq:edge function', 'level:eq:error'],
|
|
uuid: 'request-id',
|
|
user: 'user@example.com',
|
|
date,
|
|
latency: [0, 100],
|
|
'timing.dns': [0, 50],
|
|
sort: { id: 'custom-field', desc: false },
|
|
size: 0,
|
|
start: 0,
|
|
direction: 'prev',
|
|
cursor: date[0],
|
|
edge_auth: false,
|
|
id: 'opened-log',
|
|
live: true,
|
|
})
|
|
|
|
expect(search).toMatchObject({
|
|
filter: ['log_type:eq:edge function', 'level:eq:error'],
|
|
uuid: 'request-id',
|
|
user: 'user@example.com',
|
|
date,
|
|
latency: [0, 100],
|
|
'timing.dns': [0, 50],
|
|
sort: { id: 'custom-field', desc: false },
|
|
size: 0,
|
|
start: 0,
|
|
direction: 'prev',
|
|
cursor: date[0],
|
|
edge_auth: false,
|
|
})
|
|
expect(search).not.toHaveProperty('id')
|
|
expect(search).not.toHaveProperty('live')
|
|
})
|
|
|
|
it('rejects invalid runtime filters instead of silently querying an unfiltered list', () => {
|
|
const search = JSON.parse('{"filter":"level:eq:error"}')
|
|
expect(() => toQuerySearchParams(search)).toThrow('Invalid unified logs search parameters')
|
|
})
|
|
})
|
|
|
|
describe('getUniqueLogRows', () => {
|
|
it('shows each log once when timestamp-based query pages overlap', () => {
|
|
const rows = getUniqueLogRows([
|
|
{
|
|
data: [
|
|
{ id: 'a', page: 1 },
|
|
{ id: 'b', page: 1 },
|
|
],
|
|
},
|
|
{
|
|
data: [
|
|
{ id: 'b', page: 2 },
|
|
{ id: 'c', page: 2 },
|
|
],
|
|
},
|
|
{},
|
|
])
|
|
|
|
expect(rows).toEqual([
|
|
{ id: 'a', page: 1 },
|
|
{ id: 'b', page: 1 },
|
|
{ id: 'c', page: 2 },
|
|
])
|
|
})
|
|
|
|
it('keeps the preview empty until query pages arrive', () => {
|
|
expect(getUniqueLogRows(undefined)).toEqual([])
|
|
})
|
|
})
|
|
|
|
describe('getLogRowClassName', () => {
|
|
it('distinguishes errors and dims logs received before live mode started', () => {
|
|
const errorRow = { level: 'error' as const, timestamp: 1_000_000, date: new Date(1000) }
|
|
|
|
expect(getLogRowClassName(errorRow)).toContain('bg-destructive/5')
|
|
expect(getLogRowClassName(errorRow)).toContain('opacity-100')
|
|
expect(getLogRowClassName(errorRow, 2000)).toContain('opacity-50')
|
|
expect(getLogRowClassName(errorRow, 500)).toContain('opacity-100')
|
|
})
|
|
})
|
|
|
|
describe('getComputeLogsAvailability', () => {
|
|
const computeFilter = ['log_type:eq:compute']
|
|
|
|
it('preserves an unresolved platform filter without allowing it into queries or sync', () => {
|
|
const availability = getComputeLogsAvailability({
|
|
isPlatform: true,
|
|
flagsLoaded: false,
|
|
computeEnabled: false,
|
|
})
|
|
|
|
expect(gateLogTypeFilters(computeFilter, { compute: availability.preserveComputeFilter })).toBe(
|
|
computeFilter
|
|
)
|
|
expect(gateLogTypeFilters(computeFilter, { compute: availability.canQueryCompute })).toEqual([])
|
|
expect(availability.readyToSyncFilters).toBe(false)
|
|
})
|
|
|
|
it.each([
|
|
[
|
|
'allows Compute when the platform flag is enabled',
|
|
{ isPlatform: true, flagsLoaded: true, computeEnabled: true },
|
|
true,
|
|
],
|
|
[
|
|
'removes Compute when the platform flag is disabled',
|
|
{ isPlatform: true, flagsLoaded: true, computeEnabled: false },
|
|
false,
|
|
],
|
|
[
|
|
'syncs generic filters immediately while keeping Compute unavailable on self-hosted',
|
|
{ isPlatform: false, computeEnabled: false },
|
|
false,
|
|
],
|
|
])('%s', (_, input, computeAvailable) => {
|
|
expect(getComputeLogsAvailability(input)).toEqual({
|
|
canQueryCompute: computeAvailable,
|
|
preserveComputeFilter: computeAvailable,
|
|
readyToSyncFilters: true,
|
|
})
|
|
})
|
|
})
|
|
|
|
describe('wrapIlikePattern', () => {
|
|
it.each([
|
|
['foo', '%foo%'],
|
|
['foo%', 'foo%'],
|
|
['%foo', '%foo'],
|
|
['fo_bar', 'fo_bar'],
|
|
])('wraps %s only when it has no %% or _ wildcard', (value, expected) => {
|
|
expect(wrapIlikePattern(value)).toBe(expected)
|
|
})
|
|
})
|
|
|
|
describe('isOfferedColumnFilter', () => {
|
|
const builtIn = ['level', 'status', 'log_type']
|
|
const offered = ['level']
|
|
const orGroup = (...columns: string[]) => ({
|
|
group: 'or',
|
|
filters: columns.map((column) => ({ column, operator: '=', value: 'x' })),
|
|
})
|
|
|
|
it('requires built-in columns to be offered but always allows other columns', () => {
|
|
expect(isOfferedColumnFilter({ id: 'level', value: null }, builtIn, offered)).toBe(true)
|
|
expect(isOfferedColumnFilter({ id: 'status', value: null }, builtIn, offered)).toBe(false)
|
|
expect(isOfferedColumnFilter({ id: 'attribute.status', value: null }, builtIn, offered)).toBe(
|
|
true
|
|
)
|
|
})
|
|
|
|
it('offers everything when the view does not restrict filters', () => {
|
|
expect(isOfferedColumnFilter({ id: 'status', value: null }, builtIn)).toBe(true)
|
|
expect(isOfferedColumnFilter({ id: 'group.0', value: orGroup('status') }, builtIn)).toBe(true)
|
|
})
|
|
|
|
it('offers a group only when every column in it is offered', () => {
|
|
const offeredGroup = { id: 'group.0', value: orGroup('level', 'attribute.status') }
|
|
const mixedGroup = { id: 'group.1', value: orGroup('level', 'status') }
|
|
expect(isOfferedColumnFilter(offeredGroup, builtIn, offered)).toBe(true)
|
|
expect(isOfferedColumnFilter(mixedGroup, builtIn, offered)).toBe(false)
|
|
})
|
|
})
|