import { QueryClient } from '@tanstack/react-query' import { act, fireEvent, screen, waitFor, within } from '@testing-library/react' import type { components } from 'api-types' import { mockAnimationsApi } from 'jsdom-testing-mocks' import { delay, HttpResponse } from 'msw' import { NuqsAdapter } from 'nuqs/adapters/react' import { beforeEach, describe, expect, it, vi } from 'vitest' import { PipelineCreationFlow as CreationFlow } from './PipelineCreationFlow' import type { ReplicationCostEstimateData } from '@/data/replication/cost-estimate-query' import { replicationKeys } from '@/data/replication/keys' import { DOCS_URL } from '@/lib/constants' import { PipelineRequestStatusProvider } from '@/state/replication-pipeline-request-status' import { createTestRouter, customRender } from '@/tests/lib/custom-render' import { addAPIMock } from '@/tests/lib/msw' vi.mock('@/components/layouts/Navigation/LayoutHeader/HomeIcon', () => ({ HomeIcon: () => ( Supabase home ), })) vi.mock('@/components/layouts/AppLayout/OrganizationDropdown', () => ({ OrganizationDropdown: () => Organisation, })) vi.mock('@/components/layouts/Navigation/NavigationBar/OrgSelector', () => ({ OrgSelector: () => Organisation, })) vi.mock('@/components/layouts/AppLayout/ProjectDropdown', () => ({ ProjectDropdown: () => Project, })) const PipelineCreationFlow = (props: Parameters[0]) => ( ) mockAnimationsApi() const onUrlUpdate = vi.fn() beforeEach(() => { onUrlUpdate.mockClear() addAPIMock({ method: 'get', path: '/platform/replication/:ref/sources/:source_id/publications/:publication_name/cost-estimate', response: () => HttpResponse.json({ currency: 'usd', pipeline: { hourly_cost: 0.05, monthly_cost: 36.5 }, streaming: { rate_per_gb: 3 }, table_copy: { rate_per_gb: 0.6, total_bytes: 0, total_cost: 0, tables: [] }, }), }) addAPIMock({ method: 'post', path: '/platform/replication/:ref/destinations/validate', response: () => HttpResponse.json({ validation_failures: [], }), }) addAPIMock({ method: 'get', path: '/platform/replication/:ref/sources', response: () => HttpResponse.json({ sources: [ { id: 1, name: 'default', tenant_id: 'tenant', config: { host: 'db.internal', port: 5432, name: 'postgres', username: 'postgres' }, }, ], }), }) addAPIMock({ method: 'get', path: '/platform/replication/v2/:ref/sources/:source_id/publications', response: () => HttpResponse.json({ publications: [{ name: 'analytics' }], }), }) addAPIMock({ method: 'get', path: '/platform/replication/v2/:ref/sources/:source_id/publications/:publication_name', response: () => HttpResponse.json({ name: 'analytics', config: { type: 'tables', tables: [], operations: ['insert'], publish_via_partition_root: false, }, tables: [], }), }) }) // nuqs 2.7.1 resets the testing adapter queue on every render and drops any // queued write. wait for the url before changing steps again. nuqs 2.10.1 and // later only reset on the first render, so these waits can go once studio // moves off the 2.7.1 pin. const waitForStepQuery = (queryString: string) => waitFor(() => expect(onUrlUpdate).toHaveBeenLastCalledWith(expect.objectContaining({ queryString })) ) const clickContinue = () => fireEvent.click(footerButton('Continue')) const next = async (queryString: string) => { clickContinue() await waitForStepQuery(queryString) } const fillConnection = () => { const fields = [ ['Pipeline name', 'Warehouse'], ['Project ID', 'my-project'], ['Dataset ID', 'dataset'], ['Service account key', '{}'], ] as const fields.forEach(([name, value]) => { fireEvent.change(screen.getByLabelText(name), { target: { value } }) }) } const stepStatus = () => screen.getByText(/^Step \d of 4/) const isHtmlElement = (value: unknown): value is HTMLElement => value instanceof HTMLElement const stepCard = () => { const card = stepStatus().nextElementSibling const isCard = isHtmlElement(card) if (!isCard) { throw new Error('step status is not followed by the step card') } return card } const stepFooter = () => { const footer = stepCard().lastElementChild if (!isHtmlElement(footer)) { throw new Error('step card has no footer') } return within(footer) } // *ByRole computes accessible names and visibility through getComputedStyle on // every ancestor, which dominated this suite's runtime. The footer only holds // text buttons, so they are found by their label instead. const footerButton = (name: string) => { const button = stepFooter().getByText(name).closest('button') if (!button) { throw new Error(`step footer has no ${name} button`) } return button } // Lookups that only locate a control skip the visibility check with // `hidden: true`; absence and visibility assertions keep it. const withinRegion = (name: string) => within(screen.getByRole('region', { name, hidden: true })) const destinationType = () => within(screen.getByRole('radiogroup', { name: 'Destination type', hidden: true })) const selectDestination = (name: RegExp) => fireEvent.click(destinationType().getByLabelText(name)) const toggleAdvancedSettings = (region: ReturnType) => fireEvent.click(region.getByRole('button', { name: /Advanced settings/, hidden: true })) const continueToData = () => next('?step=data') const selectPublication = async () => { fireEvent.click(screen.getByText('Select publication')) await waitFor(() => expect(screen.queryByText('analytics')).toBeInTheDocument()) fireEvent.click(screen.getByText('analytics')) } const back = async (queryString: string) => { fireEvent.click(footerButton('Back')) await waitForStepQuery(queryString) } const renderWizard = ( ui: Parameters[0], options?: { queryClient?: QueryClient; rateLimitFactor?: number } ) => customRender(ui, { queryClient: options?.queryClient, nuqs: { hasMemory: true, onUrlUpdate, ...(options?.rateLimitFactor === undefined ? {} : { rateLimitFactor: options.rateLimitFactor }), }, }) describe('PipelineCreationFlow', () => { it('keeps header navigation pending until dirty changes are discarded', async () => { const testRouter = createTestRouter('/project/default/database/pipelines/new') const navigateSpy = vi.spyOn(testRouter, 'navigate') customRender( , { router: testRouter } ) selectDestination(/Snowflake/) fireEvent.click(screen.getByRole('link', { name: 'Supabase home', hidden: true })) expect(await screen.findByRole('alertdialog')).toBeInTheDocument() expect(navigateSpy).not.toHaveBeenCalled() fireEvent.click( within(screen.getByRole('alertdialog')).getByRole('button', { name: 'Keep editing', hidden: true, }) ) expect(navigateSpy).not.toHaveBeenCalled() fireEvent.click(screen.getByRole('link', { name: 'Supabase home', hidden: true })) const discard = await screen.findByRole('button', { name: 'Discard changes' }) fireEvent.click(discard) await waitFor(() => expect(navigateSpy).toHaveBeenCalledWith(expect.objectContaining({ to: '/organizations' })) ) }) it.each([ ['BigQuery', 'bigquery#configure-bigquery-as-a-destination'], ['DuckLake', 'ducklake#choose-a-configuration-mode'], ['Snowflake', 'snowflake#prepare-snowflake-resources'], ['ClickHouse', 'clickhouse#configure-clickhouse-as-a-destination'], ] as const)('matches the connection subtitle and Docs to %s', async (type, path) => { addAPIMock({ method: 'get', path: '/platform/projects/:ref', response: () => HttpResponse.json({ id: 1, ref: 'default', name: 'Test project', organization_id: 1, status: 'ACTIVE_HEALTHY', cloud_provider: 'AWS', region: 'us-east-1', db_host: 'db.default.supabase.co', restUrl: 'https://default.supabase.co/rest/v1/', connectionString: null, inserted_at: '2026-01-01T00:00:00Z', updated_at: '2026-01-01T00:00:00Z', subscription_id: 'subscription', is_branch_enabled: false, is_physical_backups_enabled: false, high_availability: false, integration_source: null, }), }) addAPIMock({ method: 'get', path: '/platform/storage/:ref/buckets', response: () => HttpResponse.json([]), }) renderWizard( ) expect(screen.getByText('Where should this database be replicated?')).toBeInTheDocument() expect(screen.queryByRole('link', { name: 'Docs' })).not.toBeInTheDocument() selectDestination(new RegExp(type)) await next('?step=connection') expect( screen.getByText(`Name this pipeline and enter credentials for ${type}.`) ).toBeInTheDocument() expect(screen.getByRole('link', { name: 'Docs' })).toHaveAttribute( 'href', `${DOCS_URL}/guides/database/replication/pipelines/${path}` ) await back('') expect(screen.queryByRole('link', { name: 'Docs' })).not.toBeInTheDocument() }) const reachReview = async ( onClose = vi.fn(), queryClient?: QueryClient, shouldWaitForEstimate = true ) => { renderWizard(, { queryClient, rateLimitFactor: 1, }) await next('?step=connection') fillConnection() await continueToData() await selectPublication() await next('?step=review') if (shouldWaitForEstimate) await waitFor(() => expect(footerButton('Start pipeline')).toBeEnabled()) } const mockCreation = ({ failCreate = false, failStart = false } = {}) => { const requests: unknown[] = [] const starts = vi.fn() const destinationRequests: unknown[] = [] const pipelineRequests: unknown[] = [] addAPIMock({ method: 'post', path: '/platform/replication/:ref/destinations/validate', response: async ({ request }) => { destinationRequests.push(await request.json()) return HttpResponse.json({ validation_failures: [], }) }, }) addAPIMock({ method: 'post', path: '/platform/replication/:ref/pipelines/validate', response: async ({ request }) => { pipelineRequests.push(await request.json()) return HttpResponse.json({ validation_failures: [], }) }, }) addAPIMock({ method: 'post', path: '/platform/replication/:ref/destinations-pipelines', response: async ({ request }) => { requests.push(await request.json()) return failCreate && requests.length === 1 ? HttpResponse.json<{ message: string }>( { message: 'Create unavailable' }, { status: 503 } ) : HttpResponse.json({ destination_id: 7, pipeline_id: '8', }) }, }) addAPIMock({ method: 'post', path: '/platform/replication/:ref/pipelines/:pipeline_id/start', response: () => { starts() return failStart ? HttpResponse.json<{ message: string }>( { message: 'Start unavailable' }, { status: 503 } ) : HttpResponse.json>({}) }, }) addAPIMock({ method: 'get', path: '/platform/replication/:ref/pipelines/:pipeline_id/status', response: () => HttpResponse.json({ pipeline_id: '8', status: { name: 'stopped' }, }), }) return { requests, starts, destinationRequests, pipelineRequests } } it.each([ [false, false], [true, true], ])( 'places review failures and keeps the current position (connection: %s, warnings: %s)', async (hasConnectionFailure, hasWarnings) => { const { requests } = mockCreation() await reachReview() const validationStatus = screen.getByRole('status', { name: 'Pipeline validation status' }) expect(validationStatus).toBeEmptyDOMElement() const scrollIntoView = vi.fn() vi.spyOn(HTMLElement.prototype, 'scrollIntoView').mockImplementation(scrollIntoView) addAPIMock({ method: 'post', path: '/platform/replication/:ref/destinations/validate', response: () => HttpResponse.json({ validation_failures: hasConnectionFailure ? [ { name: 'Credentials', reason: 'Check destination credentials.', failure_type: 'critical', }, ] : [], }), }) addAPIMock({ method: 'post', path: '/platform/replication/:ref/pipelines/validate', response: () => HttpResponse.json({ validation_failures: [ { name: 'Publication', reason: 'Check publication settings.', failure_type: 'critical', }, ...(hasWarnings ? [ { name: 'Retention', reason: 'Review retention settings.', failure_type: 'warning' as const, }, ] : []), ], }), }) fireEvent.click(footerButton('Start pipeline')) await screen.findByText('Check publication settings.') expect(screen.getByRole('status', { name: 'Pipeline validation status' })).toBe( validationStatus ) const connection = withinRegion('Connection review') const data = withinRegion('Data review') if (hasConnectionFailure) expect(connection.getByText('Check destination credentials.')).toBeInTheDocument() expect(connection.getByRole('button', { name: 'Edit connection' })).toBeInTheDocument() expect(connection.queryByText('Check publication settings.')).not.toBeInTheDocument() expect(data.getByText('Check publication settings.')).toBeInTheDocument() expect(data.getByRole('button', { name: 'Edit data' })).toBeInTheDocument() expect( screen.getByText( hasWarnings ? 'Fix the above issues and review warnings before starting the pipeline.' : 'Fix the above issues before starting the pipeline.' ) ).toBeInTheDocument() expect(scrollIntoView).not.toHaveBeenCalled() expect(requests).toHaveLength(0) } ) it.each([false, true])( 'leaves after one committed creation, including start failure: %s', async (failStart) => { const { requests, starts, destinationRequests, pipelineRequests } = mockCreation({ failStart, }) const onClose = vi.fn() await reachReview(onClose) fireEvent.click(footerButton('Start pipeline')) expect(footerButton('Back')).toBeDisabled() expect(screen.getByDisplayValue('Warehouse')).toBeDisabled() fireEvent.click(footerButton('Start pipeline')) await waitFor(() => expect(onClose).toHaveBeenCalledOnce()) // Stay locked until the document leaves, even when starting failed. expect(footerButton('Start pipeline')).toBeDisabled() fireEvent.click(footerButton('Start pipeline')) expect(requests).toEqual([ expect.objectContaining({ source_id: 1, destination_name: 'Warehouse', destination_config: expect.objectContaining({ big_query: expect.objectContaining({ project_id: 'my-project', dataset_id: 'dataset' }), }), pipeline_config: expect.objectContaining({ publication_name: 'analytics' }), }), ]) expect(destinationRequests.at(-1)).toMatchObject({ source_id: 1, pipeline_config: expect.objectContaining({ publication_name: 'analytics' }), }) expect(pipelineRequests).toEqual([ { source_id: 1, config: expect.objectContaining({ publication_name: 'analytics' }) }, ]) expect(starts).toHaveBeenCalledOnce() expect(screen.queryByRole('alertdialog')).not.toBeInTheDocument() const unload = new Event('beforeunload', { cancelable: true }) window.dispatchEvent(unload) expect(unload.defaultPrevented).toBe(false) } ) it('edits each section from Review without losing values', async () => { await reachReview() expect(screen.getByText('Check these details, then start the pipeline.')).toBeInTheDocument() expect(screen.queryByRole('link', { name: 'Docs' })).not.toBeInTheDocument() fireEvent.click( withinRegion('Connection review').getByRole('button', { name: 'Edit connection', hidden: true, }) ) await waitForStepQuery('?step=connection') expect(screen.getByText(/Step 2 of 4/)).toBeInTheDocument() expect(screen.getByLabelText('Pipeline name')).toHaveValue('Warehouse') await next('?step=data') expect(screen.getByText(/Step 3 of 4/)).toBeInTheDocument() expect( screen.getByText('Select a publication and which existing rows to copy during initial sync.') ).toBeInTheDocument() expect(screen.getByRole('link', { name: 'Docs' })).toHaveAttribute( 'href', `${DOCS_URL}/guides/database/replication/pipelines#choosing-which-tables-to-copy` ) await next('?step=review') fireEvent.click( withinRegion('Data review').getByRole('button', { name: 'Edit data', hidden: true }) ) await waitForStepQuery('?step=data') expect(screen.getByText(/Step 3 of 4/)).toBeInTheDocument() await next('?step=review') fireEvent.click( withinRegion('Destination review').getByRole('button', { name: 'Edit destination', hidden: true, }) ) // destination is the default step, so clearOnDefault removes the query. await waitForStepQuery('') expect(screen.getByText(/Step 1 of 4/)).toBeInTheDocument() await next('?step=connection') expect(screen.getByText(/Step 2 of 4/)).toBeInTheDocument() expect(screen.getByLabelText('Pipeline name')).toHaveValue('Warehouse') }) it('preserves reviewed values when creation fails and retries safely', async () => { const { requests, starts } = mockCreation({ failCreate: true }) const onClose = vi.fn() await reachReview(onClose) fireEvent.click(footerButton('Start pipeline')) await waitFor(() => expect(requests).toHaveLength(1)) await waitFor(() => expect(footerButton('Start pipeline')).toBeEnabled()) expect(onClose).not.toHaveBeenCalled() expect(screen.getByDisplayValue('Warehouse')).toBeInTheDocument() expect(starts).not.toHaveBeenCalled() fireEvent.click(footerButton('Start pipeline')) await waitFor(() => expect(onClose).toHaveBeenCalledOnce()) expect(requests).toHaveLength(2) expect(requests[1]).toEqual(requests[0]) expect(starts).toHaveBeenCalledOnce() }) it('blocks critical final validation and requires confirmation for warnings', async () => { const { requests, destinationRequests } = mockCreation() const onClose = vi.fn() await reachReview(onClose) let validationRequests = 0 let failure_type: 'critical' | 'warning' = 'critical' let reason = 'Check the publication.' addAPIMock({ method: 'post', path: '/platform/replication/:ref/pipelines/validate', response: () => { validationRequests++ return HttpResponse.json({ validation_failures: [{ name: 'Publication issue', reason, failure_type }], }) }, }) fireEvent.click(footerButton('Start pipeline')) expect(await screen.findByText('Check the publication.')).toBeInTheDocument() expect(requests).toHaveLength(0) expect(onClose).not.toHaveBeenCalled() failure_type = 'warning' await waitFor(() => expect(footerButton('Start pipeline')).toBeEnabled()) fireEvent.click(footerButton('Start pipeline')) await waitFor(() => footerButton('Start pipeline anyway')) expect( screen.getByText('Review the warnings above before starting the pipeline.') ).toBeInTheDocument() await waitFor(() => expect(footerButton('Start pipeline anyway')).toBeEnabled()) expect(screen.getByText('Check the publication.')).toBeInTheDocument() expect(requests).toHaveLength(0) await back('?step=data') await back('?step=connection') fireEvent.change(screen.getByLabelText('Pipeline name'), { target: { value: 'Updated warehouse' }, }) await next('?step=data') expect(screen.getByText(/Step 3 of 4/)).toBeInTheDocument() await next('?step=review') expect(screen.getByText(/Step 4 of 4/)).toBeInTheDocument() expect(screen.queryByText('Check the publication.')).not.toBeInTheDocument() fireEvent.click(footerButton('Start pipeline')) await waitFor(() => expect(footerButton('Start pipeline anyway')).toBeEnabled()) const validatedDestinations = destinationRequests.length const validatedPipelines = validationRequests fireEvent.click(footerButton('Start pipeline anyway')) expect(screen.getByText('Check the publication.')).toBeInTheDocument() await waitFor(() => expect(onClose).toHaveBeenCalledOnce()) expect(requests).toHaveLength(1) expect(destinationRequests).toHaveLength(validatedDestinations) expect(validationRequests).toBe(validatedPipelines) }) it('retains Review and retries after a final validation request fails', async () => { const { requests } = mockCreation() const onClose = vi.fn() await reachReview(onClose) let shouldFail = true addAPIMock({ method: 'post', path: '/platform/replication/:ref/pipelines/validate', response: () => shouldFail ? HttpResponse.json<{ message: string }>( { message: 'Validation unavailable' }, { status: 503 } ) : HttpResponse.json({ validation_failures: [], }), }) fireEvent.click(footerButton('Start pipeline')) await waitFor(() => expect(footerButton('Start pipeline')).toBeDisabled()) await waitFor(() => expect(footerButton('Start pipeline')).toBeEnabled()) expect(screen.getByText(/Step 4 of 4/)).toBeInTheDocument() expect(screen.getByDisplayValue('Warehouse')).toBeInTheDocument() expect(requests).toHaveLength(0) shouldFail = false fireEvent.click(footerButton('Start pipeline')) await waitFor(() => expect(onClose).toHaveBeenCalledOnce()) expect(requests).toHaveLength(1) }) it('returns to Data when a publication is removed after review', async () => { const { requests } = mockCreation() await reachReview() addAPIMock({ method: 'get', path: '/platform/replication/v2/:ref/sources/:source_id/publications', response: () => HttpResponse.json({ publications: [], }), }) fireEvent.click(footerButton('Start pipeline')) await screen.findByText(/Step 3 of 4/) expect(requests).toHaveLength(0) }) it('requires connection fields and reveals invalid advanced settings', async () => { renderWizard() await next('?step=connection') const connection = withinRegion('Destination connection') toggleAdvancedSettings(connection) fireEvent.change(connection.getByLabelText('Connection pool size'), { target: { value: '-1' }, }) toggleAdvancedSettings(connection) fireEvent.click(footerButton('Continue')) await waitFor(() => expect(screen.queryAllByText(/Project ID is required\./).length).toBeGreaterThan(0) ) expect(connection.getByLabelText('Connection pool size')).toBeVisible() toggleAdvancedSettings(connection) expect( connection.queryByRole('spinbutton', { name: 'Connection pool size' }) ).not.toBeInTheDocument() fireEvent.click(footerButton('Continue')) expect(connection.getByLabelText('Connection pool size')).toBeVisible() fireEvent.change(connection.getByLabelText('Connection pool size'), { target: { value: '5' }, }) expect(screen.queryByText('Check connection settings')).not.toBeInTheDocument() }) it('recovers from request and critical failures and reviews warnings before continuing', async () => { let attempt = 0 const requests: unknown[] = [] addAPIMock({ method: 'post', path: '/platform/replication/:ref/destinations/validate', response: async ({ request }) => { requests.push(await request.json()) await delay(10) attempt++ if (attempt === 1) return HttpResponse.json<{ message: string }>({ message: 'Try again' }, { status: 500 }) return HttpResponse.json({ validation_failures: [ { name: 'Credentials', reason: 'Check credentials', failure_type: attempt === 2 ? 'critical' : 'warning', }, ], }) }, }) renderWizard( ) await next('?step=connection') fillConnection() clickContinue() expect(footerButton('Back')).toBeDisabled() await waitFor(() => expect(screen.queryByText('Try again')).toBeInTheDocument()) fireEvent.change(screen.getByLabelText('Project ID'), { target: { value: 'retry-project' }, }) expect(screen.getByText('Try again')).toBeInTheDocument() await back('') selectDestination(/Snowflake/) await next('?step=connection') expect(screen.queryByText('Try again')).not.toBeInTheDocument() await back('') selectDestination(/BigQuery/) await next('?step=connection') expect(screen.queryByText('Try again')).not.toBeInTheDocument() fireEvent.click(footerButton('Continue')) await waitFor(() => expect(screen.queryByText('Check credentials')).toBeInTheDocument()) await waitFor(() => expect(footerButton('Continue')).toBeEnabled()) fireEvent.change(screen.getByLabelText('Project ID'), { target: { value: 'edited-project' }, }) expect(screen.getByText('Check credentials')).toBeInTheDocument() fireEvent.change(screen.getByLabelText('Project ID'), { target: { value: 'retry-project' }, }) expect(screen.getByText('Check credentials')).toBeInTheDocument() fireEvent.click(footerButton('Continue')) await waitFor(() => expect(screen.queryByText(/Replication can start/)).toBeInTheDocument()) expect(screen.getByText(/Step 2 of 4/)).toBeInTheDocument() expect(requests[2]).not.toHaveProperty('pipeline_config') expect(requests[2]).not.toHaveProperty('source_id') }) it('invalidates connection verification after edits and ignores stale responses', async () => { addAPIMock({ method: 'post', path: '/platform/replication/:ref/destinations/validate', response: async () => { await delay(10) return HttpResponse.json({ validation_failures: [ { name: 'Credentials', reason: 'Check credentials', failure_type: 'warning' }, ], }) }, }) renderWizard() await next('?step=connection') fillConnection() clickContinue() await waitFor(() => expect(screen.queryByText(/Replication can start/)).toBeInTheDocument()) fireEvent.change(screen.getByLabelText('Project ID'), { target: { value: 'changed' }, }) expect(footerButton('Continue')).toBeInTheDocument() expect(screen.queryByText('Check credentials')).not.toBeInTheDocument() fireEvent.click(footerButton('Continue')) fireEvent.change(screen.getByLabelText('Project ID'), { target: { value: 'changed-during-test' }, }) await waitFor(() => expect(footerButton('Continue')).toBeEnabled()) expect(screen.getByText(/Step 2 of 4/)).toBeInTheDocument() expect(screen.queryByText(/Replication can start/)).not.toBeInTheDocument() }) it('uses browser Back and Forward to revisit steps without losing values', async () => { window.history.replaceState(null, '', '/project/default/database/pipelines/new') customRender( ) clickContinue() await waitFor(() => expect(window.location.search).toBe('?step=connection')) fireEvent.change(screen.getByLabelText('Project ID'), { target: { value: 'history-project' }, }) fillConnection() clickContinue() await waitFor(() => expect(window.location.search).toBe('?step=data')) await act(async () => { await new Promise((resolve) => { window.addEventListener('popstate', () => resolve(), { once: true }) window.history.back() }) }) expect(screen.getByText(/Step 2 of 4/)).toBeInTheDocument() expect(screen.getByLabelText('Project ID')).toHaveValue('my-project') await act(async () => { await new Promise((resolve) => { window.addEventListener('popstate', () => resolve(), { once: true }) window.history.forward() }) }) expect(screen.getByText(/Step 3 of 4/)).toBeInTheDocument() await act(async () => { await new Promise((resolve) => { window.addEventListener('popstate', () => resolve(), { once: true }) window.history.back() }) }) await waitFor(() => expect(window.location.search).toBe('?step=connection')) expect(screen.getByLabelText('Project ID')).toHaveValue('my-project') fireEvent.change(screen.getByLabelText('Project ID'), { target: { value: 'changed-project' }, }) await act(async () => { await new Promise((resolve) => { window.addEventListener('popstate', () => resolve(), { once: true }) window.history.forward() }) }) await waitFor(() => expect(window.location.search).toBe('?step=connection')) expect(footerButton('Continue')).toBeInTheDocument() }) it.each(['connection', 'data', 'review', 'invalid'])( 'starts a fresh session at Destination for step=%s', async (step) => { window.history.replaceState(null, '', `/project/default/database/pipelines/new?step=${step}`) customRender( ) await waitFor(() => expect(window.location.search).toBe('')) expect(screen.getByText(/Step 1 of 4/)).toBeInTheDocument() } ) it('preserves connection and data values across destination changes', async () => { renderWizard( , { rateLimitFactor: 1 } ) expect(screen.getByText(/Step [1-4] of 4/)).toHaveTextContent('Step 1 of 4') await next('?step=connection') fireEvent.change(screen.getByLabelText('Pipeline name'), { target: { value: 'Warehouse' }, }) fireEvent.change(screen.getByLabelText('Project ID'), { target: { value: 'my-project' }, }) fillConnection() await continueToData() expect(screen.getByText(/Step [1-4] of 4/)).toHaveTextContent('Step 3 of 4') const data = withinRegion('Data') toggleAdvancedSettings(data) fireEvent.change(data.getByLabelText('Batch wait time'), { target: { value: '750' }, }) await back('?step=connection') expect(screen.getByLabelText('Project ID')).toHaveValue('my-project') await back('') selectDestination(/Snowflake/) await next('?step=connection') expect(screen.getByLabelText('Pipeline name')).toHaveValue('Warehouse') await back('') selectDestination(/BigQuery/) await next('?step=connection') expect(screen.getByLabelText('Project ID')).toHaveValue('my-project') await continueToData() const dataAfterReturn = withinRegion('Data') toggleAdvancedSettings(dataAfterReturn) expect(dataAfterReturn.getByLabelText('Batch wait time')).toHaveValue(750) }) it('shows one publication loading failure before Continue and clears it after a successful retry', async () => { let shouldFail = true addAPIMock({ method: 'get', path: '/platform/replication/v2/:ref/sources/:source_id/publications/:publication_name', response: () => shouldFail ? HttpResponse.json<{ message: string }>({ message: 'Unavailable' }, { status: 503 }) : HttpResponse.json({ name: 'analytics', config: { type: 'tables', tables: [], operations: ['insert'], publish_via_partition_root: false, }, tables: [], }), }) renderWizard() await next('?step=connection') fillConnection() await continueToData() await selectPublication() await screen.findByText('Publication tables could not be loaded') toggleAdvancedSettings(withinRegion('Data')) expect(screen.queryByText('Table settings could not be loaded')).not.toBeInTheDocument() expect(screen.queryByText(/Partition handling could not be loaded/)).not.toBeInTheDocument() await waitFor(() => expect(screen.getAllByText('Publication tables could not be loaded')).toHaveLength(1) ) expect(screen.getByText('Click Continue to try again.')).toBeInTheDocument() expect(screen.queryByText(/Refresh the page before changing/)).not.toBeInTheDocument() clickContinue() await waitFor(() => expect(footerButton('Continue')).toBeEnabled()) await waitFor(() => expect(screen.getAllByText('Publication tables could not be loaded')).toHaveLength(1) ) shouldFail = false await next('?step=review') expect(screen.queryByText('Publication tables could not be loaded')).not.toBeInTheDocument() }) it('asks for another publication when the selected publication was deleted', async () => { renderWizard() await next('?step=connection') fillConnection() await continueToData() await selectPublication() addAPIMock({ method: 'get', path: '/platform/replication/v2/:ref/sources/:source_id/publications', response: () => HttpResponse.json({ publications: [], }), }) addAPIMock({ method: 'get', path: '/platform/replication/v2/:ref/sources/:source_id/publications/:publication_name', response: () => HttpResponse.json<{ message: string }>({ message: 'Not found' }, { status: 404 }), }) clickContinue() await screen.findByText('The publication analytics was not found.') await screen.findByText('Select an available publication.') expect(screen.queryByText('Publication tables could not be loaded')).not.toBeInTheDocument() expect(screen.queryByText('Click Continue to try again.')).not.toBeInTheDocument() expect(stepStatus()).toHaveTextContent('Step 3 of 4') }) it('blocks incomplete data and reveals invalid advanced settings', async () => { renderWizard(, { rateLimitFactor: 1, }) await next('?step=connection') fillConnection() await continueToData() clickContinue() await waitFor(() => expect(screen.queryByText('Select an available publication.')).toBeInTheDocument() ) await selectPublication() await waitFor(() => expect(screen.queryByText('Select an available publication.')).not.toBeInTheDocument() ) const data = withinRegion('Data') toggleAdvancedSettings(data) fireEvent.change(data.getByLabelText('Batch wait time'), { target: { value: '-1' }, }) toggleAdvancedSettings(data) clickContinue() await waitFor(() => expect(data.queryByRole('spinbutton', { name: 'Batch wait time' })).toBeVisible() ) expect(screen.getByText('Check data settings')).toBeInTheDocument() fireEvent.change(data.getByLabelText('Batch wait time'), { target: { value: '750' }, }) expect(screen.queryByText('Check data settings')).not.toBeInTheDocument() }) it('retries publication metadata failures and keeps data values after Review', async () => { renderWizard(, { rateLimitFactor: 1, }) await next('?step=connection') fillConnection() await continueToData() await selectPublication() const data = withinRegion('Data') toggleAdvancedSettings(data) fireEvent.change(data.getByLabelText('Batch wait time'), { target: { value: '750' }, }) let shouldFail = true addAPIMock({ method: 'get', path: '/platform/replication/v2/:ref/sources/:source_id/publications', response: () => shouldFail ? HttpResponse.json<{ message: string }>({ message: 'Unavailable' }, { status: 500 }) : HttpResponse.json({ publications: [{ name: 'analytics' }], }), }) clickContinue() await waitFor(() => expect(screen.queryByText('Click Continue to try again.')).toBeInTheDocument() ) shouldFail = false await next('?step=review') await back('?step=data') const dataAfterReturn = withinRegion('Data') await waitFor(() => toggleAdvancedSettings(dataAfterReturn)) expect(dataAfterReturn.getByLabelText('Batch wait time')).toHaveValue(750) }) it('blocks browser Forward to Review after data changes', async () => { window.history.replaceState(null, '', '/project/default/database/pipelines/new') customRender( ) clickContinue() await waitFor(() => expect(window.location.search).toBe('?step=connection')) fillConnection() clickContinue() await waitFor(() => expect(window.location.search).toBe('?step=data')) await waitFor(() => expect(screen.queryByText('Select publication')).toBeInTheDocument()) await selectPublication() clickContinue() await waitFor(() => expect(window.location.search).toBe('?step=review')) await act(async () => { await new Promise((resolve) => { window.addEventListener('popstate', () => resolve(), { once: true }) window.history.back() }) }) const data = withinRegion('Data') toggleAdvancedSettings(data) fireEvent.change(data.getByLabelText('Batch wait time'), { target: { value: '-1' }, }) await act(async () => { await new Promise((resolve) => { window.addEventListener('popstate', () => resolve(), { once: true }) window.history.forward() }) }) await waitFor(() => expect(window.location.search).toBe('?step=data')) expect(withinRegion('Data').getByLabelText('Batch wait time')).toHaveValue(-1) }) it('confirms destination-only changes and treats a reverted selection as pristine', async () => { const onClose = vi.fn() customRender( ) selectDestination(/Snowflake/) fireEvent.click(screen.getByRole('button', { name: 'Close', hidden: true })) await waitFor(() => expect(screen.queryByRole('alertdialog')).toBeInTheDocument()) expect(onClose).not.toHaveBeenCalled() fireEvent.click( within(screen.getByRole('alertdialog')).getByRole('button', { name: 'Keep editing', hidden: true, }) ) selectDestination(/BigQuery/) fireEvent.click(screen.getByRole('button', { name: 'Close', hidden: true })) expect(onClose).toHaveBeenCalledOnce() }) it('confirms dirty dismissal and allows keeping the form', async () => { const onClose = vi.fn() renderWizard() await next('?step=connection') fireEvent.change(screen.getByLabelText('Pipeline name'), { target: { value: 'Keep me' }, }) const unload = new Event('beforeunload', { cancelable: true }) window.dispatchEvent(unload) expect(unload.defaultPrevented).toBe(true) fireEvent.click(screen.getByRole('button', { name: 'Close', hidden: true })) await waitFor(() => expect(screen.queryByRole('alertdialog')).toBeInTheDocument()) fireEvent.click( within(screen.getByRole('alertdialog')).getByRole('button', { name: 'Keep editing', hidden: true, }) ) expect(onClose).not.toHaveBeenCalled() fireEvent.click(screen.getByRole('link', { name: 'Supabase home', hidden: true })) await waitFor(() => expect(screen.queryByRole('alertdialog')).toBeInTheDocument()) expect(onClose).not.toHaveBeenCalled() fireEvent.click( within(screen.getByRole('alertdialog')).getByRole('button', { name: 'Keep editing', hidden: true, }) ) expect(screen.getByLabelText('Pipeline name')).toHaveValue('Keep me') fireEvent.click(screen.getByRole('button', { name: 'Close', hidden: true })) const discardChanges = await waitFor(() => { const button = screen.queryByRole('button', { name: 'Discard changes' }) expect(button).toBeInTheDocument() if (!isHtmlElement(button)) { throw new Error('discard changes button is missing') } return button }) fireEvent.click(discardChanges) await waitFor(() => expect(onClose).toHaveBeenCalledOnce()) const confirmedUnload = new Event('beforeunload', { cancelable: true }) window.dispatchEvent(confirmedUnload) expect(confirmedUnload.defaultPrevented).toBe(false) }) it('closes a pristine form and blocks progression without destinations', () => { const onClose = vi.fn() customRender() expect(footerButton('Continue')).toBeDisabled() fireEvent.click(screen.getByRole('button', { name: 'Close', hidden: true })) expect(onClose).toHaveBeenCalledOnce() }) it.each(['success', 'error'] as const)( 'waits for the initial cost estimate and permits starting after %s', async (result) => { const { requests } = mockCreation() let finishEstimate = () => {} const gate = new Promise((resolve) => { finishEstimate = resolve }) addAPIMock({ method: 'get', path: '/platform/replication/:ref/sources/:source_id/publications/:publication_name/cost-estimate', response: async () => { await gate if (result === 'error') return HttpResponse.json<{ message: string }>( { message: 'Estimate unavailable' }, { status: 503 } ) return HttpResponse.json({ currency: 'usd', pipeline: { hourly_cost: 0.05, monthly_cost: 36.5 }, streaming: { rate_per_gb: 3 }, table_copy: { rate_per_gb: 0.6, total_bytes: 0, total_cost: 0, tables: [] }, }) }, }) await reachReview(vi.fn(), undefined, false) const start = footerButton('Start pipeline') expect(start).toBeDisabled() fireEvent.click(start) expect(requests).toHaveLength(0) expect(footerButton('Back')).toBeEnabled() finishEstimate() await waitFor(() => expect(start).toBeEnabled()) if (result === 'error') expect(screen.getByText(/A cost estimate is unavailable/)).toBeInTheDocument() fireEvent.click(start) await waitFor(() => expect(requests).toHaveLength(1)) } ) it('keeps starting available during a background cost refresh with a cached estimate', async () => { const queryClient = new QueryClient({ defaultOptions: { queries: { retry: false } } }) const key = replicationKeys.costEstimate('default', 1, 'analytics') queryClient.setQueryData(key, { currency: 'usd', pipeline: { hourly_cost: 0.05, monthly_cost: 36.5 }, streaming: { rate_per_gb: 3 }, table_copy: { rate_per_gb: 0.6, total_bytes: 0, total_cost: 0, tables: [] }, }) addAPIMock({ method: 'get', path: '/platform/replication/:ref/sources/:source_id/publications/:publication_name/cost-estimate', response: async () => { await delay('infinite') return HttpResponse.json({ currency: 'usd', pipeline: { hourly_cost: 0.05, monthly_cost: 36.5 }, streaming: { rate_per_gb: 3 }, table_copy: { rate_per_gb: 0.6, total_bytes: 0, total_cost: 0, tables: [] }, }) }, }) await reachReview(vi.fn(), queryClient) await waitFor(() => expect(queryClient.getQueryState(key)?.fetchStatus).toBe('fetching')) expect(footerButton('Start pipeline')).toBeEnabled() expect(screen.getByRole('heading', { name: 'Initial sync' })).toBeInTheDocument() }) })