-
Notifications
You must be signed in to change notification settings - Fork 735
Expand file tree
/
Copy pathcopilotRemoteAgent.test.ts
More file actions
422 lines (340 loc) · 14.4 KB
/
copilotRemoteAgent.test.ts
File metadata and controls
422 lines (340 loc) · 14.4 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
/*---------------------------------------------------------------------------------------------
* Copyright (c) Microsoft Corporation. All rights reserved.
* Licensed under the MIT License. See License.txt in the project root for license information.
*--------------------------------------------------------------------------------------------*/
import { default as assert } from 'assert';
import { SinonSandbox, createSandbox } from 'sinon';
import * as vscode from 'vscode';
import { CopilotRemoteAgentManager } from '../../github/copilotRemoteAgent';
import { MockCommandRegistry } from '../mocks/mockCommandRegistry';
import { MockTelemetry } from '../mocks/mockTelemetry';
import { CredentialStore } from '../../github/credentials';
import { RepositoriesManager } from '../../github/repositoriesManager';
import { MockExtensionContext } from '../mocks/mockExtensionContext';
import { Resource } from '../../common/resources';
import { PullRequestModel } from '../../github/pullRequestModel';
import { MockGitHubRepository } from '../mocks/mockGitHubRepository';
import { GitHubRemote } from '../../common/remote';
import { Protocol } from '../../common/protocol';
import { GitHubServerType } from '../../common/authentication';
import { ReposManagerState } from '../../github/folderRepositoryManager';
import { CopilotPRStatus } from '../../common/copilot';
const telemetry = new MockTelemetry();
const protocol = new Protocol('https://github.com/github/test.git');
const remote = new GitHubRemote('test', 'github/test', protocol, GitHubServerType.GitHubDotCom);
describe('CopilotRemoteAgentManager', function () {
let sinon: SinonSandbox;
let manager: CopilotRemoteAgentManager;
let credentialStore: CredentialStore;
let reposManager: RepositoriesManager;
let context: MockExtensionContext;
let mockRepo: MockGitHubRepository;
beforeEach(function () {
sinon = createSandbox();
MockCommandRegistry.install(sinon);
// Mock workspace configuration to return disabled by default
sinon.stub(vscode.workspace, 'getConfiguration').callsFake((section?: string) => ({
get: sinon.stub().callsFake((key: string, defaultValue?: any) => {
if (section === 'githubPR.copilotRemoteAgent' && key === 'enabled') {
return false; // Default to disabled
}
if (section === 'githubPR.copilotRemoteAgent' && key === 'autoCommitAndPushEnabled') {
return false;
}
if (section === 'githubPR.copilotRemoteAgent' && key === 'promptForConfirmation') {
return true;
}
return defaultValue;
}),
update: sinon.stub().resolves(),
has: sinon.stub().returns(true),
inspect: sinon.stub()
} as any));
context = new MockExtensionContext();
credentialStore = new CredentialStore(telemetry, context);
reposManager = new RepositoriesManager(credentialStore, telemetry);
mockRepo = new MockGitHubRepository(remote, credentialStore, telemetry, sinon);
manager = new CopilotRemoteAgentManager(credentialStore, reposManager, telemetry, context);
Resource.initialize(context);
});
afterEach(function () {
manager.dispose();
reposManager.dispose();
credentialStore.dispose();
context.dispose();
mockRepo.dispose();
sinon.restore();
});
describe('enabled', function () {
it('should return false when coding agent is disabled by default', function () {
// The config should default to disabled state
const enabled = manager.enabled;
assert.strictEqual(enabled, false);
});
it('should reflect configuration changes', function () {
// Test would require mocking workspace configuration
// For now, just test the getter exists
assert.strictEqual(typeof manager.enabled, 'boolean');
});
});
describe('autoCommitAndPushEnabled', function () {
it('should return boolean value', function () {
const autoCommitEnabled = manager.autoCommitAndPushEnabled;
assert.strictEqual(typeof autoCommitEnabled, 'boolean');
});
});
describe('isAssignable()', function () {
it('should return false when no repository info is available', async function () {
// No folder managers setup
const result = await manager.isAssignable();
assert.strictEqual(result, false);
});
it('should return false when assignable users cannot be fetched', async function () {
// Mock repository manager state but no assignable users
sinon.stub(manager, 'repoInfo').resolves(undefined);
const result = await manager.isAssignable();
assert.strictEqual(result, false);
});
});
describe('isAvailable()', function () {
it('should return false when manager is disabled', async function () {
sinon.stub(manager, 'enabled').get(() => false);
const result = await manager.isAvailable();
assert.strictEqual(result, false);
});
it('should return false when no repo info is available', async function () {
sinon.stub(manager, 'enabled').get(() => true);
sinon.stub(manager, 'repoInfo').resolves(undefined);
const result = await manager.isAvailable();
assert.strictEqual(result, false);
});
it('should return false when copilot API is not available', async function () {
sinon.stub(manager, 'enabled').get(() => true);
sinon.stub(manager, 'repoInfo').resolves({
owner: 'test',
repo: 'test',
baseRef: 'main',
remote: remote,
repository: {} as any,
ghRepository: mockRepo,
fm: {} as any
});
// copilotApi will return undefined by default in tests
const result = await manager.isAvailable();
assert.strictEqual(result, false);
});
});
describe('repoInfo()', function () {
it('should return undefined when no folder managers exist', async function () {
const result = await manager.repoInfo();
assert.strictEqual(result, undefined);
});
it('should return undefined when no repository is found', async function () {
// Mock empty folder managers
sinon.stub(reposManager, 'folderManagers').get(() => []);
const result = await manager.repoInfo();
assert.strictEqual(result, undefined);
});
});
describe('addFollowUpToExistingPR()', function () {
it('should return undefined when no repo info is available', async function () {
sinon.stub(manager, 'repoInfo').resolves(undefined);
const result = await manager.addFollowUpToExistingPR(123, 'test prompt');
assert.strictEqual(result, undefined);
});
it('should return undefined when PR is not found', async function () {
sinon.stub(manager, 'repoInfo').resolves({
owner: 'test',
repo: 'test',
baseRef: 'main',
remote: remote,
repository: {} as any,
ghRepository: mockRepo,
fm: {} as any
});
sinon.stub(mockRepo, 'getPullRequest').resolves(undefined);
const result = await manager.addFollowUpToExistingPR(123, 'test prompt');
assert.strictEqual(result, undefined);
});
});
describe('invokeRemoteAgent()', function () {
it('should return error when copilot API is not available', async function () {
const result = await manager.invokeRemoteAgent('test prompt', 'test context');
assert.strictEqual(result.state, 'error');
if (result.state === 'error') {
assert(result.error.includes('Failed to initialize Copilot API'));
}
});
it('should return error when no repository info is available', async function () {
// Mock copilot API to be available but no repo info
sinon.stub(manager as any, '_copilotApiPromise').value(Promise.resolve({} as any));
sinon.stub(manager, 'repoInfo').resolves(undefined);
const result = await manager.invokeRemoteAgent('test prompt', 'test context');
assert.strictEqual(result.state, 'error');
if (result.state === 'error') {
assert(result.error.includes('No repository information found'));
}
});
});
describe('getSessionLogsFromAction()', function () {
it('should return empty array when copilot API is not available', async function () {
const mockPr = {} as PullRequestModel;
const result = await manager.getSessionLogsFromAction(mockPr);
assert.strictEqual(Array.isArray(result), true);
assert.strictEqual(result.length, 0);
});
});
describe('getWorkflowStepsFromAction()', function () {
it('should return empty array when no workflow run is found', async function () {
const mockPr = {} as PullRequestModel;
sinon.stub(manager, 'getLatestCodingAgentFromAction').resolves(undefined);
const result = await manager.getWorkflowStepsFromAction(mockPr);
assert.strictEqual(Array.isArray(result), true);
assert.strictEqual(result.length, 0);
});
});
describe('getSessionLogFromPullRequest()', function () {
it('should return undefined when copilot API is not available', async function () {
const mockPr = {} as PullRequestModel;
const result = await manager.getSessionLogFromPullRequest(mockPr);
assert.strictEqual(result, undefined);
});
});
describe('hasNotification()', function () {
it('should return false when no notification exists', function () {
const result = manager.hasNotification('owner', 'repo', 123);
assert.strictEqual(result, false);
});
});
describe('getStateForPR()', function () {
it('should return default state for unknown PR', function () {
const result = manager.getStateForPR('owner', 'repo', 123);
// Should return a valid CopilotPRStatus
assert(Object.values(CopilotPRStatus).includes(result));
});
});
describe('getCounts()', function () {
it('should return valid counts object', function () {
const result = manager.getCounts();
assert.strictEqual(typeof result.total, 'number');
assert.strictEqual(typeof result.inProgress, 'number');
assert.strictEqual(typeof result.error, 'number');
assert(result.total >= 0);
assert(result.inProgress >= 0);
assert(result.error >= 0);
});
});
describe('notificationsCount', function () {
it('should return non-negative number', function () {
const count = manager.notificationsCount;
assert.strictEqual(typeof count, 'number');
assert(count >= 0);
});
});
describe('provideChatSessions()', function () {
it('should return empty array when copilot API is not available', async function () {
const token = new vscode.CancellationTokenSource().token;
const result = await manager.provideChatSessions(token);
assert.strictEqual(Array.isArray(result), true);
assert.strictEqual(result.length, 0);
});
it('should return empty array when cancellation is requested', async function () {
const tokenSource = new vscode.CancellationTokenSource();
tokenSource.cancel();
const result = await manager.provideChatSessions(tokenSource.token);
assert.strictEqual(Array.isArray(result), true);
assert.strictEqual(result.length, 0);
});
});
describe('provideChatSessionContent()', function () {
it('should return empty session when copilot API is not available', async function () {
const token = new vscode.CancellationTokenSource().token;
const result = await manager.provideChatSessionContent('123', token);
assert.strictEqual(Array.isArray(result.history), true);
assert.strictEqual(result.history.length, 0);
assert.strictEqual(result.requestHandler, undefined);
});
it('should return empty session when cancellation is requested', async function () {
const tokenSource = new vscode.CancellationTokenSource();
tokenSource.cancel();
const result = await manager.provideChatSessionContent('123', tokenSource.token);
assert.strictEqual(Array.isArray(result.history), true);
assert.strictEqual(result.history.length, 0);
});
it('should return empty session for invalid PR number', async function () {
const token = new vscode.CancellationTokenSource().token;
const result = await manager.provideChatSessionContent('invalid', token);
assert.strictEqual(Array.isArray(result.history), true);
assert.strictEqual(result.history.length, 0);
});
});
describe('refreshChatSessions()', function () {
it('should fire change event', function () {
let eventFired = false;
const disposable = manager.onDidChangeChatSessions(() => {
eventFired = true;
});
manager.refreshChatSessions();
assert.strictEqual(eventFired, true);
disposable.dispose();
});
});
describe('event handlers', function () {
it('should expose onDidChangeStates event', function () {
assert.strictEqual(typeof manager.onDidChangeStates, 'function');
});
it('should expose onDidChangeNotifications event', function () {
assert.strictEqual(typeof manager.onDidChangeNotifications, 'function');
});
it('should expose onDidCreatePullRequest event', function () {
assert.strictEqual(typeof manager.onDidCreatePullRequest, 'function');
});
it('should expose onDidChangeChatSessions event', function () {
assert.strictEqual(typeof manager.onDidChangeChatSessions, 'function');
});
});
describe('waitRepoManagerInitialization()', function () {
it('should resolve immediately when repos are loaded', async function () {
// Mock the state as already loaded
sinon.stub(reposManager, 'state').get(() => ReposManagerState.RepositoriesLoaded);
// This should resolve quickly
const startTime = Date.now();
await (manager as any).waitRepoManagerInitialization();
const endTime = Date.now();
// Should be very fast since it should return immediately
assert(endTime - startTime < 100);
});
it('should resolve immediately when authentication is needed', async function () {
sinon.stub(reposManager, 'state').get(() => ReposManagerState.NeedsAuthentication);
const startTime = Date.now();
await (manager as any).waitRepoManagerInitialization();
const endTime = Date.now();
assert(endTime - startTime < 100);
});
});
describe('badge notification functionality', function () {
it('should expose onDidChangeSessionBadges event', function () {
assert.strictEqual(typeof manager.onDidChangeSessionBadges, 'function');
});
it('should track unviewed completed sessions', function () {
// Initially no unviewed sessions
assert.strictEqual(manager.getUnviewedSessionCount(), 0);
assert.strictEqual(manager.hasUnviewedCompletion('123'), false);
});
it('should mark sessions as viewed', function () {
// Mark a session as viewed (this should not throw even if session doesn't exist)
manager.markSessionAsViewed('123');
assert.strictEqual(manager.hasUnviewedCompletion('123'), false);
});
it('should handle badge state changes', function () {
let eventFired = false;
const disposable = manager.onDidChangeSessionBadges(() => {
eventFired = true;
});
// Marking a non-existent session as viewed should not fire event
manager.markSessionAsViewed('non-existent');
assert.strictEqual(eventFired, false);
disposable.dispose();
});
});
});