Files
supabase/apps/studio/components/interfaces/App/CommandMenu/DatabaseEntitySearchCommands.test.tsx
Joshen Lim 53ffdf75c9 Joshenlim/fe 4945 cmd k support searching for all table (#53262)
## Context

Probably the final piece for this series of changes towards resource
aware Cmd K. We currently only present results for tables when searching
in Cmd K, this PR adds in other entity types like views, materialized
views, foreign tables, and partitioned tables (only the parent)

Note that the results excludes entities created from database extensions
to reduce the chance of noise

<img width="500" alt="image"
src="https://github.com/user-attachments/assets/27bf5026-937a-4883-b365-75694d551a39"
/>

## Related changes
- Tweaked the entity icon for partitioned tables a little to be blue
(previously gray) and
- Added an entity icon for child partition tables in the table editor
that's slightly lighter than the partition table icon, currently child
partition tables just render with the same icon as tables which can be
confusing
<img width="286" height="128" alt="image"
src="https://github.com/user-attachments/assets/638af7c3-512d-4264-bfae-0a8827b3ef94"
/>
- Note that i'm planning to look into this separately RE how to better
render child partition tables so this might go away subsequently

## Performance

As always, ran a claude benchmarking session - this runs on debounced
keystrokes against a live catalog, so it's benchmarked at scale:
**45,653 `pg_class` rows**
- 12k tables
- 3,143 views
- 3,002 matviews
- 3,001 foreign tables)
Taking a median of 15 runs, all variants measured against the same
catalog in one process.

| scenario | query | master | this PR | worst |
|---|---|---|---|---|
| exact table name | `t_500` | 19.5ms | 24.3ms | 29.1ms |
| view name | `v_10` | 19.3ms | 23.7ms | 25.8ms |
| foreign table name | `ft_10` | 18.9ms | 24.1ms | 26.9ms |
| no match | `zzz_nothing` | 19.4ms | 24.0ms | 27.5ms |
| broad prefix (~3k matches) | `t_1` | 30.8ms | 38.7ms | 45.1ms |
| broad matview prefix | `mv_1` | 26.6ms | 37.5ms | 41.7ms |
2026-11-12 11:37:45 +08:00

555 lines
19 KiB
TypeScript

