Files
supabase/apps/studio/components/interfaces/MCP/Secrets/McpSecretsCard.test.tsx
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

163 lines
4.9 KiB
TypeScript

import { fireEvent, screen, waitFor } from '@testing-library/react'
import userEvent from '@testing-library/user-event'
import { describe, expect, it, vi } from 'vitest'
import type { SecretRequest } from './McpSecrets.types'
import { McpSecretsCard } from './McpSecretsCard'
import { customRender } from '@/tests/lib/custom-render'
const request: SecretRequest = {
tool: 'create_edge_function_secret',
ref: 'aaaaaaaaaaaaaaaaaaaa',
project: 'acme-production',
account: 'ops@example.com',
keyName: 'OPENAI_API_KEY',
}
const noop = () => {}
const renderCard = (state: Parameters<typeof McpSecretsCard>[0]['state']) =>
customRender(
<McpSecretsCard
state={state}
isSaving={false}
onSave={noop}
onCancel={noop}
onSwitchAccount={noop}
/>
)
const secretField = () => screen.getByLabelText<HTMLInputElement>('Secret value')
describe('McpSecretsCard', () => {
it('does not carry a typed secret across a change of request', async () => {
const user = userEvent.setup()
const { rerender } = renderCard({ status: 'form', request })
await user.click(secretField())
await user.paste('sk-for-the-first-request')
await user.click(screen.getByRole('button', { name: 'Show secret value' }))
expect(secretField().value).toBe('sk-for-the-first-request')
expect(secretField().type).toBe('text')
rerender(
<McpSecretsCard
state={{ status: 'form', request: { ...request, keyName: 'RESEND_API_KEY' } }}
isSaving={false}
onSave={noop}
onCancel={noop}
onSwitchAccount={noop}
/>
)
await waitFor(() => expect(screen.getByDisplayValue('RESEND_API_KEY')).toBeInTheDocument())
expect(secretField().value).toBe('')
expect(secretField().type).toBe('password')
})
it('resets when only the project changes, since a ref can move under one name', async () => {
const user = userEvent.setup()
const { rerender } = renderCard({ status: 'form', request })
await user.click(secretField())
await user.paste('sk-for-project-a')
rerender(
<McpSecretsCard
state={{
status: 'form',
request: { ...request, ref: 'bbbbbbbbbbbbbbbbbbbb', project: 'acme-production' },
}}
isSaving={false}
onSave={noop}
onCancel={noop}
onSwitchAccount={noop}
/>
)
await waitFor(() => expect(secretField().value).toBe(''))
})
it('keeps what the user typed when the request is unchanged', async () => {
const user = userEvent.setup()
const { rerender } = renderCard({ status: 'form', request })
await user.click(secretField())
await user.paste('sk-still-being-typed')
rerender(
<McpSecretsCard
state={{
status: 'form',
request: { ...request, existingSecret: { updatedAt: new Date().toISOString() } },
}}
isSaving={false}
onSave={noop}
onCancel={noop}
onSwitchAccount={noop}
/>
)
expect(await screen.findByText(/already exists/)).toBeInTheDocument()
expect(secretField().value).toBe('sk-still-being-typed')
})
it('keeps the prefix-warning live region mounted before there is a warning to announce', async () => {
const user = userEvent.setup()
renderCard({
status: 'form',
request: { ...request, providerHint: { name: 'OpenAI', prefix: 'sk-' } },
})
const liveRegion = screen.getByRole('status')
expect(liveRegion).toHaveAttribute('aria-live', 'polite')
expect(liveRegion).toBeEmptyDOMElement()
await user.click(secretField())
await user.paste('not-a-real-prefix')
await waitFor(() => expect(liveRegion).toHaveTextContent(/usually start with sk-/))
expect(screen.getByRole('status')).toBe(liveRegion)
})
it('blocks an empty submit and explains why, without calling onSave', async () => {
const onSave = vi.fn()
customRender(
<McpSecretsCard
state={{ status: 'form', request }}
isSaving={false}
onSave={onSave}
onCancel={noop}
onSwitchAccount={noop}
/>
)
fireEvent.click(screen.getByRole('button', { name: 'Save' }))
expect(await screen.findByText('Enter the key value')).toBeInTheDocument()
expect(secretField()).toHaveAttribute('aria-invalid', 'true')
expect(secretField().getAttribute('aria-describedby')).toContain('form-item-message')
expect(onSave).not.toHaveBeenCalled()
})
it('submits the value exactly as typed, with no trimming', async () => {
const onSave = vi.fn()
const user = userEvent.setup()
customRender(
<McpSecretsCard
state={{ status: 'form', request }}
isSaving={false}
onSave={onSave}
onCancel={noop}
onSwitchAccount={noop}
/>
)
await user.click(secretField())
await user.paste(' sk-padded ')
fireEvent.click(screen.getByRole('button', { name: 'Save' }))
await waitFor(() => expect(onSave).toHaveBeenCalledWith(' sk-padded '))
})
})