Files
supabase/apps/studio/components/interfaces/Storage/FilesBuckets/FilesBucket.schema.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

113 lines
3.7 KiB
TypeScript

import { describe, expect, it } from 'vitest'
import { z } from 'zod'
import {
bucketVersioningFormFields,
S3_MAX_NONCURRENT_VERSIONS,
superRefineBucketVersioning,
type BucketVersioningFormValues,
} from './FilesBucket.schema'
const values = (
overrides: Partial<BucketVersioningFormValues> = {}
): BucketVersioningFormValues => ({
enable_versioning: true,
version_expiry_days: 30,
max_noncurrent_versions: 10,
expiration_mode: 'and',
...overrides,
})
const refine = (formValues: BucketVersioningFormValues) => {
const result = z
.custom<BucketVersioningFormValues>()
.superRefine(superRefineBucketVersioning)
.safeParse(formValues)
if (result.success) return []
return result.error.issues.map((issue) => ({
path: issue.path.join('.'),
message: issue.message,
}))
}
describe('superRefineBucketVersioning', () => {
it('accepts a valid policy', () => {
expect(refine(values())).toEqual([])
})
it('skips validation entirely when versioning is off', () => {
// Stale numbers must not block an unrelated bucket-settings save.
expect(refine(values({ enable_versioning: false, version_expiry_days: -5 }))).toEqual([])
})
it('treats empty values as "no condition set" rather than zero', () => {
expect(refine(values({ version_expiry_days: '', max_noncurrent_versions: '' }))).toEqual([])
})
it('rejects a retention window below one day', () => {
expect(refine(values({ version_expiry_days: 0 })).map((i) => i.path)).toEqual([
'version_expiry_days',
])
})
it('rejects a version cap with no expiration age, which S3 will not accept', () => {
const issues = refine(values({ version_expiry_days: '', max_noncurrent_versions: 10 }))
expect(issues).toHaveLength(1)
expect(issues[0].path).toBe('max_noncurrent_versions')
expect(issues[0].message).toMatch(/expiration age/i)
})
it('rejects a version cap below one', () => {
expect(refine(values({ max_noncurrent_versions: 0 })).map((i) => i.path)).toEqual([
'max_noncurrent_versions',
])
})
it('rejects exceeding the S3 ceiling of 100 versions', () => {
const issues = refine(values({ max_noncurrent_versions: S3_MAX_NONCURRENT_VERSIONS + 1 }))
expect(issues).toHaveLength(1)
expect(issues[0].message).toContain(String(S3_MAX_NONCURRENT_VERSIONS))
})
it('accepts exactly the S3 ceiling', () => {
expect(refine(values({ max_noncurrent_versions: S3_MAX_NONCURRENT_VERSIONS }))).toEqual([])
})
it('reports both fields independently', () => {
const issues = refine(values({ version_expiry_days: 0, max_noncurrent_versions: 0 }))
expect(issues.map((i) => i.path).sort()).toEqual([
'max_noncurrent_versions',
'version_expiry_days',
])
})
})
describe('bucketVersioningFormFields', () => {
const schema = z.object(bucketVersioningFormFields)
it('defaults to versioning off with no conditions set', () => {
expect(schema.parse({})).toEqual({
enable_versioning: false,
version_expiry_days: '',
max_noncurrent_versions: '',
expiration_mode: 'and',
})
})
it('coerces numeric strings from the input element', () => {
const parsed = schema.parse({ version_expiry_days: '45', max_noncurrent_versions: '5' })
expect(parsed.version_expiry_days).toBe(45)
expect(parsed.max_noncurrent_versions).toBe(5)
})
it('preserves the empty sentinel instead of coercing it to zero', () => {
// `Number('')` is 0, so a bare coercion would turn "no condition" into "expire immediately".
expect(schema.parse({ version_expiry_days: '' }).version_expiry_days).toBe('')
})
it('rejects a fractional retention window', () => {
expect(schema.safeParse({ version_expiry_days: 1.5 }).success).toBe(false)
})
})