integration-utils.test.ts262 lines · main
1import { beforeEach, describe, expect, it, vi } from 'vitest'
2
3import {
4 getInitialMigrationSQLFromGitHubRepo,
5 getIntegrationConfigurationUrl,
6} from './integration-utils'
7import type {
8 GitHubAccount,
9 Integration,
10 VercelAccount,
11 VercelTeamAccount,
12} from '@/data/integrations/integrations.types'
13
14vi.mock('@/data/fetchers', () => ({
15 fetchHandler: vi.fn(),
16}))
17
18describe('integration-utils', () => {
19 beforeEach(() => {
20 vi.clearAllMocks()
21 })
22
23 describe('getInitialMigrationSQLFromGitHubRepo', () => {
24 it('should return null when no externalId is provided', async () => {
25 const result = await getInitialMigrationSQLFromGitHubRepo()
26 expect(result).toBeNull()
27 })
28
29 it('should fetch and combine migration files correctly', async () => {
30 const mockGitHubUrl = 'https://github.com/org/repo/tree/main/examples/with-briven'
31 const mockBrivenFiles = [{ name: 'seed.sql', download_url: 'https://github.com/seed.sql' }]
32 const mockMigrationFiles = [
33 { name: '20230101000000_initial.sql', download_url: 'https://github.com/migration1.sql' },
34 { name: '20230101000001_second.sql', download_url: 'https://github.com/migration2.sql' },
35 ]
36
37 const mockMigrationContent1 = 'CREATE TABLE users (id serial PRIMARY KEY);'
38 const mockMigrationContent2 = 'CREATE TABLE posts (id serial PRIMARY KEY);'
39 const mockSeedContent = 'INSERT INTO users (id) VALUES (1);'
40
41 // Mock the fetch responses
42 const { fetchHandler } = await import('@/data/fetchers')
43 const mockFetchHandler = fetchHandler as unknown as ReturnType<typeof vi.fn>
44 mockFetchHandler
45 .mockResolvedValueOnce(
46 new Response(JSON.stringify(mockBrivenFiles), {
47 status: 200,
48 statusText: 'OK',
49 headers: { 'Content-Type': 'application/json' },
50 })
51 )
52 .mockResolvedValueOnce(
53 new Response(JSON.stringify(mockMigrationFiles), {
54 status: 200,
55 statusText: 'OK',
56 headers: { 'Content-Type': 'application/json' },
57 })
58 )
59 .mockResolvedValueOnce(
60 new Response(mockSeedContent, {
61 status: 200,
62 statusText: 'OK',
63 headers: { 'Content-Type': 'text/plain' },
64 })
65 )
66 .mockResolvedValueOnce(
67 new Response(mockMigrationContent1, {
68 status: 200,
69 statusText: 'OK',
70 headers: { 'Content-Type': 'text/plain' },
71 })
72 )
73 .mockResolvedValueOnce(
74 new Response(mockMigrationContent2, {
75 status: 200,
76 statusText: 'OK',
77 headers: { 'Content-Type': 'text/plain' },
78 })
79 )
80
81 const result = await getInitialMigrationSQLFromGitHubRepo(mockGitHubUrl)
82
83 expect(result).toContain(mockMigrationContent1)
84 expect(result).toContain(mockMigrationContent2)
85 expect(result).toContain(mockSeedContent)
86 expect(result).toContain('create schema if not exists briven_migrations')
87 expect(result).toContain('create table if not exists briven_migrations.schema_migrations')
88 })
89
90 it('should handle GitHub API errors gracefully', async () => {
91 const mockGitHubUrl = 'https://github.com/org/repo/tree/main/examples/with-briven'
92
93 const { fetchHandler } = await import('@/data/fetchers')
94 const mockFetchHandler = fetchHandler as unknown as ReturnType<typeof vi.fn>
95
96 mockFetchHandler
97 .mockResolvedValueOnce(
98 new Response(null, {
99 status: 404,
100 statusText: 'Not Found',
101 headers: { 'Content-Type': 'application/json' },
102 })
103 )
104 .mockResolvedValueOnce(
105 new Response(null, {
106 status: 404,
107 statusText: 'Not Found',
108 headers: { 'Content-Type': 'application/json' },
109 })
110 )
111
112 const result = await getInitialMigrationSQLFromGitHubRepo(mockGitHubUrl)
113 expect(result).toBeNull()
114 })
115 })
116
117 describe('getIntegrationConfigurationUrl', () => {
118 it('should return correct Vercel configuration URL for personal account', () => {
119 const vercelIntegration: Integration = {
120 id: '123',
121 added_by: {
122 username: 'testuser',
123 id: '123',
124 primary_email: 'test@example.com',
125 },
126 inserted_at: '2024-01-01',
127 updated_at: '2024-01-01',
128 connections: [],
129 organization: { slug: 'org' },
130 integration: { name: 'Vercel' },
131 metadata: {
132 account: {
133 type: 'Personal',
134 name: 'Test User',
135 avatar: 'test-avatar',
136 source: 'marketplace',
137 owner_id: '123',
138 } as VercelAccount,
139 configuration_id: '123',
140 },
141 }
142
143 const result = getIntegrationConfigurationUrl(vercelIntegration)
144 expect(result).toBe('https://vercel.com/dashboard/integrations/123')
145 })
146
147 it('should return correct Vercel configuration URL for team account', () => {
148 const vercelIntegration: Integration = {
149 id: '123',
150 added_by: {
151 username: 'testuser',
152 id: '123',
153 primary_email: 'test@example.com',
154 },
155 inserted_at: '2024-01-01',
156 updated_at: '2024-01-01',
157 connections: [],
158 organization: { slug: 'org' },
159 integration: { name: 'Vercel' },
160 metadata: {
161 account: {
162 type: 'Team',
163 name: 'Test Team',
164 avatar: 'test-avatar',
165 source: 'marketplace',
166 owner_id: '123',
167 team_id: 'team123',
168 team_slug: 'my-team',
169 } as VercelTeamAccount,
170 configuration_id: '123',
171 },
172 }
173
174 const result = getIntegrationConfigurationUrl(vercelIntegration)
175 expect(result).toBe('https://vercel.com/dashboard/my-team/integrations/123')
176 })
177
178 it('should return correct GitHub configuration URL for personal account', () => {
179 const githubIntegration: Integration = {
180 id: '456',
181 added_by: {
182 username: 'testuser',
183 id: '123',
184 primary_email: 'test@example.com',
185 },
186 inserted_at: '2024-01-01',
187 updated_at: '2024-01-01',
188 connections: [],
189 organization: { slug: 'org' },
190 integration: { name: 'GitHub' },
191 metadata: {
192 account: {
193 type: 'User',
194 name: 'Test User',
195 avatar: 'test-avatar',
196 installed_by_user_id: 123,
197 } as GitHubAccount,
198 installation_id: 456,
199 },
200 }
201
202 const result = getIntegrationConfigurationUrl(githubIntegration)
203 expect(result).toBe('https://github.com/settings/installations/456')
204 })
205
206 it('should return correct GitHub configuration URL for organization', () => {
207 const githubIntegration: Integration = {
208 id: '456',
209 added_by: {
210 username: 'testuser',
211 id: '123',
212 primary_email: 'test@example.com',
213 },
214 inserted_at: '2024-01-01',
215 updated_at: '2024-01-01',
216 connections: [],
217 organization: { slug: 'org' },
218 integration: { name: 'GitHub' },
219 metadata: {
220 account: {
221 type: 'Organization',
222 name: 'org-name',
223 avatar: 'test-avatar',
224 installed_by_user_id: 123,
225 } as GitHubAccount,
226 installation_id: 456,
227 },
228 }
229
230 const result = getIntegrationConfigurationUrl(githubIntegration)
231 expect(result).toBe('https://github.com/organizations/org-name/settings/installations/456')
232 })
233
234 it('should return empty string for unknown integration', () => {
235 const unknownIntegration = {
236 id: '789',
237 added_by: {
238 username: 'testuser',
239 id: '123',
240 primary_email: 'test@example.com',
241 },
242 inserted_at: '2024-01-01',
243 updated_at: '2024-01-01',
244 connections: [],
245 organization: { slug: 'org' },
246 integration: { name: 'Unknown' as any },
247 metadata: {
248 account: {
249 type: 'User',
250 name: 'Test User',
251 avatar: 'test-avatar',
252 installed_by_user_id: 123,
253 } as GitHubAccount,
254 installation_id: 789,
255 },
256 } as Integration
257
258 const result = getIntegrationConfigurationUrl(unknownIntegration)
259 expect(result).toBe('')
260 })
261 })
262})