api.test.ts 3.4 KB

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