Skip to content

The Postgres development platform. Supabase gives you a dedicated Postgres database to build your web, mobile, and AI applications.

22.1 KB
Raw Blame
import { QueryClient } from '@tanstack/react-query'
import { act, screen, waitFor } from '@testing-library/react'
import userEvent from '@testing-library/user-event'
import { HttpResponse } from 'msw'
import { useEffect, useRef, type ReactNode } from 'react'
import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest'

import { ExplorerQueryTab } from '../ExplorerQueryTab'
import { projectKeys } from '@/data/projects/keys'
import type { ReadReplicasData } from '@/data/read-replicas/replicas-query'
import { explorerQueryState } from '@/state/explorer-query'
import { createTabId, createTabsState, TabsStateContext } from '@/state/tabs'
import { customRender } from '@/tests/lib/custom-render'
import { addAPIMock } from '@/tests/lib/msw'
import { setupSqlEditorMocks } from '@/tests/lib/sql-editor-test-utils'

const testContext = vi.hoisted(() => ({
  track: vi.fn(),
  flags: { otelLegacyLogs: true } as Record<string, boolean>,
  params: { ref: 'default', id: 'query-test' } as { ref?: string; id?: string },
  /** Simulated editor selection — the mocked CodeEditor's fake editor reads this. */
  selectedText: undefined as string | undefined,
}))

vi.mock('@/lib/telemetry/track', () => ({ useTrack: () => testContext.track }))

vi.mock('common', async (importOriginal) => {
  const actual = await importOriginal<typeof import('common')>()
  return {
    ...actual,
    IS_PLATFORM: true,
    useParams: () => testContext.params,
    useFlag: (flag: string) => testContext.flags[flag] ?? false,
  }
})

vi.mock('@/components/ui/CodeEditor/CodeEditor', () => ({
  CodeEditor: ({
    value,
    onInputChange,
    onMount,
  }: {
    value: string
    onInputChange?: (value: string | undefined) => void
    onMount?: (editor: any, monaco: any) => void
  }) => {
    // Kept fresh via a ref (rather than closed over) so the fake editor's
    // `getValue` reflects edits made after mount, same as the real editor would.
    const valueRef = useRef(value)
    valueRef.current = value

    useEffect(() => {
      const hasSelection = testContext.selectedText !== undefined
      // Matches this instance's selection at the moment it mounts — a fresh editor
      // instance (e.g. after "Show query" remounts it) has no memory of a prior
      // instance's selection, so this must be read fresh rather than carried over.
      const selection = hasSelection
        ? { startLineNumber: 1, endLineNumber: 2, startColumn: 1, endColumn: 5 }
        : null

      onMount?.(
        {
          getValue: () => valueRef.current,
          getSelection: () => selection,
          getModel: () => ({ getValueInRange: () => testContext.selectedText }),
          onDidBlurEditorWidget: () => () => {},
          // Intentionally never invoked here — the real editor only fires this on a
          // subsequent user-driven selection change, not eagerly on mount. Tests that
          // need to simulate a live selection change should call this directly.
          onDidChangeCursorSelection: () => ({ dispose: () => {} }),
          addAction: () => {},
        },
        { KeyMod: { CtrlCmd: 1, Shift: 2 }, KeyCode: { KeyK: 3, Enter: 4 } }
      )
      // eslint-disable-next-line react-hooks/exhaustive-deps
    }, [])

    return (
      <textarea
        aria-label="SQL editor"
        value={value}
        onChange={(e) => onInputChange?.(e.target.value)}
      />
    )
  },
}))

vi.mock('../QueryEditor/QuerySourceMenu', () => ({
  QuerySourceMenu: ({
    roleImpersonationState,
  }: {
    roleImpersonationState?: { role?: { type: string; role?: string } }
  }) => (
    <div data-testid="impersonated-role">
      {roleImpersonationState?.role?.type === 'postgrest'
        ? roleImpersonationState.role.role
        : 'none'}
    </div>
  ),
}))

