Files
supabase/apps/studio/components/interfaces/ProjectCreation/ProjectCreation.utils.test.ts
Alaister Young aa3643f39d fix(studio): restrict geolocated default region to provider regions (#49141)
Follow-up to #49131. For `AWS_NIMBUS` orgs, the new-project form's
Region trigger could show a region that wasn't in the dropdown at all
(e.g. "Southeast Asia (Singapore)" while the list only offered "East US
(North Virginia)"). The geolocation-based default region
(`useDefaultRegionQuery`) picked the nearest region from **all** AWS
regions and seeded it into `dbRegion` unvalidated, ignoring the
provider's restricted region list.

**Changed:**

- `getDefaultRegionOption` now computes the nearest region only over the
provider's available regions (new `getDefaultRegionCandidateKeys`
helper). The flag-based restricted pool (`defaultRegionRestrictedPool`)
narrows within that set and is ignored if the intersection would be
empty.
- The form's default-region selection is extracted into
`resolveDefaultDbRegion` (`ProjectCreation.utils.ts`): High Availability
region first, then the recommended smart region, then the geolocated
default — used only when the provider actually offers that region —
falling back to the provider's static default.
- `getAvailableRegions` takes an injectable `environment` param (same
pattern as `getHighAvailabilityRegionCode`) so the prod-only Nimbus
region list is unit-testable.

**Added:**

- Unit tests for `getDefaultRegionCandidateKeys` (provider clamping
incl. Nimbus on prod, restricted-pool intersection, empty-intersection
fallback), `getAvailableRegions` across environments, and
`resolveDefaultDbRegion` (branch priority plus the fallback when the
geolocated region isn't offered).

## To test

- Emulate a Nimbus org locally by setting `"infra:cloud_providers":
["AWS_NIMBUS"]` in
`apps/studio/hooks/custom-content/custom-content.json`, then open the
new-project form: the Region trigger must show the same region the
dropdown offers (locally that's only Southeast Asia (Singapore)). To
reproduce the original mismatch path, stub
`https://www.cloudflare.com/cdn-cgi/trace` to return `loc=US` — the
trigger should still be clamped to the provider's region rather than
showing a US region
- Block or fail the Cloudflare trace request: the trigger should fall
back to the provider's static default region, not sit blank or loading
- Restore the normal provider list: the smart-region flow ("General
regions" + "Specific regions" with Recommended badges) is unaffected —
the geolocation request doesn't even fire on that path — and toggling
High Availability still transitions the region list cleanly

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

## Summary by CodeRabbit

## Summary by CodeRabbit

- **Bug Fixes**
- Region suggestions now respect the selected cloud provider and
deployment environment.
- Project creation avoids unavailable geolocated regions and falls back
to a supported provider default.
- Restricted region pools now fall back reliably to available provider
regions.
- AWS Nimbus selection reflects the active environment while preserving
high-availability and smart-region behavior.

- **Tests**
- Added coverage for provider-specific, environment-specific,
restricted, and fallback region selection scenarios.

<!-- end of auto-generated comment: release notes by coderabbit.ai -->

---------

Co-authored-by: Alaister Young <10985857+alaister@users.noreply.github.com>
2026-08-21 15:51:03 +08:00

118 lines
4.0 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,
} 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('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', undefined],
])('resolves the %s region restriction', (environment, expectedRegion) => {
expect(getHighAvailabilityRegionCode(environment)).toBe(expectedRegion)
})
it.each([
['local', undefined],
['staging', 'us-east-1'],
['prod', undefined],
])(
'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)
}
)
})