import { HttpResponse } from 'msw' import { describe, expect, it } from 'vitest' import { getMaxReplicas, getReadReplicas, READ_REPLICA_COMPUTE_CAPS, READ_REPLICAS_MAX_COUNT, } from './replicas-query' import { addAPIMock } from '@/tests/lib/msw' describe('getMaxReplicas', () => { it('returns 0 for ineligible compute sizes (pico, nano, micro)', () => { expect(getMaxReplicas('ci_pico')).toBe(0) expect(getMaxReplicas('ci_nano')).toBe(0) expect(getMaxReplicas('ci_micro')).toBe(0) }) it('returns 4 for ci_small', () => { expect(getMaxReplicas('ci_small')).toBe(4) }) it('returns 4 for ci_medium', () => { expect(getMaxReplicas('ci_medium')).toBe(4) }) it('returns 4 for ci_large', () => { expect(getMaxReplicas('ci_large')).toBe(4) }) it('returns READ_REPLICAS_MAX_COUNT for ci_xlarge and above', () => { expect(getMaxReplicas('ci_xlarge')).toBe(READ_REPLICAS_MAX_COUNT) expect(getMaxReplicas('ci_2xlarge')).toBe(READ_REPLICAS_MAX_COUNT) }) it('returns READ_REPLICAS_MAX_COUNT for any unknown compute tier', () => { expect(getMaxReplicas('unknown_tier')).toBe(READ_REPLICAS_MAX_COUNT) }) it('returns READ_REPLICAS_MAX_COUNT when no compute addon is provided', () => { expect(getMaxReplicas(undefined)).toBe(READ_REPLICAS_MAX_COUNT) }) it('all eligible READ_REPLICA_COMPUTE_CAPS are greater than 0 and at most READ_REPLICAS_MAX_COUNT', () => { for (const [, cap] of Object.entries(READ_REPLICA_COMPUTE_CAPS)) { if (cap > 0) { expect(cap).toBeLessThanOrEqual(READ_REPLICAS_MAX_COUNT) } } }) }) describe('getReadReplicas', () => { it('forwards the given headers on the request', async () => { let receivedAuthHeader: string | null = null addAPIMock({ method: 'get', path: '/platform/projects/:ref/databases', response: ({ request }) => { receivedAuthHeader = request.headers.get('Authorization') return HttpResponse.json([]) }, }) await getReadReplicas({ projectRef: 'default' }, undefined, { Authorization: 'Bearer test-token', }) expect(receivedAuthHeader).toBe('Bearer test-token') }) })