// react-resizable-panels needs real layout to mount panel content, which jsdom can't provide.
vi.mock('ui', async (importOriginal) => {
  const actual = await importOriginal<typeof import('ui')>()
  const Passthrough = ({ children, ...props }: { children?: ReactNode }) => (
    <div {...props}>{children}</div>
  )
  return {
    ...actual,
    ResizableHandle: (props: Record<string, unknown>) => <div {...props} />,
    ResizablePanel: Passthrough,
    ResizablePanelGroup: Passthrough,
  }
})

vi.mock('../QueryEditor/DisplaySettingsButton', () => ({
  DisplaySettingsButton: ({
    display,
    disabled,
    onChange,
  }: {
    display: { view: 'table' | 'chart' }
    disabled: boolean
    onChange: (display: {
      view: 'table' | 'chart'
      chart: {
        type: 'bar'
        x_column: string
        y_series: string[]
        cumulative: boolean
        scale: 'linear'
        show_labels: boolean
      }
    }) => void
  }) => (
    <button
      aria-label="Result settings"
      disabled={disabled}
      tabIndex={0}
      onClick={() =>
        onChange({
          view: 'chart',
          chart: {
            type: 'bar',
            x_column: 'day',
            y_series: ['requests'],
            cumulative: false,
            scale: 'linear',
            show_labels: true,
          },
        })
      }
    >
      {display.view}
    </button>
  ),
}))

vi.mock('../QueryEditor/QueryResultChart', () => ({
  QueryResultChart: () => <div>Chart results</div>,
}))

const renderQueryTab = (tabsState = createTabsState('default'), queryClient?: QueryClient) =>
  customRender(
    <TabsStateContext.Provider value={tabsState}>
      <ExplorerQueryTab />
    </TabsStateContext.Provider>,
    { queryClient }
  )

/**
 * By the time a query tab can auto-run, its project is already cached - the Explorer home tab
 * that created the draft required the same project data to do so. Priming the cache here
 * mirrors that instead of racing this test against a cold fetch of `/platform/projects/:ref`.
 */
const createWarmProjectQueryClient = (ref: string = 'default') => {
  const queryClient = new QueryClient({
    defaultOptions: { queries: { retry: false }, mutations: { retry: false } },
  })
  queryClient.setQueryData(projectKeys.detail(ref), {
    id: 1,
    ref,
    organization_id: 1,
    name: 'Test Project',
    status: 'ACTIVE_HEALTHY',
    cloud_provider: 'AWS',
    region: 'us-east-1',
    db_host: `db.${ref}.supabase.co`,
    restUrl: `https://${ref}.supabase.co/rest/v1/`,
    inserted_at: '2024-01-01T00:00:00Z',
    updated_at: '2024-01-01T00:00:00Z',
    subscription_id: 'sub_123',
    is_branch_enabled: false,
    is_physical_backups_enabled: false,
    high_availability: false,
    integration_source: null,
    connectionString: 'postgresql://postgres@localhost:5432/postgres',
    is_hibernating: false,
  })
  return queryClient
}

const createDraft = (
  source:
    | { _tag: 'database'; database_identifier?: string }
    | {
        _tag: 'logs'
        time_range: { _tag: 'relative_time_range'; amount: number; unit: 'hour' }
      },
  sql: string = 'select 1',
  { autoRun = false }: { autoRun?: boolean } = {}
) => {
  explorerQueryState.removeDraft({ id: 'query-test', projectRef: 'default' })
  explorerQueryState.createDraft({
    id: 'query-test',
    projectRef: 'default',
    sql,
    source,
    autoRun,
  })
}

/**
 * Mocks the generic pg-meta query endpoint and returns the SQL of every executed query.
 * Background prefetches (intellisense keywords/functions/schemas/table-columns, event
 * triggers) hit the same endpoint, distinguished from an actual run by their non-empty
 * `key` search param — an executed query's `key` is `''`.
 */
