@@ -14,9 +14,14 @@ import * as sinon from "sinon";
14
14
import * as telemetry from "../../src/telemetry/telemetry" ;
15
15
import { GetNextMessageResponse , MessageType } from "../../src/models/contracts/copilot" ;
16
16
import { ActivityObject , ActivityStatus } from "../../src/sharedInterfaces/telemetry" ;
17
+ import MainController from "../../src/controllers/mainController" ;
18
+ import ConnectionManager , { ConnectionInfo } from "../../src/controllers/connectionManager" ;
17
19
18
20
suite ( "Chat Agent Request Handler Tests" , ( ) => {
19
21
let mockCopilotService : TypeMoq . IMock < CopilotService > ;
22
+ let mockMainController : TypeMoq . IMock < MainController > ;
23
+ let mockConnectionManager : TypeMoq . IMock < ConnectionManager > ;
24
+ let mockConnectionInfo : TypeMoq . IMock < ConnectionInfo > ;
20
25
let mockVscodeWrapper : TypeMoq . IMock < VscodeWrapper > ;
21
26
let mockContext : TypeMoq . IMock < vscode . ExtensionContext > ;
22
27
let mockLmChat : TypeMoq . IMock < vscode . LanguageModelChat > ;
@@ -86,6 +91,20 @@ suite("Chat Agent Request Handler Tests", () => {
86
91
// Mock CopilotService
87
92
mockCopilotService = TypeMoq . Mock . ofType < CopilotService > ( ) ;
88
93
94
+ // Mock connectionManager
95
+ mockConnectionManager = TypeMoq . Mock . ofType < ConnectionManager > ( ) ;
96
+
97
+ // Mock ConnectionInfo
98
+ mockConnectionInfo = TypeMoq . Mock . ofType < ConnectionInfo > ( ) ;
99
+ mockConnectionInfo . setup ( ( x ) => x . credentials . server ) . returns ( ( ) => "server" ) ;
100
+ mockConnectionInfo . setup ( ( x ) => x . credentials . database ) . returns ( ( ) => "database" ) ;
101
+
102
+ // Mock MainController
103
+ mockMainController = TypeMoq . Mock . ofType < MainController > ( ) ;
104
+ mockMainController
105
+ . setup ( ( x ) => x . connectionManager )
106
+ . returns ( ( ) => mockConnectionManager . object ) ;
107
+
89
108
// Mock VscodeWrapper
90
109
mockVscodeWrapper = TypeMoq . Mock . ofType < VscodeWrapper > ( ) ;
91
110
mockVscodeWrapper . setup ( ( x ) => x . activeTextEditorUri ) . returns ( ( ) => sampleConnectionUri ) ;
@@ -137,10 +156,19 @@ suite("Chat Agent Request Handler Tests", () => {
137
156
} ) ( ) ,
138
157
) ;
139
158
140
- // Mock Language Model API
159
+ // Had to create a real object instead of using TypeMoq for the response object
160
+ const mockResponseObject = {
161
+ stream : ( async function * ( ) {
162
+ yield new vscode . LanguageModelTextPart ( sampleReplyText ) ;
163
+ } ) ( ) ,
164
+ text : ( async function * ( ) {
165
+ yield sampleReplyText ;
166
+ } ) ( ) ,
167
+ } ;
168
+
141
169
mockLmChat
142
170
. setup ( ( x ) => x . sendRequest ( TypeMoq . It . isAny ( ) , TypeMoq . It . isAny ( ) , TypeMoq . It . isAny ( ) ) )
143
- . returns ( ( ) => Promise . resolve ( mockLanguageModelChatResponse . object ) ) ;
171
+ . returns ( ( ) => Promise . resolve ( mockResponseObject ) ) ;
144
172
145
173
// Mock TextDocument for reference handling
146
174
mockTextDocument = TypeMoq . Mock . ofType < vscode . TextDocument > ( ) ;
@@ -184,6 +212,7 @@ suite("Chat Agent Request Handler Tests", () => {
184
212
mockCopilotService . object ,
185
213
mockVscodeWrapper . object ,
186
214
mockContext . object ,
215
+ mockMainController . object ,
187
216
) ;
188
217
189
218
expect ( handler ) . to . be . a ( "function" ) ;
@@ -197,6 +226,7 @@ suite("Chat Agent Request Handler Tests", () => {
197
226
mockCopilotService . object ,
198
227
mockVscodeWrapper . object ,
199
228
mockContext . object ,
229
+ mockMainController . object ,
200
230
) ;
201
231
202
232
const result = await handler (
@@ -224,6 +254,11 @@ suite("Chat Agent Request Handler Tests", () => {
224
254
)
225
255
. returns ( ( ) => Promise . resolve ( true ) ) ;
226
256
257
+ // Mock the getConnectionInfo method to return a valid connection
258
+ mockConnectionManager
259
+ . setup ( ( x ) => x . getConnectionInfo ( TypeMoq . It . isAnyString ( ) ) )
260
+ . returns ( ( ) => mockConnectionInfo . object ) ;
261
+
227
262
// Mock the getNextMessage to return a Complete message type
228
263
const completeResponse : GetNextMessageResponse = {
229
264
conversationUri : sampleConversationUri ,
@@ -248,6 +283,7 @@ suite("Chat Agent Request Handler Tests", () => {
248
283
mockCopilotService . object ,
249
284
mockVscodeWrapper . object ,
250
285
mockContext . object ,
286
+ mockMainController . object ,
251
287
) ;
252
288
253
289
const result = await handler (
@@ -282,6 +318,43 @@ suite("Chat Agent Request Handler Tests", () => {
282
318
TypeMoq . Times . once ( ) ,
283
319
) ;
284
320
321
+ mockChatStream . verify (
322
+ ( x ) => x . markdown ( TypeMoq . It . is ( ( msg ) => msg . toString ( ) . startsWith ( "> 🟢" ) ) ) ,
323
+ TypeMoq . Times . once ( ) ,
324
+ ) ;
325
+
326
+ expect ( result ) . to . deep . equal ( {
327
+ metadata : { command : "" , correlationId : sampleCorrelationId } ,
328
+ } ) ;
329
+ } ) ;
330
+
331
+ test ( "Handles conversation with disconnected editor" , async ( ) => {
332
+ // Mock the getConnectionInfo method to return an invalid connection
333
+ mockConnectionManager
334
+ . setup ( ( x ) => x . getConnectionInfo ( TypeMoq . It . isAnyString ( ) ) )
335
+ . returns ( ( ) => {
336
+ return undefined ;
337
+ } ) ;
338
+
339
+ const handler = createSqlAgentRequestHandler (
340
+ mockCopilotService . object ,
341
+ mockVscodeWrapper . object ,
342
+ mockContext . object ,
343
+ mockMainController . object ,
344
+ ) ;
345
+
346
+ const result = await handler (
347
+ mockChatRequest . object ,
348
+ mockChatContext . object ,
349
+ mockChatStream . object ,
350
+ mockToken . object ,
351
+ ) ;
352
+
353
+ mockChatStream . verify (
354
+ ( x ) => x . markdown ( TypeMoq . It . is ( ( msg ) => msg . toString ( ) . startsWith ( "> ⚠️" ) ) ) ,
355
+ TypeMoq . Times . once ( ) ,
356
+ ) ;
357
+
285
358
expect ( result ) . to . deep . equal ( {
286
359
metadata : { command : "" , correlationId : sampleCorrelationId } ,
287
360
} ) ;
@@ -299,6 +372,11 @@ suite("Chat Agent Request Handler Tests", () => {
299
372
)
300
373
. returns ( ( ) => Promise . resolve ( true ) ) ;
301
374
375
+ // Mock the getConnectionInfo method to return a valid connection
376
+ mockConnectionManager
377
+ . setup ( ( x ) => x . getConnectionInfo ( TypeMoq . It . isAnyString ( ) ) )
378
+ . returns ( ( ) => mockConnectionInfo . object ) ;
379
+
302
380
// First return a Fragment message type
303
381
const fragmentResponse : GetNextMessageResponse = {
304
382
conversationUri : sampleConversationUri ,
@@ -337,6 +415,7 @@ suite("Chat Agent Request Handler Tests", () => {
337
415
mockCopilotService . object ,
338
416
mockVscodeWrapper . object ,
339
417
mockContext . object ,
418
+ mockMainController . object ,
340
419
) ;
341
420
342
421
await handler (
@@ -370,10 +449,16 @@ suite("Chat Agent Request Handler Tests", () => {
370
449
)
371
450
. throws ( new Error ( "Connection failed" ) ) ;
372
451
452
+ // Mock the getConnectionInfo method to return a valid connection
453
+ mockConnectionManager
454
+ . setup ( ( x ) => x . getConnectionInfo ( TypeMoq . It . isAnyString ( ) ) )
455
+ . returns ( ( ) => mockConnectionInfo . object ) ;
456
+
373
457
const handler = createSqlAgentRequestHandler (
374
458
mockCopilotService . object ,
375
459
mockVscodeWrapper . object ,
376
460
mockContext . object ,
461
+ mockMainController . object ,
377
462
) ;
378
463
379
464
await handler (
0 commit comments