Files
supabase/apps/studio/components/ui/AIAssistantPanel/EdgeFunctionRenderer.test.tsx
Saxon Fletcher bd76d7fc34 feat(studio): wrap assistant Edge Function approval in a Confirm card (#49168)
<img width="1512" height="862" alt="image"
src="https://github.com/user-attachments/assets/79a6d4dc-dcd2-489f-97d7-3ee7a0196b7d"
/>


## 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 / UI refactor.

## What is the current behavior?

Assistant Edge Function approval nests `ConfirmFooter` under the
function block. `addToolApprovalResponse` is wired whenever state is
`approval-requested`, including automatic approvals.

## What is the new behavior?

Introduces a `Confirm` card that owns the frame, with the footer
attached below the body. Edge Function approval uses that card.
Interactive Approve/Deny only runs for manual `approval-requested` parts
(`!approval.isAutomatic`), matching the [AI SDK tool-approvals `useChat`
guidelines](https://ai-sdk.dev/docs/agents/tool-approvals).

SQL still uses `DisplayBlockRenderer` until #49170. `ConfirmFooter` is
inlined into `Confirm` so SQL can keep importing the named footer until
that PR.

## Additional context

Part of stack #49171. Base: `chore/ai-sdk-7` (#49167).

Notebook proposal Confirm wrapping is **not** in this stack — that file
lives on [#49159](https://github.com/supabase/supabase/pull/49159).
Follow up after that stack merges.

## Test plan

- [ ] Deploy-edge-function tool part shows Confirm with Skip / Deploy
- [ ] Existing-function replace warning still requires the second
confirm
- [ ] After approve, footer morphs to loading and buttons disable
- [ ] `Confirm.utils.test.ts` and `EdgeFunctionRenderer.test.tsx` pass

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

* **New Features**
* Added confirmation cards for AI-assisted actions, including approve
and cancel controls.
* Improved handling of manual approval requests for SQL execution,
notebook changes, and Edge Function deployment.
* Added support for customizing report and Edge Function block styling.

* **Bug Fixes**
* Automatic approvals no longer appear as pending manual confirmations.
  * Skipped SQL actions now provide clearer messaging.

* **Tests**
* Expanded coverage for approval states, confirmation controls, and
automatic decisions.
<!-- end of auto-generated comment: release notes by coderabbit.ai -->

---------

Co-authored-by: Cursor <cursoragent@cursor.com>
Co-authored-by: Claude Opus 5 <noreply@anthropic.com>
2026-08-20 11:35:34 +10:00

153 lines
4.2 KiB
TypeScript

import { screen } from '@testing-library/react'
import userEvent from '@testing-library/user-event'
import { type ReactNode } from 'react'
import { beforeEach, describe, expect, it, vi } from 'vitest'
import { EdgeFunctionRenderer } from './EdgeFunctionRenderer'
import { render } from '@/tests/helpers'
const {
mockTrack,
mockUseEdgeFunctionQuery,
mockUseParams,
mockUseProjectSettingsV2Query,
mockUseSelectedOrganizationQuery,
} = vi.hoisted(() => ({
mockTrack: vi.fn(),
mockUseEdgeFunctionQuery: vi.fn(),
mockUseParams: vi.fn(),
mockUseProjectSettingsV2Query: vi.fn(),
mockUseSelectedOrganizationQuery: vi.fn(),
}))
vi.mock('common', async () => {
const actual = await vi.importActual<typeof import('common')>('common')
return {
...actual,
useParams: mockUseParams,
}
})
vi.mock('@/data/config/project-settings-v2-query', () => ({
useProjectSettingsV2Query: mockUseProjectSettingsV2Query,
}))
vi.mock('@/data/edge-functions/edge-function-query', () => ({
useEdgeFunctionQuery: mockUseEdgeFunctionQuery,
}))
vi.mock('@/lib/telemetry/track', () => ({
useTrack: () => mockTrack,
}))
vi.mock('@/hooks/misc/useSelectedOrganization', () => ({
useSelectedOrganizationQuery: mockUseSelectedOrganizationQuery,
}))
vi.mock('../EdgeFunctionBlock/EdgeFunctionBlock', () => ({
EdgeFunctionBlock: ({
showReplaceWarning,
onCancelReplace,
onConfirmReplace,
}: {
showReplaceWarning?: boolean
onCancelReplace?: () => void
onConfirmReplace?: () => void
}) => (
<div>
{showReplaceWarning && (
<div>
<p>An edge function with this name already exists.</p>
<button tabIndex={0} onClick={onCancelReplace}>
Cancel
</button>
<button tabIndex={0} onClick={onConfirmReplace}>
Replace function
</button>
</div>
)}
</div>
),
}))
vi.mock('./Confirm', () => ({
Confirm: ({
children,
confirmLabel,
onConfirm,
}: {
children?: ReactNode
confirmLabel?: string
onConfirm?: () => void
}) => (
<div>
{children}
<button tabIndex={0} onClick={onConfirm}>
{confirmLabel ?? 'Confirm'}
</button>
</div>
),
}))
describe('EdgeFunctionRenderer', () => {
beforeEach(() => {
mockTrack.mockReset()
mockUseEdgeFunctionQuery.mockReset()
mockUseParams.mockReturnValue({ ref: 'project-ref' })
mockUseProjectSettingsV2Query.mockReturnValue({ data: undefined })
mockUseSelectedOrganizationQuery.mockReturnValue({ data: { slug: 'org-slug' } })
})
it('only deploys an existing function from the replace warning confirmation', async () => {
const user = userEvent.setup()
const onApprove = vi.fn()
mockUseEdgeFunctionQuery.mockReturnValue({ data: { slug: 'hello-world' } })
render(
<EdgeFunctionRenderer
label="Deploy Edge Function"
code="Deno.serve(() => new Response('ok'))"
functionName="hello-world"
confirmState="approval-requested"
onApprove={onApprove}
/>
)
await user.click(screen.getByRole('button', { name: 'Deploy' }))
expect(screen.getByText('An edge function with this name already exists.')).toBeInTheDocument()
expect(onApprove).not.toHaveBeenCalled()
await user.click(screen.getByRole('button', { name: 'Deploy' }))
expect(onApprove).not.toHaveBeenCalled()
expect(mockTrack).not.toHaveBeenCalled()
await user.click(screen.getByRole('button', { name: 'Replace function' }))
expect(onApprove).toHaveBeenCalledTimes(1)
expect(mockTrack).toHaveBeenCalledTimes(1)
})
it('deploys immediately when no existing function is found', async () => {
const user = userEvent.setup()
const onApprove = vi.fn()
mockUseEdgeFunctionQuery.mockReturnValue({ data: undefined })
render(
<EdgeFunctionRenderer
label="Deploy Edge Function"
code="Deno.serve(() => new Response('ok'))"
functionName="hello-world"
confirmState="approval-requested"
onApprove={onApprove}
/>
)
await user.click(screen.getByRole('button', { name: 'Deploy' }))
expect(onApprove).toHaveBeenCalledTimes(1)
expect(mockTrack).toHaveBeenCalledTimes(1)
})
})