1+ /*---------------------------------------------------------------------------------------------
2+ * Copyright (c) Microsoft Corporation. All rights reserved.
3+ * Licensed under the MIT License. See License.txt in the project root for license information.
4+ *--------------------------------------------------------------------------------------------*/
5+
6+ import { default as assert } from 'assert' ;
7+ import { createSandbox , SinonSandbox } from 'sinon' ;
8+ import * as vscode from 'vscode' ;
9+
10+ import { CopilotRemoteAgentManager } from '../../github/copilotRemoteAgent' ;
11+ import { CredentialStore } from '../../github/credentials' ;
12+ import { RepositoriesManager } from '../../github/repositoriesManager' ;
13+ import { MockTelemetry } from '../mocks/mockTelemetry' ;
14+ import { MockExtensionContext } from '../mocks/mockExtensionContext' ;
15+ import { ReposManagerState } from '../../github/folderRepositoryManager' ;
16+
17+ describe ( 'CopilotRemoteAgentManager' , function ( ) {
18+ let sinon : SinonSandbox ;
19+ let manager : CopilotRemoteAgentManager ;
20+ let credentialStore : CredentialStore ;
21+ let repositoriesManager : RepositoriesManager ;
22+ let telemetry : MockTelemetry ;
23+
24+ beforeEach ( function ( ) {
25+ sinon = createSandbox ( ) ;
26+ telemetry = new MockTelemetry ( ) ;
27+ const context = new MockExtensionContext ( ) ;
28+ credentialStore = new CredentialStore ( telemetry , context ) ;
29+ repositoriesManager = new RepositoriesManager ( credentialStore , telemetry ) ;
30+ manager = new CopilotRemoteAgentManager ( credentialStore , repositoriesManager , telemetry ) ;
31+ } ) ;
32+
33+ afterEach ( function ( ) {
34+ sinon . restore ( ) ;
35+ } ) ;
36+
37+ describe ( 'provideChatSessions' , function ( ) {
38+ it ( 'should wait for repository manager initialization' , async function ( ) {
39+ // Arrange
40+ const token = new vscode . CancellationTokenSource ( ) . token ;
41+
42+ // Mock the copilot API to return undefined (no API available)
43+ sinon . stub ( manager as any , 'copilotApi' ) . get ( ( ) => Promise . resolve ( undefined ) ) ;
44+
45+ // Spy on waitRepoManagerInitialization to verify it's called
46+ const waitSpy = sinon . stub ( manager as any , 'waitRepoManagerInitialization' ) . resolves ( ) ;
47+
48+ // Act
49+ const result = await manager . provideChatSessions ( token ) ;
50+
51+ // Assert
52+ assert ( waitSpy . calledOnce , 'waitRepoManagerInitialization should be called' ) ;
53+ assert . strictEqual ( result . length , 0 , 'Should return empty array when no copilot API' ) ;
54+ } ) ;
55+
56+ it ( 'should call waitRepoManagerInitialization before getAllCodingAgentPRs' , async function ( ) {
57+ // Arrange
58+ const token = new vscode . CancellationTokenSource ( ) . token ;
59+
60+ // Create mock copilot API
61+ const mockCopilotApi = {
62+ getAllCodingAgentPRs : sinon . stub ( ) . resolves ( [ ] )
63+ } ;
64+
65+ sinon . stub ( manager as any , 'copilotApi' ) . get ( ( ) => Promise . resolve ( mockCopilotApi ) ) ;
66+
67+ // Spy on waitRepoManagerInitialization
68+ const waitSpy = sinon . stub ( manager as any , 'waitRepoManagerInitialization' ) . resolves ( ) ;
69+
70+ // Act
71+ await manager . provideChatSessions ( token ) ;
72+
73+ // Assert - verify waitRepoManagerInitialization was called before getAllCodingAgentPRs
74+ assert ( waitSpy . calledBefore ( mockCopilotApi . getAllCodingAgentPRs ) ,
75+ 'waitRepoManagerInitialization should be called before getAllCodingAgentPRs' ) ;
76+ } ) ;
77+
78+ it ( 'should return early if cancelled before waiting for repo manager' , async function ( ) {
79+ // Arrange
80+ const tokenSource = new vscode . CancellationTokenSource ( ) ;
81+ tokenSource . cancel ( ) ; // Cancel immediately
82+ const token = tokenSource . token ;
83+
84+ // Mock the copilot API
85+ const mockCopilotApi = {
86+ getAllCodingAgentPRs : sinon . stub ( ) . resolves ( [ ] )
87+ } ;
88+ sinon . stub ( manager as any , 'copilotApi' ) . get ( ( ) => Promise . resolve ( mockCopilotApi ) ) ;
89+
90+ // Spy on waitRepoManagerInitialization
91+ const waitSpy = sinon . stub ( manager as any , 'waitRepoManagerInitialization' ) . resolves ( ) ;
92+
93+ // Act
94+ const result = await manager . provideChatSessions ( token ) ;
95+
96+ // Assert - should return early and not call waitRepoManagerInitialization
97+ assert ( ! waitSpy . called , 'waitRepoManagerInitialization should not be called when token is cancelled' ) ;
98+ assert ( ! mockCopilotApi . getAllCodingAgentPRs . called , 'getAllCodingAgentPRs should not be called when token is cancelled' ) ;
99+ assert . strictEqual ( result . length , 0 , 'Should return empty array when cancelled' ) ;
100+ } ) ;
101+ } ) ;
102+ } ) ;
0 commit comments