import { fireEvent, screen, waitFor, within } from '@testing-library/react' import userEvent from '@testing-library/user-event' import { platformComponents as components } from 'api-types' import dayjs from 'dayjs' import duration from 'dayjs/plugin/duration' import { mockAnimationsApi } from 'jsdom-testing-mocks' import { HttpResponse } from 'msw' import { beforeEach, describe, expect, test, vi } from 'vitest' import { WarehouseOverviewTab } from './OverviewTab' import type { SchemasData } from '@/data/database/schemas-query' import type { ProjectDetail } from '@/data/projects/project-detail-query' import type { TablesData } from '@/data/tables/tables-query' import type { WarehouseMetricsResponse } from '@/data/warehouse/warehouse-metrics-api-types' import { customRender } from '@/tests/lib/custom-render' import { addAPIMock, type APIErrorBody } from '@/tests/lib/msw' type PublicationDetailsResponse = components['schemas']['PublicationDetailsResponse_Output'] type ReplicationSourcesResponse = components['schemas']['SourcesResponse_Output'] type RunQueryBody = components['schemas']['RunQueryBody'] type WarehouseSetupStatusResponse = components['schemas']['WarehouseSetupStatusResponse_Output'] type WarehouseDeleteResponse = components['schemas']['WarehouseDeleteResponse_Output'] type WarehouseSetupResponse = components['schemas']['WarehouseSetupResponse_Output'] const mockTrack = vi.fn() vi.mock('@/lib/telemetry/track', () => ({ useTrack: () => mockTrack })) vi.mock('sonner', () => ({ toast: { success: vi.fn(), error: vi.fn() }, })) // Exercised by its own unit tests, and it fires four upstream queries of its own. vi.mock('./WarehouseSchemaTablePicker', () => ({ WarehouseSchemaTablePicker: ({ error, isEditing, onSubmit, }: { error?: { message: string } | null isEditing?: boolean onSubmit: (targets: [{ type: 'table'; schema: string; name: string }]) => void }) => (

Tables

