Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
microsoft
GitHub Repository: microsoft/vscode
Path: blob/main/extensions/copilot/src/extension/chatSessions/copilotcli/node/test/missionControlApiClient.spec.ts
13406 views
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 type { AuthenticationSession } from 'vscode';
7
import { describe, expect, it, vi } from 'vitest';
8
import type { IAuthenticationService } from '../../../../../platform/authentication/common/authentication';
9
import { INTEGRATION_ID } from '../../../../../platform/endpoint/common/licenseAgreement';
10
import type { ILogService } from '../../../../../platform/log/common/logService';
11
import { type FetchOptions, type IFetcherService, HeadersImpl, Response } from '../../../../../platform/networking/common/fetcherService';
12
import { Emitter } from '../../../../../util/vs/base/common/event';
13
import { MissionControlApiClient } from '../missionControlApiClient';
14
15
function createResponse(body: string): Response {
16
return Response.fromText(200, 'OK', new HeadersImpl({ 'content-type': 'application/json' }), body, 'test-stub');
17
}
18
19
describe('MissionControlApiClient', () => {
20
it('uses the shared integration id for all mission control requests', async () => {
21
const requests: Array<{ url: string; options: FetchOptions }> = [];
22
const fetcherService = {
23
_serviceBrand: undefined,
24
onDidFetch: new Emitter().event,
25
onDidCompleteFetch: new Emitter().event,
26
getUserAgentLibrary: () => 'test',
27
fetch: vi.fn(async (url: string, options: FetchOptions) => {
28
requests.push({ url, options });
29
30
if (url.endsWith('/commands')) {
31
return createResponse(JSON.stringify({ commands: [] }));
32
}
33
34
if (url.endsWith('/agents/sessions')) {
35
return createResponse(JSON.stringify({ id: 'mc-session', task_id: 'task-1' }));
36
}
37
38
return createResponse('{}');
39
}),
40
createWebSocket: vi.fn(),
41
disconnectAll: vi.fn(),
42
makeAbortController: vi.fn(),
43
isAbortError: vi.fn(() => false),
44
isInternetDisconnectedError: vi.fn(() => false),
45
isFetcherError: vi.fn(() => false),
46
isNetworkProcessCrashedError: vi.fn(() => false),
47
getUserMessageForFetcherError: vi.fn(() => ''),
48
fetchWithPagination: vi.fn(),
49
} as unknown as IFetcherService;
50
51
const githubSession = {
52
accessToken: 'github-token',
53
} as AuthenticationSession;
54
const authenticationService = {
55
_serviceBrand: undefined,
56
getGitHubSession: vi.fn(async () => githubSession),
57
getCopilotToken: vi.fn(async () => ({ token: 'copilot-token', endpoints: { api: 'https://api.github.test/' } })),
58
} as unknown as IAuthenticationService;
59
const logService = {
60
_serviceBrand: undefined,
61
trace: vi.fn(),
62
debug: vi.fn(),
63
info: vi.fn(),
64
warn: vi.fn(),
65
error: vi.fn(),
66
show: vi.fn(),
67
createSubLogger: () => logService,
68
withExtraTarget: () => logService,
69
} as unknown as ILogService;
70
71
const client = new MissionControlApiClient(authenticationService, fetcherService, logService);
72
73
await client.createSession(1, 2, 'task-1', {});
74
await client.submitEvents('mc-session', [], []);
75
await client.getPendingCommands('mc-session');
76
await client.deleteSession('mc-session');
77
78
expect(requests).toHaveLength(4);
79
expect(requests.map(({ options }) => options.headers?.['Copilot-Integration-Id'])).toEqual([
80
INTEGRATION_ID,
81
INTEGRATION_ID,
82
INTEGRATION_ID,
83
INTEGRATION_ID,
84
]);
85
expect(requests.map(({ url }) => url)).toEqual([
86
'https://api.github.test/agents/sessions',
87
'https://api.github.test/agents/sessions/mc-session/events',
88
'https://api.github.test/agents/sessions/mc-session/commands',
89
'https://api.github.test/agents/sessions/mc-session',
90
]);
91
});
92
});
93
94