Files
Gildas Garcia 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

115 lines
3.7 KiB
TypeScript

import { screen } from '@testing-library/react'
import userEvent from '@testing-library/user-event'
import type { components } from 'api-types'
import { HttpResponse } from 'msw'
import { Button } from 'ui'
import { useCurrentPage, useSetPage } from 'ui-patterns/CommandMenu'
import { beforeEach, describe, expect, it, vi } from 'vitest'
import { useApiKeysCommands } from './ApiKeys'
import { customRender } from '@/tests/lib/custom-render'
import { addAPIMock } from '@/tests/lib/msw'
type ApiKeyResponse = components['schemas']['ApiKeyResponse_Output']
const { mockUseAsyncCheckPermissions, mockUseHighAvailability, mockUseSelectedProjectQuery } =
vi.hoisted(() => ({
mockUseAsyncCheckPermissions: vi.fn(),
mockUseHighAvailability: vi.fn(),
mockUseSelectedProjectQuery: vi.fn(),
}))
vi.mock('@/hooks/misc/useCheckPermissions', () => ({
useAsyncCheckPermissions: mockUseAsyncCheckPermissions,
}))
vi.mock('@/hooks/misc/useHighAvailability', () => ({
useHighAvailability: mockUseHighAvailability,
}))
vi.mock('@/hooks/misc/useSelectedProject', () => ({
useSelectedProjectQuery: mockUseSelectedProjectQuery,
}))
const API_KEYS: ApiKeyResponse[] = [
{ api_key: 'anon-key', name: 'anon', type: 'legacy' },
{ api_key: 'service-key', name: 'service_role', type: 'legacy' },
{
api_key: 'publishable-key',
hash: 'hash',
id: 'publishable-id',
inserted_at: '2025-02-16T22:24:42.115195Z',
name: 'default',
type: 'publishable',
},
{
api_key: 'secret-key',
hash: 'hash',
id: 'secret-id',
inserted_at: '2025-02-16T22:24:42.115195Z',
name: 'sb_secret',
type: 'secret',
},
]
/** Renders the API keys command page so its commands can be asserted on. */
const CommandPageHarness = () => {
useApiKeysCommands()
const setPage = useSetPage()
const page = useCurrentPage()
const commands =
page && 'sections' in page ? page.sections.flatMap((section) => section.commands) : []
return (
<>
<Button onClick={() => setPage('API Keys')}>Open API keys page</Button>
<ul>
{commands.map((command) => (
<li key={command.id}>{command.name}</li>
))}
</ul>
</>
)
}
async function renderCommandPage() {
customRender(<CommandPageHarness />)
await userEvent.click(screen.getByRole('button', { name: 'Open API keys page' }))
}
describe('useApiKeysCommands', () => {
beforeEach(() => {
mockUseAsyncCheckPermissions.mockReturnValue({ can: true })
mockUseSelectedProjectQuery.mockReturnValue({
data: { id: 1, ref: 'default', name: 'default' },
})
mockUseHighAvailability.mockReturnValue({ isHighAvailability: false, isPending: false })
addAPIMock({
method: 'get',
path: '/v1/projects/:ref/api-keys',
response: () => HttpResponse.json<ApiKeyResponse[]>(API_KEYS),
})
})
it('omits the legacy key commands on High Availability projects', async () => {
mockUseHighAvailability.mockReturnValue({ isHighAvailability: true, isPending: false })
await renderCommandPage()
expect(await screen.findByText('Copy publishable key')).toBeInTheDocument()
expect(screen.getByText('Copy secret key (sb_secret)')).toBeInTheDocument()
expect(screen.queryByText('Copy anonymous API key')).not.toBeInTheDocument()
expect(screen.queryByText('Copy service API key')).not.toBeInTheDocument()
})
it('includes the legacy key commands on other projects', async () => {
await renderCommandPage()
expect(await screen.findByText('Copy anonymous API key')).toBeInTheDocument()
expect(screen.getByText('Copy service API key')).toBeInTheDocument()
expect(screen.getByText('Copy publishable key')).toBeInTheDocument()
expect(screen.getByText('Copy secret key (sb_secret)')).toBeInTheDocument()
})
})