Files
supabase/apps/studio/components/interfaces/Auth/PerformanceSettingsForm.utils.test.ts
Ali Waseem 652311feb5 fix(studio): guard Auth Performance allocation strategy select against invalid values (#47896)
## Summary
- The Connection management "Allocation strategy" select on the Auth >
Performance page called its `onValueChange` handler's conversion logic
with whatever value it was given, with no validation. If that handler
ever fired with a value outside the `'percent' | 'connections'` enum, it
would silently overwrite a correctly loaded config, converting it to the
wrong absolute connection count and leaving the strategy dropdown in a
blank/inconsistent state.
- Extracted the percent/connections conversion into a pure, unit-tested
`convertPoolSize()` helper (`PerformanceSettingsForm.utils.ts`) and
added a guard so `onValueChange` ignores any value that isn't a
recognized allocation unit.

## How to test
1. Under **Connection management**, switch **Allocation strategy** back
and forth between "Absolute number of connections" and "Percent of max
connections" — the value should convert correctly each time and the
dropdown should never render blank.
2. Save, then hard-reload the page — the saved strategy and value should
persist as shown.

## Test plan
- [x] `PerformanceSettingsForm.utils.test.ts` — unit tests covering both
conversion directions, clamping, and the invalid-value guard
- [x] `PerformanceSettingsForm.test.tsx` — MSW-backed component test
verifying persisted percent/absolute configs render correctly on load
- [x] `pnpm test:studio`
- [x] `pnpm typecheck`

<!-- This is an auto-generated comment: release notes by coderabbit.ai
-->

## Summary by CodeRabbit

* **Improvements**
* Switching database pool allocation strategies now automatically
converts values between percentage and connection-based units.
* Values are rounded and constrained appropriately to remain within
supported limits.
  * Allocation settings now handle invalid or zero values more safely.
* **Tests**
* Added coverage verifying persisted allocation strategies and pool-size
conversion behavior.

<!-- end of auto-generated comment: release notes by coderabbit.ai -->
2026-07-13 14:16:52 -06:00

104 lines
2.4 KiB
TypeScript

import { describe, expect, it } from 'vitest'
import { convertPoolSize, isAllocationUnit } from './PerformanceSettingsForm.utils'
describe('isAllocationUnit', () => {
it('accepts "percent"', () => {
expect(isAllocationUnit('percent')).toBe(true)
})
it('accepts "connections"', () => {
expect(isAllocationUnit('connections')).toBe(true)
})
it('rejects an empty string', () => {
expect(isAllocationUnit('')).toBe(false)
})
it('rejects undefined', () => {
expect(isAllocationUnit(undefined)).toBe(false)
})
it('rejects null', () => {
expect(isAllocationUnit(null)).toBe(false)
})
it('rejects an unrelated string', () => {
expect(isAllocationUnit('bytes')).toBe(false)
})
it('rejects a non-string value', () => {
expect(isAllocationUnit(15)).toBe(false)
})
})
describe('convertPoolSize', () => {
it('returns the value unchanged when the unit does not change', () => {
expect(
convertPoolSize({
fromUnit: 'percent',
toUnit: 'percent',
currentValue: 15,
maxConnectionLimit: 60,
})
).toBe(15)
})
it('converts connections to percent, rounding up', () => {
// Matches the original customer report: 10 connections out of 60 -> 17%
expect(
convertPoolSize({
fromUnit: 'connections',
toUnit: 'percent',
currentValue: 10,
maxConnectionLimit: 60,
})
).toBe(17)
})
it('converts percent to connections, rounding down', () => {
// Matches the reproduction: 15% of 60 -> 9 connections
expect(
convertPoolSize({
fromUnit: 'percent',
toUnit: 'connections',
currentValue: 15,
maxConnectionLimit: 60,
})
).toBe(9)
})
it('clamps a connections value above the max when converting to percent', () => {
expect(
convertPoolSize({
fromUnit: 'connections',
toUnit: 'percent',
currentValue: 100,
maxConnectionLimit: 60,
})
).toBe(100)
})
it('clamps a percent value above 100 when converting to connections', () => {
expect(
convertPoolSize({
fromUnit: 'percent',
toUnit: 'connections',
currentValue: 150,
maxConnectionLimit: 60,
})
).toBe(60)
})
it('handles a zero value', () => {
expect(
convertPoolSize({
fromUnit: 'connections',
toUnit: 'percent',
currentValue: 0,
maxConnectionLimit: 60,
})
).toBe(0)
})
})