Skip to content

The Postgres development platform. Supabase gives you a dedicated Postgres database to build your web, mobile, and AI applications.

4.5 KB
Raw Blame
import { describe, expect, it } from 'vitest'

import { buildEntityMaps, getApiEndpoint } from './DataApi.utils'
import type { ProjectJsonSchemaPaths } from '@/data/docs/project-json-schema-query'
import type { LoadBalancer } from '@/data/read-replicas/load-balancers-query'
import type { Database } from '@/data/read-replicas/replicas-query'

const makeDatabase = (
  identifier: string,
  restUrl: string
): Pick<Database, 'identifier' | 'restUrl'> => ({ identifier, restUrl })

const makeLoadBalancer = (endpoint: string): Pick<LoadBalancer, 'endpoint'> => ({ endpoint })

describe('getApiEndpoint', () => {
  it('returns custom domain URL with /rest/v1/ when custom domain is active and primary database is selected', () => {
    expect(
      getApiEndpoint({
        selectedDatabaseId: 'project-ref',
        projectRef: 'project-ref',
        resolvedEndpoint: 'https://api.example.com',
        loadBalancers: undefined,
        selectedDatabase: makeDatabase(
          'project-ref',
          'https://project-ref.supabase.co/rest/v1/'
        ) as Database,
      })
    ).toBe('https://api.example.com/rest/v1/')
  })

  it('returns database restUrl when custom domain is active but a replica is selected', () => {
    expect(
      getApiEndpoint({
        selectedDatabaseId: 'replica-1',
        projectRef: 'project-ref',
        resolvedEndpoint: 'https://api.example.com',
        loadBalancers: undefined,
        selectedDatabase: makeDatabase(
          'replica-1',
          'https://replica-1.supabase.co/rest/v1/'
        ) as Database,
      })
    ).toBe('https://replica-1.supabase.co/rest/v1/')
  })

  it('normalizes a replica restUrl without a trailing slash', () => {
    expect(
      getApiEndpoint({
        selectedDatabaseId: 'replica-1',
        projectRef: 'project-ref',
        resolvedEndpoint: undefined,
        loadBalancers: undefined,
        selectedDatabase: makeDatabase(
          'replica-1',
          'https://replica-1.supabase.co/rest/v1'
        ) as Database,
      })
    ).toBe('https://replica-1.supabase.co/rest/v1/')
  })

  it('returns load balancer endpoint with /rest/v1/ when load balancer is selected', () => {
    expect(
      getApiEndpoint({
        selectedDatabaseId: 'load-balancer',
        projectRef: 'project-ref',
        resolvedEndpoint: 'https://project-ref.supabase.co',
        loadBalancers: [makeLoadBalancer('https://lb.supabase.co') as LoadBalancer],
        selectedDatabase: undefined,
      })
    ).toBe('https://lb.supabase.co/rest/v1/')
  })

  it('returns empty string when load balancer is selected but none exist', () => {
    expect(
      getApiEndpoint({
        selectedDatabaseId: 'load-balancer',
        projectRef: 'project-ref',
        resolvedEndpoint: 'https://project-ref.supabase.co',
        loadBalancers: undefined,
        selectedDatabase: undefined,
      })
    ).toBe('')
  })
})

describe('buildEntityMaps', () => {
  it.each([undefined, {}])('returns empty maps for %j paths', (paths) => {
    expect(buildEntityMaps(paths)).toEqual({ resources: {}, rpcs: {} })
  })

  it('skips the root path', () => {
    const paths: ProjectJsonSchemaPaths = {
      '/': { get: {} as ProjectJsonSchemaPaths[string]['get'] },
    }
    expect(buildEntityMaps(paths)).toEqual({ resources: {}, rpcs: {} })
  })

  it('enriches entities with displayName and camelCase', () => {
    const paths: ProjectJsonSchemaPaths = {
      '/user_profiles': { get: {} as ProjectJsonSchemaPaths[string]['get'] },
      '/rpc/get_user_count': { post: {} as ProjectJsonSchemaPaths[string]['post'] },
    }
    const result = buildEntityMaps(paths)

    expect(result.resources['user_profiles']).toEqual({
      id: 'user_profiles',
      displayName: 'user profiles',
      camelCase: 'userProfiles',
    })
    expect(result.rpcs['get_user_count']).toEqual({
      id: 'get_user_count',
      displayName: 'get user count',
      camelCase: 'getUserCount',
    })
  })

  it('classifies rpc/ paths as rpcs and other paths as resources', () => {
    const paths: ProjectJsonSchemaPaths = {
      '/': { get: {} as ProjectJsonSchemaPaths[string]['get'] },
      '/users': { get: {} as ProjectJsonSchemaPaths[string]['get'] },
      '/rpc/hello': { post: {} as ProjectJsonSchemaPaths[string]['post'] },
      '/posts': { get: {} as ProjectJsonSchemaPaths[string]['get'] },
      '/rpc/goodbye': { post: {} as ProjectJsonSchemaPaths[string]['post'] },
    }
    const result = buildEntityMaps(paths)
    expect(Object.keys(result.resources)).toEqual(['users', 'posts'])
    expect(Object.keys(result.rpcs)).toEqual(['hello', 'goodbye'])
  })
})