Files
supabase/apps/studio/components/interfaces/Database/Replication/ReplicationPipelineStatus/ReplicationPipelineStatus.utils.test.ts
Joshen Lim 60b51d74d5 Audit existing unit tests + Remove redundant ones (#52215)
## 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
2026-10-23 18:51:27 +08:00

121 lines
3.8 KiB
TypeScript

import { describe, expect, test } from 'vitest'
import {
getFormattedLagValue,
getSlotBudgetSeverity,
getSlotHealthSeverity,
getTableSyncLagLabel,
} from './ReplicationPipelineStatus.utils'
import { getFieldDisplay, SLOT_LAG_FIELDS } from './SlotLagMetrics'
describe('lag availability', () => {
test.each([null, undefined, Number.NaN, Number.POSITIVE_INFINITY])(
'formats unavailable measurements (%s) without reporting zero',
(value) => {
expect(getFormattedLagValue('bytes', value).display).toBe('Unavailable')
expect(getFormattedLagValue('duration', value).display).toBe('Unavailable')
}
)
test('distinguishes caught-up lag, unavailable lag, and exhausted retention', () => {
const lag = SLOT_LAG_FIELDS.find((field) => field.key === 'confirmed_flush_lsn_bytes')!
const headroom = SLOT_LAG_FIELDS.find((field) => field.key === 'safe_wal_size_bytes')!
expect(getFieldDisplay(lag, 0).display).toBe('Caught up')
expect(getFieldDisplay(lag, null).display).toBe('Unavailable')
expect(getFieldDisplay(headroom, null).display).toBe('Unavailable')
expect(getFieldDisplay(headroom, -1024).display).toBe('Budget exceeded')
})
})
describe('slot health', () => {
test.each([
[null, null, 'normal'],
[null, 0, 'critical'],
[0, -1024, 'critical'],
[100, 100, 'normal'],
[75, 25, 'warning'],
[90, 10, 'critical'],
] as const)('classifies retained %s and headroom %s as %s', (retained, headroom, severity) => {
expect(getSlotBudgetSeverity(retained, headroom)).toBe(severity)
})
test('preserves lost-slot severity when distances are unavailable', () => {
expect(
getSlotHealthSeverity({
wal_status: 'lost',
restart_lsn_bytes: null,
safe_wal_size_bytes: null,
})
).toBe('critical')
})
})
describe('getTableSyncLagLabel', () => {
// Missing or invalid headroom must not be reported as unlimited (or any) retention
test.each([
['null', { safe_wal_size_bytes: null }],
['omitted', {}],
['NaN', { safe_wal_size_bytes: Number.NaN }],
])('omits WAL retention when headroom is %s', (_, headroom) => {
expect(
getTableSyncLagLabel({
active: true,
wal_status: 'reserved',
restart_lsn_bytes: 0,
confirmed_flush_lsn_bytes: 0,
...headroom,
})
).toEqual([])
})
test('reports unavailable lag without hiding a lost slot', () => {
expect(
getTableSyncLagLabel({
active: false,
wal_status: 'lost',
restart_lsn_bytes: null,
confirmed_flush_lsn_bytes: null,
safe_wal_size_bytes: null,
})
).toEqual(['Lag unavailable', 'Some changes lost'])
})
test('reports the backlog, finite WAL retention, and last check-in', () => {
expect(
getTableSyncLagLabel({
active: true,
wal_status: 'reserved',
restart_lsn_bytes: 4096,
confirmed_flush_lsn_bytes: 2048,
safe_wal_size_bytes: 1_363_148_800,
reply_time_lag: 4800,
})
).toEqual(['2 KB WAL backlog', '1.3 GB WAL retention remaining', 'Last check-in 4.80 s'])
})
test('reports slot risk without treating the expected inactive connection as a fault', () => {
expect(
getTableSyncLagLabel({
active: false,
wal_status: 'unreserved',
restart_lsn_bytes: 0,
confirmed_flush_lsn_bytes: 0,
safe_wal_size_bytes: 0,
})
).toEqual(['0 bytes WAL retention remaining', 'Some changes at risk'])
})
test('reports an exceeded WAL retention budget', () => {
expect(
getTableSyncLagLabel({
active: true,
wal_status: 'reserved',
restart_lsn_bytes: 0,
confirmed_flush_lsn_bytes: 0,
safe_wal_size_bytes: -1,
})
).toEqual(['WAL retention budget exceeded'])
})
})