const mockExecutedQueries = () => {
  const executedQueries: string[] = []
  addAPIMock({
    method: 'post',
    path: '/platform/pg-meta/:ref/query',
    response: async ({ request }) => {
      const key = new URL(request.url).searchParams.get('key')
      if (key !== '') return HttpResponse.json([])
      const { query } = (await request.json()) as { query: string }
      executedQueries.push(query)
      return HttpResponse.json([])
    },
  })
  return executedQueries
}

beforeEach(() => {
  testContext.track.mockClear()
  setupSqlEditorMocks()
  testContext.flags.otelLegacyLogs = true
  testContext.params = { ref: 'default', id: 'query-test' }
  testContext.selectedText = undefined
  explorerQueryState.removeDraft({ id: 'query-test', projectRef: 'default' })
})

afterEach(() => explorerQueryState.flushPendingPersistence())

describe('QueryTab execution', () => {
  it('keeps loading while dynamic route parameters are unavailable', () => {
    testContext.params = {}

    renderQueryTab()

    expect(screen.getByRole('status', { name: 'Loading query' })).toBeInTheDocument()
    expect(screen.queryByText('Query draft not found')).not.toBeInTheDocument()
  })

  it('auto-runs a draft created with autoRun, then clears the flag', async () => {
    // `/platform/pg-meta/:ref/query` is also hit by unrelated background metadata fetches
    // (autocomplete definitions, event triggers, ...), so the run is asserted via the stored
    // result it produces rather than by counting requests to that shared endpoint.
    createDraft({ _tag: 'database' }, 'select 1', { autoRun: true })

    renderQueryTab(createTabsState('default'), createWarmProjectQueryClient())

    await waitFor(() => expect(explorerQueryState.results['query-test']).toBeDefined())
    expect(explorerQueryState.results['query-test']).toMatchObject({ rows: [] })
    expect(explorerQueryState.drafts['query-test']?.pendingAutoRun).toBe(false)
    const queryEvents = testContext.track.mock.calls.filter(([action]) =>
      action.startsWith('explorer_query_')
    )
    expect(queryEvents.map(([action]) => action)).toEqual([
      'explorer_query_submitted',
      'explorer_query_completed',
    ])
    expect(queryEvents[0][1]).toMatchObject({
      surface: 'query_tab',
      queryId: 'query-test',
      source: 'database',
    })
    expect(queryEvents[1][1].runId).toBe(queryEvents[0][1].runId)
    expect(queryEvents[0][1]).not.toHaveProperty('sql')
  })

  it('does not auto-run a draft created without autoRun', async () => {
    createDraft({ _tag: 'database' })

    renderQueryTab()
    await screen.findByRole('button', { name: 'Run' })

    await new Promise((resolve) => setTimeout(resolve, 50))
    expect(explorerQueryState.results['query-test']).toBeUndefined()
  })

  it('records an unavailable error and skips the logs endpoint when the flag is off', async () => {
    testContext.flags.otelLegacyLogs = false
    createDraft({
      _tag: 'logs',
      time_range: { _tag: 'relative_time_range', amount: 1, unit: 'hour' },
    })
    const requests: Request[] = []
    addAPIMock({
      method: 'post',
      path: '/platform/projects/:ref/analytics/endpoints/logs.all.otel',
      response: ({ request }) => {
        requests.push(request)
        return HttpResponse.json({ result: [] })
      },
    })

    renderQueryTab()
    const runButton = await screen.findByRole('button', { name: 'Run' })
    await waitFor(() => expect(runButton).toBeEnabled())
    await userEvent.click(runButton)

    expect(
      await screen.findByText("Error: Querying logs isn't available for this project yet.")
    ).toBeInTheDocument()
    expect(requests).toHaveLength(0)
    expect(
      testContext.track.mock.calls.filter(([action]) => action.startsWith('explorer_query_'))
    ).toEqual([
      [
        'explorer_query_submitted',
        expect.objectContaining({ source: 'logs', surface: 'query_tab' }),
        expect.objectContaining({ project: 'default' }),
      ],
      [
        'explorer_query_failed',
        expect.objectContaining({ failureReason: 'logs_unavailable' }),
        expect.objectContaining({ project: 'default' }),
      ],
    ])
  })

  it('records a terminal failure when database execution rejects', async () => {
    createDraft({ _tag: 'database' })
    addAPIMock({
      method: 'post',
      path: '/platform/pg-meta/:ref/query',
      response: ({ request }) =>
        new URL(request.url).searchParams.get('key') === ''
          ? HttpResponse.json({ message: 'Query failed' }, { status: 500 })
          : HttpResponse.json([]),
    })

    renderQueryTab()
    const runButton = await screen.findByRole('button', { name: 'Run' })
    await waitFor(() => expect(runButton).toBeEnabled())
    await userEvent.click(runButton)

    await waitFor(() =>
      expect(
        testContext.track.mock.calls.filter(([action]) => action.startsWith('explorer_query_'))
      ).toHaveLength(2)
    )
    const [submitted, failed] = testContext.track.mock.calls.filter(([action]) =>
      action.startsWith('explorer_query_')
    )
    expect(submitted[0]).toBe('explorer_query_submitted')
    expect(failed[0]).toBe('explorer_query_failed')
    expect(failed[1]).toMatchObject({
      runId: submitted[1].runId,
      failureReason: 'execution_error',
    })
    expect(failed[1]).not.toHaveProperty('message')
  })

  it('exposes and persists the shared notebook result view options', async () => {
    createDraft({ _tag: 'database' })
    explorerQueryState.setResult({
      id: 'query-test',
      result: { rows: [{ day: 'Monday', requests: 10 }], executedAt: 1 },
    })

    renderQueryTab()
    const settingsButton = await screen.findByRole('button', { name: 'Result settings' })
    expect(settingsButton).toBeEnabled()
    expect(settingsButton).toHaveTextContent('table')

    await userEvent.click(settingsButton)

    await waitFor(() =>
      expect(explorerQueryState.drafts['query-test']).toMatchObject({
        view: 'chart',
        chart: { type: 'bar', x_column: 'day', y_series: ['requests'] },
      })
    )
    expect(settingsButton).toHaveTextContent('chart')
  })

  it('waits for replicas, then fails closed when the selected database is absent', async () => {
    createDraft({ _tag: 'database', database_identifier: 'missing-replica' })
    let releaseReplicas: () => void = () => undefined
    const replicasPending = new Promise<void>((resolve) => {
      releaseReplicas = resolve
    })
    addAPIMock({
      method: 'get',
      path: '/platform/projects/:ref/databases',
      response: async () => {
        await replicasPending
        return HttpResponse.json<ReadReplicasData>([])
      },
    })
    const executedQueries = mockExecutedQueries()

    renderQueryTab()
    const runButton = await screen.findByRole('button', { name: 'Run' })
    expect(runButton).toBeAriaDisabled()

    act(() => releaseReplicas())
    await waitFor(() => expect(runButton).toBeEnabled())
    await userEvent.click(runButton)

    expect(
      await screen.findByText('Error: Unable to run query: Connection string is missing')
    ).toBeInTheDocument()
    expect(executedQueries).toHaveLength(0)
  })

  it('resolves a relative logs range before sending the request', async () => {
    createDraft({
      _tag: 'logs',
      time_range: { _tag: 'relative_time_range', amount: 2, unit: 'hour' },
    })
    const bodies: Array<{ iso_timestamp_start: string; iso_timestamp_end: string }> = []
    addAPIMock({
      method: 'post',
      path: '/platform/projects/:ref/analytics/endpoints/logs.all.otel',
      response: async ({ request }) => {
        bodies.push((await request.json()) as (typeof bodies)[number])
        return HttpResponse.json({ result: [] })
      },
    })

    renderQueryTab()
    const runButton = await screen.findByRole('button', { name: 'Run' })
    await waitFor(() => expect(runButton).toBeEnabled())
    await userEvent.click(runButton)
    await waitFor(() => expect(bodies).toHaveLength(1))

    expect(
      new Date(bodies[0].iso_timestamp_end).getTime() -
        new Date(bodies[0].iso_timestamp_start).getTime()
    ).toBe(2 * 60 * 60 * 1000)
  })

  it('isolates the impersonated role selection per query tab', async () => {
    createDraft({ _tag: 'database' })
    explorerQueryState.removeDraft({ id: 'query-test-2', projectRef: 'default' })
    explorerQueryState.createDraft({ id: 'query-test-2', projectRef: 'default', sql: 'select 2' })
    explorerQueryState.setRole({
      id: 'query-test',
      role: { type: 'postgrest', role: 'service_role' },
    })

    const { rerender } = renderQueryTab()
    expect(await screen.findByTestId('impersonated-role')).toHaveTextContent('service_role')

    testContext.params = { ref: 'default', id: 'query-test-2' }
    rerender(
      <TabsStateContext.Provider value={createTabsState('default')}>
        <ExplorerQueryTab />
      </TabsStateContext.Provider>
    )

    expect(await screen.findByTestId('impersonated-role')).toHaveTextContent('none')

    await act(async () => {
      explorerQueryState.removeDraft({ id: 'query-test-2', projectRef: 'default' })
    })
  })

  it('persists the preview tab once typing starts in the SQL editor', async () => {
    createDraft({ _tag: 'database' })

    const tabsState = createTabsState('default')
    const tabId = createTabId('query', { id: 'query-test' })
    tabsState.addTab({ id: tabId, type: 'query', metadata: { queryId: 'query-test' } })
    expect(tabsState.tabsMap[tabId]?.isPreview).toBe(true)

    renderQueryTab(tabsState)

    const sqlEditor = await screen.findByRole('textbox', { name: 'SQL editor' })
    await userEvent.type(sqlEditor, 'x')

    await waitFor(() => expect(tabsState.tabsMap[tabId]?.isPreview).toBe(false))
  })

  it('blocks a destructive query behind a confirmation modal, then runs it once confirmed', async () => {
    createDraft({ _tag: 'database' }, 'delete from foo')
    const executedQueries = mockExecutedQueries()

    renderQueryTab()
    const runButton = await screen.findByRole('button', { name: 'Run' })
    await waitFor(() => expect(runButton).toBeEnabled())
    await userEvent.click(runButton)

    expect(await screen.findByText('Potential issue detected')).toBeInTheDocument()
    expect(executedQueries).toHaveLength(0)
    expect(
      testContext.track.mock.calls.filter(([action]) => action.startsWith('explorer_query_'))
    ).toEqual([])

    await userEvent.click(screen.getByRole('button', { name: 'Run query' }))

    await waitFor(() => expect(executedQueries).toHaveLength(1))
  })

  it('cancels a blocked query without running it', async () => {
    createDraft({ _tag: 'database' }, 'update foo set bar = 1')
    const executedQueries = mockExecutedQueries()

    renderQueryTab()
    const runButton = await screen.findByRole('button', { name: 'Run' })
    await waitFor(() => expect(runButton).toBeEnabled())
    await userEvent.click(runButton)

    expect(await screen.findByText('Potential issue detected')).toBeInTheDocument()

    await userEvent.click(screen.getByRole('button', { name: 'Cancel' }))

    expect(screen.queryByText('Potential issue detected')).not.toBeInTheDocument()
    expect(executedQueries).toHaveLength(0)
  })

  it('runs a CREATE TABLE query with RLS enabled once "Run and enable RLS" is chosen', async () => {
    createDraft({ _tag: 'database' }, 'create table foo (id int)')
    const executedQueries = mockExecutedQueries()

    renderQueryTab()
    const runButton = await screen.findByRole('button', { name: 'Run' })
    await waitFor(() => expect(runButton).toBeEnabled())
    await userEvent.click(runButton)

    expect(await screen.findByText('Potential issue detected')).toBeInTheDocument()
    expect(executedQueries).toHaveLength(0)

    await userEvent.click(screen.getByRole('button', { name: 'Run and enable RLS' }))

    await waitFor(() => expect(executedQueries).toHaveLength(1))
    expect(executedQueries[0]).toContain('create table foo (id int)')
    expect(executedQueries[0]).toContain('ALTER TABLE foo ENABLE ROW LEVEL SECURITY;')
  })

  it('runs the full query from the Run button even when text is selected', async () => {
    createDraft({ _tag: 'database' }, 'select 1;\nselect 2;')
    testContext.selectedText = 'select 2;'

    const executedQueries = mockExecutedQueries()

    renderQueryTab()
    const runButton = await screen.findByRole('button', { name: 'Run' })
    await waitFor(() => expect(runButton).toBeEnabled())
    await userEvent.click(runButton)

    await waitFor(() => expect(executedQueries).toHaveLength(1))
    expect(executedQueries[0]).toContain('select 1')
    expect(executedQueries[0]).toContain('select 2')
  })

  it('runs only the selected text from the Run selected SQL menu item', async () => {
    createDraft({ _tag: 'database' }, 'select 1;\nselect 2;')
    testContext.selectedText = 'select 2;'

    const executedQueries = mockExecutedQueries()

    renderQueryTab()
    const runButton = await screen.findByRole('button', { name: 'Run' })
    await waitFor(() => expect(runButton).toBeEnabled())

    await userEvent.click(screen.getByRole('button', { name: 'More actions' }))
    await userEvent.click(await screen.findByRole('menuitem', { name: 'Run selected SQL' }))

    await waitFor(() => expect(executedQueries).toHaveLength(1))
    expect(executedQueries[0]).toContain('select 2')
    expect(executedQueries[0]).not.toContain('select 1')
  })

  it('drops the stale "Run selected SQL" state once the query panel is hidden and shown again', async () => {
    createDraft({ _tag: 'database' }, 'select 1;\nselect 2;')
    testContext.selectedText = 'select 2;'

    const executedQueries = mockExecutedQueries()

    renderQueryTab()
    await userEvent.click(await screen.findByRole('button', { name: 'More actions' }))
    expect(await screen.findByRole('menuitem', { name: 'Run selected SQL' })).toBeEnabled()
    await userEvent.keyboard('{Escape}')

    // Hiding the query panel unmounts CodeEditor entirely. Simulate the selection being
    // gone by the time it's shown again (a fresh editor instance has no selection yet).
    const hideQueryButton = document.querySelector('.lucide-eye-off')?.closest('button')
    expect(hideQueryButton).toBeInstanceOf(HTMLButtonElement)
    await userEvent.click(hideQueryButton as HTMLButtonElement)
    testContext.selectedText = undefined

    const showQueryButton = document.querySelector('.lucide-eye')?.closest('button')
    expect(showQueryButton).toBeInstanceOf(HTMLButtonElement)
    await userEvent.click(showQueryButton as HTMLButtonElement)

    await userEvent.click(screen.getByRole('button', { name: 'More actions' }))
    expect(await screen.findByRole('menuitem', { name: 'Run selected SQL' })).toHaveAttribute(
      'aria-disabled',
      'true'
    )
    await userEvent.keyboard('{Escape}')

    const runButton = await screen.findByRole('button', { name: 'Run' })
    await userEvent.click(runButton)

    await waitFor(() => expect(executedQueries).toHaveLength(1))
    expect(executedQueries[0]).toContain('select 1')
    expect(executedQueries[0]).toContain('select 2')
  })
})