api.test.ts98 lines · main
| 1 | import { beforeEach, describe, expect, it, vi } from 'vitest' |
| 2 | |
| 3 | describe('constants/api', () => { |
| 4 | beforeEach(() => { |
| 5 | vi.resetModules() |
| 6 | }) |
| 7 | |
| 8 | describe('PROJECT_ANALYTICS_URL', () => { |
| 9 | it('should be undefined when LOGFLARE_URL is not set', async () => { |
| 10 | vi.stubEnv('LOGFLARE_URL', '') |
| 11 | const { PROJECT_ANALYTICS_URL } = await import('./api') |
| 12 | expect(PROJECT_ANALYTICS_URL).toBeUndefined() |
| 13 | }) |
| 14 | |
| 15 | it('should use LOGFLARE_URL when set', async () => { |
| 16 | vi.stubEnv('LOGFLARE_URL', 'https://logflare.example.com') |
| 17 | const { PROJECT_ANALYTICS_URL } = await import('./api') |
| 18 | expect(PROJECT_ANALYTICS_URL).toBe('https://logflare.example.com/api/') |
| 19 | }) |
| 20 | }) |
| 21 | |
| 22 | describe('PROJECT_REST_URL', () => { |
| 23 | it('should construct URL from BRIVEN_PUBLIC_URL', async () => { |
| 24 | vi.stubEnv('BRIVEN_PUBLIC_URL', 'https://test.supabase.co') |
| 25 | const { PROJECT_REST_URL } = await import('./api') |
| 26 | expect(PROJECT_REST_URL).toBe('https://test.supabase.co/rest/v1/') |
| 27 | }) |
| 28 | |
| 29 | it('should use default localhost when BRIVEN_PUBLIC_URL is not set', async () => { |
| 30 | vi.stubEnv('BRIVEN_PUBLIC_URL', '') |
| 31 | const { PROJECT_REST_URL } = await import('./api') |
| 32 | expect(PROJECT_REST_URL).toBe('http://localhost:8000/rest/v1/') |
| 33 | }) |
| 34 | }) |
| 35 | |
| 36 | describe('PROJECT_ENDPOINT', () => { |
| 37 | it('should extract host from BRIVEN_PUBLIC_URL', async () => { |
| 38 | vi.stubEnv('BRIVEN_PUBLIC_URL', 'https://test.supabase.co:3000') |
| 39 | const { PROJECT_ENDPOINT } = await import('./api') |
| 40 | expect(PROJECT_ENDPOINT).toBe('test.supabase.co:3000') |
| 41 | }) |
| 42 | |
| 43 | it('should use default localhost host', async () => { |
| 44 | vi.stubEnv('BRIVEN_PUBLIC_URL', '') |
| 45 | const { PROJECT_ENDPOINT } = await import('./api') |
| 46 | expect(PROJECT_ENDPOINT).toBe('localhost:8000') |
| 47 | }) |
| 48 | }) |
| 49 | |
| 50 | describe('PROJECT_ENDPOINT_PROTOCOL', () => { |
| 51 | it('should extract protocol without colon from BRIVEN_PUBLIC_URL', async () => { |
| 52 | vi.stubEnv('BRIVEN_PUBLIC_URL', 'https://test.supabase.co') |
| 53 | const { PROJECT_ENDPOINT_PROTOCOL } = await import('./api') |
| 54 | expect(PROJECT_ENDPOINT_PROTOCOL).toBe('https') |
| 55 | }) |
| 56 | |
| 57 | it('should use http for default localhost', async () => { |
| 58 | vi.stubEnv('BRIVEN_PUBLIC_URL', '') |
| 59 | const { PROJECT_ENDPOINT_PROTOCOL } = await import('./api') |
| 60 | expect(PROJECT_ENDPOINT_PROTOCOL).toBe('http') |
| 61 | }) |
| 62 | }) |
| 63 | |
| 64 | describe('DEFAULT_PROJECT', () => { |
| 65 | it('should have correct default values', async () => { |
| 66 | vi.stubEnv('DEFAULT_PROJECT_NAME', '') |
| 67 | const { DEFAULT_PROJECT } = await import('./api') |
| 68 | |
| 69 | expect(DEFAULT_PROJECT).toEqual({ |
| 70 | id: 1, |
| 71 | ref: 'default', |
| 72 | name: 'Default Project', |
| 73 | organization_id: 1, |
| 74 | cloud_provider: 'localhost', |
| 75 | status: 'ACTIVE_HEALTHY', |
| 76 | region: 'local', |
| 77 | inserted_at: '2021-08-02T06:40:40.646Z', |
| 78 | }) |
| 79 | }) |
| 80 | |
| 81 | it('should use DEFAULT_PROJECT_NAME env var when set', async () => { |
| 82 | vi.stubEnv('DEFAULT_PROJECT_NAME', 'My Custom Project') |
| 83 | const { DEFAULT_PROJECT } = await import('./api') |
| 84 | expect(DEFAULT_PROJECT.name).toBe('My Custom Project') |
| 85 | }) |
| 86 | |
| 87 | it('should have static id and ref', async () => { |
| 88 | const { DEFAULT_PROJECT } = await import('./api') |
| 89 | expect(DEFAULT_PROJECT.id).toBe(1) |
| 90 | expect(DEFAULT_PROJECT.ref).toBe('default') |
| 91 | }) |
| 92 | |
| 93 | it('should have localhost cloud_provider', async () => { |
| 94 | const { DEFAULT_PROJECT } = await import('./api') |
| 95 | expect(DEFAULT_PROJECT.cloud_provider).toBe('localhost') |
| 96 | }) |
| 97 | }) |
| 98 | }) |