Replicated tables picker {!!error && Picker error: {error.message}}
), })) beforeEach(() => addAPIMock({ method: 'get', path: '/platform/warehouse/:ref/metrics', response: () => HttpResponse.json({ metrics: [] }), }) ) mockAnimationsApi() dayjs.extend(duration) const FDW_STATUS: WarehouseSetupStatusResponse['fdw_status'] = { extension_available: true, extension_installed: true, foreign_schema_imported: true, schema_created: true, server_configured: true, wrapper_installed: true, } const mockSetupStatus = (status: Partial) => addAPIMock({ method: 'get', path: '/platform/warehouse/:ref/setup-status', response: () => HttpResponse.json({ fdw_status: FDW_STATUS, setup_status: 'not_started', steps: [], tables: [], ...status, }), }) const PROJECT: ProjectDetail = { cloud_provider: 'AWS', connectionString: 'postgresql://postgres@localhost:5432/postgres', db_host: 'db.default.supabase.co', dbVersion: 'supabase-postgres-17.6.1.001', high_availability: false, id: 1, inserted_at: '2026-01-01T00:00:00.000Z', integration_source: null, is_branch_enabled: false, is_hibernating: false, is_physical_backups_enabled: false, name: 'Test project', organization_id: 1, ref: 'default', region: 'us-east-1', restUrl: 'https://default.supabase.co/rest/v1', status: 'ACTIVE_HEALTHY', subscription_id: 'subscription-1', updated_at: '2026-01-01T00:00:00.000Z', } const SCHEMAS: SchemasData = [ { id: 1, name: 'public', owner: 'postgres', comment: null }, { id: 2, name: 'analytics', owner: 'postgres', comment: null }, ] const createTable = (id: number, schema: string, name: string): TablesData[number] => ({ id, schema, name, rls_enabled: false, rls_forced: false, replica_identity: 'DEFAULT', bytes: 1024, size: '1024 bytes', live_rows_estimate: 10, dead_rows_estimate: 0, comment: null, primary_keys: [], relationships: [], }) const TABLES: TablesData = [ createTable(1, 'public', 'orders'), createTable(2, 'public', 'customers'), createTable(3, 'analytics', 'events'), createTable(4, 'analytics', 'sessions'), ] const SOURCES: ReplicationSourcesResponse = { sources: [ { id: 1, name: 'default', tenant_id: 'tenant', config: { host: 'db.internal', name: 'main-db', port: 5432, username: 'etl_user' }, }, ], } // Every table of `public` plus one table of `analytics`: one schema target, one table target. const PUBLICATION_TABLES: PublicationDetailsResponse['tables'] = [ { id: 1, schema: 'public', name: 'orders', kind: 'table', partition_parent_id: null }, { id: 2, schema: 'public', name: 'customers', kind: 'table', partition_parent_id: null }, { id: 3, schema: 'analytics', name: 'events', kind: 'table', partition_parent_id: null }, ] // The disable card reads what is currently replicated from these queries. const mockReplicatedTableQueries = () => { const publicationRequests: string[] = [] addAPIMock({ method: 'post', path: '/platform/pg-meta/:ref/query', response: async ({ request }) => { const body = (await request.json()) as RunQueryBody const result = body.query.includes("obj_description(n.oid, 'pg_namespace')") ? SCHEMAS : TABLES return HttpResponse.json(result) }, }) addAPIMock({ method: 'get', path: '/platform/replication/:ref/sources', response: () => HttpResponse.json(SOURCES), }) addAPIMock({ method: 'get', path: '/platform/replication/v2/:ref/sources/:source_id/publications/:publication_name', response: ({ params }) => { publicationRequests.push(String(params.publication_name)) return HttpResponse.json({ name: 'supabase_warehouse', config: { type: 'tables', tables: PUBLICATION_TABLES.map(({ id, schema, name }) => ({ id, schema, name, columns: null, row_filter: null, })), operations: ['insert', 'update', 'delete', 'truncate'], publish_via_partition_root: false, }, tables: PUBLICATION_TABLES, }) }, }) return { publicationRequests } } describe('WarehouseOverviewTab', () => { beforeEach(() => { mockTrack.mockClear() addAPIMock({ method: 'get', path: '/platform/projects/:ref', response: PROJECT }) }) test('offers only the picker before setup', async () => { mockSetupStatus({ setup_status: 'not_started' }) customRender() expect(await screen.findByText('Replicated tables picker')).toBeInTheDocument() expect(screen.queryByText('External access')).not.toBeInTheDocument() expect(screen.queryByRole('button', { name: 'Disable Warehouse' })).not.toBeInTheDocument() }) test.each(['setting_up', 'copying'] as const)( 'shows setup progress for %s', async (setupStatus) => { mockSetupStatus({ setup_status: setupStatus, tables: setupStatus === 'copying' ? [ { schema: 'public', name: 'orders', copy_name: 'public.orders', state: 'live' }, { schema: 'public', name: 'customers', copy_name: 'public.customers', state: 'syncing', lag_ms: 0, }, ] : [], }) customRender() expect(await screen.findByText('Warehouse is being set up')).toBeInTheDocument() expect(screen.getByRole('heading', { name: 'Status' })).toBeInTheDocument() if (setupStatus === 'copying') { expect( screen.getByText('Backfilling selected tables. 1 of 2 tables synced.') ).toBeInTheDocument() expect(screen.getByText('orders')).toBeInTheDocument() expect(screen.getByText('customers')).toBeInTheDocument() expect(screen.queryByText('Caught up')).not.toBeInTheDocument() } } ) test('keeps the table picker available after setup fails and shows the latest retry error', async () => { mockSetupStatus({ setup_status: 'error', steps: [{ name: 'warehouse_copy', status: 'error', message: 'Initial setup failed' }], tables: [{ schema: 'public', name: 'orders', copy_name: 'public.orders', state: 'error' }], }) addAPIMock({ method: 'post', path: '/platform/warehouse/:ref/setup', response: () => HttpResponse.json({ message: 'Retry could not be started' }, { status: 500 }), }) customRender() expect(await screen.findByText('Picker error: Initial setup failed')).toBeInTheDocument() await userEvent.click(screen.getByRole('button', { name: 'Submit initial tables' })) expect(await screen.findByText('Picker error: Retry could not be started')).toBeInTheDocument() expect(screen.queryByText('Picker error: Initial setup failed')).not.toBeInTheDocument() }) test('shows Status, Tables, Connect, then Disable once setup is complete', async () => { mockSetupStatus({ setup_status: 'complete', tables: [ { schema: 'public', name: 'orders', copy_name: 'public.orders', state: 'live', lag_ms: 4000, }, { schema: 'public', name: 'customers', copy_name: 'public.customers', state: 'syncing', lag_ms: 18000, }, { schema: 'analytics', name: 'events', copy_name: 'analytics.events', state: 'error' }, ], }) mockReplicatedTableQueries() customRender() // findByRole throws on duplicates, so this also guards the section titles staying distinct. for (const name of ['Status', 'Tables', 'Connect', 'Disable']) { expect(await screen.findByRole('heading', { name })).toBeInTheDocument() } expect(screen.getByText('Replicated tables picker')).toBeInTheDocument() expect(screen.getByText('Live')).toBeInTheDocument() expect(screen.getByText('Backfilling')).toBeInTheDocument() expect(screen.getByText('Error')).toBeInTheDocument() expect(screen.getByText('Caught up')).toBeInTheDocument() expect(screen.queryByText(/behind$/)).not.toBeInTheDocument() expect(screen.getByRole('columnheader', { name: 'Lag' })).toBeInTheDocument() expect(screen.queryByRole('columnheader', { name: 'Size' })).not.toBeInTheDocument() const headings = screen .getAllByRole('heading') .map((heading) => heading.textContent) .filter((heading) => ['Status', 'Tables', 'Connect', 'Disable'].includes(heading ?? '')) expect(headings).toEqual(['Status', 'Tables', 'Connect', 'Disable']) }) test('tracks initial setup as enablement', async () => { mockSetupStatus({ setup_status: 'not_started' }) addAPIMock({ method: 'post', path: '/platform/warehouse/:ref/setup', response: () => HttpResponse.json({ pipeline_id: '1', tables: [] }), }) customRender() await userEvent.click(await screen.findByRole('button', { name: 'Submit initial tables' })) await waitFor(() => expect(mockTrack).toHaveBeenCalledWith('warehouse_enabled', { source: 'integrations_overview', schemaTargetCount: 0, tableTargetCount: 1, }) ) }) test('does not track an edited table selection as enablement', async () => { mockSetupStatus({ setup_status: 'complete' }) mockReplicatedTableQueries() addAPIMock({ method: 'post', path: '/platform/warehouse/:ref/setup', response: () => HttpResponse.json({ pipeline_id: '1', tables: [] }), }) customRender() await userEvent.click(await screen.findByRole('button', { name: 'Submit edited tables' })) await waitFor(() => expect(screen.getByText('Replicated tables picker')).toBeInTheDocument()) expect(mockTrack).not.toHaveBeenCalledWith('warehouse_enabled', expect.anything()) }) test('preserves copied data by default, resets the option on reopen, and tracks what was replicated', async () => { mockSetupStatus({ setup_status: 'complete' }) const { publicationRequests } = mockReplicatedTableQueries() const deleteRequests: (string | null)[] = [] addAPIMock({ method: 'delete', path: '/platform/warehouse/:ref', response: async ({ request }) => { deleteRequests.push(new URL(request.url).searchParams.get('delete_data')) return HttpResponse.json({ status: 'accepted' }, { status: 202 }) }, }) customRender() await userEvent.click(await screen.findByRole('button', { name: 'Disable Warehouse' })) let dialog = await screen.findByRole('alertdialog') expect(dialog).toHaveTextContent('Deletes the copied data and its storage permanently') expect(within(dialog).getByRole('checkbox')).not.toBeChecked() expect(screen.queryByPlaceholderText('Type the project ref to confirm')).not.toBeInTheDocument() await userEvent.click(within(dialog).getByRole('checkbox')) expect(within(dialog).getByRole('checkbox')).toBeChecked() await userEvent.click(within(dialog).getByRole('button', { name: 'Cancel' })) await waitFor(() => expect(screen.queryByRole('alertdialog')).not.toBeInTheDocument()) expect(deleteRequests).toEqual([]) await userEvent.click(screen.getByRole('button', { name: 'Disable Warehouse' })) dialog = await screen.findByRole('alertdialog') expect(within(dialog).getByRole('checkbox')).not.toBeChecked() await waitFor(() => expect(publicationRequests).toEqual(['supabase_warehouse'])) fireEvent.click(within(dialog).getByRole('button', { name: 'Disable Warehouse' })) await waitFor(() => expect(deleteRequests).toEqual(['false'])) await waitFor(() => expect(mockTrack).toHaveBeenCalledWith('warehouse_disabled', { schemaTargetCount: 1, tableTargetCount: 1, }) ) }) test('shows disable progress and polls until Warehouse can be enabled again', async () => { let status: WarehouseSetupStatusResponse['setup_status'] = 'disabling' addAPIMock({ method: 'get', path: '/platform/warehouse/:ref/setup-status', response: () => HttpResponse.json({ setup_status: status, fdw_status: null, tables: [], steps: [], }), }) vi.useFakeTimers({ shouldAdvanceTime: true }) try { customRender() expect(await screen.findByText('Warehouse is being disabled')).toBeInTheDocument() expect(screen.queryByRole('button', { name: 'Disable Warehouse' })).not.toBeInTheDocument() status = 'disabled' // The panel polls every 3s while disabling. await vi.advanceTimersByTimeAsync(3000) expect(await screen.findByText('Replicated tables picker')).toBeInTheDocument() } finally { vi.useRealTimers() } }) test.each([false, true])( 'retries failed cleanup with the saved delete_data option (%s)', async (shouldDeleteData) => { mockSetupStatus({ setup_status: 'deletion_failed', error: 'Storage cleanup failed', delete_data: shouldDeleteData, }) const deleteRequests: (string | null)[] = [] addAPIMock({ method: 'delete', path: '/platform/warehouse/:ref', response: ({ request }) => { deleteRequests.push(new URL(request.url).searchParams.get('delete_data')) return HttpResponse.json( { message: 'Cleanup retry failed' }, { status: 500 } ) }, }) customRender() expect(await screen.findByText('Storage cleanup failed')).toBeInTheDocument() await userEvent.click(screen.getByRole('button', { name: 'Retry' })) expect(await screen.findByText('Cleanup retry failed')).toBeInTheDocument() expect(deleteRequests).toEqual([String(shouldDeleteData)]) } ) test('shows a disable error and allows retrying from the open confirmation', async () => { mockSetupStatus({ setup_status: 'complete' }) mockReplicatedTableQueries() const deleteRequests: (string | null)[] = [] let attempts = 0 addAPIMock({ method: 'delete', path: '/platform/warehouse/:ref', response: ({ request }) => { deleteRequests.push(new URL(request.url).searchParams.get('delete_data')) attempts += 1 if (attempts === 1) { return HttpResponse.json( { message: 'Disable request failed' }, { status: 500 } ) } return HttpResponse.json({ status: 'accepted' }, { status: 202 }) }, }) customRender() await userEvent.click(await screen.findByRole('button', { name: 'Disable Warehouse' })) const dialog = await screen.findByRole('alertdialog') const confirm = within(dialog).getByRole('button', { name: 'Disable Warehouse' }) await userEvent.click( within(dialog).getByRole('checkbox', { name: 'Also delete copied Warehouse data' }) ) fireEvent.click(confirm) const message = await within(dialog).findByText('Disable request failed') expect( message.compareDocumentPosition(within(dialog).getByRole('checkbox')) & Node.DOCUMENT_POSITION_FOLLOWING ).toBeTruthy() expect(dialog).toBeVisible() expect(confirm).toBeEnabled() expect(within(dialog).getByRole('checkbox')).toBeChecked() fireEvent.click(confirm) await waitFor(() => expect(attempts).toBe(2)) expect(deleteRequests).toEqual(['true', 'true']) await waitFor(() => expect(screen.queryByRole('alertdialog')).not.toBeInTheDocument()) }) test('explains legacy storage errors below the deletion option and lets users retain data', async () => { mockSetupStatus({ setup_status: 'complete' }) mockReplicatedTableQueries() const deleteRequests: (string | null)[] = [] addAPIMock({ method: 'delete', path: '/platform/warehouse/:ref', response: ({ request }) => { const shouldDeleteData = new URL(request.url).searchParams.get('delete_data') deleteRequests.push(shouldDeleteData) if (shouldDeleteData === 'true') { return HttpResponse.json( { message: 'Legacy Warehouse data must be retained when disabling Warehouse.' }, { status: 400 } ) } return HttpResponse.json({ status: 'accepted' }, { status: 202 }) }, }) customRender() await userEvent.click(await screen.findByRole('button', { name: 'Disable Warehouse' })) const dialog = await screen.findByRole('alertdialog') const checkbox = within(dialog).getByRole('checkbox', { name: 'Also delete copied Warehouse data', }) await userEvent.click(checkbox) fireEvent.click(within(dialog).getByRole('button', { name: 'Disable Warehouse' })) const message = await within(dialog).findByText( 'Turn this option off to disable Warehouse and keep the copied data.' ) expect(within(dialog).getByText('Copied data cannot be deleted')).toBeInTheDocument() expect( checkbox.compareDocumentPosition(message) & Node.DOCUMENT_POSITION_FOLLOWING ).toBeTruthy() expect(dialog).not.toHaveTextContent('Try refreshing') expect(within(dialog).queryByText(/contact support/i)).not.toBeInTheDocument() expect(checkbox).toBeChecked() await userEvent.click(checkbox) fireEvent.click(within(dialog).getByRole('button', { name: 'Disable Warehouse' })) await waitFor(() => expect(screen.queryByRole('alertdialog')).not.toBeInTheDocument()) expect(deleteRequests).toEqual(['true', 'false']) }) test('shows a status query failure without blocking an unrelated route', async () => { addAPIMock({ method: 'get', path: '/platform/warehouse/:ref/setup-status', response: () => HttpResponse.json({ message: 'Boom' }, { status: 500 }), }) customRender() expect(await screen.findByText('Failed to load Warehouse status')).toBeInTheDocument() }) })