Skip to content

Commit 613f782

Browse files
committed
feat(tests): added testing package, overhauled tests
1 parent 3201aba commit 613f782

File tree

111 files changed

+20228
-1883
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

111 files changed

+20228
-1883
lines changed

apps/sim/app/api/__test-utils__/utils.ts

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,12 @@
1+
import { createMockLogger as createSimTestingMockLogger } from '@sim/testing'
12
import { NextRequest } from 'next/server'
23
import { vi } from 'vitest'
34

5+
/**
6+
* Re-export createMockLogger from @sim/testing for tests that need to create their own instance
7+
*/
8+
export { createMockLogger } from '@sim/testing'
9+
410
export interface MockUser {
511
id: string
612
email: string
@@ -214,12 +220,11 @@ export const mockDb = {
214220
})),
215221
}
216222

217-
export const mockLogger = {
218-
info: vi.fn(),
219-
warn: vi.fn(),
220-
error: vi.fn(),
221-
debug: vi.fn(),
222-
}
223+
/**
224+
* Mock logger using @sim/testing createMockLogger.
225+
* This provides a consistent mock logger across all API tests.
226+
*/
227+
export const mockLogger = createSimTestingMockLogger()
223228

224229
export const mockUser = {
225230
id: 'user-123',
@@ -729,7 +734,8 @@ export function mockKnowledgeSchemas() {
729734
}
730735

731736
/**
732-
* Mock console logger
737+
* Mock console logger using the shared mockLogger instance.
738+
* This ensures tests can assert on the same mockLogger instance exported from this module.
733739
*/
734740
export function mockConsoleLogger() {
735741
vi.doMock('@/lib/logs/console/logger', () => ({

apps/sim/app/api/auth/oauth/connections/route.test.ts

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
* @vitest-environment node
55
*/
66
import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest'
7-
import { createMockRequest } from '@/app/api/__test-utils__/utils'
7+
import { createMockLogger, createMockRequest } from '@/app/api/__test-utils__/utils'
88

99
describe('OAuth Connections API Route', () => {
1010
const mockGetSession = vi.fn()
@@ -14,12 +14,7 @@ describe('OAuth Connections API Route', () => {
1414
where: vi.fn().mockReturnThis(),
1515
limit: vi.fn(),
1616
}
17-
const mockLogger = {
18-
info: vi.fn(),
19-
warn: vi.fn(),
20-
error: vi.fn(),
21-
debug: vi.fn(),
22-
}
17+
const mockLogger = createMockLogger()
2318
const mockParseProvider = vi.fn()
2419
const mockEvaluateScopeCoverage = vi.fn()
2520

apps/sim/app/api/auth/oauth/credentials/route.test.ts

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66

77
import { NextRequest } from 'next/server'
88
import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest'
9+
import { createMockLogger } from '@/app/api/__test-utils__/utils'
910

1011
describe('OAuth Credentials API Route', () => {
1112
const mockGetSession = vi.fn()
@@ -17,12 +18,7 @@ describe('OAuth Credentials API Route', () => {
1718
where: vi.fn().mockReturnThis(),
1819
limit: vi.fn(),
1920
}
20-
const mockLogger = {
21-
info: vi.fn(),
22-
warn: vi.fn(),
23-
error: vi.fn(),
24-
debug: vi.fn(),
25-
}
21+
const mockLogger = createMockLogger()
2622

2723
const mockUUID = 'mock-uuid-12345678-90ab-cdef-1234-567890abcdef'
2824

apps/sim/app/api/auth/oauth/disconnect/route.test.ts

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -4,20 +4,15 @@
44
* @vitest-environment node
55
*/
66
import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest'
7-
import { createMockRequest } from '@/app/api/__test-utils__/utils'
7+
import { createMockLogger, createMockRequest } from '@/app/api/__test-utils__/utils'
88

99
describe('OAuth Disconnect API Route', () => {
1010
const mockGetSession = vi.fn()
1111
const mockDb = {
1212
delete: vi.fn().mockReturnThis(),
1313
where: vi.fn(),
1414
}
15-
const mockLogger = {
16-
info: vi.fn(),
17-
warn: vi.fn(),
18-
error: vi.fn(),
19-
debug: vi.fn(),
20-
}
15+
const mockLogger = createMockLogger()
2116

2217
const mockUUID = 'mock-uuid-12345678-90ab-cdef-1234-567890abcdef'
2318

apps/sim/app/api/auth/oauth/token/route.test.ts

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
* @vitest-environment node
55
*/
66
import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest'
7-
import { createMockRequest } from '@/app/api/__test-utils__/utils'
7+
import { createMockLogger, createMockRequest } from '@/app/api/__test-utils__/utils'
88

99
describe('OAuth Token API Routes', () => {
1010
const mockGetUserId = vi.fn()
@@ -13,12 +13,7 @@ describe('OAuth Token API Routes', () => {
1313
const mockAuthorizeCredentialUse = vi.fn()
1414
const mockCheckHybridAuth = vi.fn()
1515

16-
const mockLogger = {
17-
info: vi.fn(),
18-
warn: vi.fn(),
19-
error: vi.fn(),
20-
debug: vi.fn(),
21-
}
16+
const mockLogger = createMockLogger()
2217

2318
const mockUUID = 'mock-uuid-12345678-90ab-cdef-1234-567890abcdef'
2419
const mockRequestId = mockUUID.slice(0, 8)

apps/sim/app/api/auth/oauth/utils.test.ts

Lines changed: 33 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,11 @@
33
*
44
* @vitest-environment node
55
*/
6+
7+
import { createSession, loggerMock } from '@sim/testing'
68
import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest'
79

8-
const mockSession = { user: { id: 'test-user-id' } }
10+
const mockSession = createSession({ userId: 'test-user-id' })
911
const mockGetSession = vi.fn()
1012

1113
vi.mock('@/lib/auth', () => ({
@@ -29,14 +31,7 @@ vi.mock('@/lib/oauth/oauth', () => ({
2931
OAUTH_PROVIDERS: {},
3032
}))
3133

32-
vi.mock('@/lib/logs/console/logger', () => ({
33-
createLogger: vi.fn().mockReturnValue({
34-
info: vi.fn(),
35-
warn: vi.fn(),
36-
error: vi.fn(),
37-
debug: vi.fn(),
38-
}),
39-
}))
34+
vi.mock('@/lib/logs/console/logger', () => loggerMock)
4035

4136
import { db } from '@sim/db'
4237
import { refreshOAuthToken } from '@/lib/oauth'
@@ -47,14 +42,14 @@ import {
4742
refreshTokenIfNeeded,
4843
} from '@/app/api/auth/oauth/utils'
4944

50-
const mockDb = db as any
45+
const mockDbTyped = db as any
5146
const mockRefreshOAuthToken = refreshOAuthToken as any
5247

5348
describe('OAuth Utils', () => {
5449
beforeEach(() => {
5550
vi.clearAllMocks()
5651
mockGetSession.mockResolvedValue(mockSession)
57-
mockDb.limit.mockReturnValue([])
52+
mockDbTyped.limit.mockReturnValue([])
5853
})
5954

6055
afterEach(() => {
@@ -69,14 +64,14 @@ describe('OAuth Utils', () => {
6964
})
7065

7166
it('should get user ID from workflow when workflowId is provided', async () => {
72-
mockDb.limit.mockReturnValueOnce([{ userId: 'workflow-owner-id' }])
67+
mockDbTyped.limit.mockReturnValueOnce([{ userId: 'workflow-owner-id' }])
7368

7469
const userId = await getUserId('request-id', 'workflow-id')
7570

76-
expect(mockDb.select).toHaveBeenCalled()
77-
expect(mockDb.from).toHaveBeenCalled()
78-
expect(mockDb.where).toHaveBeenCalled()
79-
expect(mockDb.limit).toHaveBeenCalledWith(1)
71+
expect(mockDbTyped.select).toHaveBeenCalled()
72+
expect(mockDbTyped.from).toHaveBeenCalled()
73+
expect(mockDbTyped.where).toHaveBeenCalled()
74+
expect(mockDbTyped.limit).toHaveBeenCalledWith(1)
8075
expect(userId).toBe('workflow-owner-id')
8176
})
8277

@@ -89,7 +84,7 @@ describe('OAuth Utils', () => {
8984
})
9085

9186
it('should return undefined if workflow is not found', async () => {
92-
mockDb.limit.mockReturnValueOnce([])
87+
mockDbTyped.limit.mockReturnValueOnce([])
9388

9489
const userId = await getUserId('request-id', 'nonexistent-workflow-id')
9590

@@ -100,20 +95,20 @@ describe('OAuth Utils', () => {
10095
describe('getCredential', () => {
10196
it('should return credential when found', async () => {
10297
const mockCredential = { id: 'credential-id', userId: 'test-user-id' }
103-
mockDb.limit.mockReturnValueOnce([mockCredential])
98+
mockDbTyped.limit.mockReturnValueOnce([mockCredential])
10499

105100
const credential = await getCredential('request-id', 'credential-id', 'test-user-id')
106101

107-
expect(mockDb.select).toHaveBeenCalled()
108-
expect(mockDb.from).toHaveBeenCalled()
109-
expect(mockDb.where).toHaveBeenCalled()
110-
expect(mockDb.limit).toHaveBeenCalledWith(1)
102+
expect(mockDbTyped.select).toHaveBeenCalled()
103+
expect(mockDbTyped.from).toHaveBeenCalled()
104+
expect(mockDbTyped.where).toHaveBeenCalled()
105+
expect(mockDbTyped.limit).toHaveBeenCalledWith(1)
111106

112107
expect(credential).toEqual(mockCredential)
113108
})
114109

115110
it('should return undefined when credential is not found', async () => {
116-
mockDb.limit.mockReturnValueOnce([])
111+
mockDbTyped.limit.mockReturnValueOnce([])
117112

118113
const credential = await getCredential('request-id', 'nonexistent-id', 'test-user-id')
119114

@@ -127,7 +122,7 @@ describe('OAuth Utils', () => {
127122
id: 'credential-id',
128123
accessToken: 'valid-token',
129124
refreshToken: 'refresh-token',
130-
accessTokenExpiresAt: new Date(Date.now() + 3600 * 1000), // 1 hour in the future
125+
accessTokenExpiresAt: new Date(Date.now() + 3600 * 1000),
131126
providerId: 'google',
132127
}
133128

@@ -142,7 +137,7 @@ describe('OAuth Utils', () => {
142137
id: 'credential-id',
143138
accessToken: 'expired-token',
144139
refreshToken: 'refresh-token',
145-
accessTokenExpiresAt: new Date(Date.now() - 3600 * 1000), // 1 hour in the past
140+
accessTokenExpiresAt: new Date(Date.now() - 3600 * 1000),
146141
providerId: 'google',
147142
}
148143

@@ -155,8 +150,8 @@ describe('OAuth Utils', () => {
155150
const result = await refreshTokenIfNeeded('request-id', mockCredential, 'credential-id')
156151

157152
expect(mockRefreshOAuthToken).toHaveBeenCalledWith('google', 'refresh-token')
158-
expect(mockDb.update).toHaveBeenCalled()
159-
expect(mockDb.set).toHaveBeenCalled()
153+
expect(mockDbTyped.update).toHaveBeenCalled()
154+
expect(mockDbTyped.set).toHaveBeenCalled()
160155
expect(result).toEqual({ accessToken: 'new-token', refreshed: true })
161156
})
162157

@@ -165,7 +160,7 @@ describe('OAuth Utils', () => {
165160
id: 'credential-id',
166161
accessToken: 'expired-token',
167162
refreshToken: 'refresh-token',
168-
accessTokenExpiresAt: new Date(Date.now() - 3600 * 1000), // 1 hour in the past
163+
accessTokenExpiresAt: new Date(Date.now() - 3600 * 1000),
169164
providerId: 'google',
170165
}
171166

@@ -181,7 +176,7 @@ describe('OAuth Utils', () => {
181176
id: 'credential-id',
182177
accessToken: 'token',
183178
refreshToken: null,
184-
accessTokenExpiresAt: new Date(Date.now() - 3600 * 1000), // 1 hour in the past
179+
accessTokenExpiresAt: new Date(Date.now() - 3600 * 1000),
185180
providerId: 'google',
186181
}
187182

@@ -198,11 +193,11 @@ describe('OAuth Utils', () => {
198193
id: 'credential-id',
199194
accessToken: 'valid-token',
200195
refreshToken: 'refresh-token',
201-
accessTokenExpiresAt: new Date(Date.now() + 3600 * 1000), // 1 hour in the future
196+
accessTokenExpiresAt: new Date(Date.now() + 3600 * 1000),
202197
providerId: 'google',
203198
userId: 'test-user-id',
204199
}
205-
mockDb.limit.mockReturnValueOnce([mockCredential])
200+
mockDbTyped.limit.mockReturnValueOnce([mockCredential])
206201

207202
const token = await refreshAccessTokenIfNeeded('credential-id', 'test-user-id', 'request-id')
208203

@@ -215,11 +210,11 @@ describe('OAuth Utils', () => {
215210
id: 'credential-id',
216211
accessToken: 'expired-token',
217212
refreshToken: 'refresh-token',
218-
accessTokenExpiresAt: new Date(Date.now() - 3600 * 1000), // 1 hour in the past
213+
accessTokenExpiresAt: new Date(Date.now() - 3600 * 1000),
219214
providerId: 'google',
220215
userId: 'test-user-id',
221216
}
222-
mockDb.limit.mockReturnValueOnce([mockCredential])
217+
mockDbTyped.limit.mockReturnValueOnce([mockCredential])
223218

224219
mockRefreshOAuthToken.mockResolvedValueOnce({
225220
accessToken: 'new-token',
@@ -230,13 +225,13 @@ describe('OAuth Utils', () => {
230225
const token = await refreshAccessTokenIfNeeded('credential-id', 'test-user-id', 'request-id')
231226

232227
expect(mockRefreshOAuthToken).toHaveBeenCalledWith('google', 'refresh-token')
233-
expect(mockDb.update).toHaveBeenCalled()
234-
expect(mockDb.set).toHaveBeenCalled()
228+
expect(mockDbTyped.update).toHaveBeenCalled()
229+
expect(mockDbTyped.set).toHaveBeenCalled()
235230
expect(token).toBe('new-token')
236231
})
237232

238233
it('should return null if credential not found', async () => {
239-
mockDb.limit.mockReturnValueOnce([])
234+
mockDbTyped.limit.mockReturnValueOnce([])
240235

241236
const token = await refreshAccessTokenIfNeeded('nonexistent-id', 'test-user-id', 'request-id')
242237

@@ -248,11 +243,11 @@ describe('OAuth Utils', () => {
248243
id: 'credential-id',
249244
accessToken: 'expired-token',
250245
refreshToken: 'refresh-token',
251-
accessTokenExpiresAt: new Date(Date.now() - 3600 * 1000), // 1 hour in the past
246+
accessTokenExpiresAt: new Date(Date.now() - 3600 * 1000),
252247
providerId: 'google',
253248
userId: 'test-user-id',
254249
}
255-
mockDb.limit.mockReturnValueOnce([mockCredential])
250+
mockDbTyped.limit.mockReturnValueOnce([mockCredential])
256251

257252
mockRefreshOAuthToken.mockResolvedValueOnce(null)
258253

0 commit comments

Comments
 (0)