mirror of
https://github.com/supabase/supabase.git
synced 2026-12-04 07:29:16 +00:00
Part 1 of a 6-PR stack migrating all `next/link` imports in `apps/studio` onto TanStack Router's `Link` (or plain `<a>` for known external URLs) and removing `compat/next/link.tsx`. | Part | PR | Scope | | --- | --- | --- | | **1** | **#54304 ← this PR** | **`data/prefetchers/PrefetchableLink.tsx` and `components/ui/**`** | | 2 | #54305 | `components/layouts/**` and top-level shell components | | 3 | #54306 | Database, TableGridEditor, Storage, SQLEditor, BranchManagement, Branching, Functions, Realtime | | 4 | #54307 | Integrations, Settings, Observability, LogDrains, Issues, Linter, Reports, QueryPerformance, QuerySources, ConfigDrift, DiskManagement, Compute | | 5 | #54308 | Remaining `components/interfaces/**` (Org, Billing, Auth, Support, Home, SignIn, ConnectSheet, etc.) | | 6 | #54309 | Remove `compat/next/link.tsx` shim, Vite alias, and update basePath tests/docs | Migrates `data/prefetchers/PrefetchableLink.tsx` and all shared `components/ui/**` components off `next/link`. **Added:** - `apps/studio/components/ui/DynamicLink.tsx` (with unit tests) — shared `forwardRef` link wrapper for dynamic URL strings that runs `splitInternalUrl` and renders a plain `<a>` for external URLs (or same-origin URLs outside the app's `basePath`) vs TanStack `<Link>` for internal paths **Changed:** - Replaced `next/link` in `apps/studio/data/prefetchers/PrefetchableLink.tsx` and all 30 importing components under `apps/studio/components/ui/**` (`InlineLink`, `CardButton`, `CommandItemLink`, `ObservabilityLink`, `ProductMenuItem`, `ResourceItem`, `UpgradePlanButton`, `ToggleSpendCapButton`, banners, AI assistant panel links, etc.) - Used typed `<Link to="..." params={...} search={...} hash="..." />` for known internal routes, `<DynamicLink href={...} />` for dynamic/untyped URL strings (whether internal with query/hash or external), and plain `<a>` for known external links - Updated `apps/studio/routes/index.tsx` `validateSearch` return type to `{ next?: string }` so `<Link to="/">` compiles without requiring `search={{ next: undefined }}` - Updated `hooks/misc/__tests__/useTableEditorFiltersSort.navigation.test.tsx`, `PolicyTableRow.utils.test.ts`, `SupportAssistantSuccessCard.test.tsx`, and `useOrganizationRestrictions.test.tsx` to render inside a TanStack Router context (`customRender` / `RouterContextProvider`) Addresses [FE-5108](https://linear.app/supabase/issue/FE-5108/replace-the-nextlink-shim-with-tanstack-link) ## To test - [ ] Click internal and external `InlineLink`, `CardButton`, `ProductMenuItem`, and `PrefetchableLink` targets and verify client-side navigation vs external anchors - [ ] Verify banner links (upgrade, resource exhaustion, queue operations) preserve query params and hashes - [ ] Confirm Studio typecheck, lint, and unit tests pass --------- Co-authored-by: Alaister Young <10985857+alaister@users.noreply.github.com>
308 lines
10 KiB
TypeScript
308 lines
10 KiB
TypeScript
import { SupportCategories } from '@supabase/shared-types/out/constants'
|
|
import { act, screen, waitFor } from '@testing-library/react'
|
|
import userEvent from '@testing-library/user-event'
|
|
import { beforeEach, describe, expect, it, vi } from 'vitest'
|
|
|
|
import type { SubmittedSupportRequest } from './SupportForm.state'
|
|
import { NO_PROJECT_MARKER } from './SupportForm.utils'
|
|
import { SupportAssistantSuccessCardContent as SupportAssistantSuccessCard } from '@/components/ui/AIAssistantPanel/SupportAssistantSuccessCardContent'
|
|
import { customRender } from '@/tests/lib/custom-render'
|
|
|
|
const {
|
|
chatInstances,
|
|
chats,
|
|
mockNewChat,
|
|
mockOpenSidebar,
|
|
mockSelectChat,
|
|
mockSyncSupportChatToFront,
|
|
mockTrack,
|
|
} = vi.hoisted(() => ({
|
|
chatInstances: {} as Record<string, MockChat>,
|
|
chats: {} as Record<string, { messages: unknown[]; supportMetadata?: unknown }>,
|
|
mockNewChat: vi.fn(),
|
|
mockOpenSidebar: vi.fn(),
|
|
mockSelectChat: vi.fn(),
|
|
mockSyncSupportChatToFront: vi.fn(),
|
|
mockTrack: vi.fn(),
|
|
}))
|
|
|
|
type MockChat = {
|
|
messages: Array<{ id: string; role: string; parts: Array<{ type: string; text: string }> }>
|
|
'~registerMessagesCallback': ReturnType<typeof vi.fn>
|
|
}
|
|
|
|
vi.mock('streamdown', () => ({
|
|
Streamdown: ({ children }: { children: string }) => (
|
|
<div data-testid="assistant-preview-message">{children}</div>
|
|
),
|
|
}))
|
|
|
|
vi.mock('@/components/layouts/ProjectLayout/LayoutSidebar/LayoutSidebarProvider', () => ({
|
|
SIDEBAR_KEYS: {
|
|
AI_ASSISTANT: 'ai-assistant',
|
|
},
|
|
}))
|
|
|
|
vi.mock('@/state/ai-assistant-state', () => ({
|
|
useAiAssistantStateSnapshot: () => ({
|
|
chatInstances,
|
|
newChat: mockNewChat,
|
|
selectChat: mockSelectChat,
|
|
}),
|
|
useAiAssistantState: () => ({
|
|
chats,
|
|
selectChat: mockSelectChat,
|
|
}),
|
|
}))
|
|
|
|
vi.mock('@/state/ai-chat-front-sync', () => ({
|
|
syncSupportChatToFront: mockSyncSupportChatToFront,
|
|
}))
|
|
|
|
vi.mock('@/state/sidebar-manager-state', () => ({
|
|
useSidebarManagerSnapshot: () => ({
|
|
openSidebar: mockOpenSidebar,
|
|
}),
|
|
}))
|
|
|
|
vi.mock('@/lib/telemetry/track', () => ({
|
|
useTrack: () => mockTrack,
|
|
}))
|
|
|
|
let mockCurrentProjectRef: string | undefined = 'project-1'
|
|
|
|
vi.mock('@/hooks/misc/useSelectedProject', () => ({
|
|
useSelectedProjectQuery: () => ({
|
|
data: mockCurrentProjectRef ? { ref: mockCurrentProjectRef } : undefined,
|
|
}),
|
|
}))
|
|
|
|
const supportRequest: SubmittedSupportRequest = {
|
|
organizationSlug: 'org-1',
|
|
projectRef: 'project-1',
|
|
category: SupportCategories.PROBLEM,
|
|
severity: 'Normal',
|
|
subject: 'API requests fail',
|
|
message: 'Requests fail with 500s',
|
|
affectedServices: 'api',
|
|
library: 'javascript',
|
|
allowSupportAccess: true,
|
|
dashboardLogs: undefined,
|
|
threadRef: 'thread-ref-1',
|
|
frontConversationId: 'front-conversation-1',
|
|
}
|
|
|
|
describe('SupportAssistantSuccessCard', () => {
|
|
let nextChatMessages: MockChat['messages']
|
|
let emitChatMessagesChange: (() => void) | undefined
|
|
|
|
function createMockChat(messages: MockChat['messages'] = []) {
|
|
return {
|
|
messages,
|
|
'~registerMessagesCallback': vi.fn((onStoreChange: () => void) => {
|
|
emitChatMessagesChange = onStoreChange
|
|
return vi.fn()
|
|
}),
|
|
}
|
|
}
|
|
|
|
beforeEach(() => {
|
|
Object.keys(chatInstances).forEach((key) => delete chatInstances[key])
|
|
Object.keys(chats).forEach((key) => delete chats[key])
|
|
mockNewChat.mockReset()
|
|
mockOpenSidebar.mockReset()
|
|
mockSelectChat.mockReset()
|
|
mockSyncSupportChatToFront.mockReset()
|
|
mockTrack.mockReset()
|
|
nextChatMessages = []
|
|
emitChatMessagesChange = undefined
|
|
mockCurrentProjectRef = 'project-1'
|
|
sessionStorage.clear()
|
|
|
|
mockNewChat.mockImplementation(() => {
|
|
chatInstances['chat-1'] = createMockChat(nextChatMessages)
|
|
chats['chat-1'] = { messages: nextChatMessages }
|
|
return 'chat-1'
|
|
})
|
|
})
|
|
|
|
it('creates an assistant chat with the submitted support request', async () => {
|
|
customRender(<SupportAssistantSuccessCard request={supportRequest} />)
|
|
|
|
await waitFor(() => {
|
|
expect(mockNewChat).toHaveBeenCalledTimes(1)
|
|
})
|
|
|
|
expect(mockNewChat).toHaveBeenCalledWith(
|
|
expect.objectContaining({
|
|
name: 'Support request',
|
|
initialMessage: expect.stringContaining('<support>'),
|
|
})
|
|
)
|
|
expect(mockNewChat.mock.calls[0]?.[0].initialMessage).toContain(
|
|
'A support request has already been submitted'
|
|
)
|
|
})
|
|
|
|
it('shows a loading preview before the assistant responds', async () => {
|
|
customRender(<SupportAssistantSuccessCard request={supportRequest} />)
|
|
|
|
expect(await screen.findByRole('heading', { name: 'While you wait' })).toBeInTheDocument()
|
|
expect(screen.getByRole('button', { name: /open assistant response/i })).toBeInTheDocument()
|
|
expect(screen.queryByTestId('assistant-preview-message')).not.toBeInTheDocument()
|
|
})
|
|
|
|
it('renders the full assistant response preview inside the clipped content area', async () => {
|
|
const longResponse = 'A'.repeat(500)
|
|
nextChatMessages = [
|
|
{
|
|
id: 'assistant-message',
|
|
role: 'assistant',
|
|
parts: [{ type: 'text', text: longResponse }],
|
|
},
|
|
]
|
|
|
|
customRender(<SupportAssistantSuccessCard request={supportRequest} />)
|
|
|
|
const preview = await screen.findByTestId('assistant-preview-message')
|
|
expect(preview).toHaveTextContent(longResponse)
|
|
expect(preview.closest('[class*="max-h-48"]')).toHaveClass('overflow-hidden')
|
|
})
|
|
|
|
it('updates the preview when the shared chat receives an assistant message', async () => {
|
|
customRender(<SupportAssistantSuccessCard request={supportRequest} />)
|
|
|
|
await waitFor(() => {
|
|
expect(chatInstances['chat-1']?.['~registerMessagesCallback']).toHaveBeenCalled()
|
|
})
|
|
|
|
act(() => {
|
|
chatInstances['chat-1'].messages = [
|
|
{
|
|
id: 'assistant-message',
|
|
role: 'assistant',
|
|
parts: [{ type: 'text', text: 'Try checking the API logs first.' }],
|
|
},
|
|
]
|
|
emitChatMessagesChange?.()
|
|
})
|
|
|
|
expect(await screen.findByTestId('assistant-preview-message')).toHaveTextContent(
|
|
'Try checking the API logs first.'
|
|
)
|
|
})
|
|
|
|
it('opens the generated assistant chat when the action is clicked', async () => {
|
|
const user = userEvent.setup()
|
|
customRender(<SupportAssistantSuccessCard request={supportRequest} />)
|
|
|
|
const button = await screen.findByRole('button', { name: /open assistant response/i })
|
|
await user.click(button)
|
|
|
|
expect(mockTrack).toHaveBeenCalledWith(
|
|
'support_assistant_follow_up_card_clicked',
|
|
{ ticketCategory: SupportCategories.PROBLEM },
|
|
{
|
|
project: 'project-1',
|
|
organization: 'org-1',
|
|
}
|
|
)
|
|
expect(mockSelectChat).toHaveBeenCalledWith('chat-1')
|
|
expect(mockOpenSidebar).toHaveBeenCalledWith('ai-assistant')
|
|
})
|
|
|
|
it('tags the chat as a support chat and syncs it to Front on first open', async () => {
|
|
const user = userEvent.setup()
|
|
customRender(<SupportAssistantSuccessCard request={supportRequest} />)
|
|
|
|
const button = await screen.findByRole('button', { name: /open assistant response/i })
|
|
await user.click(button)
|
|
|
|
expect(chats['chat-1'].supportMetadata).toMatchObject({
|
|
isSupportChat: true,
|
|
lifecycleStatus: 'bot_active',
|
|
subject: 'API requests fail',
|
|
category: SupportCategories.PROBLEM,
|
|
severity: 'Normal',
|
|
organizationSlug: 'org-1',
|
|
projectRef: 'project-1',
|
|
allowSupportAccess: true,
|
|
threadRef: 'thread-ref-1',
|
|
frontConversationId: 'front-conversation-1',
|
|
lastSyncedMessageCount: 0,
|
|
isSyncing: false,
|
|
isLifecycleSyncing: false,
|
|
})
|
|
|
|
// The initial flush is behind a dynamic import, so it lands asynchronously
|
|
await waitFor(() => {
|
|
expect(mockSyncSupportChatToFront).toHaveBeenCalledWith(
|
|
'chat-1',
|
|
expect.objectContaining({ chats })
|
|
)
|
|
})
|
|
|
|
// A second open must not re-tag the chat or trigger another initial flush
|
|
const taggedMetadata = chats['chat-1'].supportMetadata
|
|
await user.click(button)
|
|
|
|
await waitFor(() => {
|
|
expect(mockSelectChat).toHaveBeenCalledTimes(2)
|
|
})
|
|
expect(chats['chat-1'].supportMetadata).toBe(taggedMetadata)
|
|
expect(mockSyncSupportChatToFront).toHaveBeenCalledTimes(1)
|
|
})
|
|
|
|
it('does not render or create a chat when no project is selected', () => {
|
|
customRender(
|
|
<SupportAssistantSuccessCard
|
|
request={{ ...supportRequest, projectRef: NO_PROJECT_MARKER, organizationSlug: 'org-1' }}
|
|
/>
|
|
)
|
|
|
|
expect(screen.queryByText(/assistant response/i)).not.toBeInTheDocument()
|
|
expect(mockNewChat).not.toHaveBeenCalled()
|
|
})
|
|
|
|
describe('when not already on the ticket project page', () => {
|
|
beforeEach(() => {
|
|
mockCurrentProjectRef = undefined
|
|
})
|
|
|
|
it('renders a handoff link with an opaque token, keeping the ticket content out of the URL', async () => {
|
|
customRender(<SupportAssistantSuccessCard request={supportRequest} />)
|
|
|
|
expect(await screen.findByRole('heading', { name: 'While you wait' })).toBeInTheDocument()
|
|
const link = screen.getByRole('link', { name: /open assistant in project/i })
|
|
expect(link).toHaveAttribute('href', expect.stringContaining('/project/project-1?'))
|
|
|
|
const href = link.getAttribute('href') ?? ''
|
|
const token = new URL(href, 'https://example.com').searchParams.get('assistantHandoff')
|
|
expect(token).toBeTruthy()
|
|
// The message (and the rest of the ticket) must never appear in the URL itself.
|
|
expect(href).not.toContain(encodeURIComponent(supportRequest.message))
|
|
|
|
// The actual ticket content is stashed in sessionStorage instead, keyed by that token.
|
|
expect(sessionStorage.getItem(`assistant-handoff:${token}`)).toBe(
|
|
JSON.stringify(supportRequest)
|
|
)
|
|
expect(mockNewChat).not.toHaveBeenCalled()
|
|
})
|
|
|
|
it('tracks the click and does not touch the sidebar manager directly', async () => {
|
|
const user = userEvent.setup()
|
|
customRender(<SupportAssistantSuccessCard request={supportRequest} />)
|
|
|
|
const link = await screen.findByRole('link', { name: /open assistant in project/i })
|
|
await user.click(link)
|
|
|
|
expect(mockTrack).toHaveBeenCalledWith(
|
|
'support_assistant_follow_up_card_clicked',
|
|
{ ticketCategory: SupportCategories.PROBLEM },
|
|
{ project: 'project-1', organization: 'org-1' }
|
|
)
|
|
expect(mockOpenSidebar).not.toHaveBeenCalled()
|
|
})
|
|
})
|
|
})
|