Files
supabase/apps/studio/components/interfaces/DiskManagement/ComputeAndDiskUsageCharts.test.tsx
Lukas BernertandClaude Fable 5 f38928ad90 fix(studio): resolve disk limits from the project's instance configuration (#52367)
## Problem

Studio validates disk IOPS/throughput against one minimum value per
compute size. Projects whose hardware supports more get blocked below
their actual limits, charts and banners can show the wrong baseline, and
burst charts are gated by a hardcoded size list that is wrong for some
configs.

Fixes PROD-674

## What changed

- New per-configuration limits table:
`packages/shared-data/instance-disk-limits.ts` (values from the AWS
EBS-optimized tables).
- The size-wide floors in `compute-disk-limits.ts` are now derived from
that table as per-size minimums. Published values are unchanged and
pinned in tests.
- New `useProjectDiskLimits()` resolves a project's limits from its
primary database, falling back to the floors.
- Disk form validates against the project's actual limits (floors when a
different compute size is selected). New inline hint when provisioning
between baseline and max.
- Burst charts/tiles/metrics show only if baseline is below max.
- Chart reference lines and the disk IO banner use the resolved values.

## How to test

1. Settings → Database → Disk on a Large+ project: IOPS/throughput
accept values up to the project's actual limits, error above them, and
show the burst hint between baseline and max.
2. Reports: burst metrics are offered only for projects whose disk IO
bursts; a saved burst chart on a non-bursting project renders an
"unavailable" placeholder instead.
3. `pnpm --filter studio vitest run
components/interfaces/DiskManagement/DiskManagement.test.ts
data/reports/database-charts.test.ts`

---------

Co-authored-by: Claude Fable 5 <noreply@anthropic.com>
2026-11-11 17:58:26 +01:00

175 lines
6.7 KiB
TypeScript

