mirror of
https://github.com/supabase/supabase.git
synced 2026-12-04 15:39:13 +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
126 lines
4.2 KiB
TypeScript
126 lines
4.2 KiB
TypeScript
// @vitest-environment jsdom
|
|
import { SupportCategories } from '@supabase/shared-types/out/constants'
|
|
import { beforeEach, describe, expect, it, vi } from 'vitest'
|
|
|
|
import {
|
|
buildSupportAssistantPrompt,
|
|
consumeAssistantHandoff,
|
|
parseSupportAssistantPrompt,
|
|
storeAssistantHandoff,
|
|
} from './SupportAssistant.utils'
|
|
import type { SubmittedSupportRequest } from './SupportForm.state'
|
|
|
|
const supportRequest: SubmittedSupportRequest = {
|
|
organizationSlug: 'org-1',
|
|
projectRef: 'project-1',
|
|
category: SupportCategories.PROBLEM,
|
|
severity: 'Normal',
|
|
subject: 'API requests fail',
|
|
message: 'Requests fail with <500> & timeouts',
|
|
affectedServices: 'api;database',
|
|
library: 'javascript',
|
|
allowSupportAccess: true,
|
|
dashboardLogs: 'https://example.com/logs',
|
|
}
|
|
|
|
describe('SupportAssistant utils', () => {
|
|
beforeEach(() => {
|
|
sessionStorage.clear()
|
|
})
|
|
|
|
it('formats support requests as tagged assistant prompts', () => {
|
|
const prompt = buildSupportAssistantPrompt(supportRequest)
|
|
|
|
expect(prompt).toContain('<support>')
|
|
expect(prompt).toContain('<assistant_context>')
|
|
expect(prompt).toContain('a human member of the Supabase Support team is already looking at it')
|
|
expect(prompt).toContain('<subject>API requests fail</subject>')
|
|
expect(prompt).not.toContain('<organization_slug>')
|
|
expect(prompt).not.toContain('<project_ref>')
|
|
})
|
|
|
|
it('parses and unescapes tagged assistant prompts', () => {
|
|
const parsed = parseSupportAssistantPrompt(buildSupportAssistantPrompt(supportRequest))
|
|
|
|
expect(parsed).toMatchObject({
|
|
category: 'Problem',
|
|
severity: 'Normal',
|
|
subject: 'API requests fail',
|
|
message: 'Requests fail with <500> & timeouts',
|
|
support_access: 'Granted',
|
|
dashboard_logs: 'Attached',
|
|
})
|
|
})
|
|
|
|
it('falls back when optional support request fields are missing', () => {
|
|
const parsed = parseSupportAssistantPrompt(
|
|
buildSupportAssistantPrompt({
|
|
...supportRequest,
|
|
organizationSlug: undefined,
|
|
projectRef: undefined,
|
|
library: undefined,
|
|
dashboardLogs: undefined,
|
|
allowSupportAccess: false,
|
|
})
|
|
)
|
|
|
|
expect(parsed).toMatchObject({
|
|
library: 'Not provided',
|
|
support_access: 'Not granted',
|
|
dashboard_logs: 'Not attached',
|
|
})
|
|
})
|
|
|
|
it('returns null for text without a valid support payload', () => {
|
|
expect(parseSupportAssistantPrompt('Help me debug this issue')).toBeNull()
|
|
expect(parseSupportAssistantPrompt('<support></support>')).toBeNull()
|
|
})
|
|
|
|
it('preserves literal percent characters in the message (no URL encoding involved)', () => {
|
|
const request = { ...supportRequest, message: '100% CPU, literal %20 text' }
|
|
storeAssistantHandoff('token-1', request)
|
|
|
|
expect(consumeAssistantHandoff('token-1')?.message).toBe('100% CPU, literal %20 text')
|
|
})
|
|
|
|
it('round-trips a request and removes the entry after consuming it, so it cannot be replayed', () => {
|
|
storeAssistantHandoff('token-1', supportRequest)
|
|
|
|
expect(consumeAssistantHandoff('token-1')).toEqual(supportRequest)
|
|
expect(consumeAssistantHandoff('token-1')).toBeNull()
|
|
})
|
|
|
|
it('returns null for a token that was never stored', () => {
|
|
expect(consumeAssistantHandoff('unknown-token')).toBeNull()
|
|
})
|
|
|
|
it('returns null for malformed JSON', () => {
|
|
sessionStorage.setItem('assistant-handoff:token-1', '{not valid json')
|
|
|
|
expect(consumeAssistantHandoff('token-1')).toBeNull()
|
|
})
|
|
|
|
it('returns null for validly-shaped JSON that does not match the expected schema', () => {
|
|
sessionStorage.setItem('assistant-handoff:token-1', JSON.stringify({ foo: 'bar' }))
|
|
expect(consumeAssistantHandoff('token-1')).toBeNull()
|
|
|
|
sessionStorage.setItem(
|
|
'assistant-handoff:token-2',
|
|
JSON.stringify({ ...supportRequest, allowSupportAccess: 'yes' })
|
|
)
|
|
expect(consumeAssistantHandoff('token-2')).toBeNull()
|
|
})
|
|
|
|
it('still returns the parsed value when cleanup (removeItem) throws', () => {
|
|
storeAssistantHandoff('token-1', supportRequest)
|
|
|
|
const removeItemSpy = vi.spyOn(Storage.prototype, 'removeItem').mockImplementation(() => {
|
|
throw new Error('storage unavailable')
|
|
})
|
|
|
|
expect(consumeAssistantHandoff('token-1')).toEqual(supportRequest)
|
|
|
|
removeItemSpy.mockRestore()
|
|
})
|
|
})
|