mirror of
https://github.com/supabase/supabase.git
synced 2026-12-04 07:29:16 +00:00
## Context
_TLDR: Changes here speeds up unit tests on CI from ~14 minutes to ~3
minutes_ 😂
_TLDR 2: The file changes are solely only test files +
`studio-unit-tests.yml`_
We've currently got a lot of unit tests in the repository which while
great for coverage, hasn't been ideally written. There's a lot of
redundancy + overly specific tests that break easily, and our current
unit test takes about 15 minutes to run in the GHA per PR. Hence am
doing a audit clean up of all existing unit tests in the studio repo.
## Changes involved
- `.test.ts` files now run in `node`, `.test.tsx` in `jsdom`
- jsdom costs about 1s of setup per file. A `.ts` test that needs the
DOM opts in with `// @vitest-environment jsdom` (46 files do).
- Removed redundant and over-specific tests
- Duplicates are merged into `test.each`. Tests that only covered
libraries, existed for coverage, or asserted class names and snapshots
are deleted. 19 files are merged into a sibling or deleted.
- Fixed about 15 tests that couldn't fail
- Examples: unawaited assertions, fixtures equal to the default, and a
`semverGte` test calling `semverLte`.
- Sped up the slowest files
- Real sleeps are replaced with fake timers, `user.type` with paste, and
Radix option queries are scoped to the open listbox. For example,
`RestoringState` went from 18s to 0.3s and
`useTableEditorFiltersSort.navigation` from 20s to 3s.
- These ^ already shave down the unit tests time from 14 mins to 9 mins,
but am also sharding unit tests across 4 jobs to increase speed up
further which then shaves down time to 3 mins now
279 lines
7.9 KiB
TypeScript
279 lines
7.9 KiB
TypeScript
import { describe, expect, it } from 'vitest'
|
|
|
|
import {
|
|
parseConnectionsData,
|
|
parseInfrastructureMetrics,
|
|
parseNumericValue,
|
|
} from './DatabaseInfrastructureSection.utils'
|
|
import type {
|
|
InfraMonitoringMultiResponse,
|
|
InfraMonitoringSingleResponse,
|
|
} from '@/data/analytics/infra-monitoring-query'
|
|
|
|
describe('parseNumericValue', () => {
|
|
it('returns number value as-is', () => {
|
|
expect(parseNumericValue(42)).toBe(42)
|
|
expect(parseNumericValue(0)).toBe(0)
|
|
expect(parseNumericValue(3.14)).toBe(3.14)
|
|
})
|
|
|
|
it('parses valid string numbers', () => {
|
|
expect(parseNumericValue('42')).toBe(42)
|
|
expect(parseNumericValue('3.14')).toBe(3.14)
|
|
expect(parseNumericValue('0')).toBe(0)
|
|
})
|
|
|
|
it('returns 0 for invalid string values', () => {
|
|
expect(parseNumericValue('invalid')).toBe(0)
|
|
expect(parseNumericValue('')).toBe(0)
|
|
expect(parseNumericValue('NaN')).toBe(0)
|
|
})
|
|
|
|
it('returns 0 for undefined', () => {
|
|
expect(parseNumericValue(undefined)).toBe(0)
|
|
})
|
|
})
|
|
|
|
describe('parseInfrastructureMetrics', () => {
|
|
it('returns null for undefined data', () => {
|
|
expect(parseInfrastructureMetrics(undefined)).toBe(null)
|
|
})
|
|
|
|
it('parses valid metrics from response', () => {
|
|
const mockResponse: InfraMonitoringMultiResponse = {
|
|
data: [],
|
|
series: {
|
|
avg_cpu_usage: { format: 'percent', total: 150, totalAverage: 50, yAxisLimit: 100 },
|
|
ram_usage: { format: 'bytes', total: 180, totalAverage: 60, yAxisLimit: 100 },
|
|
disk_fs_used_system: {
|
|
format: 'bytes',
|
|
total: 20,
|
|
totalAverage: 20,
|
|
yAxisLimit: 100,
|
|
},
|
|
disk_fs_used_wal: {
|
|
format: 'bytes',
|
|
total: 10,
|
|
totalAverage: 10,
|
|
yAxisLimit: 100,
|
|
},
|
|
pg_database_size: {
|
|
format: 'bytes',
|
|
total: 30,
|
|
totalAverage: 30,
|
|
yAxisLimit: 100,
|
|
},
|
|
disk_fs_size: {
|
|
format: 'bytes',
|
|
total: 200,
|
|
totalAverage: 200,
|
|
yAxisLimit: 100,
|
|
},
|
|
disk_io_budget: {
|
|
format: 'percent',
|
|
total: 210,
|
|
totalAverage: 70,
|
|
yAxisLimit: 100,
|
|
},
|
|
},
|
|
}
|
|
|
|
const result = parseInfrastructureMetrics(mockResponse)
|
|
|
|
expect(result).toEqual({
|
|
cpu: { current: 50, max: 100 },
|
|
ram: { current: 60, max: 100 },
|
|
disk: { current: 30, max: 100 },
|
|
diskIo: { current: 70, max: 100 },
|
|
})
|
|
})
|
|
|
|
it('uses average remaining balance in the selected window rather than consumed IO or its lowest sample', () => {
|
|
const response: InfraMonitoringMultiResponse = {
|
|
data: [
|
|
{ period_start: 't1', values: { disk_io_budget: '98', disk_io_consumption: '2' } },
|
|
{ period_start: 't2', values: { disk_io_budget: '100', disk_io_consumption: '0' } },
|
|
],
|
|
series: {
|
|
disk_io_budget: { format: '%', total: 198, totalAverage: 99, yAxisLimit: 100 },
|
|
disk_io_consumption: { format: '%', total: 2, totalAverage: 1, yAxisLimit: 100 },
|
|
},
|
|
}
|
|
|
|
expect(parseInfrastructureMetrics(response)?.diskIo.current).toBe(99)
|
|
})
|
|
|
|
it('keeps missing disk IO balance unavailable', () => {
|
|
const mockResponse: InfraMonitoringMultiResponse = {
|
|
data: [],
|
|
series: {},
|
|
}
|
|
|
|
const result = parseInfrastructureMetrics(mockResponse)
|
|
|
|
expect(result).toEqual({
|
|
cpu: { current: 0, max: 100 },
|
|
ram: { current: 0, max: 100 },
|
|
disk: { current: 0, max: 100 },
|
|
diskIo: { current: undefined, max: 100 },
|
|
})
|
|
})
|
|
|
|
it('handles partial metrics data', () => {
|
|
const mockResponse: InfraMonitoringMultiResponse = {
|
|
data: [],
|
|
series: {
|
|
avg_cpu_usage: { format: 'percent', total: 150, totalAverage: 50, yAxisLimit: 100 },
|
|
},
|
|
}
|
|
|
|
const result = parseInfrastructureMetrics(mockResponse)
|
|
|
|
expect(result).toEqual({
|
|
cpu: { current: 50, max: 100 },
|
|
ram: { current: 0, max: 100 },
|
|
disk: { current: 0, max: 100 },
|
|
diskIo: { current: undefined, max: 100 },
|
|
})
|
|
})
|
|
|
|
it('handles single-response format (legacy)', () => {
|
|
const mockResponse: InfraMonitoringSingleResponse = {
|
|
data: [],
|
|
format: 'percent',
|
|
total: 150,
|
|
totalAverage: 50,
|
|
yAxisLimit: 100,
|
|
}
|
|
|
|
const result = parseInfrastructureMetrics(mockResponse)
|
|
|
|
expect(result).toEqual({
|
|
cpu: { current: 0, max: 100 },
|
|
ram: { current: 0, max: 100 },
|
|
disk: { current: 0, max: 100 },
|
|
diskIo: { current: undefined, max: 100 },
|
|
})
|
|
})
|
|
})
|
|
|
|
describe('parseConnectionsData', () => {
|
|
const buildResponse = (
|
|
values: Array<string | number | undefined>
|
|
): InfraMonitoringMultiResponse => ({
|
|
data: values.map((v, i) => ({
|
|
period_start: `2026-05-22T00:0${i}:00.000Z`,
|
|
values: { pg_stat_database_num_backends: v as string | undefined },
|
|
})),
|
|
series: {
|
|
pg_stat_database_num_backends: {
|
|
format: 'number',
|
|
total: 0,
|
|
totalAverage: 0,
|
|
yAxisLimit: 100,
|
|
},
|
|
},
|
|
})
|
|
|
|
it('returns zeros when data is undefined', () => {
|
|
expect(parseConnectionsData(undefined, undefined)).toEqual({ peak: 0, max: 0 })
|
|
expect(parseConnectionsData(undefined, { maxConnections: 100 })).toEqual({
|
|
peak: 0,
|
|
max: 100,
|
|
})
|
|
})
|
|
|
|
it('returns the peak value across the window, not totalAverage', () => {
|
|
const mockInfraData = buildResponse([4, 4, 6, 4, 5, 4])
|
|
// totalAverage in series is intentionally stale to ensure we ignore it
|
|
mockInfraData.series.pg_stat_database_num_backends.totalAverage = 25
|
|
|
|
const result = parseConnectionsData(mockInfraData, { maxConnections: 100 })
|
|
|
|
expect(result).toEqual({ peak: 6, max: 100 })
|
|
})
|
|
|
|
it('rounds the peak value', () => {
|
|
const mockInfraData = buildResponse([3, 5.6, 4])
|
|
|
|
const result = parseConnectionsData(mockInfraData, { maxConnections: 100 })
|
|
|
|
expect(result).toEqual({ peak: 6, max: 100 })
|
|
})
|
|
|
|
it('handles string values when computing peak', () => {
|
|
const mockInfraData = buildResponse(['4', '7', '5.4'])
|
|
|
|
const result = parseConnectionsData(mockInfraData, { maxConnections: 100 })
|
|
|
|
expect(result).toEqual({ peak: 7, max: 100 })
|
|
})
|
|
|
|
it.each([
|
|
['missing', [5, undefined, 7, undefined]],
|
|
['empty-string', ['5', '', '7', '']],
|
|
])('skips %s values when computing peak', (_, values) => {
|
|
expect(parseConnectionsData(buildResponse(values), { maxConnections: 100 })).toEqual({
|
|
peak: 7,
|
|
max: 100,
|
|
})
|
|
})
|
|
|
|
it('returns peak connections even when maxConnectionsData is undefined', () => {
|
|
const mockInfraData = buildResponse([4, 9, 6])
|
|
|
|
const result = parseConnectionsData(mockInfraData, undefined)
|
|
|
|
expect(result).toEqual({ peak: 9, max: 0 })
|
|
})
|
|
|
|
it('returns 0 max when maxConnections is missing from data object', () => {
|
|
const mockInfraData = buildResponse([4])
|
|
|
|
const result = parseConnectionsData(mockInfraData, {})
|
|
|
|
expect(result).toEqual({ peak: 4, max: 0 })
|
|
})
|
|
|
|
it('returns 0 peak when data array is empty', () => {
|
|
const mockInfraData: InfraMonitoringMultiResponse = {
|
|
data: [],
|
|
series: {},
|
|
}
|
|
|
|
const result = parseConnectionsData(mockInfraData, { maxConnections: 100 })
|
|
|
|
expect(result).toEqual({ peak: 0, max: 100 })
|
|
})
|
|
|
|
it('returns 0 peak when connections metric is missing from data points', () => {
|
|
const mockInfraData: InfraMonitoringMultiResponse = {
|
|
data: [
|
|
{
|
|
period_start: '2026-05-22T00:00:00.000Z',
|
|
values: { avg_cpu_usage: '50' },
|
|
},
|
|
],
|
|
series: {},
|
|
}
|
|
|
|
const result = parseConnectionsData(mockInfraData, { maxConnections: 100 })
|
|
|
|
expect(result).toEqual({ peak: 0, max: 100 })
|
|
})
|
|
|
|
it('handles single-response format (legacy)', () => {
|
|
const mockInfraData: InfraMonitoringSingleResponse = {
|
|
data: [],
|
|
format: 'number',
|
|
total: 150,
|
|
totalAverage: 25,
|
|
yAxisLimit: 100,
|
|
}
|
|
|
|
const result = parseConnectionsData(mockInfraData, { maxConnections: 100 })
|
|
|
|
expect(result).toEqual({ peak: 0, max: 100 })
|
|
})
|
|
})
|