33 *
44 * @vitest -environment node
55 */
6+
7+ import { createSession , loggerMock } from '@sim/testing'
68import { 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' } )
911const mockGetSession = vi . fn ( )
1012
1113vi . 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
4136import { db } from '@sim/db'
4237import { 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
5146const mockRefreshOAuthToken = refreshOAuthToken as any
5247
5348describe ( '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