mirror of
https://github.com/supabase/supabase.git
synced 2026-12-03 23:18:57 +00:00
## 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
191 lines
6.1 KiB
TypeScript
191 lines
6.1 KiB
TypeScript
import { untrustedSql } from '@supabase/pg-meta'
|
|
import { describe, expect, it } from 'vitest'
|
|
|
|
import {
|
|
buildUpsertPayload,
|
|
canEditSnippet,
|
|
isLoadedSnippet,
|
|
isSnippetOwner,
|
|
validateMoveToFolder,
|
|
type LoadedSnippet,
|
|
} from './sql-editor-rules'
|
|
import type { SnippetWithContent } from '@/data/content/sql-folders-query'
|
|
import { untrustedLogSql } from '@/data/logs/safe-analytics-sql'
|
|
|
|
function makeLogSnippet(): LoadedSnippet {
|
|
return {
|
|
id: 'log-snippet-1',
|
|
name: 'My Logs Query',
|
|
description: '',
|
|
visibility: 'user',
|
|
project_id: 42,
|
|
owner_id: 7,
|
|
folder_id: null,
|
|
favorite: false,
|
|
status: 'saved',
|
|
inserted_at: '2024-01-01T00:00:00.000Z',
|
|
updated_at: '2024-01-01T00:00:00.000Z',
|
|
type: 'log_sql',
|
|
content: {
|
|
content_id: 'log-snippet-1',
|
|
schema_version: '1',
|
|
unchecked_sql: untrustedLogSql('select * from logs'),
|
|
},
|
|
}
|
|
}
|
|
|
|
function makeSnippet(
|
|
overrides: Omit<Partial<SnippetWithContent>, 'content' | 'type'> = {}
|
|
): LoadedSnippet {
|
|
return {
|
|
id: 'snippet-1',
|
|
name: 'My Query',
|
|
description: 'A description',
|
|
visibility: 'user',
|
|
project_id: 42,
|
|
owner_id: 7,
|
|
folder_id: null,
|
|
favorite: false,
|
|
status: 'saved',
|
|
inserted_at: '2024-01-01T00:00:00.000Z',
|
|
updated_at: '2024-01-01T00:00:00.000Z',
|
|
...overrides,
|
|
type: 'sql',
|
|
content: {
|
|
content_id: 'snippet-1',
|
|
schema_version: '1',
|
|
unchecked_sql: untrustedSql('SELECT * FROM users;'),
|
|
},
|
|
}
|
|
}
|
|
|
|
describe('canEditSnippet', () => {
|
|
it('returns true for owner of a project-visibility snippet', () => {
|
|
const snippet = makeSnippet({ visibility: 'project', owner_id: 7 })
|
|
expect(canEditSnippet(snippet, 7)).toBe(true)
|
|
})
|
|
|
|
it('returns false for non-owner of a project-visibility snippet', () => {
|
|
const snippet = makeSnippet({ visibility: 'project', owner_id: 7 })
|
|
expect(canEditSnippet(snippet, 99)).toBe(false)
|
|
})
|
|
|
|
it('returns true for non-owner of a user-visibility snippet', () => {
|
|
const snippet = makeSnippet({ visibility: 'user', owner_id: 7 })
|
|
expect(canEditSnippet(snippet, 99)).toBe(true)
|
|
})
|
|
|
|
it('returns false when profileId is undefined and snippet is project-visibility', () => {
|
|
const snippet = makeSnippet({ visibility: 'project', owner_id: 7 })
|
|
expect(canEditSnippet(snippet, undefined)).toBe(false)
|
|
})
|
|
})
|
|
|
|
describe('isSnippetOwner', () => {
|
|
it('returns true when profileId matches owner_id', () => {
|
|
const snippet = makeSnippet({ owner_id: 7 })
|
|
expect(isSnippetOwner(snippet, 7)).toBe(true)
|
|
})
|
|
|
|
it('returns false when profileId does not match owner_id', () => {
|
|
const snippet = makeSnippet({ owner_id: 7 })
|
|
expect(isSnippetOwner(snippet, 99)).toBe(false)
|
|
})
|
|
|
|
it('returns false when profileId is undefined', () => {
|
|
const snippet = makeSnippet({ owner_id: 7 })
|
|
expect(isSnippetOwner(snippet, undefined)).toBe(false)
|
|
})
|
|
})
|
|
|
|
describe('validateMoveToFolder', () => {
|
|
it('returns { ok: false } when visibility is project and folderId is set', () => {
|
|
const result = validateMoveToFolder({ visibility: 'project', folderId: 'folder-abc' })
|
|
expect(result).toEqual({ ok: false, error: 'Shared snippet cannot be within a folder' })
|
|
})
|
|
|
|
it('returns { ok: true } when visibility is project and folderId is null', () => {
|
|
const result = validateMoveToFolder({ visibility: 'project', folderId: null })
|
|
expect(result).toEqual({ ok: true })
|
|
})
|
|
|
|
it('returns { ok: true } when visibility is project and folderId is undefined', () => {
|
|
const result = validateMoveToFolder({ visibility: 'project', folderId: undefined })
|
|
expect(result).toEqual({ ok: true })
|
|
})
|
|
|
|
it('returns { ok: true } when visibility is user and folderId is set', () => {
|
|
const result = validateMoveToFolder({ visibility: 'user', folderId: 'folder-abc' })
|
|
expect(result).toEqual({ ok: true })
|
|
})
|
|
})
|
|
|
|
describe('buildUpsertPayload', () => {
|
|
it('emits the snippet type — log_sql for a logs snippet (not silently rewritten to sql)', () => {
|
|
const payload = buildUpsertPayload(makeLogSnippet(), 'log-snippet-1')
|
|
expect(payload.type).toBe('log_sql')
|
|
expect((payload.content as { content_id: string }).content_id).toBe('log-snippet-1')
|
|
})
|
|
|
|
it('passes through provided values', () => {
|
|
const snippet = makeSnippet()
|
|
const payload = buildUpsertPayload(snippet, 'snippet-1')
|
|
|
|
expect(payload.id).toBe('snippet-1')
|
|
expect(payload.type).toBe('sql')
|
|
expect(payload.name).toBe('My Query')
|
|
expect(payload.description).toBe('A description')
|
|
expect(payload.visibility).toBe('user')
|
|
expect(payload.project_id).toBe(42)
|
|
expect(payload.owner_id).toBe(7)
|
|
expect(payload.favorite).toBe(false)
|
|
})
|
|
|
|
it('applies defaults for missing optional fields', () => {
|
|
const snippet = makeSnippet({
|
|
name: undefined,
|
|
description: undefined,
|
|
visibility: undefined,
|
|
project_id: undefined,
|
|
favorite: undefined,
|
|
})
|
|
expect(buildUpsertPayload(snippet, 'snippet-1')).toMatchObject({
|
|
name: 'Untitled',
|
|
description: '',
|
|
visibility: 'user',
|
|
project_id: 0,
|
|
favorite: false,
|
|
})
|
|
})
|
|
|
|
it('sets content.content_id to the supplied id', () => {
|
|
const snippet = makeSnippet()
|
|
const payload = buildUpsertPayload(snippet, 'my-id')
|
|
expect((payload.content as any).content_id).toBe('my-id')
|
|
})
|
|
|
|
it('converts folder_id null to undefined in the payload', () => {
|
|
const snippet = makeSnippet({ folder_id: null })
|
|
const payload = buildUpsertPayload(snippet, 'snippet-1')
|
|
expect(payload.folder_id).toBeUndefined()
|
|
})
|
|
|
|
it('passes through a non-null folder_id', () => {
|
|
const snippet = makeSnippet({ folder_id: 'folder-xyz' })
|
|
const payload = buildUpsertPayload(snippet, 'snippet-1')
|
|
expect(payload.folder_id).toBe('folder-xyz')
|
|
})
|
|
})
|
|
|
|
describe('isLoadedSnippet', () => {
|
|
it('returns true when content is present', () => {
|
|
const snippet = makeSnippet()
|
|
expect(isLoadedSnippet(snippet)).toBe(true)
|
|
})
|
|
|
|
it('returns false when content is undefined', () => {
|
|
const snippet = { ...makeSnippet(), content: undefined }
|
|
expect(isLoadedSnippet(snippet)).toBe(false)
|
|
})
|
|
})
|