Files
Gildas GarciaandAlaister Young 737b8595f2 Update API types (#50234)
## Problem

platform, v1 and v2 have been already completely migrated and introduced
some changes.

Some types have been renamed, some outputs and inputs updated.

## Solution

- Update the API types
- Fix the TS errors

## Update

Taking this over to unblock #50134, which needs the new scoped token
permission ids from the regenerated types.

- Merged `master`.
- Regenerated `api-v2.d.ts` from the production spec. The previous files
came from a local API that exposed a webhook events endpoint production
doesn't have yet. Production has since added standardized 400 error
responses on the v2 organization endpoints. `api-v1.d.ts` and
`platform.d.ts` already matched production.
- Fixed `verify-production-types`. It formatted the regenerated files in
a temp directory outside the repository, so Prettier fell back to its
defaults and the comparison could never match the committed files. It
now passes the repository config explicitly. `pnpm api:verify-types`
passes on this branch.
- Verified locally: `pnpm typecheck`, `pnpm api:verify-types`, Studio
unit tests.

<!-- This is an auto-generated comment: release notes by coderabbit.ai
-->
## Summary by CodeRabbit

* **Bug Fixes**
* Preserved descriptions when saving, sharing, moving, or unsharing
notebooks, reports, SQL snippets, and saved queries.
* Improved handling of empty or null values across notebook
descriptions, billing usage, pooler settings, and infrastructure fields.
* Improved read-replica connection handling, including read-only
connection strings.
* Updated storage configuration and capability handling to match current
settings.

* **API and Compatibility**
* Updated organization, project, storage, OAuth, billing, and
infrastructure data handling to match current API responses.
  * OAuth app creation and updates now require scopes.
<!-- end of auto-generated comment: release notes by coderabbit.ai -->

---------

Co-authored-by: Alaister Young <10985857+alaister@users.noreply.github.com>
2026-09-11 12:17:49 +08:00

94 lines
3.3 KiB
TypeScript

import { fireEvent, screen, waitFor } from '@testing-library/react'
import { components } from 'api-types'
import { HttpResponse } from 'msw'
import { beforeEach, describe, expect, it, vi } from 'vitest'
import { ApiKeyPill } from './ApiKeyPill'
import type { APIKeysData } from '@/data/api-keys/api-keys-query'
import { customRender } from '@/tests/lib/custom-render'
import { addAPIMock } from '@/tests/lib/msw'
type ApiKeyResponse = components['schemas']['ApiKeyResponse_Output']
// Permissions are non-network global state — mock the hook (the skill's allowed
// exception), matching the neighboring PublishableAPIKeys.test.tsx.
const { mockUseAsyncCheckPermissions } = vi.hoisted(() => ({
mockUseAsyncCheckPermissions: vi.fn(),
}))
vi.mock('@/hooks/misc/useCheckPermissions', () => ({
useAsyncCheckPermissions: mockUseAsyncCheckPermissions,
}))
// CopyButton writes via copyToClipboard from 'ui'. Stub just that export so we can
// assert the value handed to the clipboard without depending on jsdom's
// document.hasFocus() / navigator.clipboard. Everything else in 'ui' stays real.
const { mockCopyToClipboard } = vi.hoisted(() => ({ mockCopyToClipboard: vi.fn() }))
vi.mock('ui', async (importOriginal) => ({
...(await importOriginal<typeof import('ui')>()),
copyToClipboard: mockCopyToClipboard,
}))
// The masked value the list endpoint returns for a secret key (first 15 chars),
// and the full key the reveal-by-id endpoint returns.
const MASKED = 'sb_secret_abcde'
const FULL = 'sb_secret_abcdefghijklmnop'
// ApiKeyPill is strongly typed against the query's secret-key shape; the fields it
// reads are id/type/api_key, so cast a minimal object through unknown.
const secretKey = {
id: 'secret',
name: 'secret',
type: 'secret',
api_key: MASKED,
prefix: MASKED,
} as unknown as Extract<APIKeysData[number], { type: 'secret' }>
function mockRevealById() {
addAPIMock({
method: 'get',
path: '/v1/projects/:ref/api-keys/:id',
response: () =>
HttpResponse.json<ApiKeyResponse>({
id: 'secret',
name: 'secret',
type: 'secret',
api_key: FULL,
prefix: MASKED,
}),
})
}
describe('ApiKeyPill', () => {
beforeEach(() => {
mockUseAsyncCheckPermissions.mockReturnValue({ can: true, isLoading: false })
mockCopyToClipboard.mockClear()
})
it('masks the secret key by default and does not expose the full key', () => {
customRender(<ApiKeyPill apiKey={secretKey} />)
expect(screen.getByText(MASKED)).toBeInTheDocument()
expect(screen.queryByText(FULL.slice(15))).not.toBeInTheDocument()
expect(screen.getByRole('button', { name: 'Reveal API key' })).toBeInTheDocument()
})
it('reveals the full secret key when the reveal button is clicked', async () => {
mockRevealById()
customRender(<ApiKeyPill apiKey={secretKey} />)
fireEvent.click(screen.getByRole('button', { name: 'Reveal API key' }))
expect(await screen.findByText(FULL.slice(15))).toBeInTheDocument()
})
it('copies the full secret key to the clipboard', async () => {
mockRevealById()
customRender(<ApiKeyPill apiKey={secretKey} />)
fireEvent.click(screen.getByRole('button', { name: 'Copy API key' }))
await waitFor(() => expect(mockCopyToClipboard).toHaveBeenCalledTimes(1))
await expect(Promise.resolve(mockCopyToClipboard.mock.calls[0][0])).resolves.toBe(FULL)
})
})