Files
supabase/apps/studio/hooks/misc/useSelectedProject.utils.test.ts
Drew McLellan bc9d68a813 fix(studio): handle architecture-prefixed Postgres versions in version helpers (#53162)
## Problem

`getDatabaseMajorVersion` and `getSemanticVersion` in
`apps/studio/lib/helpers.ts` returned `NaN` for Postgres versions with
an architecture prefix, e.g. `supabase-postgres-arm64-17.9.0.019`
(expected 17 / 1790019). `parseAppVersionKey` already handled `arm64-`
and `x86_64-`, but the two helpers did not.

Fixes FE-4974
Closes #53109

## Solution

- `formatSemver` now strips any leading architecture token
(`^[a-z][a-z0-9_]*-`) rather than a hardcoded list. A version segment
always starts with a digit, so this covers `arm`, `arm64`, `x86_64`,
`aarch64`, `amd64` and future values.
- `APP_VERSION_KEY_RE` in `useSelectedProject.utils.ts` uses the same
rule, so the two parsers stay consistent.
- Because the rule is generic, strings like
`supabase-postgres-custom-123` would otherwise parse as a number.
`formatSemver` now rejects anything that doesn't look like `\d+.\d+`
after stripping, and both helpers return `0` for it (matching their
existing empty-string behavior). Callers only compare the result
numerically, so `0` behaves like the previous `NaN`.

Tests cover each prefix on both helpers and on `parseAppVersionKey`, a
release suffix (`-vault-rc1`), and non-version strings.

Not changed: `getSemanticVersion` concatenates segments without padding
(`17.10.0.001` becomes 171001, which compares below `17.9.0.019`). That
predates this change and its callers pin to a single major line.
2026-11-09 15:42:24 +00:00

156 lines
5.0 KiB
TypeScript

import { describe, expect, it } from 'vitest'
import {
appVersionAtLeast,
compareAppVersions,
isOrioleDbVersionAtLeast,
parseAppVersionKey,
} from './useSelectedProject.utils'
describe('parseAppVersionKey', () => {
it('parses a dbVersion string into a version key', () => {
expect(parseAppVersionKey('supabase-postgres-17.9.9.999-orioledb')).toEqual([17, 9, 9, 999])
})
it('parses dbVersion strings with an arch prefix', () => {
expect(parseAppVersionKey('supabase-postgres-arm64-17.9.0.019')).toEqual([17, 9, 0, 19])
expect(parseAppVersionKey('supabase-postgres-x86_64-17.9.0.019')).toEqual([17, 9, 0, 19])
})
it.each(['arm', 'aarch64', 'amd64'])('parses the %s arch prefix', (arch) => {
expect(parseAppVersionKey(`supabase-postgres-${arch}-17.9.0.019`)).toEqual([17, 9, 0, 19])
})
it('returns null for non-conforming strings', () => {
expect(parseAppVersionKey('CUSTOM_AMI_SENTINEL')).toBeNull()
expect(parseAppVersionKey('not-a-version')).toBeNull()
})
it('returns null for null or undefined input', () => {
expect(parseAppVersionKey(null)).toBeNull()
expect(parseAppVersionKey(undefined)).toBeNull()
})
})
describe('compareAppVersions', () => {
it('returns a negative number when a is older than b', () => {
expect(
compareAppVersions('supabase-postgres-17.8.9.999', 'supabase-postgres-17.9.0.019')
).toBeLessThan(0)
})
it('returns zero when versions are equal', () => {
expect(compareAppVersions('supabase-postgres-17.9.0.019', 'supabase-postgres-17.9.0.019')).toBe(
0
)
})
it('returns a positive number when a is newer than b', () => {
expect(
compareAppVersions('supabase-postgres-17.11.0.001', 'supabase-postgres-17.9.0.019')
).toBeGreaterThan(0)
})
it('compares numerically rather than by segment digit-width', () => {
// Regression case for the digit-concatenation bug in lib/helpers.ts'
// getSemanticVersion, where '15.1.1.2' (-> '15112') compares as less than
// '14.1.0.44' (-> '141044') despite being the newer version.
expect(
compareAppVersions('supabase-postgres-15.1.1.2', 'supabase-postgres-14.1.0.44')
).toBeGreaterThan(0)
})
it('throws on unparseable input', () => {
expect(() => compareAppVersions('not-a-version', 'supabase-postgres-17.9.0.019')).toThrow()
expect(() => compareAppVersions('supabase-postgres-17.9.0.019', 'not-a-version')).toThrow()
})
})
describe('appVersionAtLeast', () => {
it('returns true when the version is above or equal to the threshold', () => {
expect(appVersionAtLeast('supabase-postgres-17.9.9.999', 'supabase-postgres-17.9.0.019')).toBe(
true
)
expect(appVersionAtLeast('supabase-postgres-17.9.0.019', 'supabase-postgres-17.9.0.019')).toBe(
true
)
})
it('returns false when the version is below the threshold', () => {
expect(appVersionAtLeast('supabase-postgres-17.8.9.999', 'supabase-postgres-17.9.0.019')).toBe(
false
)
})
it('returns false for an unparseable version instead of throwing', () => {
expect(appVersionAtLeast('not-a-version', 'supabase-postgres-17.9.0.019')).toBe(false)
})
})
describe('isOrioleDbVersionAtLeast', () => {
it('returns true when the engine version is above the threshold', () => {
expect(
isOrioleDbVersionAtLeast(
'supabase-postgres-17.11.0.002-orioledb',
'supabase-postgres-17.11.0.001-orioledb'
)
).toBe(true)
expect(
isOrioleDbVersionAtLeast(
'supabase-postgres-17.12.0.001-orioledb',
'supabase-postgres-17.11.0.001-orioledb'
)
).toBe(true)
})
it('returns true when the engine version equals the threshold', () => {
expect(
isOrioleDbVersionAtLeast(
'supabase-postgres-17.11.0.001-orioledb',
'supabase-postgres-17.11.0.001-orioledb'
)
).toBe(true)
})
it('returns false when the engine version is below the threshold', () => {
expect(
isOrioleDbVersionAtLeast(
'supabase-postgres-17.10.9.999-orioledb',
'supabase-postgres-17.11.0.001-orioledb'
)
).toBe(false)
// wal-g (scheduled backup) support landed at 17.9.0.019, which is still
// below the Public Beta cutoff — these projects remain alpha.
expect(
isOrioleDbVersionAtLeast(
'supabase-postgres-17.9.0.019-orioledb',
'supabase-postgres-17.11.0.001-orioledb'
)
).toBe(false)
})
it('returns false for a non-OrioleDB dbVersion', () => {
expect(
isOrioleDbVersionAtLeast(
'supabase-postgres-17.11.0.001',
'supabase-postgres-17.11.0.001-orioledb'
)
).toBe(false)
})
it('returns false for undefined dbVersion', () => {
expect(isOrioleDbVersionAtLeast(undefined, 'supabase-postgres-17.11.0.001-orioledb')).toBe(
false
)
})
it('returns false for a non-OrioleDB threshold', () => {
expect(
isOrioleDbVersionAtLeast(
'supabase-postgres-17.11.0.001-orioledb',
'supabase-postgres-17.11.0.001'
)
).toBe(false)
})
})