Files
supabase/apps/studio/components/interfaces/ProjectCreation/ProjectCreation.utils.test.ts
T
Joshen Lim 0d3b73794b Joshenlim/fe 4465 experiment with best available region selection (#50851)
## Context

Adds a "Best available region" option in the region selector for the
project creation form
- Should only show up for free plan organizations (will be selected as
the default option instead of the recommended option from GET
`/available-regions`)
- "Recommended" badges will also be hidden in this scenario
- Behaviour should be status quo for non free plan organizations
<img width="500" alt="image"
src="https://github.com/user-attachments/assets/de0a183d-37c0-445d-98ca-c5aaf6353e73"
/>

## To test
Important to ensure that project creation still behaves as per usual
- [ ] Free plan: Creating a project with "best available region" select
creates the project if the recommended region from GET
`/available-regions`
- A quick way to check this is to swap to a paid org and see the
"recommended" general region
- [ ] Free plan: Can also create a project with other regions selected
as per usual
- [ ] Non free plan: Can create project as per usual
- [ ] Verify that everything is status quo if configcat feature flag is
off
2026-09-24 17:38:58 +08:00

168 lines
5.4 KiB
TypeScript

import { AWS_REGIONS } from 'shared-data'
import { describe, expect, it } from 'vitest'
import {
HIGH_AVAILABILITY_INSTANCE_SIZE,
HIGH_AVAILABILITY_POSTGRES_ENGINE,
HIGH_AVAILABILITY_RELEASE_CHANNEL,
} from './ProjectCreation.constants'
import {
filterHighAvailabilityRegions,
getAvailableRegions,
getHighAvailabilityRegionCode,
resolveDefaultDbRegion,
resolveSelectedRegionOptionType,
} from './ProjectCreation.utils'
describe('resolveDefaultDbRegion', () => {
const base = {
cloudProvider: 'AWS_NIMBUS',
isHighAvailabilityRestricted: false,
highAvailabilityRegionName: undefined,
isSmartRegionEnabled: false,
recommendedSmartRegion: undefined,
autoDefaultRegion: undefined,
fixedDefaultRegion: AWS_REGIONS.EAST_US.displayName,
environment: 'prod',
} as const
it('prefers the high availability region when restricted, even while it is still loading', () => {
expect(
resolveDefaultDbRegion({
...base,
isHighAvailabilityRestricted: true,
highAvailabilityRegionName: AWS_REGIONS.EAST_US.displayName,
})
).toBe(AWS_REGIONS.EAST_US.displayName)
expect(resolveDefaultDbRegion({ ...base, isHighAvailabilityRestricted: true })).toBeUndefined()
})
it('uses the recommended smart region when smart regions are enabled', () => {
expect(
resolveDefaultDbRegion({
...base,
cloudProvider: 'AWS',
isSmartRegionEnabled: true,
recommendedSmartRegion: 'Americas',
autoDefaultRegion: AWS_REGIONS.SOUTHEAST_ASIA.displayName,
})
).toBe('Americas')
})
it('uses the geolocated region when the provider offers it', () => {
expect(
resolveDefaultDbRegion({
...base,
cloudProvider: 'AWS',
autoDefaultRegion: AWS_REGIONS.SOUTHEAST_ASIA.displayName,
})
).toBe(AWS_REGIONS.SOUTHEAST_ASIA.displayName)
})
it('falls back to the fixed default when the provider does not offer the geolocated region', () => {
expect(
resolveDefaultDbRegion({ ...base, autoDefaultRegion: AWS_REGIONS.SOUTHEAST_ASIA.displayName })
).toBe(AWS_REGIONS.EAST_US.displayName)
})
it('falls back to the fixed default when no geolocated region resolved', () => {
expect(resolveDefaultDbRegion(base)).toBe(AWS_REGIONS.EAST_US.displayName)
})
})
describe('resolveSelectedRegionOptionType', () => {
const smartGroupRegions = [{ name: 'Americas' }, { name: 'APAC' }]
const specificRegions = [{ name: 'ap-southeast-1' }, { name: 'us-east-1' }]
it('returns general when the "Best available region" shortcut was used', () => {
expect(
resolveSelectedRegionOptionType({
isBestAvailableSelected: true,
dbRegion: 'ap-southeast-1',
smartGroupRegions,
specificRegions,
})
).toBe('general')
})
it('returns general when the region was picked from the smart group list', () => {
expect(
resolveSelectedRegionOptionType({
isBestAvailableSelected: false,
dbRegion: 'Americas',
smartGroupRegions,
specificRegions,
})
).toBe('general')
})
it('returns specific when the region was picked from the specific regions list', () => {
expect(
resolveSelectedRegionOptionType({
isBestAvailableSelected: false,
dbRegion: 'ap-southeast-1',
smartGroupRegions,
specificRegions,
})
).toBe('specific')
})
it('returns undefined when the region matches neither list', () => {
expect(
resolveSelectedRegionOptionType({
isBestAvailableSelected: false,
dbRegion: undefined,
smartGroupRegions,
specificRegions,
})
).toBeUndefined()
})
})
describe('getAvailableRegions', () => {
it.each(['local', 'staging', 'prod'])('returns all AWS regions for AWS on %s', (environment) => {
expect(getAvailableRegions('AWS', environment)).toEqual(AWS_REGIONS)
expect(getAvailableRegions('AWS_K8S', environment)).toEqual(AWS_REGIONS)
})
it.each([
['local', { SOUTHEAST_ASIA: AWS_REGIONS.SOUTHEAST_ASIA }],
['staging', { SOUTHEAST_ASIA: AWS_REGIONS.SOUTHEAST_ASIA }],
['prod', { EAST_US: AWS_REGIONS.EAST_US }],
])('returns the single AWS_NIMBUS region on %s', (environment, expectedRegions) => {
expect(getAvailableRegions('AWS_NIMBUS', environment)).toEqual(expectedRegions)
})
})
describe('High Availability project creation constraints', () => {
it('pins the Alpha Postgres engine, release channel, and compute size', () => {
expect(HIGH_AVAILABILITY_POSTGRES_ENGINE).toBe('17')
expect(HIGH_AVAILABILITY_RELEASE_CHANNEL).toBe('ga')
expect(HIGH_AVAILABILITY_INSTANCE_SIZE).toBe('large')
})
it.each([
['local', 'eu-central-1'],
['staging', 'us-east-1'],
['prod', 'us-east-1'],
])('resolves the %s region restriction', (environment, expectedRegion) => {
expect(getHighAvailabilityRegionCode(environment)).toBe(expectedRegion)
})
it.each([
['local', undefined],
['staging', 'us-east-1'],
['prod', 'us-east-1'],
])(
'applies the %s region restriction to high availability projects',
(environment, expectedRegion) => {
const regions = [{ code: 'us-east-1' }, { code: 'eu-central-1' }, { code: 'ap-southeast-1' }]
expect(filterHighAvailabilityRegions(regions, true, environment)).toEqual(
expectedRegion === undefined ? regions : [{ code: expectedRegion }]
)
expect(filterHighAvailabilityRegions(regions, false, environment)).toEqual(regions)
}
)
})