import { QueryClient } from '@tanstack/react-query'
import { act, screen } from '@testing-library/react'
import { HttpResponse } from 'msw'
import { afterEach, describe, expect, it, vi } from 'vitest'
import { ComputeAndDiskUsageCharts } from './ComputeAndDiskUsageCharts'
import type { InfraMonitoringMultiResponse } from '@/data/analytics/infra-monitoring-query'
import { analyticsKeys } from '@/data/analytics/keys'
import { projectKeys } from '@/data/projects/keys'
import { customRender } from '@/tests/lib/custom-render'
import { addAPIMock, type APIErrorBody } from '@/tests/lib/msw'
function renderUsage(
balances: Array<string | undefined>,
computeSize = 'micro',
options: { hasError?: boolean; responseReady?: Promise<void>; instanceType?: string } = {}
) {
const windows: Array<{ startDate: string | null; endDate: string | null }> = []
if (options.instanceType) {
// Burstability resolves from the primary database's instance configuration;
// the global databases mock returns an always-bursting t4g.nano.
addAPIMock({
method: 'get',
path: '/platform/projects/:ref/databases',
response: [
{
cloud_provider: 'AWS',
connectionString: '123',
connection_string_read_only: '123',
db_host: '123',
db_name: 'postgres',
db_port: 5432,
db_user: 'postgres',
identifier: 'default',
inserted_at: '2025-02-16T22:24:42.115195',
region: 'us-east-1',
restUrl: 'https://default.supabase.co',
size: options.instanceType,
status: 'ACTIVE_HEALTHY',
},
],
})
}
addAPIMock({
method: 'get',
path: '/platform/projects/:ref/infra-monitoring',
response: async ({ request }) => {
await options.responseReady
if (options.hasError) {
return HttpResponse.json<APIErrorBody>({ message: 'Usage unavailable' }, { status: 500 })
}
const params = new URL(request.url).searchParams
windows.push({ startDate: params.get('startDate'), endDate: params.get('endDate') })
const attributes = new URL(request.url).searchParams.get('attributes')?.split(',') ?? []
const data = balances.map((balance, index) => ({
period_start: `2026-10-${10 + index}T00:00:00.000Z`,
values: {
max_cpu_usage: '40',
ram_usage: '60',
disk_io_budget: attributes.includes('disk_io_budget') ? balance : undefined,
disk_io_consumption: balance === undefined ? undefined : String(100 - Number(balance)),
pg_database_size: '40',
disk_fs_used_wal: '5',
disk_fs_used_system: '5',
disk_fs_size: '100',
},
}))
return HttpResponse.json<InfraMonitoringMultiResponse>({ series: {}, data })
},
})
const queryClient = new QueryClient({
defaultOptions: { queries: { retry: false, staleTime: Infinity } },
})
queryClient.setQueryData(projectKeys.detail('default'), {
ref: 'default',
status: 'ACTIVE_HEALTHY',
infra_compute_size: computeSize,
})
const rendered = customRender(<ComputeAndDiskUsageCharts />, { queryClient })
return { ...rendered, queryClient, windows }
}
describe('ComputeAndDiskUsageCharts', () => {
afterEach(() => {
vi.useRealTimers()
})
it('shows empty states without presenting missing disk metrics as zero', async () => {
renderUsage([])
expect(await screen.findByText('No compute data')).toBeInTheDocument()
expect(screen.getByText('No disk data')).toBeInTheDocument()
expect(screen.queryByText('0%')).not.toBeInTheDocument()
})
it('shows both chart errors when the usage request fails', async () => {
renderUsage([], 'micro', { hasError: true })
expect(await screen.findAllByText('Failed to load usage data')).toHaveLength(2)
})
it('waits for loaded data before showing metrics or empty states', async () => {
const { promise: responseReady, resolve } = Promise.withResolvers<void>()
renderUsage(['99'], 'micro', { responseReady })
expect(screen.queryByText('99%')).not.toBeInTheDocument()
expect(screen.queryByText('No compute data')).not.toBeInTheDocument()
expect(screen.queryByText('No disk data')).not.toBeInTheDocument()
resolve()
expect(await screen.findByText('99%')).toBeInTheDocument()
})
it('refreshes the request window after seven days without remounting', async () => {
vi.useFakeTimers({ toFake: ['Date'] })
vi.setSystemTime(new Date('2026-10-12T12:00:00.000Z'))
const { queryClient, windows } = renderUsage(['99'])
expect(await screen.findByText('99%')).toBeInTheDocument()
vi.setSystemTime(new Date('2026-10-19T12:00:00.000Z'))
await act(() =>
queryClient.refetchQueries({
queryKey: analyticsKeys.infraMonitoringGroup('default', {}).slice(0, -1),
})
)
expect(windows).toEqual([
{ startDate: '2026-10-05T12:00:00.000Z', endDate: '2026-10-12T12:00:00.000Z' },
{ startDate: '2026-10-12T12:00:00.000Z', endDate: '2026-10-19T12:00:00.000Z' },
])
})
it('displays remaining balance without inflating healthy compute usage', async () => {
renderUsage(['99'])
expect(await screen.findByText('99%')).toBeInTheDocument()
expect(screen.getByRole('heading', { name: 'Disk IO balance' })).toBeInTheDocument()
expect(screen.getAllByText('60%')).toHaveLength(2)
expect(screen.queryByText('1%')).not.toBeInTheDocument()
})
it('displays the lowest sampled balance and scores its consumed amount', async () => {
renderUsage(['99', '5', '90'])
expect(await screen.findByText('5%')).toBeInTheDocument()
expect(screen.getByText('95%')).toBeInTheDocument()
})
it('hides burst balance and excludes it from compute usage on dedicated IO compute', async () => {
renderUsage(['5'], '4xlarge', { instanceType: 'm6g.4xlarge' })
expect(await screen.findByText('40%')).toBeInTheDocument()
expect(screen.queryByRole('heading', { name: 'Disk IO balance' })).not.toBeInTheDocument()
expect(screen.getAllByText('60%')).toHaveLength(2)
expect(screen.queryByText('95%')).not.toBeInTheDocument()
})
it('keeps burst balance for 4XL configurations whose disk IO bursts', async () => {
renderUsage(['99', '5', '90'], '4xlarge', { instanceType: 'm6i.4xlarge' })
expect(await screen.findByText('5%')).toBeInTheDocument()
expect(screen.getByText('95%')).toBeInTheDocument()
})
it('displays missing balance as unavailable without inflating compute usage', async () => {
renderUsage([undefined])
expect(await screen.findByText('40%')).toBeInTheDocument()
expect(screen.getByText('—')).toBeInTheDocument()
expect(screen.getAllByText('60%')).toHaveLength(2)
expect(screen.queryByText('0%')).not.toBeInTheDocument()
expect(screen.queryByText('100%')).not.toBeInTheDocument()
})
})