import { act, screen, waitFor } from '@testing-library/react'
import userEvent from '@testing-library/user-event'
import { HttpResponse } from 'msw'
import { useEffect, useState } from 'react'
import {
CommandHeader,
CommandMenuInput,
CommandMenuList,
CommandWrapper,
PageType,
useCommands,
usePageComponent,
useRegisterPage,
useSetCommandMenuOpen,
useSetPage,
useSetQuery,
} from 'ui-patterns/CommandMenu'
import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest'
import { ENTITY_SEARCH_DEBOUNCE_MS } from './DatabaseEntitySearch.utils'
import { useDatabaseEntitySearchCommands } from './DatabaseEntitySearchCommands'
import type { EntitySearchData } from '@/data/database/entity-search-query'
import { customRender } from '@/tests/lib/custom-render'
import { addAPIMock } from '@/tests/lib/msw'
const { mockUseSelectedProjectQuery } = vi.hoisted(() => ({
mockUseSelectedProjectQuery: vi.fn(),
}))
vi.mock('@/hooks/misc/useSelectedProject', () => ({
useSelectedProjectQuery: mockUseSelectedProjectQuery,
}))
const SEARCH_RESULT: EntitySearchData = {
tables: [
{
id: 101,
schema: 'public',
name: 'blog_posts',
type: 'r',
rls_enabled: true,
policy_count: 4,
},
{
id: 102,
schema: 'blog',
name: 'blog_posts_archive',
type: 'r',
rls_enabled: false,
policy_count: 1,
},
],
policies: [
{
id: 201,
name: 'Authors can edit their posts',
command: 'UPDATE',
schema: 'public',
table: 'blog_posts',
table_id: 101,
},
{
id: 202,
name: 'Anyone can read archived posts',
command: 'SELECT',
schema: 'blog',
table: 'blog_posts_archive',
table_id: 102,
},
],
triggers: [
{
id: 401,
name: 'audit_blog_changes',
activation: 'AFTER',
events: ['INSERT', 'UPDATE'],
schema: 'public',
table: 'blog_posts',
table_id: 101,
matched_function: null,
},
{
id: 402,
name: 'set_updated_at',
activation: 'BEFORE',
events: ['UPDATE'],
schema: 'blog',
table: 'blog_posts_archive',
table_id: 102,
matched_function: null,
},
{
id: 403,
name: 'fill_audit_fields',
activation: 'BEFORE',
events: ['INSERT'],
schema: 'public',
table: 'comments',
table_id: 103,
matched_function: 'set_audit_fields',
},
],
functions: [
{ id: 301, schema: 'public', name: 'get_blog_post', type: 'function', args: 'id bigint' },
{ id: 302, schema: 'blog', name: 'archive_blog_posts', type: 'procedure', args: '' },
{ id: 303, schema: 'public', name: 'touch_blog_post', type: 'function', args: '' },
],
}
/** Renders the registered command sections as a plain list, driven by a text input. */
const CommandMenuHarness = () => {
useDatabaseEntitySearchCommands()
const setQuery = useSetQuery()
const setOpen = useSetCommandMenuOpen()
const sections = useCommands()
const [value, setValue] = useState('')
useEffect(() => setOpen(true), [setOpen])
return (
<>
<input
aria-label="Search"
value={value}
onChange={(event) => {
setValue(event.target.value)
setQuery(event.target.value)
}}
/>
{sections.map((section) => (
<section key={section.id} aria-label={section.name}>
{section.commands.map((command) => (
<div key={command.id}>
<span>{command.name}</span>
{command.badge?.()}
{'route' in command && <span>{command.route}</span>}
</div>
))}
</section>
))}
</>
)
}
describe('useDatabaseEntitySearchCommands', () => {
let searchRequests: string[]
beforeEach(() => {
searchRequests = []
mockUseSelectedProjectQuery.mockReturnValue({
data: { id: 1, ref: 'default', name: 'default', connectionString: 'encrypted' },
})
addAPIMock({
method: 'post',
path: '/platform/pg-meta/:ref/query',
response: async ({ request }) => {
const { query } = (await request.json()) as { query: string }
searchRequests.push(query)
return HttpResponse.json([SEARCH_RESULT])
},
})
})
it('lists matching tables with their policy count and RLS status', async () => {
customRender(<CommandMenuHarness />)
await userEvent.type(screen.getByRole('textbox', { name: 'Search' }), 'blog_posts')
const section = await screen.findByRole('region', { name: 'Tables' })
expect(section).toHaveTextContent('public.blog_posts')
expect(section).toHaveTextContent('4 policies • RLS enabled')
expect(section).toHaveTextContent('blog.blog_posts_archive')
expect(section).toHaveTextContent('1 policy • RLS disabled')
expect(section).toHaveTextContent('/project/default/editor/101?schema=public')
})
it('describes views by their security mode, and says nothing for kinds that have none', async () => {
addAPIMock({
method: 'post',
path: '/platform/pg-meta/:ref/query',
response: () =>
HttpResponse.json([
{
...SEARCH_RESULT,
tables: [
{
id: 110,
schema: 'public',
name: 'blog_posts_v',
type: 'v',
security_invoker: true,
},
{
id: 111,
schema: 'public',
name: 'blog_posts_public',
type: 'v',
security_invoker: false,
},
{ id: 112, schema: 'public', name: 'blog_posts_daily', type: 'm' },
{ id: 113, schema: 'public', name: 'blog_posts_remote', type: 'f' },
],
policies: [],
triggers: [],
functions: [],
},
]),
})
customRender(<CommandMenuHarness />)
await userEvent.type(screen.getByRole('textbox', { name: 'Search' }), 'blog_posts')
const section = await screen.findByRole('region', { name: 'Tables' })
expect(section).toHaveTextContent('Security invoker')
expect(section).toHaveTextContent('Security definer')
// A materialized view supports neither RLS nor security_invoker, so claiming
// "RLS disabled" would read as a setting the user could change
expect(section).not.toHaveTextContent('RLS')
// All four are reachable in the table editor, same as from its sidebar
expect(section).toHaveTextContent('/project/default/editor/112?schema=public')
expect(section).toHaveTextContent('/project/default/editor/113?schema=public')
})
it('lists matching policies with their command and table, linking to the policy editor', async () => {
customRender(<CommandMenuHarness />)
await userEvent.type(screen.getByRole('textbox', { name: 'Search' }), 'blog_posts')
const section = await screen.findByRole('region', { name: 'Policies' })
expect(section).toHaveTextContent('Authors can edit their posts')
expect(section).toHaveTextContent('UPDATE on public.blog_posts')
// The policies page lists policies per schema, so the link has to carry it
expect(section).toHaveTextContent('/project/default/database/policies?schema=public&edit=201')
expect(section).toHaveTextContent('/project/default/database/policies?schema=blog&edit=202')
})
it('lists matching triggers with their timing and table, linking to the trigger editor', async () => {
customRender(<CommandMenuHarness />)
await userEvent.type(screen.getByRole('textbox', { name: 'Search' }), 'blog_posts')
const section = await screen.findByRole('region', { name: 'Triggers' })
expect(section).toHaveTextContent('audit_blog_changes')
expect(section).toHaveTextContent('AFTER INSERT OR UPDATE on public.blog_posts')
expect(section).toHaveTextContent('BEFORE UPDATE on blog.blog_posts_archive')
// A trigger found through its function says which function it runs, instead of its timing
expect(section).toHaveTextContent('fill_audit_fields')
expect(section).toHaveTextContent('Runs set_audit_fields')
expect(section).not.toHaveTextContent('BEFORE INSERT on public.comments')
expect(section.textContent?.match(/Runs /g)).toHaveLength(1)
// The triggers page lists triggers per schema, so the link has to carry it
expect(section).toHaveTextContent(
'/project/default/database/triggers/data?schema=public&edit=401'
)
expect(section).toHaveTextContent(
'/project/default/database/triggers/data?schema=blog&edit=402'
)
})
it('lists matching functions with their arguments, linking to the function editor', async () => {
customRender(<CommandMenuHarness />)
await userEvent.type(screen.getByRole('textbox', { name: 'Search' }), 'blog_post')
const section = await screen.findByRole('region', { name: 'Functions' })
expect(section).toHaveTextContent('public.get_blog_post')
expect(section).toHaveTextContent('(id bigint)')
expect(section).toHaveTextContent('blog.archive_blog_posts')
expect(section).toHaveTextContent('procedure')
// Functions without parameters have no details to show
expect(section).toHaveTextContent('public.touch_blog_post')
expect(section).not.toHaveTextContent('()')
// The functions page lists functions per schema, so the link has to carry it
expect(section).toHaveTextContent('/project/default/database/functions?schema=public&edit=301')
expect(section).toHaveTextContent('/project/default/database/functions?schema=blog&edit=302')
})
it('debounces typing into a single database query', async () => {
customRender(<CommandMenuHarness />)
await userEvent.type(screen.getByRole('textbox', { name: 'Search' }), 'blog_posts')
await screen.findByRole('region', { name: 'Tables' })
expect(searchRequests).toHaveLength(1)
})
it('does not query the database for short searches', async () => {
customRender(<CommandMenuHarness />)
await userEvent.type(screen.getByRole('textbox', { name: 'Search' }), 'bl')
// Wait past the debounce window before asserting nothing was sent
await new Promise((resolve) => setTimeout(resolve, 500))
expect(searchRequests).toHaveLength(0)
expect(screen.queryByRole('region', { name: 'Tables' })).not.toBeInTheDocument()
expect(screen.queryByRole('region', { name: 'Policies' })).not.toBeInTheDocument()
expect(screen.queryByRole('region', { name: 'Triggers' })).not.toBeInTheDocument()
expect(screen.queryByRole('region', { name: 'Functions' })).not.toBeInTheDocument()
})
it('does not show results from a previous project while searching another', async () => {
let project = { id: 1, ref: 'default', name: 'default', connectionString: 'encrypted' }
mockUseSelectedProjectQuery.mockImplementation(() => ({ data: project }))
let resolveSecondSearch: (() => void) | undefined
addAPIMock({
method: 'post',
path: '/platform/pg-meta/:ref/query',
response: async ({ params }) => {
// Hold the second project's search in flight until the test releases it
if (params.ref === 'other') {
await new Promise<void>((resolve) => (resolveSecondSearch = resolve))
}
return HttpResponse.json([SEARCH_RESULT])
},
})
const { rerender } = customRender(<CommandMenuHarness />)
const input = screen.getByRole('textbox', { name: 'Search' })
await userEvent.type(input, 'blog')
await screen.findByRole('region', { name: 'Tables' })
project = { id: 2, ref: 'other', name: 'other', connectionString: 'encrypted' }
rerender(<CommandMenuHarness />)
await userEvent.type(input, '_posts')
await waitFor(() => expect(resolveSecondSearch).toBeDefined())
expect(screen.queryByRole('region', { name: 'Tables' })).not.toBeInTheDocument()
resolveSecondSearch?.()
const section = await screen.findByRole('region', { name: 'Tables' })
expect(section).toHaveTextContent('/project/other/editor/101?schema=public')
})
it('keeps a trigger found through its function visible while a refined search loads', async () => {
let requests = 0
let resolveRefinedSearch: (() => void) | undefined
addAPIMock({
method: 'post',
path: '/platform/pg-meta/:ref/query',
response: async () => {
requests += 1
// Hold the refined search in flight until the test releases it
if (requests === 2) {
await new Promise<void>((resolve) => (resolveRefinedSearch = resolve))
}
return HttpResponse.json([
{
...SEARCH_RESULT,
tables: [],
policies: [],
functions: [],
triggers: [SEARCH_RESULT.triggers[2]],
},
])
},
})
customRender(<CommandMenuHarness />)
const input = screen.getByRole('textbox', { name: 'Search' })
await userEvent.type(input, 'set_audit')
expect(await screen.findByText(/Runs set_audit_fields/)).toBeInTheDocument()
// Neither the trigger's name nor its table match the refined search, only its function
await userEvent.type(input, '_fields')
await waitFor(() => expect(resolveRefinedSearch).toBeDefined())
expect(screen.getByText(/Runs set_audit_fields/)).toBeInTheDocument()
resolveRefinedSearch?.()
})
})
/** Renders the real command menu list, to check how results interact with its empty state. */
const CommandMenuListHarness = () => {
useDatabaseEntitySearchCommands()
const setOpen = useSetCommandMenuOpen()
useEffect(() => setOpen(true), [setOpen])
return (
<CommandWrapper>
<CommandHeader>
<CommandMenuInput />
</CommandHeader>
<CommandMenuList />
</CommandWrapper>
)
}
describe('useDatabaseEntitySearchCommands in the command menu list', () => {
beforeEach(() => {
mockUseSelectedProjectQuery.mockReturnValue({
data: { id: 1, ref: 'default', name: 'default', connectionString: 'encrypted' },
})
})
it('hides the empty state once table results arrive', async () => {
addAPIMock({
method: 'post',
path: '/platform/pg-meta/:ref/query',
response: () => HttpResponse.json([SEARCH_RESULT]),
})
customRender(<CommandMenuListHarness />)
await userEvent.type(screen.getByRole('combobox'), 'blog_posts')
expect(await screen.findByText('4 policies • RLS enabled')).toBeInTheDocument()
await waitFor(() => expect(screen.queryByText('No results found.')).not.toBeInTheDocument())
})
it.each([
['archive_blog_posts', 'a function'],
['audit_blog', 'a trigger'],
])(
'lists tables, policies, triggers, then functions, even when %s matches best',
async (search) => {
addAPIMock({
method: 'post',
path: '/platform/pg-meta/:ref/query',
response: () => HttpResponse.json([SEARCH_RESULT]),
})
customRender(<CommandMenuListHarness />)
await userEvent.type(screen.getByRole('combobox'), search)
await screen.findByText('Functions')
const headings = screen.getAllByText(/^(Tables|Policies|Triggers|Functions)$/)
expect(headings.map((heading) => heading.textContent)).toEqual([
'Tables',
'Policies',
'Triggers',
'Functions',
])
}
)
it('lists functions before triggers that only match through the function they run', async () => {
addAPIMock({
method: 'post',
path: '/platform/pg-meta/:ref/query',
response: () =>
HttpResponse.json([
{
tables: [],
policies: [],
triggers: [SEARCH_RESULT.triggers[2]],
functions: [
{ id: 304, schema: 'public', name: 'set_audit_fields', type: 'function', args: '' },
],
},
]),
})
customRender(<CommandMenuListHarness />)
await userEvent.type(screen.getByRole('combobox'), 'set_audit')
await screen.findByText('Runs set_audit_fields')
const headings = screen.getAllByText(/^(Tables|Policies|Triggers|Functions)$/)
expect(headings.map((heading) => heading.textContent)).toEqual(['Functions', 'Triggers'])
})
})
const SUB_PAGE = 'Search Database Tables'
/** Mirrors a context search sub-page: its own input, which also sets the shared query. */
const SubPageSearch = () => {
const setQuery = useSetQuery()
const [value, setValue] = useState('')
return (
<input
aria-label="Search database tables"
value={value}
onChange={(event) => {
setValue(event.target.value)
setQuery(event.target.value)
}}
/>
)
}
/** Mirrors PageSwitch, where a sub-page replaces the command list entirely. */
const SubPageHarness = () => {
useDatabaseEntitySearchCommands()
const setQuery = useSetQuery()
const setOpen = useSetCommandMenuOpen()
const setPage = useSetPage()
const PageComponent = usePageComponent()
const [value, setValue] = useState('')
useRegisterPage(SUB_PAGE, { type: PageType.Component, component: SubPageSearch })
useEffect(() => setOpen(true), [setOpen])
if (PageComponent) return <PageComponent />
return (
<>
<input
aria-label="Search"
value={value}
onChange={(event) => {
setValue(event.target.value)
setQuery(event.target.value)
}}
/>
<button tabIndex={0} onClick={() => setPage(SUB_PAGE)}>
Open sub-page
</button>
</>
)
}
describe('useDatabaseEntitySearchCommands on a sub-page', () => {
let searchRequests: string[]
beforeEach(() => {
vi.useFakeTimers({ shouldAdvanceTime: true })
searchRequests = []
mockUseSelectedProjectQuery.mockReturnValue({
data: { id: 1, ref: 'default', name: 'default', connectionString: 'encrypted' },
})
addAPIMock({
method: 'post',
path: '/platform/pg-meta/:ref/query',
response: async ({ request }) => {
const { query } = (await request.json()) as { query: string }
searchRequests.push(query)
return HttpResponse.json([SEARCH_RESULT])
},
})
})
afterEach(() => {
vi.useRealTimers()
})
it('does not search the database while a sub-page shows its own results', async () => {
const user = userEvent.setup({ advanceTimers: vi.advanceTimersByTime })
const settleDebounce = () =>
act(() => vi.advanceTimersByTimeAsync(ENTITY_SEARCH_DEBOUNCE_MS * 2))
customRender(<SubPageHarness />)
// Searching from the top level does reach the database
await user.type(screen.getByRole('textbox', { name: 'Search' }), 'blog_posts')
await settleDebounce()
expect(searchRequests).toHaveLength(1)
// The sub-page runs its own search, so typing there must not send a second request
await user.click(screen.getByRole('button', { name: 'Open sub-page' }))
await user.type(screen.getByRole('textbox', { name: 'Search database tables' }), 'colors')
await settleDebounce()
expect(searchRequests).toHaveLength(1)
})
})