project-briven-client.test.ts124 lines · main
1import * as brivenJs from '@supabase/supabase-js'
2import { beforeEach, describe, expect, it, vi } from 'vitest'
3
4import { createProjectBrivenClient } from './project-briven-client'
5import * as apiKeysUtils from '@/data/api-keys/temp-api-keys-utils'
6
7vi.mock('@supabase/supabase-js', () => ({
8 createClient: vi.fn(),
9}))
10
11vi.mock('@/data/api-keys/temp-api-keys-utils', () => ({
12 getOrRefreshTemporaryApiKey: vi.fn(),
13}))
14
15describe('project-briven-client', () => {
16 beforeEach(() => {
17 vi.clearAllMocks()
18 })
19
20 describe('createProjectBrivenClient', () => {
21 it('should create a Briven client with temporary API key', async () => {
22 const mockApiKey = 'test-api-key-123'
23 const mockClient = { from: vi.fn() }
24 const projectRef = 'test-project-ref'
25 const clientEndpoint = 'https://test.supabase.co'
26
27 vi.mocked(apiKeysUtils.getOrRefreshTemporaryApiKey).mockResolvedValue({
28 apiKey: mockApiKey,
29 expiryTimeMs: Date.now() + 3600000,
30 })
31 vi.mocked(brivenJs.createClient).mockReturnValue(mockClient as any)
32
33 const result = await createProjectBrivenClient(projectRef, clientEndpoint)
34
35 expect(apiKeysUtils.getOrRefreshTemporaryApiKey).toHaveBeenCalledWith(projectRef)
36 expect(brivenJs.createClient).toHaveBeenCalledWith(clientEndpoint, mockApiKey, {
37 auth: {
38 persistSession: false,
39 autoRefreshToken: false,
40 detectSessionInUrl: false,
41 storage: {
42 getItem: expect.any(Function),
43 setItem: expect.any(Function),
44 removeItem: expect.any(Function),
45 },
46 },
47 })
48 expect(result).toBe(mockClient)
49 })
50
51 it('should configure storage to not persist session', async () => {
52 const mockApiKey = 'test-api-key-456'
53 const mockClient = { from: vi.fn() }
54
55 vi.mocked(apiKeysUtils.getOrRefreshTemporaryApiKey).mockResolvedValue({
56 apiKey: mockApiKey,
57 expiryTimeMs: Date.now() + 3600000,
58 })
59 vi.mocked(brivenJs.createClient).mockReturnValue(mockClient as any)
60
61 await createProjectBrivenClient('ref', 'https://example.com')
62
63 const config = vi.mocked(brivenJs.createClient).mock.calls[0][2]
64 if (!config?.auth?.storage) throw new Error('storage config is missing')
65 const storage = config.auth.storage
66
67 // Test storage methods return expected values
68 expect(storage.getItem('any-key')).toBeNull()
69 expect(storage.setItem('key', 'value')).toBeUndefined()
70 expect(storage.removeItem('key')).toBeUndefined()
71 })
72
73 it('should throw error if API key retrieval fails', async () => {
74 const error = new Error('Failed to get API key')
75 vi.mocked(apiKeysUtils.getOrRefreshTemporaryApiKey).mockRejectedValue(error)
76
77 await expect(createProjectBrivenClient('ref', 'https://example.com')).rejects.toThrow(
78 'Failed to get API key'
79 )
80
81 expect(brivenJs.createClient).not.toHaveBeenCalled()
82 })
83
84 it('should pass through different project refs and endpoints', async () => {
85 const mockApiKey = 'api-key'
86 const mockClient = { from: vi.fn() }
87
88 vi.mocked(apiKeysUtils.getOrRefreshTemporaryApiKey).mockResolvedValue({
89 apiKey: mockApiKey,
90 expiryTimeMs: Date.now() + 3600000,
91 })
92 vi.mocked(brivenJs.createClient).mockReturnValue(mockClient as any)
93
94 await createProjectBrivenClient('project-123', 'https://project123.supabase.co')
95
96 expect(apiKeysUtils.getOrRefreshTemporaryApiKey).toHaveBeenCalledWith('project-123')
97 expect(brivenJs.createClient).toHaveBeenCalledWith(
98 'https://project123.supabase.co',
99 mockApiKey,
100 expect.any(Object)
101 )
102 })
103
104 it('should disable session persistence options', async () => {
105 const mockApiKey = 'api-key'
106 const mockClient = { from: vi.fn() }
107
108 vi.mocked(apiKeysUtils.getOrRefreshTemporaryApiKey).mockResolvedValue({
109 apiKey: mockApiKey,
110 expiryTimeMs: Date.now() + 3600000,
111 })
112 vi.mocked(brivenJs.createClient).mockReturnValue(mockClient as any)
113
114 await createProjectBrivenClient('ref', 'https://example.com')
115
116 const config = vi.mocked(brivenJs.createClient).mock.calls[0][2]
117 if (!config?.auth) throw new Error('auth config is missing')
118
119 expect(config.auth.persistSession).toBe(false)
120 expect(config.auth.autoRefreshToken).toBe(false)
121 expect(config.auth.detectSessionInUrl).toBe(false)
122 })
123 })
124})