Files
supabase/apps/studio/components/interfaces/Database/Replication/CreatePipeline/usePipelineReviewCosts.test.tsx
Danny WhiteandJoshen Lim d1e251181f fix(studio): improve pipeline cost estimate loading and recovery (#52056)
## Problem

Pipeline review permits starting before the initial cost estimate
finishes. Estimate failures and background refresh errors also need to
preserve a usable review state.

## Solution

Share cost-query state between the wizard and cost display. Disable
**Start pipeline** only until the first estimate attempt finishes. After
its first failure, show a flush, description-only note and permit
starting while retries continue. Retain cached estimates if a background
refresh fails, and announce unavailable estimates through a persistent
status region.

_All “After” states for Review step:_

| Loading | Loaded | Failed |
| --- | --- | --- |
| <img width="1024" height="686" alt="31887"
src="https://github.com/user-attachments/assets/80b8623e-65ab-4bbb-8fcf-1e5e29a0dcd5"
/> | <img width="1024" height="686" alt="New Pipeline Supabase"
src="https://github.com/user-attachments/assets/ce49550c-d8cf-42b3-98f3-4520ae6b1229"
/> | <img width="1024" height="686" alt="62823"
src="https://github.com/user-attachments/assets/958b2bb3-e7ee-4214-b900-2e634adca05f"
/> |

## To test

1. Enable **Pipeline creation wizard** in Feature Previews and open
`/project/<ref>/database/pipelines/new`. Complete Connection and Data to
reach Review. With network throttling, **Start pipeline** stays disabled
during the initial estimate request; **Back** remains available.
2. Filter Network for `cost-estimate`. Block the GET ending
`/replication/<ref>/sources/<id>/publications/<name>/cost-estimate`,
then reload and complete the wizard again to clear the in-memory
estimate cache. After the first failed request, expect “A cost estimate
is unavailable. You can still start the pipeline.” in a full-width note,
and an enabled **Start pipeline**.
3. Remove the block and obtain an estimate. Block the request again, go
Back and return to Review. A failed background refresh should retain the
cached numbers and allow starting.
4. Check desktop and phone widths. Stop before creating the pipeline and
remove the request block.

---------

Co-authored-by: Joshen Lim <joshenlimek@gmail.com>
2026-10-22 11:05:44 +11:00

170 lines
6.1 KiB
TypeScript

import { QueryClient } from '@tanstack/react-query'
import { act, screen, waitFor } from '@testing-library/react'
import type { components } from 'api-types'
import { HttpResponse } from 'msw'
import { useForm } from 'react-hook-form'
import { beforeEach, expect, test } from 'vitest'
import type { DestinationPanelSchemaType } from '../DestinationPanel/DestinationForm/DestinationForm.schema'
import { PipelineReviewCosts } from './PipelineReviewCosts'
import { usePipelineReviewCosts } from './usePipelineReviewCosts'
import type { ReplicationCostEstimateData } from '@/data/replication/cost-estimate-query'
import { replicationKeys } from '@/data/replication/keys'
import type { ReplicationPublicationData } from '@/data/replication/publication-query'
import { customRender } from '@/tests/lib/custom-render'
import { addAPIMock, type APIErrorBody } from '@/tests/lib/msw'
const publication: ReplicationPublicationData = {
name: 'analytics',
config: {
type: 'tables',
tables: [],
operations: ['insert'],
publish_via_partition_root: false,
},
tables: [],
}
const defaultValues: DestinationPanelSchemaType = {
name: 'Analytics',
publicationName: 'analytics',
tableSyncCopyMode: 'include_all_tables',
tableSyncCopyTableIds: [],
maxFillMs: 500,
maxTableSyncWorkers: 4,
maxCopyConnectionsPerTable: 1,
maxStalenessMins: 0,
projectId: 'example-project',
datasetId: 'analytics',
serviceAccountKey: '',
connectionPoolSize: 5,
}
const key = replicationKeys.costEstimate('default', 1, 'analytics')
const sources: components['schemas']['SourcesResponse_Output'] = {
sources: [
{
id: 1,
name: 'default',
tenant_id: 'tenant',
config: { host: 'localhost', port: 5432, name: 'postgres', username: 'postgres' },
},
],
}
const ReviewCosts = () => {
const { control } = useForm<DestinationPanelSchemaType>({ defaultValues })
const costs = usePipelineReviewCosts({ control, publication })
return (
<>
<PipelineReviewCosts control={control} publication={publication} />
<button tabIndex={0} disabled={costs.isInitialCostEstimateLoading}>
Start pipeline
</button>
</>
)
}
beforeEach(() => {
addAPIMock({
method: 'get',
path: '/platform/replication/:ref/sources',
response: () => HttpResponse.json<components['schemas']['SourcesResponse_Output']>(sources),
})
addAPIMock({
method: 'get',
path: '/platform/replication/:ref/sources/:source_id/publications/:publication_name/cost-estimate',
response: () =>
HttpResponse.json<APIErrorBody>({ message: 'Estimate unavailable' }, { status: 503 }),
})
})
test('permits starting and shows the unavailable note after the first failure while retrying', async () => {
const queryClient = new QueryClient({
defaultOptions: { queries: { retry: 1, retryDelay: 60_000 } },
})
queryClient.setQueryData(replicationKeys.sources('default'), sources)
const { unmount } = customRender(<ReviewCosts />, { queryClient })
const status = screen.getByRole('status')
expect(screen.getByRole('button', { name: 'Start pipeline' })).toBeDisabled()
expect(status).toBeEmptyDOMElement()
await waitFor(() => expect(status).toHaveTextContent('A cost estimate is unavailable.'))
expect(screen.getByRole('status')).toBe(status)
expect(screen.getByRole('button', { name: 'Start pipeline' })).toBeEnabled()
expect(queryClient.getQueryState(key)).toMatchObject({
status: 'pending',
fetchStatus: 'fetching',
fetchFailureCount: 1,
})
unmount()
queryClient.clear()
})
test('keeps cached costs visible after a background refresh fails', async () => {
const queryClient = new QueryClient({ defaultOptions: { queries: { retry: false } } })
queryClient.setQueryData<ReplicationCostEstimateData>(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: [] },
})
queryClient.setQueryData(replicationKeys.sources('default'), sources)
const { unmount } = customRender(<ReviewCosts />, { queryClient })
await waitFor(() => expect(queryClient.getQueryState(key)?.status).toBe('error'))
expect(screen.getByText('$0.05/hour')).toBeInTheDocument()
expect(screen.getByText('$3.00/GB')).toBeInTheDocument()
expect(screen.getByRole('status')).toBeEmptyDOMElement()
expect(screen.getByRole('button', { name: 'Start pipeline' })).toBeEnabled()
unmount()
queryClient.clear()
})
test('keeps starting available while refetching an estimate that previously failed', async () => {
const queryClient = new QueryClient({ defaultOptions: { queries: { retry: false } } })
queryClient.setQueryData(replicationKeys.sources('default'), sources)
const { unmount } = customRender(<ReviewCosts />, { queryClient })
await waitFor(() => expect(queryClient.getQueryState(key)?.status).toBe('error'))
expect(screen.getByRole('button', { name: 'Start pipeline' })).toBeEnabled()
let finishRefetch = () => {}
const refetchResponse = new Promise<void>((resolve) => {
finishRefetch = resolve
})
let markRefetchStarted = () => {}
const refetchStarted = new Promise<void>((resolve) => {
markRefetchStarted = resolve
})
addAPIMock({
method: 'get',
path: '/platform/replication/:ref/sources/:source_id/publications/:publication_name/cost-estimate',
response: async () => {
markRefetchStarted()
await refetchResponse
return HttpResponse.json<APIErrorBody>({ message: 'Estimate unavailable' }, { status: 503 })
},
})
try {
await act(async () => {
void queryClient.refetchQueries({ queryKey: key })
await refetchStarted
await new Promise((resolve) => setTimeout(resolve, 0))
})
expect(queryClient.getQueryState(key)).toMatchObject({
status: 'pending',
fetchStatus: 'fetching',
fetchFailureCount: 0,
errorUpdateCount: 1,
})
expect(screen.getByRole('button', { name: 'Start pipeline' })).toBeEnabled()
expect(screen.getByRole('status')).toHaveTextContent('A cost estimate is unavailable.')
} finally {
finishRefetch()
unmount()
queryClient.clear()
}
})