mirror of
https://github.com/supabase/supabase.git
synced 2026-12-06 08:29:01 +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
483 lines
15 KiB
TypeScript
483 lines
15 KiB
TypeScript
// @vitest-environment jsdom
|
|
import { waitFor } from '@testing-library/react'
|
|
import dayjs from 'dayjs'
|
|
import { HttpResponse } from 'msw'
|
|
import { describe, expect, it } from 'vitest'
|
|
|
|
import {
|
|
aggregate1MinTo2Min,
|
|
mapResponseToAnalyticsData,
|
|
useInfraMonitoringQueries,
|
|
} from './infra-monitoring-queries'
|
|
import type {
|
|
InfraMonitoringMultiData,
|
|
InfraMonitoringMultiResponse,
|
|
InfraMonitoringSingleResponse,
|
|
} from './infra-monitoring-query'
|
|
import { customRenderHook } from '@/tests/lib/custom-render'
|
|
import { addAPIMock } from '@/tests/lib/msw'
|
|
|
|
const mockMultiResponse: InfraMonitoringMultiResponse = {
|
|
data: [
|
|
{
|
|
period_start: '2024-01-02 03:04:00',
|
|
values: {
|
|
max_cpu_usage: '1.5',
|
|
ram_usage: undefined,
|
|
},
|
|
},
|
|
{
|
|
period_start: '2024-01-02 04:04:00',
|
|
values: {
|
|
max_cpu_usage: undefined,
|
|
ram_usage: '3',
|
|
},
|
|
},
|
|
],
|
|
series: {
|
|
max_cpu_usage: { format: 'percent', total: 2.5, totalAverage: 1.25, yAxisLimit: 100 },
|
|
ram_usage: { format: 'bytes', total: 3, totalAverage: 1.5, yAxisLimit: 200 },
|
|
},
|
|
}
|
|
|
|
// TODO(raulb): Remove this mock once API always returns multi-attribute format
|
|
const mockSingleResponse: InfraMonitoringSingleResponse = {
|
|
data: [
|
|
{
|
|
period_start: '2024-01-02 03:04:00',
|
|
disk_io_budget: '75.5',
|
|
},
|
|
{
|
|
period_start: '2024-01-02 04:04:00',
|
|
disk_io_budget: '80.2',
|
|
},
|
|
],
|
|
format: 'percent',
|
|
total: 155.7,
|
|
totalAverage: 77.85,
|
|
yAxisLimit: 100,
|
|
}
|
|
|
|
describe('mapResponseToAnalyticsData', () => {
|
|
describe('multi-attribute response format', () => {
|
|
it('maps attribute series and coerces values to numbers', () => {
|
|
const result = mapResponseToAnalyticsData(mockMultiResponse, ['max_cpu_usage', 'ram_usage'])
|
|
|
|
expect(result.max_cpu_usage?.data).toStrictEqual([
|
|
{
|
|
period_start: '2024-01-02 03:04:00',
|
|
periodStartFormatted: '03:04 02 Jan',
|
|
max_cpu_usage: 1.5,
|
|
},
|
|
{
|
|
period_start: '2024-01-02 04:04:00',
|
|
periodStartFormatted: '04:04 02 Jan',
|
|
max_cpu_usage: 0,
|
|
},
|
|
])
|
|
|
|
expect(result.ram_usage?.data[0].ram_usage).toBe(0)
|
|
expect(result.ram_usage?.data[1].ram_usage).toBe(3)
|
|
expect(result.max_cpu_usage).toMatchObject({
|
|
format: 'percent',
|
|
total: 2.5,
|
|
yAxisLimit: 100,
|
|
})
|
|
})
|
|
|
|
it('omits attributes that are missing series metadata', () => {
|
|
const result = mapResponseToAnalyticsData(mockMultiResponse, [
|
|
'max_cpu_usage',
|
|
'ram_usage',
|
|
'pg_stat_database_num_backends',
|
|
])
|
|
|
|
expect(result.max_cpu_usage).toBeDefined()
|
|
expect(result.ram_usage).toBeDefined()
|
|
expect(result.pg_stat_database_num_backends).toBeUndefined()
|
|
})
|
|
|
|
it('handles single attribute the same as multiple attributes', () => {
|
|
const singleResult = mapResponseToAnalyticsData(mockMultiResponse, ['max_cpu_usage'])
|
|
const multiResult = mapResponseToAnalyticsData(mockMultiResponse, [
|
|
'max_cpu_usage',
|
|
'ram_usage',
|
|
])
|
|
|
|
expect(singleResult.max_cpu_usage).toStrictEqual(multiResult.max_cpu_usage)
|
|
})
|
|
})
|
|
|
|
// TODO(raulb): Remove this test block once API always returns multi-attribute format
|
|
describe('single-attribute response format (legacy)', () => {
|
|
it('maps single-attribute response and coerces values to numbers', () => {
|
|
const result = mapResponseToAnalyticsData(mockSingleResponse as InfraMonitoringMultiData, [
|
|
'disk_io_budget',
|
|
])
|
|
|
|
expect(result.disk_io_budget?.data).toStrictEqual([
|
|
{
|
|
period_start: '2024-01-02 03:04:00',
|
|
periodStartFormatted: '03:04 02 Jan',
|
|
disk_io_budget: 75.5,
|
|
},
|
|
{
|
|
period_start: '2024-01-02 04:04:00',
|
|
periodStartFormatted: '04:04 02 Jan',
|
|
disk_io_budget: 80.2,
|
|
},
|
|
])
|
|
|
|
expect(result.disk_io_budget).toMatchObject({
|
|
format: 'percent',
|
|
total: 155.7,
|
|
yAxisLimit: 100,
|
|
})
|
|
})
|
|
|
|
it('returns empty object when no attributes provided', () => {
|
|
const result = mapResponseToAnalyticsData(mockSingleResponse as InfraMonitoringMultiData, [])
|
|
|
|
expect(result).toStrictEqual({})
|
|
})
|
|
})
|
|
|
|
describe('output format consistency', () => {
|
|
it('produces the same output structure for both response formats', () => {
|
|
// Create equivalent responses in both formats for the same data
|
|
const multiFormat: InfraMonitoringMultiResponse = {
|
|
data: [{ period_start: '2024-01-02 03:04:00', values: { disk_io_budget: '75.5' } }],
|
|
series: {
|
|
disk_io_budget: { format: 'percent', total: 75.5, totalAverage: 75.5, yAxisLimit: 100 },
|
|
},
|
|
}
|
|
|
|
const singleFormat: InfraMonitoringSingleResponse = {
|
|
data: [{ period_start: '2024-01-02 03:04:00', disk_io_budget: '75.5' }],
|
|
format: 'percent',
|
|
total: 75.5,
|
|
totalAverage: 75.5,
|
|
yAxisLimit: 100,
|
|
}
|
|
|
|
const multiResult = mapResponseToAnalyticsData(multiFormat, ['disk_io_budget'])
|
|
const singleResult = mapResponseToAnalyticsData(singleFormat as InfraMonitoringMultiData, [
|
|
'disk_io_budget',
|
|
])
|
|
|
|
// Both should produce identical output
|
|
expect(singleResult.disk_io_budget).toStrictEqual(multiResult.disk_io_budget)
|
|
})
|
|
})
|
|
})
|
|
|
|
describe('2m interval aggregation', () => {
|
|
it('aggregates 1m data points into 2m buckets for multi-attribute response', () => {
|
|
const mock1MinData: InfraMonitoringMultiResponse = {
|
|
data: [
|
|
{
|
|
period_start: '2024-01-02 03:00:00',
|
|
values: { max_cpu_usage: '10', ram_usage: '100' },
|
|
},
|
|
{
|
|
period_start: '2024-01-02 03:01:00',
|
|
values: { max_cpu_usage: '20', ram_usage: '200' },
|
|
},
|
|
{
|
|
period_start: '2024-01-02 03:02:00',
|
|
values: { max_cpu_usage: '15', ram_usage: '150' },
|
|
},
|
|
{
|
|
period_start: '2024-01-02 03:03:00',
|
|
values: { max_cpu_usage: '25', ram_usage: '250' },
|
|
},
|
|
],
|
|
series: {
|
|
max_cpu_usage: { format: 'percent', total: 70, totalAverage: 17.5, yAxisLimit: 100 },
|
|
ram_usage: { format: 'bytes', total: 700, totalAverage: 175, yAxisLimit: 200 },
|
|
},
|
|
}
|
|
|
|
const responseWith2m = {
|
|
...mock1MinData,
|
|
_originalInterval: '2m',
|
|
} as InfraMonitoringMultiData & { _originalInterval?: string }
|
|
|
|
const result = mapResponseToAnalyticsData(responseWith2m, ['max_cpu_usage', 'ram_usage'])
|
|
|
|
expect(result.max_cpu_usage?.data).toHaveLength(2)
|
|
// Check that values are correctly averaged
|
|
expect(result.max_cpu_usage?.data[0].max_cpu_usage).toBe(15) // (10 + 20) / 2
|
|
expect(result.max_cpu_usage?.data[1].max_cpu_usage).toBe(20) // (15 + 25) / 2
|
|
// Check that buckets are on even minutes
|
|
expect(dayjs(result.max_cpu_usage?.data[0].period_start).minute() % 2).toBe(0)
|
|
expect(dayjs(result.max_cpu_usage?.data[1].period_start).minute() % 2).toBe(0)
|
|
|
|
expect(result.ram_usage?.data[0].ram_usage).toBe(150) // (100 + 200) / 2
|
|
expect(result.ram_usage?.data[1].ram_usage).toBe(200) // (150 + 250) / 2
|
|
})
|
|
|
|
it('aggregates 1m data points into 2m buckets for single-attribute response', () => {
|
|
const mock1MinData: InfraMonitoringSingleResponse = {
|
|
data: [
|
|
{ period_start: '2024-01-02 03:00:00', disk_io_budget: '10' },
|
|
{ period_start: '2024-01-02 03:01:00', disk_io_budget: '20' },
|
|
{ period_start: '2024-01-02 03:02:00', disk_io_budget: '15' },
|
|
{ period_start: '2024-01-02 03:03:00', disk_io_budget: '25' },
|
|
],
|
|
format: 'percent',
|
|
total: 70,
|
|
totalAverage: 17.5,
|
|
yAxisLimit: 100,
|
|
}
|
|
|
|
const responseWith2m = {
|
|
...mock1MinData,
|
|
_originalInterval: '2m',
|
|
} as InfraMonitoringMultiData & { _originalInterval?: string }
|
|
|
|
const result = mapResponseToAnalyticsData(responseWith2m, ['disk_io_budget'])
|
|
|
|
expect(result.disk_io_budget?.data).toHaveLength(2)
|
|
// Check that values are correctly averaged
|
|
expect(result.disk_io_budget?.data[0].disk_io_budget).toBe(15) // (10 + 20) / 2
|
|
expect(result.disk_io_budget?.data[1].disk_io_budget).toBe(20) // (15 + 25) / 2
|
|
// Check that buckets are on even minutes
|
|
expect(dayjs(result.disk_io_budget?.data[0].period_start).minute() % 2).toBe(0)
|
|
expect(dayjs(result.disk_io_budget?.data[1].period_start).minute() % 2).toBe(0)
|
|
})
|
|
|
|
it('handles odd number of 1m data points', () => {
|
|
const mock1MinData: InfraMonitoringMultiResponse = {
|
|
data: [
|
|
{
|
|
period_start: '2024-01-02 03:00:00',
|
|
values: { max_cpu_usage: '10' },
|
|
},
|
|
{
|
|
period_start: '2024-01-02 03:01:00',
|
|
values: { max_cpu_usage: '20' },
|
|
},
|
|
{
|
|
period_start: '2024-01-02 03:02:00',
|
|
values: { max_cpu_usage: '15' },
|
|
},
|
|
],
|
|
series: {
|
|
max_cpu_usage: { format: 'percent', total: 45, totalAverage: 15, yAxisLimit: 100 },
|
|
},
|
|
}
|
|
|
|
const responseWith2m = {
|
|
...mock1MinData,
|
|
_originalInterval: '2m',
|
|
} as InfraMonitoringMultiData & { _originalInterval?: string }
|
|
|
|
const result = mapResponseToAnalyticsData(responseWith2m, ['max_cpu_usage'])
|
|
|
|
expect(result.max_cpu_usage?.data).toHaveLength(2)
|
|
expect(result.max_cpu_usage?.data[0].max_cpu_usage).toBe(15) // (10 + 20) / 2
|
|
expect(result.max_cpu_usage?.data[1].max_cpu_usage).toBe(15) // single point in second bucket
|
|
})
|
|
})
|
|
|
|
describe('aggregate1MinTo2Min', () => {
|
|
it('keeps commitment available when its capacity is missing from part of the bucket', () => {
|
|
const GIB = 1024 ** 3
|
|
const result = aggregate1MinTo2Min(
|
|
[
|
|
{
|
|
period_start: '2024-01-02T03:00:00Z',
|
|
ram_commit_used: 4 * GIB,
|
|
ram_commit_limit: 5 * GIB,
|
|
},
|
|
{ period_start: '2024-01-02T03:01:00Z', ram_commit_used: 4 * GIB },
|
|
],
|
|
'ram_commit_used'
|
|
)
|
|
|
|
expect(result[0].ram_commit_used).toBe(4 * GIB)
|
|
expect(result[0].ram_commit_limit).toBeUndefined()
|
|
})
|
|
|
|
it('aggregates 1-minute data points into 2-minute buckets with correct averages', () => {
|
|
const dataPoints = [
|
|
{
|
|
period_start: '2024-01-02T03:00:00Z',
|
|
periodStartFormatted: '03:00 02 Jan',
|
|
max_cpu_usage: 10,
|
|
ram_usage: 100,
|
|
},
|
|
{
|
|
period_start: '2024-01-02T03:01:00Z',
|
|
periodStartFormatted: '03:01 02 Jan',
|
|
max_cpu_usage: 20,
|
|
ram_usage: 200,
|
|
},
|
|
{
|
|
period_start: '2024-01-02T03:02:00Z',
|
|
periodStartFormatted: '03:02 02 Jan',
|
|
max_cpu_usage: 15,
|
|
ram_usage: 150,
|
|
},
|
|
{
|
|
period_start: '2024-01-02T03:03:00Z',
|
|
periodStartFormatted: '03:03 02 Jan',
|
|
max_cpu_usage: 25,
|
|
ram_usage: 250,
|
|
},
|
|
]
|
|
|
|
const result = aggregate1MinTo2Min(dataPoints)
|
|
|
|
expect(result).toHaveLength(2)
|
|
// Check that first bucket contains 03:00 and 03:01 data averaged
|
|
expect(result[0].max_cpu_usage).toBe(15) // (10 + 20) / 2
|
|
expect(result[0].ram_usage).toBe(150) // (100 + 200) / 2
|
|
expect(result[0].periodStartFormatted).toBeDefined()
|
|
expect(dayjs(result[0].period_start).minute() % 2).toBe(0) // Should be even minute
|
|
|
|
// Check that second bucket contains 03:02 and 03:03 data averaged
|
|
expect(result[1].max_cpu_usage).toBe(20) // (15 + 25) / 2
|
|
expect(result[1].ram_usage).toBe(200) // (150 + 250) / 2
|
|
expect(result[1].periodStartFormatted).toBeDefined()
|
|
expect(dayjs(result[1].period_start).minute() % 2).toBe(0) // Should be even minute
|
|
})
|
|
|
|
it('handles single data point', () => {
|
|
const dataPoints = [
|
|
{
|
|
period_start: '2024-01-02T03:00:00Z',
|
|
max_cpu_usage: 10,
|
|
},
|
|
]
|
|
|
|
const result = aggregate1MinTo2Min(dataPoints)
|
|
|
|
expect(result).toHaveLength(1)
|
|
expect(result[0].max_cpu_usage).toBe(10)
|
|
expect(result[0].periodStartFormatted).toBeDefined()
|
|
})
|
|
|
|
it('handles empty array', () => {
|
|
const result = aggregate1MinTo2Min([])
|
|
expect(result).toHaveLength(0)
|
|
})
|
|
|
|
it('handles multiple attributes correctly', () => {
|
|
const dataPoints = [
|
|
{
|
|
period_start: '2024-01-02T03:00:00Z',
|
|
attr1: 5,
|
|
attr2: 10,
|
|
attr3: 15,
|
|
},
|
|
{
|
|
period_start: '2024-01-02T03:01:00Z',
|
|
attr1: 10,
|
|
attr2: 20,
|
|
attr3: 30,
|
|
},
|
|
{
|
|
period_start: '2024-01-02T03:02:00Z',
|
|
attr1: 15,
|
|
attr2: 30,
|
|
attr3: 45,
|
|
},
|
|
]
|
|
|
|
const result = aggregate1MinTo2Min(dataPoints)
|
|
|
|
expect(result).toHaveLength(2)
|
|
expect(result[0].attr1).toBe(7.5) // (5 + 10) / 2
|
|
expect(result[0].attr2).toBe(15) // (10 + 20) / 2
|
|
expect(result[0].attr3).toBe(22.5) // (15 + 30) / 2
|
|
expect(result[1].attr1).toBe(15) // single point
|
|
expect(result[1].attr2).toBe(30)
|
|
expect(result[1].attr3).toBe(45)
|
|
})
|
|
|
|
it('ignores non-numeric values when aggregating', () => {
|
|
const dataPoints = [
|
|
{
|
|
period_start: '2024-01-02T03:00:00Z',
|
|
max_cpu_usage: 10,
|
|
label: 'test', // string value
|
|
},
|
|
{
|
|
period_start: '2024-01-02T03:01:00Z',
|
|
max_cpu_usage: 20,
|
|
label: 'test2',
|
|
},
|
|
]
|
|
|
|
const result = aggregate1MinTo2Min(dataPoints)
|
|
|
|
expect(result).toHaveLength(1)
|
|
expect(result[0].max_cpu_usage).toBe(15)
|
|
expect(result[0].label).toBeUndefined()
|
|
})
|
|
|
|
it('sorts results by period_start', () => {
|
|
const dataPoints = [
|
|
{
|
|
period_start: '2024-01-02T03:02:00Z',
|
|
value: 15,
|
|
},
|
|
{
|
|
period_start: '2024-01-02T03:00:00Z',
|
|
value: 10,
|
|
},
|
|
{
|
|
period_start: '2024-01-02T03:01:00Z',
|
|
value: 20,
|
|
},
|
|
]
|
|
|
|
const result = aggregate1MinTo2Min(dataPoints)
|
|
|
|
expect(result).toHaveLength(2)
|
|
expect(dayjs(result[0].period_start).isBefore(dayjs(result[1].period_start))).toBe(true)
|
|
})
|
|
})
|
|
|
|
describe('useInfraMonitoringQueries partial failures', () => {
|
|
it('reports errors only for failed attributes in a successful batch response', async () => {
|
|
addAPIMock({
|
|
method: 'get',
|
|
path: '/platform/projects/:ref/infra-monitoring',
|
|
response: () =>
|
|
HttpResponse.json<InfraMonitoringMultiResponse>({
|
|
...mockMultiResponse,
|
|
series: { ram_usage: mockMultiResponse.series.ram_usage },
|
|
errors: { max_cpu_usage: { message: 'CPU unavailable' } },
|
|
}),
|
|
})
|
|
const { result } = customRenderHook(() =>
|
|
useInfraMonitoringQueries(
|
|
['max_cpu_usage', 'ram_usage'],
|
|
'default',
|
|
'2024-01-02T00:00:00Z',
|
|
'2024-01-03T00:00:00Z',
|
|
'1h',
|
|
undefined,
|
|
undefined,
|
|
true
|
|
)
|
|
)
|
|
|
|
await waitFor(() => expect(result.current[1].status).toBe('success'))
|
|
expect(result.current[0]).toMatchObject({
|
|
data: undefined,
|
|
error: { message: 'CPU unavailable' },
|
|
isError: true,
|
|
status: 'error',
|
|
})
|
|
expect(result.current[1]).toMatchObject({
|
|
error: null,
|
|
isError: false,
|
|
data: { total: 3 },
|
|
})
|
|
})
|
|
})
|