mirror of
https://github.com/supabase/supabase.git
synced 2026-12-04 07:29:16 +00:00
Part 6 (final) of a 6-PR stack migrating all `next/router` and `next/compat/router` imports in `apps/studio` onto TanStack Router hooks and removing the router shims. | Part | PR | Scope | | --- | --- | --- | | 1 | #54053 | Core navigation helpers, `hooks/**`, `lib/**`, `data/prefetchers/**` | | 2 | #54054 | `components/ui/**` | | 3 | #54055 | `components/layouts/**` and `state/tabs.tsx` | | 4 | #54056 | Database, Branching, Compute, Edge Functions, Explorer, SQL Editor, Storage, Table Editor | | 5 | #54057 | Remaining `components/interfaces/**` and `QueryInsightsTable` router events | | **6** | **#54058 ← this PR** | **Remove `next/router` & `next/compat/router` shims, `_router-events.ts`, Vite aliases, and test mocks** | Now that no files in `apps/studio` (or `packages/**`) import `next/router` or `next/compat/router`, deletes the router shims, `_router-events.ts`, their Vite aliases, and the obsolete router test mocks. `RouteValidationWrapper` is intentionally left for FE-3107. **Removed:** - `apps/studio/compat/next/router.ts`, `apps/studio/compat/next/compat/router.ts`, and `apps/studio/compat/next/_router-events.ts` - `'next/router'` and `'next/compat/router'` entries from `nextShims` in `apps/studio/vite.config.ts` - Global `vi.mock('next/router', ...)` and `vi.mock('next/compat/router', ...)` in `apps/studio/tests/vitestSetup.ts`, `mockNextRouter` in `apps/studio/tests/lib/route-aware-hooks.ts`, and the unused `vi.mock('next/router')` in `tests/features/logs/LogTable.test.tsx` - Obsolete shim tests and test helpers: `compat/next/router.test.ts`, `compat/next/router-navigation.test.tsx`, `tests/config/router.tsx`, and `tests/config/router.test.tsx` **Changed:** - Simplified `createNavigationHandler` in `apps/studio/lib/navigation.ts` (and `navigation.test.ts`) to accept TanStack's `UseNavigateResult<string>` only, dropping the `NextRouter` union branch - Updated `compat/next/link-basepath-navigation.test.tsx` (renamed from `router-basepath-navigation.test.tsx`), `lib/router-url-shape.test.tsx`, and `tests/lib/route-render.test.tsx` to exercise TanStack Router hooks directly instead of the removed router shim - Cleaned up stale `next/router` comments in `compat/next/link.tsx`, `lib/internal-url.ts`, `router.tsx`, and `tests/lib/route-mock.ts` **Simplified:** - Removed `routerMock` setup from about 19 tests that no longer read it. `next-router-mock` stays for now, because the test `FrameworkProvider` still feeds `useParams` from it. - `AGENTS.md` and the `vite.config.ts` error text now say only `next/link` and `next/script` are shimmed - Renamed `compat/next/router-basepath-navigation.test.tsx` to `link-basepath-navigation.test.tsx`, and removed stale router-shim references from comments Addresses [FE-5109](https://linear.app/supabase/issue/FE-5109/replace-the-nextrouter-shims-with-tanstack-router-hooks) ## To test - [ ] Confirm `git grep -E "next/(compat/)?router|_router-events" apps/studio` returns no code or config imports (only the prose guideline in `apps/studio/AGENTS.md`) - [ ] Confirm `pnpm --filter studio run typecheck`, `pnpm --filter studio run lint`, `pnpm --filter studio run build`, Knip, and the full Studio Vitest suite pass - [ ] Spot-check basePath navigation (`next/link` + `navigateToUrl`) and multi-line query param URLs in Studio --------- Co-authored-by: Alaister Young <10985857+alaister@users.noreply.github.com>
330 lines
12 KiB
TypeScript
330 lines
12 KiB
TypeScript
import { QueryClient } from '@tanstack/react-query'
|
|
import { screen, waitFor } from '@testing-library/react'
|
|
import userEvent from '@testing-library/user-event'
|
|
import type { platformComponents } from 'api-types'
|
|
import { FeatureFlagContext, LOCAL_STORAGE_KEYS } from 'common'
|
|
import { http, HttpResponse } from 'msw'
|
|
import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest'
|
|
import { z } from 'zod'
|
|
|
|
import { EdgeFunctionRecentErrors } from './EdgeFunctionRecentErrors'
|
|
import { FeaturePreviewContextProvider } from '@/components/interfaces/App/FeaturePreview/FeaturePreviewContext'
|
|
import { miscKeys } from '@/data/misc/keys'
|
|
import { AiAssistantStateContext, createAiAssistantState } from '@/state/ai-assistant-state'
|
|
import { sidebarManagerState } from '@/state/sidebar-manager-state'
|
|
import { customRender } from '@/tests/lib/custom-render'
|
|
import { addAPIMock, mswServer, type APIErrorBody } from '@/tests/lib/msw'
|
|
|
|
type AnalyticsResponse = platformComponents['schemas']['AnalyticsResponse_Output']
|
|
|
|
vi.unmock('common')
|
|
|
|
const featureFlags = {
|
|
configcat: {
|
|
otelUnifiedLogs: true,
|
|
otelLegacyLogs: true,
|
|
pipelineCreationWizard: false,
|
|
platformWebhooks: false,
|
|
jitDbAccess: false,
|
|
explorer: false,
|
|
storageVersioningPrivateAlpha: false,
|
|
sqlEditorManualSaveForced: false,
|
|
},
|
|
posthog: {},
|
|
hasLoaded: true,
|
|
}
|
|
|
|
const OTEL_ENDPOINT = '/platform/projects/:ref/analytics/endpoints/logs.all.otel'
|
|
|
|
const errorRow = (index: number, logType = 'edge function runtime') => ({
|
|
id: `log-${index}`,
|
|
timestamp: new Date(Date.now() - index * 60_000).toISOString(),
|
|
log_type: logType,
|
|
status: null,
|
|
level: 'error',
|
|
pathname: null,
|
|
event_message: `TypeError: failure ${index}`,
|
|
method: null,
|
|
log_count: null,
|
|
logs: null,
|
|
auth_user: null,
|
|
metadata: { level: 'error' },
|
|
})
|
|
|
|
const mockLogs = ({
|
|
rows,
|
|
totalErrors,
|
|
invocationCount,
|
|
}: {
|
|
rows: ReturnType<typeof errorRow>[]
|
|
totalErrors: number
|
|
invocationCount: number
|
|
}) => {
|
|
const rowListSql: string[] = []
|
|
const countSql: string[] = []
|
|
|
|
addAPIMock({
|
|
method: 'post',
|
|
path: OTEL_ENDPOINT,
|
|
response: async ({ request }) => {
|
|
const { sql } = z.object({ sql: z.string() }).parse(await request.json())
|
|
if (sql.includes('-- unified logs: row list')) {
|
|
rowListSql.push(sql)
|
|
const limit = Number(sql.match(/LIMIT (\d+)$/)?.[1] ?? rows.length)
|
|
return HttpResponse.json<AnalyticsResponse>({ result: rows.slice(0, limit) })
|
|
}
|
|
if (sql.includes('-- unified logs: sidebar facet counts')) {
|
|
countSql.push(sql)
|
|
return HttpResponse.json<AnalyticsResponse>({
|
|
result: [
|
|
{ facet: 'total', value: 'all', count: totalErrors + invocationCount },
|
|
{ facet: 'level', value: 'error', count: totalErrors },
|
|
{ facet: 'log_type', value: 'edge function', count: invocationCount },
|
|
],
|
|
})
|
|
}
|
|
return HttpResponse.json<APIErrorBody>({ message: 'Unexpected SQL' }, { status: 400 })
|
|
},
|
|
})
|
|
|
|
return { rowListSql, countSql }
|
|
}
|
|
|
|
const renderErrors = () => {
|
|
addAPIMock({
|
|
method: 'get',
|
|
path: '/platform/projects/:ref',
|
|
response: {
|
|
cloud_provider: 'AWS',
|
|
db_host: 'db.default.supabase.co',
|
|
high_availability: false,
|
|
id: 1,
|
|
inserted_at: '2026-01-01T00:00:00Z',
|
|
integration_source: null,
|
|
is_branch_enabled: false,
|
|
is_physical_backups_enabled: false,
|
|
name: 'Test project',
|
|
organization_id: 1,
|
|
ref: 'default',
|
|
region: 'us-east-1',
|
|
restUrl: 'https://default.supabase.co',
|
|
status: 'ACTIVE_HEALTHY',
|
|
subscription_id: 'subscription-1',
|
|
updated_at: '2026-01-01T00:00:00Z',
|
|
connectionString: 'postgresql://postgres:password@localhost:5432/postgres',
|
|
},
|
|
})
|
|
const queryClient = new QueryClient({ defaultOptions: { queries: { retry: false } } })
|
|
queryClient.setQueryData(miscKeys.enabledFeaturesOverride(), { disabled_features: [] })
|
|
|
|
const aiState = createAiAssistantState()
|
|
const content = (functionId: string) => (
|
|
<FeatureFlagContext.Provider value={featureFlags}>
|
|
<FeaturePreviewContextProvider>
|
|
<AiAssistantStateContext.Provider value={aiState}>
|
|
<EdgeFunctionRecentErrors
|
|
functionId={functionId}
|
|
functionSlug="hello-world"
|
|
projectRef="default"
|
|
updatedAt={new Date(Date.now() - 60 * 60 * 1000).toISOString()}
|
|
/>
|
|
</AiAssistantStateContext.Provider>
|
|
</FeaturePreviewContextProvider>
|
|
</FeatureFlagContext.Provider>
|
|
)
|
|
const rendered = customRender(content('fn-1'), {
|
|
queryClient,
|
|
initialUrl: '/project/default/functions/hello-world',
|
|
})
|
|
return {
|
|
...rendered,
|
|
renderFunction: (functionId: string) => rendered.rerender(content(functionId)),
|
|
}
|
|
}
|
|
|
|
describe('EdgeFunctionRecentErrors', () => {
|
|
beforeEach(() => {
|
|
localStorage.setItem(LOCAL_STORAGE_KEYS.UI_PREVIEW_UNIFIED_LOGS, 'true')
|
|
})
|
|
|
|
afterEach(() => {
|
|
localStorage.removeItem(LOCAL_STORAGE_KEYS.UI_PREVIEW_UNIFIED_LOGS)
|
|
sidebarManagerState.unregisterSidebar('ai-assistant')
|
|
})
|
|
|
|
it("previews the function's latest errors and links the rest to the Logs tab", async () => {
|
|
const user = userEvent.setup()
|
|
const copy = vi.spyOn(navigator.clipboard, 'writeText')
|
|
const { rowListSql, countSql } = mockLogs({
|
|
rows: [0, 1, 2, 3, 4, 5].map((index) => errorRow(index)),
|
|
totalErrors: 8,
|
|
invocationCount: 20,
|
|
})
|
|
|
|
renderErrors()
|
|
|
|
expect(await screen.findByText('TypeError: failure 0')).toBeInTheDocument()
|
|
expect(screen.getByText('TypeError: failure 4')).toBeInTheDocument()
|
|
|
|
expect(screen.queryByRole('checkbox')).not.toBeInTheDocument()
|
|
expect(screen.queryByText('TypeError: failure 5')).not.toBeInTheDocument()
|
|
expect(await screen.findByRole('button', { name: '+3 more' })).toBeInTheDocument()
|
|
expect(screen.getByRole('button', { name: 'Explain with AI' })).toBeInTheDocument()
|
|
|
|
await user.click(screen.getByRole('button', { name: 'Copy summary' }))
|
|
const summary = copy.mock.calls[0][0]
|
|
expect(summary).toContain('I have 5 Supabase log entries from the **Edge Functions** service')
|
|
expect(summary).toContain('TypeError: failure 4')
|
|
expect(summary).not.toContain('TypeError: failure 5')
|
|
expect(summary).toContain('+3 more matching log entries not included.')
|
|
|
|
expect(rowListSql[0]).toContain(`log_attributes['function_id'] IN ('fn-1')`)
|
|
expect(rowListSql[0]).toContain(`IN ('error')`)
|
|
expect(rowListSql[0]).toMatch(/LIMIT 5$/)
|
|
|
|
expect(countSql).toHaveLength(1)
|
|
expect(countSql[0]).toContain(`log_attributes['function_id'] IN ('fn-1')`)
|
|
expect(countSql[0]).not.toContain(`IN ('error')`)
|
|
})
|
|
|
|
it('opens the clicked error in the Logs tab with the same filters', async () => {
|
|
mockLogs({ rows: [0, 1].map((index) => errorRow(index)), totalErrors: 2, invocationCount: 20 })
|
|
|
|
const { router } = renderErrors()
|
|
|
|
await userEvent.click(await screen.findByText('TypeError: failure 1'))
|
|
|
|
await waitFor(() => expect(router.state.location.search).toMatchObject({ id: 'log-1' }))
|
|
const url = new URL(router.state.location.href, 'http://localhost')
|
|
expect(url.pathname).toBe('/project/default/functions/hello-world/logs')
|
|
expect(url.searchParams.get('filter')).toBe('level:eq:error')
|
|
expect(url.searchParams.get('id')).toBe('log-1')
|
|
expect(url.searchParams.get('date')).toMatch(/^\d+-\d+$/)
|
|
expect(screen.queryByRole('button', { name: /more$/ })).not.toBeInTheDocument()
|
|
})
|
|
|
|
it('opens errors in the pre-unified Invocations or Logs tab when unified logs are off', async () => {
|
|
localStorage.setItem(LOCAL_STORAGE_KEYS.UI_PREVIEW_UNIFIED_LOGS, 'false')
|
|
mockLogs({
|
|
rows: [errorRow(0), errorRow(1, 'edge function')],
|
|
totalErrors: 2,
|
|
invocationCount: 20,
|
|
})
|
|
|
|
const { router } = renderErrors()
|
|
|
|
await userEvent.click(await screen.findByText('TypeError: failure 0'))
|
|
await waitFor(() =>
|
|
expect(router.state.location.pathname).toBe('/project/default/functions/hello-world/logs')
|
|
)
|
|
const runtimeUrl = new URL(router.state.location.href, 'http://localhost')
|
|
expect(runtimeUrl.searchParams.get('log')).toBe('log-0')
|
|
expect(runtimeUrl.searchParams.get('its')).toBeTruthy()
|
|
expect(runtimeUrl.searchParams.get('ite')).toBeTruthy()
|
|
|
|
await userEvent.click(screen.getByText('TypeError: failure 1'))
|
|
await waitFor(() =>
|
|
expect(router.state.location.pathname).toBe(
|
|
'/project/default/functions/hello-world/invocations'
|
|
)
|
|
)
|
|
const invocationUrl = new URL(router.state.location.href, 'http://localhost')
|
|
expect(invocationUrl.searchParams.get('log')).toBe('log-1')
|
|
})
|
|
|
|
it('summarizes invocations since the last deploy when there are no errors', async () => {
|
|
mockLogs({ rows: [], totalErrors: 0, invocationCount: 3 })
|
|
|
|
renderErrors()
|
|
|
|
expect(await screen.findByText('3 invocations')).toBeInTheDocument()
|
|
expect(screen.getByText(/since last deploy and no errors/)).toBeInTheDocument()
|
|
expect(screen.queryByRole('button', { name: 'Copy summary' })).not.toBeInTheDocument()
|
|
expect(screen.queryByRole('button', { name: 'Explain with AI' })).not.toBeInTheDocument()
|
|
})
|
|
|
|
it('shows a retrieval error without offering summaries of unavailable errors', async () => {
|
|
addAPIMock({
|
|
method: 'post',
|
|
path: OTEL_ENDPOINT,
|
|
response: () =>
|
|
HttpResponse.json<APIErrorBody>({ message: 'Function logs unavailable' }, { status: 500 }),
|
|
})
|
|
|
|
renderErrors()
|
|
|
|
expect(await screen.findByText(/Failed to retrieve logs/)).toBeInTheDocument()
|
|
expect(screen.queryByRole('button', { name: 'Copy summary' })).not.toBeInTheDocument()
|
|
expect(screen.queryByRole('button', { name: 'Explain with AI' })).not.toBeInTheDocument()
|
|
expect(screen.queryByText(/since last deploy and no errors/)).not.toBeInTheDocument()
|
|
})
|
|
|
|
it('clears the previous function errors and summaries while another function loads', async () => {
|
|
mockLogs({ rows: [errorRow(0)], totalErrors: 1, invocationCount: 20 })
|
|
const { renderFunction } = renderErrors()
|
|
expect(await screen.findByText('TypeError: failure 0')).toBeInTheDocument()
|
|
|
|
let releaseResponse: () => void = () => {}
|
|
const pendingResponse = new Promise<void>((resolve) => {
|
|
releaseResponse = resolve
|
|
})
|
|
addAPIMock({
|
|
method: 'post',
|
|
path: OTEL_ENDPOINT,
|
|
response: async ({ request }) => {
|
|
const { sql } = z.object({ sql: z.string() }).parse(await request.json())
|
|
if (sql.includes('sidebar facet counts'))
|
|
return HttpResponse.json<AnalyticsResponse>({ result: [] })
|
|
await pendingResponse
|
|
return HttpResponse.json<AnalyticsResponse>({ result: [errorRow(1)] })
|
|
},
|
|
})
|
|
|
|
try {
|
|
renderFunction('fn-2')
|
|
expect(screen.queryByText('TypeError: failure 0')).not.toBeInTheDocument()
|
|
expect(screen.queryByRole('button', { name: 'Copy summary' })).not.toBeInTheDocument()
|
|
expect(screen.queryByRole('button', { name: 'Explain with AI' })).not.toBeInTheDocument()
|
|
} finally {
|
|
releaseResponse()
|
|
}
|
|
expect(await screen.findByText('TypeError: failure 1')).toBeInTheDocument()
|
|
})
|
|
|
|
it('opens the Assistant with the same preview summary copied for external AI tools', async () => {
|
|
const user = userEvent.setup()
|
|
const copy = vi.spyOn(navigator.clipboard, 'writeText')
|
|
mockLogs({ rows: [errorRow(0)], totalErrors: 3, invocationCount: 20 })
|
|
const submittedMessages: Array<{ parts: Array<{ type: string; text?: string }> }> = []
|
|
mswServer.use(
|
|
http.post('/api/ai/sql/generate-v4', async ({ request }) => {
|
|
const { messages } = z
|
|
.object({
|
|
messages: z.array(
|
|
z.object({
|
|
parts: z.array(z.object({ type: z.string(), text: z.string().optional() })),
|
|
})
|
|
),
|
|
})
|
|
.parse(await request.json())
|
|
submittedMessages.push(...messages)
|
|
return new HttpResponse('data: [DONE]\n\n', {
|
|
headers: { 'content-type': 'text/event-stream', 'x-vercel-ai-ui-message-stream': 'v1' },
|
|
})
|
|
})
|
|
)
|
|
sidebarManagerState.registerSidebar('ai-assistant', () => null)
|
|
renderErrors()
|
|
|
|
await screen.findByText('TypeError: failure 0')
|
|
await user.click(screen.getByRole('button', { name: 'Copy summary' }))
|
|
await user.click(screen.getByRole('button', { name: 'Explain with AI' }))
|
|
|
|
expect(sidebarManagerState.activeSidebar?.id).toBe('ai-assistant')
|
|
await waitFor(() => {
|
|
expect(submittedMessages).toHaveLength(1)
|
|
expect(submittedMessages[0].parts).toEqual([{ type: 'text', text: copy.mock.calls[0][0] }])
|
|
})
|
|
})
|
|
})
|