import { waitFor } from '@testing-library/react' import { describe, expect, it } from 'vitest' import { useProjectApiUrl } from './project-endpoint-query' import { customRenderHook } from '@/tests/lib/custom-render' import { addAPIMock } from '@/tests/lib/msw' const mockSettings = () => addAPIMock({ method: 'get', path: '/platform/projects/:ref/settings', response: { app_config: { endpoint: 'default.supabase.co', db_schema: 'public', storage_endpoint: 'default.supabase.co', }, cloud_provider: 'AWS', db_dns_name: 'db.default.supabase.co', db_host: 'db.default.supabase.co', db_name: 'postgres', db_port: 5432, db_user: 'postgres', inserted_at: '2026-01-01T00:00:00.000Z', name: 'Production', ref: 'default', region: 'us-east-1', ssl_enforced: false, status: 'ACTIVE_HEALTHY', }, }) const mockCustomDomain = (status: '4_origin_setup_completed' | '5_services_reconfigured') => { addAPIMock({ method: 'get', path: '/platform/projects/:ref/billing/addons', response: { selected_addons: [ { type: 'custom_domain', variant: { identifier: 'cd_default', name: 'Custom domain', price: 10, price_description: 'per month', price_interval: 'monthly', price_type: 'fixed', }, }, ], available_addons: [], ref: 'default', }, }) addAPIMock({ method: 'get', path: '/v1/projects/:ref/custom-hostname', response: { status, custom_hostname: 'api.example.com', data: { errors: [], messages: [], success: true, // The hostname already reports `active` before services are reconfigured. result: { hostname: 'api.example.com', id: 'hostname-id', status: 'active' }, }, }, }) } describe('useProjectApiUrl', () => { it('keeps the project host until the custom domain finishes activating', async () => { mockSettings() mockCustomDomain('4_origin_setup_completed') const { result } = customRenderHook(() => useProjectApiUrl({ projectRef: 'default' })) await waitFor(() => expect(result.current.isSuccess).toBe(true)) expect(result.current.customEndpoint).toBeUndefined() expect(result.current.data).toBe('https://default.supabase.co') }) it('uses the custom domain once services are reconfigured', async () => { mockSettings() mockCustomDomain('5_services_reconfigured') const { result } = customRenderHook(() => useProjectApiUrl({ projectRef: 'default' })) await waitFor(() => expect(result.current.isSuccess).toBe(true)) expect(result.current.customEndpoint).toBe('https://api.example.com') expect(result.current.data).toBe('https://api.example.com') }) })