Files
supabase/apps/studio/components/interfaces/SQLEditor/useSqlEditorAi.test.tsx
Charis 21511042a3 feat(studio): assistant logs context and reports guard (#48514)
## I have read the
[CONTRIBUTING.md](https://github.com/supabase/supabase/blob/master/CONTRIBUTING.md)
file.

YES

## What kind of change does this PR introduce?

Feature — final PR (9/9) of the SQL editor logs-source stack.

**Base branch:** `charislam/sql-editor-inline-ai-clickhouse-dialect` (PR
8). Nothing here is user-visible: entry points stay behind
`sqlEditorLogsSource` + `otelLegacyLogs`, and flag rollout happens after
the whole stack merges.

## What is the current behavior?

- The Assistant has no idea a SQL editor snippet targets the logs
backend. Ask it about a logs snippet and it answers in Postgres, because
the attached query is fenced as ` ```sql ` and nothing tells the model
otherwise.
- Because the `sql` fence is what `MessageMarkdown` treats as runnable
Postgres, an attached ClickHouse query is rendered with a
Run-against-Postgres affordance and branded with `untrustedSql`.
- "Debug with Assistant" on a failed logs query produces a dialect-less
prompt, so both the in-app assistant and the copyable version get
debugged as Postgres.
- A report referencing a `log_sql` snippet runs its ClickHouse SQL
against the user's Postgres database and surfaces the resulting error.

## What is the new behavior?

**Assistant panel.** The "Current Query" chip records which backend the
attached query targets. That reaches the model two ways: each attachment
is fenced with its own dialect (` ```clickhouse ` vs ` ```sql `), and a
`containsLogsSnippets` flag rides on the user message as AI SDK
`metadata`. The server reads the flag off the conversation and prepends
the ClickHouse dialect rules plus the logs schema reference as a
non-cached context message.

Two design points worth calling out in review:

- The flag lives on the **message**, not the request body, so Retry and
the tool-approval continuation reproduce the context a message was
originally asked in — neither of those passes a per-call body.
- It's derived from **what's actually attached**, so detaching the chip
drops the claim rather than leaving the two able to disagree.

The `clickhouse` fence also keeps a logs query out of
`MessageMarkdown`'s `sql` branch, so it's no longer offered as runnable
Postgres or branded with `untrustedSql` — a boundary this stack's
distinct brands exist to prevent crossing.

**Debug flow.** `buildDebugChatArgs` attaches its query with a source
for the same reason, and names the dialect in the prompt text so the
copyable version stands on its own outside the app.

**Reports.** A report only stores a snippet id, so whether it queries
the logs backend is only knowable once the content loads. `ReportBlock`
guards on the fetched type and renders a `LogsSnippetReportBlock`
placeholder instead of executing. Double-guarded: no `sql` for a logs
snippet (so it's out of the query key and `queryFn` short-circuits even
on an explicit `refetch`) and `enabled` excludes it.

**Incidental cleanups.** `buildAssistantContextMessages` extracted out
of `generate-assistant-response`; a schema-access sentinel that was
duplicated as a string literal across two files (and compared against)
replaced with one exported constant; `SqlSnippet` deduplicated to a
single declaration; `resolveSnippetSource` / `isLogsSource` shared
instead of re-implemented per surface.

**Tests.** 4 new/extended suites. Notable cases pinned: a message with
no metadata must validate (`safeValidateUIMessages` applies
`metadataSchema` to *every* message, so a required schema would 400
every existing conversation); only *user* messages count, so a model
reply can't talk the server into a different dialect; a mixed-attachment
message is flagged without overclaiming a single source; and
`ReportBlock` registers no pg-meta mock for the logs cases, so an
unhandled request failing the test *is* the assertion that logs SQL
never reaches Postgres.

Verified: `pnpm typecheck`, `lint:ratchet` (no regression), Prettier,
and the full Studio suite (459 files / 4969 tests).

## Additional context

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

## Summary by CodeRabbit

- **New Features**
- Added support for recognizing log snippets in reports, with clear
guidance to open them in the SQL editor or remove them.
- AI Assistant now understands log snippets and provides
ClickHouse-specific context, formatting, and troubleshooting guidance.
- Snippets retain their source information when shared with the AI
Assistant.

- **Bug Fixes**
- Prevented unsupported log snippets from being executed as regular
database queries.
  - Improved source detection when opening snippets directly from links.

<!-- end of auto-generated comment: release notes by coderabbit.ai -->
2026-08-04 09:02:40 -04:00

267 lines
10 KiB
TypeScript

import { act, waitFor } from '@testing-library/react'
import { http, HttpResponse } from 'msw'
import { afterEach, beforeEach, describe, expect, it } from 'vitest'
import { useSqlEditorDiff, useSqlEditorPrompt } from './hooks'
import type { SqlSnippetSource } from './querySource'
import { DiffType } from './SQLEditor.types'
import { useSqlEditorAi } from './useSqlEditorAi'
import { SIDEBAR_KEYS } from '@/components/layouts/ProjectLayout/LayoutSidebar/LayoutSidebarProvider'
import { API_URL } from '@/lib/constants'
import { sidebarManagerState } from '@/state/sidebar-manager-state'
import { sqlEditorDiffRequestState } from '@/state/sql-editor/sql-editor-diff-request'
import { sqlEditorSessionState } from '@/state/sql-editor/sql-editor-session-state'
import { mswServer } from '@/tests/lib/msw'
import {
createInMemoryEditor,
renderSqlEditorHook,
resetSqlEditorStores,
seedSnippet,
setupSqlEditorMocks,
} from '@/tests/lib/sql-editor-test-utils'
const SNIPPET_ID = 'ai-snippet'
type HarnessProps = { editorMountCount?: number; sqlSource?: SqlSnippetSource }
/**
* Composes the diff + prompt state hooks the AI hook depends on (production
* wires these together in `SQLEditorControllers`), so tests drive the real
* accept/discard/drain flows end to end.
*/
function useAiHarness({ editorMountCount = 1, sqlSource = 'database' }: HarnessProps = {}) {
const diff = useSqlEditorDiff()
const prompt = useSqlEditorPrompt()
const ai = useSqlEditorAi({ id: SNIPPET_ID, editorMountCount, diff, prompt, sqlSource })
return { ai, diff, prompt }
}
beforeEach(() => {
resetSqlEditorStores()
setupSqlEditorMocks()
})
afterEach(() => {
resetSqlEditorStores()
})
describe('useSqlEditorAi — diff-request drain', () => {
it('copies the requested SQL straight into an empty editor and consumes the request', async () => {
sqlEditorDiffRequestState.requestDiff('select 100;', DiffType.Modification)
const inMemoryEditor = createInMemoryEditor('')
const { result } = renderSqlEditorHook(useAiHarness, { inMemoryEditor })
await waitFor(() => expect(inMemoryEditor.editor.getValue()).toBe('select 100;'))
// One-shot: the request is drained so it can't re-apply to a later editor.
expect(sqlEditorDiffRequestState.pending).toBeUndefined()
expect(result.current.diff.isDiffOpen).toBe(false)
})
it('opens a diff between existing and requested SQL when the editor is non-empty', async () => {
sqlEditorDiffRequestState.requestDiff('select 42;', DiffType.Modification)
const inMemoryEditor = createInMemoryEditor('select 1;')
const { result } = renderSqlEditorHook(useAiHarness, { inMemoryEditor })
await waitFor(() => expect(result.current.diff.isDiffOpen).toBe(true))
// The diff-sync effect pushes the resolved diff into the (in-memory) diff editor.
expect(inMemoryEditor.getDiffOriginal()).toBe('select 1;')
expect(inMemoryEditor.getDiffModified()).toBe('select 42;')
expect(sqlEditorDiffRequestState.pending).toBeUndefined()
// The editor's own contents are untouched until the diff is accepted.
expect(inMemoryEditor.editor.getValue()).toBe('select 1;')
})
it('drains a pending request exactly once across editor remounts', async () => {
sqlEditorDiffRequestState.requestDiff('select 100;', DiffType.Modification)
const inMemoryEditor = createInMemoryEditor('')
const { rerender } = renderSqlEditorHook(useAiHarness, {
inMemoryEditor,
initialProps: { editorMountCount: 1 },
})
await waitFor(() => expect(inMemoryEditor.editor.getValue()).toBe('select 100;'))
// Simulate a fresh editor mount: the consumed request must not re-apply.
inMemoryEditor.setValue('edited by user')
rerender({ editorMountCount: 2 })
await new Promise((r) => setTimeout(r, 20))
expect(inMemoryEditor.editor.getValue()).toBe('edited by user')
})
})
describe('useSqlEditorAi — accept / discard diff', () => {
async function openModificationDiff() {
sqlEditorDiffRequestState.requestDiff('select 42;', DiffType.Modification)
const inMemoryEditor = createInMemoryEditor('select 1;')
const utils = renderSqlEditorHook(useAiHarness, { inMemoryEditor })
await waitFor(() => expect(utils.result.current.diff.isDiffOpen).toBe(true))
return { ...utils, inMemoryEditor }
}
it('accepting a modification writes the diff result back into the editor and closes the diff', async () => {
const { result, inMemoryEditor } = await openModificationDiff()
await act(async () => {
await result.current.ai.acceptAiHandler()
})
await waitFor(() => expect(result.current.diff.isDiffOpen).toBe(false))
expect(inMemoryEditor.editor.getValue()).toBe('select 42;')
})
it('discarding a diff leaves the editor untouched and closes the diff', async () => {
const { result, inMemoryEditor } = await openModificationDiff()
await act(async () => {
result.current.ai.discardAiHandler()
})
await waitFor(() => expect(result.current.diff.isDiffOpen).toBe(false))
expect(inMemoryEditor.editor.getValue()).toBe('select 1;')
})
})
describe('useSqlEditorAi — completion dialect', () => {
type CompletionRequestBody = {
dialect?: string
intent?: string
completionMetadata: {
prompt: string
selection: string
textBeforeCursor: string
textAfterCursor: string
availableKeys?: string[]
}
}
/** Replaces the default completion mock so we can read what was posted. */
function captureCompletionRequests(response: string) {
const bodies: CompletionRequestBody[] = []
mswServer.use(
http.post(`${API_URL}/ai/code/complete`, async ({ request }) => {
bodies.push((await request.json()) as CompletionRequestBody)
return HttpResponse.json(response)
})
)
return bodies
}
const context = {
beforeSelection: "select timestamp from logs where source = 'edge_logs'\n",
selection: 'limit 5',
afterSelection: '',
}
it('posts the clickhouse dialect with the raw instruction and cursor context', async () => {
const bodies = captureCompletionRequests('limit 10')
const { result } = renderSqlEditorHook(useAiHarness, {
initialProps: { sqlSource: 'logs' },
})
await act(async () => {
await result.current.ai.handlePrompt('only keep 5xx responses', context)
})
expect(bodies).toHaveLength(1)
expect(bodies[0].dialect).toBe('clickhouse')
// The route assembles the schema section and the selection-wrapped code around
// the instruction, so the client posts the instruction verbatim — the same
// shape as the Postgres path — and never hand-builds prompt text.
expect(bodies[0].completionMetadata.prompt).toBe('only keep 5xx responses')
expect(bodies[0].completionMetadata.selection).toBe('limit 5')
expect(bodies[0].completionMetadata.textBeforeCursor).toBe(context.beforeSelection)
// An inline edit is not a rewrite.
expect(bodies[0].intent).toBeUndefined()
})
it('posts the postgres dialect with the raw instruction for a database snippet', async () => {
const bodies = captureCompletionRequests('limit 10')
const { result } = renderSqlEditorHook(useAiHarness, {
initialProps: { sqlSource: 'database' },
})
await act(async () => {
await result.current.ai.handlePrompt('bump the limit', context)
})
expect(bodies).toHaveLength(1)
expect(bodies[0].dialect).toBe('postgres')
expect(bodies[0].completionMetadata.prompt).toBe('bump the limit')
})
it('strips code fences and leaves clickhouse output unformatted', async () => {
captureCompletionRequests('```sql\nlimit 10\n```')
const { result } = renderSqlEditorHook(useAiHarness, {
initialProps: { sqlSource: 'logs' },
})
await act(async () => {
await result.current.ai.handlePrompt('bump the limit', context)
})
await waitFor(() => expect(result.current.diff.isDiffOpen).toBe(true))
// sql-formatter is Postgres-only, so the ClickHouse diff is the reassembled
// query verbatim — fences stripped, nothing else touched.
expect(result.current.diff.sourceSqlDiff).toEqual({
original: `${context.beforeSelection}limit 5`,
modified: `${context.beforeSelection}limit 10`,
})
})
it('still formats database output through sql-formatter', async () => {
captureCompletionRequests('limit 10')
const { result } = renderSqlEditorHook(useAiHarness, {
initialProps: { sqlSource: 'database' },
})
await act(async () => {
await result.current.ai.handlePrompt('bump the limit', context)
})
await waitFor(() => expect(result.current.diff.isDiffOpen).toBe(true))
expect(result.current.diff.sourceSqlDiff?.modified).not.toBe(
`${context.beforeSelection}limit 10`
)
})
})
describe('useSqlEditorAi — debug', () => {
it('onDebug opens the assistant sidebar and starts a debug chat from the failing snippet', async () => {
seedSnippet({ id: SNIPPET_ID, name: 'Broken query', sql: 'selct 1;' })
sqlEditorSessionState.addResultError(SNIPPET_ID, { message: 'syntax error at or near "selct"' })
const { result, aiAssistantState } = renderSqlEditorHook(useAiHarness)
await act(async () => {
await result.current.ai.onDebug()
})
// No sidebar is registered in the test tree, so opening queues a pending open.
expect(sidebarManagerState.pendingSidebarOpen).toBe(SIDEBAR_KEYS.AI_ASSISTANT)
const activeChat = aiAssistantState.chats[aiAssistantState.activeChatId ?? '']
expect(activeChat?.name).toBe('Debug SQL snippet')
// Attached with its source, which is what carries sqlSource onto the message the
// user submits from the prefilled composer.
expect(aiAssistantState.sqlSnippets).toEqual([
{ label: 'Current Query', content: 'selct 1;', source: 'database' },
])
expect(aiAssistantState.initialInput).toContain('syntax error at or near "selct"')
})
it('buildDebugPrompt embeds the snippet SQL and its error message', async () => {
seedSnippet({ id: SNIPPET_ID, name: 'Broken query', sql: 'selct 1;' })
sqlEditorSessionState.addResultError(SNIPPET_ID, { message: 'boom' })
const { result } = renderSqlEditorHook(useAiHarness)
const promptText = result.current.ai.buildDebugPrompt()
expect(promptText).toContain('boom')
expect(promptText).toContain('selct 1;')
})
})