1
- import { render , screen , fireEvent , waitFor , act } from "@testing-library/react" ;
1
+ import {
2
+ render ,
3
+ screen ,
4
+ fireEvent ,
5
+ waitFor ,
6
+ act ,
7
+ } from "@testing-library/react" ;
2
8
import "@testing-library/jest-dom" ;
3
9
import { describe , it , beforeEach , jest } from "@jest/globals" ;
4
10
import AuthDebugger from "../AuthDebugger" ;
@@ -10,7 +16,7 @@ const mockOAuthTokens = {
10
16
token_type : "Bearer" ,
11
17
expires_in : 3600 ,
12
18
refresh_token : "test_refresh_token" ,
13
- scope : "test_scope"
19
+ scope : "test_scope" ,
14
20
} ;
15
21
16
22
const mockOAuthMetadata = {
@@ -59,14 +65,14 @@ const sessionStorageMock = {
59
65
removeItem : jest . fn ( ) ,
60
66
clear : jest . fn ( ) ,
61
67
} ;
62
- Object . defineProperty ( window , ' sessionStorage' , {
68
+ Object . defineProperty ( window , " sessionStorage" , {
63
69
value : sessionStorageMock ,
64
70
} ) ;
65
71
66
72
// Mock the location.origin
67
- Object . defineProperty ( window , ' location' , {
73
+ Object . defineProperty ( window , " location" , {
68
74
value : {
69
- origin : ' http://localhost:3000' ,
75
+ origin : " http://localhost:3000" ,
70
76
} ,
71
77
} ) ;
72
78
@@ -79,11 +85,13 @@ describe("AuthDebugger", () => {
79
85
beforeEach ( ( ) => {
80
86
jest . clearAllMocks ( ) ;
81
87
sessionStorageMock . getItem . mockReturnValue ( null ) ;
82
- ( mockDiscoverOAuthMetadata as jest . Mock ) . mockResolvedValue ( mockOAuthMetadata ) ;
88
+ ( mockDiscoverOAuthMetadata as jest . Mock ) . mockResolvedValue (
89
+ mockOAuthMetadata ,
90
+ ) ;
83
91
( mockRegisterClient as jest . Mock ) . mockResolvedValue ( mockOAuthClientInfo ) ;
84
92
( mockStartAuthorization as jest . Mock ) . mockResolvedValue ( {
85
93
authorizationUrl : new URL ( "https://oauth.example.com/authorize" ) ,
86
- codeVerifier : "test_verifier"
94
+ codeVerifier : "test_verifier" ,
87
95
} ) ;
88
96
( mockExchangeAuthorization as jest . Mock ) . mockResolvedValue ( mockOAuthTokens ) ;
89
97
} ) ;
@@ -92,7 +100,7 @@ describe("AuthDebugger", () => {
92
100
return render (
93
101
< TooltipProvider >
94
102
< AuthDebugger { ...defaultProps } { ...props } />
95
- </ TooltipProvider >
103
+ </ TooltipProvider > ,
96
104
) ;
97
105
} ;
98
106
@@ -119,26 +127,27 @@ describe("AuthDebugger", () => {
119
127
await act ( async ( ) => {
120
128
renderAuthDebugger ( ) ;
121
129
} ) ;
122
-
130
+
123
131
await act ( async ( ) => {
124
132
fireEvent . click ( screen . getByText ( "Guided OAuth Flow" ) ) ;
125
133
} ) ;
126
-
134
+
127
135
expect ( screen . getByText ( "OAuth Flow Progress" ) ) . toBeInTheDocument ( ) ;
128
136
} ) ;
129
137
130
138
it ( "should show error when OAuth flow is started without sseUrl" , async ( ) => {
131
139
await act ( async ( ) => {
132
140
renderAuthDebugger ( { sseUrl : "" } ) ;
133
141
} ) ;
134
-
142
+
135
143
await act ( async ( ) => {
136
144
fireEvent . click ( screen . getByText ( "Guided OAuth Flow" ) ) ;
137
145
} ) ;
138
-
146
+
139
147
expect ( mockToast ) . toHaveBeenCalledWith ( {
140
148
title : "Error" ,
141
- description : "Please enter a server URL in the sidebar before authenticating" ,
149
+ description :
150
+ "Please enter a server URL in the sidebar before authenticating" ,
142
151
variant : "destructive" ,
143
152
} ) ;
144
153
} ) ;
@@ -153,11 +162,11 @@ describe("AuthDebugger", () => {
153
162
}
154
163
return null ;
155
164
} ) ;
156
-
165
+
157
166
await act ( async ( ) => {
158
167
renderAuthDebugger ( ) ;
159
168
} ) ;
160
-
169
+
161
170
await waitFor ( ( ) => {
162
171
expect ( screen . getByText ( / A c c e s s T o k e n : / ) ) . toBeInTheDocument ( ) ;
163
172
} ) ;
@@ -167,22 +176,22 @@ describe("AuthDebugger", () => {
167
176
// Mock console to avoid cluttering test output
168
177
const originalError = console . error ;
169
178
console . error = jest . fn ( ) ;
170
-
179
+
171
180
// Mock getItem to return invalid JSON for tokens
172
181
sessionStorageMock . getItem . mockImplementation ( ( key ) => {
173
182
if ( key === "[https://example.com] mcp_tokens" ) {
174
183
return "invalid json" ;
175
184
}
176
185
return null ;
177
186
} ) ;
178
-
187
+
179
188
await act ( async ( ) => {
180
189
renderAuthDebugger ( ) ;
181
190
} ) ;
182
-
191
+
183
192
// Component should still render despite the error
184
193
expect ( screen . getByText ( "Authentication Settings" ) ) . toBeInTheDocument ( ) ;
185
-
194
+
186
195
// Restore console.error
187
196
console . error = originalError ;
188
197
} ) ;
@@ -197,7 +206,7 @@ describe("AuthDebugger", () => {
197
206
}
198
207
return null ;
199
208
} ) ;
200
-
209
+
201
210
await act ( async ( ) => {
202
211
renderAuthDebugger ( ) ;
203
212
} ) ;
@@ -221,21 +230,23 @@ describe("AuthDebugger", () => {
221
230
await act ( async ( ) => {
222
231
renderAuthDebugger ( ) ;
223
232
} ) ;
224
-
233
+
225
234
// Start guided flow
226
235
await act ( async ( ) => {
227
236
fireEvent . click ( screen . getByText ( "Guided OAuth Flow" ) ) ;
228
237
} ) ;
229
-
238
+
230
239
// Verify metadata discovery step
231
240
expect ( screen . getByText ( "Metadata Discovery" ) ) . toBeInTheDocument ( ) ;
232
-
241
+
233
242
// Click Continue - this should trigger metadata discovery
234
243
await act ( async ( ) => {
235
244
fireEvent . click ( screen . getByText ( "Continue" ) ) ;
236
245
} ) ;
237
-
238
- expect ( mockDiscoverOAuthMetadata ) . toHaveBeenCalledWith ( "https://example.com" ) ;
246
+
247
+ expect ( mockDiscoverOAuthMetadata ) . toHaveBeenCalledWith (
248
+ "https://example.com" ,
249
+ ) ;
239
250
} ) ;
240
251
} ) ;
241
- } ) ;
252
+ } ) ;
0 commit comments