Files
Alaister YoungandAlaister Young 477e2c136f [FE-5109] (part 6) chore(studio): remove next/router shims, aliases, and test mocks (#54058)
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>
2026-12-01 15:04:27 +08:00

638 lines
22 KiB
TypeScript

import { QueryClient } from '@tanstack/react-query'
import { act, fireEvent, screen, waitFor } from '@testing-library/react'
import userEvent from '@testing-library/user-event'
import { platformComponents as components } from 'api-types'
import { mockAnimationsApi } from 'jsdom-testing-mocks'
import { http, HttpResponse } from 'msw'
import { Table, TableBody } from 'ui'
import { describe, expect, test, vi } from 'vitest'
import { DestinationRow as DestinationRowComponent } from './DestinationRow'
import type { SlotLagMetrics } from './ReplicationPipelineStatus/ReplicationPipelineStatus.types'
import type { DestinationWireResponse } from '@/data/replication/api-types'
import { API_URL } from '@/lib/constants'
import { PipelineRequestStatusProvider } from '@/state/replication-pipeline-request-status'
import { customRender } from '@/tests/lib/custom-render'
import { addAPIMock, mswServer, type APIErrorBody } from '@/tests/lib/msw'
type ReplicationPipelinesResponse = components['schemas']['PipelinesResponse_Output']
type ReplicationDestinationResponse = DestinationWireResponse<
components['schemas']['DestinationResponse_Output']
>
type ReplicationSourcesResponse = components['schemas']['SourcesResponse_Output']
type ReplicationPipelineStatusResponse = components['schemas']['PipelineStatusResponse_Output']
type ReplicationPipelineReplicationStatusResponse =
components['schemas']['PipelineReplicationStatusResponse_Output']
type ReplicationPipelineVersionResponse = components['schemas']['PipelineVersionResponse_Output']
// Tooltip/Popover descendants use Web Animations
mockAnimationsApi()
const DestinationRow = (props: { destinationId: number }) => (
<PipelineRequestStatusProvider>
<DestinationRowComponent {...props} />
</PipelineRequestStatusProvider>
)
const DESTINATION_ID = 1
const PIPELINE_ID = '42'
const addSourcesMock = () =>
addAPIMock({
method: 'get',
path: '/platform/replication/:ref/sources',
response: () =>
HttpResponse.json<ReplicationSourcesResponse>({
sources: [
{
tenant_id: 't',
id: 1,
name: 'default',
config: { host: 'db.internal', port: 5432, name: 'main-db', username: 'etl_user' },
},
],
}),
})
const addDestinationMock = (config?: ReplicationDestinationResponse['config']) =>
addAPIMock({
method: 'get',
path: '/platform/replication/:ref/destinations/:destination_id',
response: () =>
HttpResponse.json<ReplicationDestinationResponse>({
tenant_id: 't',
id: DESTINATION_ID,
name: 'My BigQuery Destination',
config: config ?? {
big_query: {
project_id: 'gcp-proj',
dataset_id: 'analytics',
connection_pool_size: 5,
},
},
}),
})
const addPipelinesMock = (waitForResponse?: Promise<void>) =>
addAPIMock({
method: 'get',
path: '/platform/replication/:ref/pipelines',
response: async () => {
await waitForResponse
return HttpResponse.json<ReplicationPipelinesResponse>({
pipelines: [
{
id: PIPELINE_ID,
tenant_id: 't',
cluster_id: 'c',
source_id: 1,
source_name: 'main-db',
destination_id: DESTINATION_ID,
destination_name: 'My BigQuery Destination',
replicator_id: 9001,
config: {
publication_name: 'supabase_realtime',
table_sync_copy: { type: 'include_all_tables' },
},
},
],
})
},
})
const addPipelineStatusMock = (statusName: ReplicationPipelineStatusResponse['status']['name']) =>
addAPIMock({
method: 'get',
path: '/platform/replication/:ref/pipelines/:pipeline_id/status',
response: () =>
HttpResponse.json<ReplicationPipelineStatusResponse>({
pipeline_id: PIPELINE_ID,
status: { name: statusName },
}),
})
const addReplicationStatusMock = (
confirmedFlushLsnBytes: number,
tableStatuses: ReplicationPipelineReplicationStatusResponse['table_statuses'] = []
) =>
addAPIMock({
method: 'get',
path: '/platform/replication/:ref/pipelines/:pipeline_id/replication-status',
response: () =>
HttpResponse.json<ReplicationPipelineReplicationStatusResponse>({
pipeline_id: PIPELINE_ID,
apply_lag: {
active: true,
wal_status: 'reserved',
restart_lsn_bytes: 0,
confirmed_flush_lsn_bytes: confirmedFlushLsnBytes,
safe_wal_size_bytes: null,
},
table_statuses: tableStatuses,
}),
})
const addVersionMock = (hasUpdate = false) =>
addAPIMock({
method: 'get',
path: '/platform/replication/:ref/pipelines/:pipeline_id/version',
response: () =>
HttpResponse.json<ReplicationPipelineVersionResponse>({
pipeline_id: PIPELINE_ID,
version: { id: 1, name: 'v0.3.0' },
...(hasUpdate ? { new_version: { id: 2, name: 'v0.4.0' } } : {}),
}),
})
describe('DestinationRow', () => {
const addAllMocks = () => {
addSourcesMock()
addDestinationMock()
addPipelinesMock()
addPipelineStatusMock('started')
addReplicationStatusMock(0)
addVersionMock()
}
test.each(['started', 'stopped', 'failed'] as const)(
'only offers stopping and deletion for an unknown destination when %s',
async (status) => {
addAllMocks()
addDestinationMock({ unknown: {} })
addPipelineStatusMock(status)
addVersionMock(true)
customRender(<DestinationRow destinationId={DESTINATION_ID} />)
expect(await screen.findByRole('img', { name: 'Unknown destination' })).toBeVisible()
await userEvent.click(await screen.findByRole('button', { name: 'Pipeline options' }))
expect(screen.getAllByRole('menuitem').map((item) => item.textContent)).toEqual([
'View details',
...(status === 'stopped' ? [] : ['Stop pipeline']),
'Delete pipeline',
])
await userEvent.click(screen.getByRole('menuitem', { name: 'Delete pipeline' }))
expect(await screen.findByPlaceholderText('Type the pipeline name')).toBeVisible()
}
)
test.each(['started', 'failed'] as const)(
'stops an unknown destination pipeline when %s',
async (status) => {
addAllMocks()
addDestinationMock({ unknown: {} })
addPipelineStatusMock(status)
addAPIMock({
method: 'post',
path: '/platform/replication/:ref/pipelines/:pipeline_id/stop',
response: () => {
addPipelineStatusMock('stopped')
return HttpResponse.json<Record<string, never>>({}, { status: 202 })
},
})
customRender(<DestinationRow destinationId={DESTINATION_ID} />)
await screen.findByRole('img', { name: 'Unknown destination' })
await screen.findByText(status === 'started' ? 'Running' : 'Failed')
await userEvent.click(screen.getByRole('button', { name: 'Pipeline options' }))
await userEvent.click(screen.getByRole('menuitem', { name: 'Stop pipeline' }))
expect(await screen.findByText('Stopped')).toBeVisible()
}
)
test('shows the destination before its pipeline details load', async () => {
let finishPipelineRequest = () => {}
const pendingPipeline = new Promise<void>((resolve) => {
finishPipelineRequest = resolve
})
addSourcesMock()
addDestinationMock()
addPipelinesMock(pendingPipeline)
addPipelineStatusMock('started')
addReplicationStatusMock(0)
addVersionMock()
customRender(
<Table>
<TableBody>
<DestinationRow destinationId={DESTINATION_ID} />
</TableBody>
</Table>
)
const row = (await screen.findByText('My BigQuery Destination')).closest('tr')
expect(row).toBeInTheDocument()
expect(row).not.toHaveAttribute('tabindex')
expect(screen.queryByRole('button', { name: 'Pipeline options' })).not.toBeInTheDocument()
await act(async () => finishPipelineRequest())
expect(await screen.findByText('supabase_realtime')).toBeInTheDocument()
expect(row).toHaveAttribute('tabindex', '0')
expect(screen.getByRole('button', { name: 'Pipeline options' })).toBeInTheDocument()
})
test('waits for asynchronous shutdown before deleting the pipeline', async () => {
addAllMocks()
let isStopping = false
let completeShutdown: () => void = () => {}
const shutdown = new Promise<void>((resolve) => {
completeShutdown = resolve
})
const shutdownStatusRequested = vi.fn()
const deleted = vi.fn()
addAPIMock({
method: 'post',
path: '/platform/replication/:ref/pipelines/:pipeline_id/stop',
response: () => {
isStopping = true
return HttpResponse.json<Record<string, never>>({}, { status: 202 })
},
})
addAPIMock({
method: 'get',
path: '/platform/replication/:ref/pipelines/:pipeline_id/status',
response: async () => {
if (isStopping) {
shutdownStatusRequested()
await shutdown
}
return HttpResponse.json<ReplicationPipelineStatusResponse>({
pipeline_id: PIPELINE_ID,
status: { name: isStopping ? 'stopped' : 'started' },
})
},
})
addAPIMock({
method: 'delete',
path: '/platform/replication/:ref/destinations-pipelines/:destination_id/:pipeline_id',
response: () => {
deleted()
return HttpResponse.json<components['schemas']['DeleteDestinationPipelineResponse_Output']>(
{ destination_deleted: true, destination_id: DESTINATION_ID, pipeline_id: PIPELINE_ID }
)
},
})
customRender(<DestinationRow destinationId={DESTINATION_ID} />)
await screen.findByText('supabase_realtime')
await userEvent.click(screen.getByRole('button', { name: 'Pipeline options' }))
await userEvent.click(screen.getByRole('menuitem', { name: 'Delete pipeline' }))
await userEvent.click(screen.getByPlaceholderText('Type the pipeline name'))
await userEvent.paste('My BigQuery Destination')
await waitFor(() =>
expect(screen.getByRole('button', { name: 'Delete pipeline' })).toBeEnabled()
)
// jsdom does not reliably submit portalled forms through button activation.
fireEvent.submit(screen.getByRole('dialog').querySelector('form')!)
await waitFor(() => expect(shutdownStatusRequested).toHaveBeenCalledOnce())
expect(deleted).not.toHaveBeenCalled()
expect(screen.getByRole('button', { name: 'Deleting…' })).toBeDisabled()
await act(async () => {
completeShutdown()
})
await waitFor(() => expect(deleted).toHaveBeenCalledOnce())
await waitFor(() =>
expect(screen.queryByRole('button', { name: 'Deleting…' })).not.toBeInTheDocument()
)
})
test('keeps deletion retryable when shutdown status cannot be verified', async () => {
addAllMocks()
let isStopping = false
const deleted = vi.fn()
addAPIMock({
method: 'post',
path: '/platform/replication/:ref/pipelines/:pipeline_id/stop',
response: () => {
isStopping = true
return HttpResponse.json<Record<string, never>>({}, { status: 202 })
},
})
addAPIMock({
method: 'get',
path: '/platform/replication/:ref/pipelines/:pipeline_id/status',
response: () => {
if (isStopping)
return HttpResponse.json<APIErrorBody>({ message: 'Status unavailable' }, { status: 503 })
return HttpResponse.json<ReplicationPipelineStatusResponse>({
pipeline_id: PIPELINE_ID,
status: { name: 'started' },
})
},
})
addAPIMock({
method: 'delete',
path: '/platform/replication/:ref/destinations-pipelines/:destination_id/:pipeline_id',
response: () => {
deleted()
return HttpResponse.json<components['schemas']['DeleteDestinationPipelineResponse_Output']>(
{ destination_deleted: true, destination_id: DESTINATION_ID, pipeline_id: PIPELINE_ID }
)
},
})
const queryClient = new QueryClient()
customRender(<DestinationRow destinationId={DESTINATION_ID} />, { queryClient })
await screen.findByText('supabase_realtime')
await userEvent.click(screen.getByRole('button', { name: 'Pipeline options' }))
await userEvent.click(screen.getByRole('menuitem', { name: 'Delete pipeline' }))
await userEvent.click(screen.getByPlaceholderText('Type the pipeline name'))
await userEvent.paste('My BigQuery Destination')
await waitFor(() =>
expect(screen.getByRole('button', { name: 'Delete pipeline' })).toBeEnabled()
)
// jsdom does not reliably submit portalled forms through button activation.
fireEvent.submit(screen.getByRole('dialog').querySelector('form')!)
await waitFor(() =>
expect(
queryClient
.getMutationCache()
.getAll()
.some((mutation) => mutation.state.status === 'error')
).toBe(true)
)
expect(deleted).not.toHaveBeenCalled()
expect(screen.getByRole('button', { name: 'Delete pipeline' })).toBeEnabled()
})
test('navigates to the pipeline when the row is clicked', async () => {
addAllMocks()
const { router } = customRender(<DestinationRow destinationId={DESTINATION_ID} />, {
initialUrl: '/project/default/database/pipelines',
})
const row = (await screen.findByText('supabase_realtime')).closest('tr')
expect(row).not.toBeNull()
await userEvent.click(row!)
await waitFor(() =>
expect(router.state.location.href).toBe(`/project/default/database/pipelines/${PIPELINE_ID}`)
)
})
test('does not navigate when the row overflow menu is opened', async () => {
addAllMocks()
const { router } = customRender(<DestinationRow destinationId={DESTINATION_ID} />, {
initialUrl: '/project/default/database/pipelines',
})
await screen.findByText('supabase_realtime')
await userEvent.click(screen.getByRole('button', { name: 'Pipeline options' }))
expect(router.state.location.href).toBe('/project/default/database/pipelines')
})
test('shows "Caught up" when confirmed_flush_lsn_bytes is 0', async () => {
addAllMocks()
customRender(<DestinationRow destinationId={DESTINATION_ID} />)
const lag = await screen.findByText('Caught up')
expect(lag.closest('[aria-live="polite"]')).toHaveAttribute('aria-atomic', 'true')
})
test('shows formatted lag value when confirmed_flush_lsn_bytes is non-zero', async () => {
addSourcesMock()
addDestinationMock()
addPipelinesMock()
addPipelineStatusMock('started')
addReplicationStatusMock(2048)
addVersionMock()
customRender(<DestinationRow destinationId={DESTINATION_ID} />)
expect(await screen.findByText('2 KB')).toBeInTheDocument()
})
test('shows unavailable lag instead of caught up when the distance is null', async () => {
addSourcesMock()
addDestinationMock()
addPipelinesMock()
addPipelineStatusMock('started')
// Exercise an unavailable measurement outside the current production OpenAPI contract.
type UnavailableLagResponse = Omit<
ReplicationPipelineReplicationStatusResponse,
'apply_lag'
> & {
apply_lag: SlotLagMetrics
}
mswServer.use(
http.get(
`${API_URL}/platform/replication/:ref/pipelines/:pipeline_id/replication-status`,
() =>
HttpResponse.json<UnavailableLagResponse>({
pipeline_id: PIPELINE_ID,
apply_lag: {
active: true,
wal_status: 'reserved',
restart_lsn_bytes: 0,
confirmed_flush_lsn_bytes: null,
safe_wal_size_bytes: null,
},
table_statuses: [],
})
)
)
addVersionMock()
customRender(<DestinationRow destinationId={DESTINATION_ID} />)
expect(await screen.findByText('Unavailable')).toBeInTheDocument()
expect(screen.queryByText('Caught up')).not.toBeInTheDocument()
})
test('shows initial sync while tables are copying', async () => {
addSourcesMock()
addDestinationMock()
addPipelinesMock()
addPipelineStatusMock('started')
addReplicationStatusMock(0, [
{
id: 1,
schema: 'public',
name: 'orders',
table_id: 1,
table_name: 'public.orders',
state: { name: 'copying_table' },
},
])
addVersionMock()
customRender(<DestinationRow destinationId={DESTINATION_ID} />)
expect(await screen.findByText('Initial sync')).toBeInTheDocument()
})
test('shows initial sync when apply lag is unavailable', async () => {
addSourcesMock()
addDestinationMock()
addPipelinesMock()
addPipelineStatusMock('started')
addAPIMock({
method: 'get',
path: '/platform/replication/:ref/pipelines/:pipeline_id/replication-status',
response: () =>
HttpResponse.json<ReplicationPipelineReplicationStatusResponse>({
pipeline_id: PIPELINE_ID,
apply_lag: null,
table_statuses: [
{
id: 1,
schema: 'public',
name: 'orders',
table_id: 1,
table_name: 'public.orders',
state: { name: 'queued' },
},
],
}),
})
addVersionMock()
customRender(<DestinationRow destinationId={DESTINATION_ID} />)
expect(await screen.findByText('Initial sync')).toBeInTheDocument()
expect(screen.queryByText('Lag unavailable')).not.toBeInTheDocument()
})
test('announces lag loading and replaces it with the resolved value', async () => {
let resolveReplicationStatus: (
response: ReplicationPipelineReplicationStatusResponse
) => void = () => {}
const replicationStatusResponse = new Promise<ReplicationPipelineReplicationStatusResponse>(
(resolve) => {
resolveReplicationStatus = resolve
}
)
addSourcesMock()
addDestinationMock()
addPipelinesMock()
addPipelineStatusMock('started')
addAPIMock({
method: 'get',
path: '/platform/replication/:ref/pipelines/:pipeline_id/replication-status',
response: async () =>
HttpResponse.json<ReplicationPipelineReplicationStatusResponse>(
await replicationStatusResponse
),
})
addVersionMock()
customRender(<DestinationRow destinationId={DESTINATION_ID} />)
expect(await screen.findByText('Loading lag')).toBeInTheDocument()
resolveReplicationStatus({
pipeline_id: PIPELINE_ID,
apply_lag: {
active: true,
wal_status: 'reserved',
restart_lsn_bytes: 0,
confirmed_flush_lsn_bytes: 0,
safe_wal_size_bytes: null,
},
table_statuses: [],
})
expect(await screen.findByText('Caught up')).toBeInTheDocument()
expect(screen.queryByText('Loading lag')).not.toBeInTheDocument()
})
test('announces when lag is unavailable', async () => {
addSourcesMock()
addDestinationMock()
addPipelinesMock()
addPipelineStatusMock('started')
addAPIMock({
method: 'get',
path: '/platform/replication/:ref/pipelines/:pipeline_id/replication-status',
response: () =>
HttpResponse.json<APIErrorBody>({ message: 'Pipeline unavailable' }, { status: 500 }),
})
addVersionMock()
customRender(<DestinationRow destinationId={DESTINATION_ID} />)
expect(await screen.findByText('Lag unavailable')).toBeInTheDocument()
})
test.each([
['started', 'Running'],
['failed', 'Failed'],
['stopped', 'Stopped'],
] as const)('shows the %s pipeline status as %s', async (statusName, label) => {
addAllMocks()
addPipelineStatusMock(statusName)
customRender(<DestinationRow destinationId={DESTINATION_ID} />)
const status = await screen.findByText(label)
expect(status.closest('[aria-live="polite"]')).toHaveAttribute('aria-atomic', 'true')
})
test('shows table errors in the subtext and on the logo when tables have replication errors', async () => {
addSourcesMock()
addDestinationMock()
addPipelinesMock()
addPipelineStatusMock('started')
addReplicationStatusMock(0, [
{
id: 1,
schema: 'public',
name: 'orders',
table_id: 1,
table_name: 'public.orders',
state: { name: 'error', reason: 'table not found', retry_policy: { policy: 'no_retry' } },
},
])
addVersionMock()
const { container } = customRender(<DestinationRow destinationId={DESTINATION_ID} />)
expect(await screen.findByText('1 table error')).toBeInTheDocument()
// StatusIcon destructive on the logo corner (packages/ui/src/components/StatusIcon.tsx)
expect(container.querySelector('.bg-destructive-600')).toBeInTheDocument()
})
test('suppresses table error signals when pipeline is stopped even with table errors', async () => {
addSourcesMock()
addDestinationMock()
addPipelinesMock()
addPipelineStatusMock('stopped')
addReplicationStatusMock(0, [
{
id: 1,
schema: 'public',
name: 'orders',
table_id: 1,
table_name: 'public.orders',
state: { name: 'error', reason: 'table not found', retry_policy: { policy: 'no_retry' } },
},
])
addVersionMock()
const { container } = customRender(<DestinationRow destinationId={DESTINATION_ID} />)
await screen.findByText('Stopped')
expect(screen.queryByText(/table error/)).not.toBeInTheDocument()
expect(container.querySelector('.bg-destructive-600')).not.toBeInTheDocument()
})
test('shows error when the pipelines API fails', async () => {
addSourcesMock()
addDestinationMock()
addAPIMock({
method: 'get',
path: '/platform/replication/:ref/pipelines',
response: () =>
HttpResponse.json<APIErrorBody>({ message: 'Internal server error' }, { status: 500 }),
})
customRender(<DestinationRow destinationId={DESTINATION_ID} />, {
queryClient: new QueryClient({ defaultOptions: { queries: { retryDelay: 0 } } }),
})
expect(await screen.findByText('Failed to retrieve pipeline information')).toBeInTheDocument()
})
})