Path: blob/main/extensions/copilot/src/extension/chatSessions/copilotcli/node/test/missionControlApiClient.spec.ts
13406 views
/*---------------------------------------------------------------------------------------------1* Copyright (c) Microsoft Corporation. All rights reserved.2* Licensed under the MIT License. See License.txt in the project root for license information.3*--------------------------------------------------------------------------------------------*/45import type { AuthenticationSession } from 'vscode';6import { describe, expect, it, vi } from 'vitest';7import type { IAuthenticationService } from '../../../../../platform/authentication/common/authentication';8import { INTEGRATION_ID } from '../../../../../platform/endpoint/common/licenseAgreement';9import type { ILogService } from '../../../../../platform/log/common/logService';10import { type FetchOptions, type IFetcherService, HeadersImpl, Response } from '../../../../../platform/networking/common/fetcherService';11import { Emitter } from '../../../../../util/vs/base/common/event';12import { MissionControlApiClient } from '../missionControlApiClient';1314function createResponse(body: string): Response {15return Response.fromText(200, 'OK', new HeadersImpl({ 'content-type': 'application/json' }), body, 'test-stub');16}1718describe('MissionControlApiClient', () => {19it('uses the shared integration id for all mission control requests', async () => {20const requests: Array<{ url: string; options: FetchOptions }> = [];21const fetcherService = {22_serviceBrand: undefined,23onDidFetch: new Emitter().event,24onDidCompleteFetch: new Emitter().event,25getUserAgentLibrary: () => 'test',26fetch: vi.fn(async (url: string, options: FetchOptions) => {27requests.push({ url, options });2829if (url.endsWith('/commands')) {30return createResponse(JSON.stringify({ commands: [] }));31}3233if (url.endsWith('/agents/sessions')) {34return createResponse(JSON.stringify({ id: 'mc-session', task_id: 'task-1' }));35}3637return createResponse('{}');38}),39createWebSocket: vi.fn(),40disconnectAll: vi.fn(),41makeAbortController: vi.fn(),42isAbortError: vi.fn(() => false),43isInternetDisconnectedError: vi.fn(() => false),44isFetcherError: vi.fn(() => false),45isNetworkProcessCrashedError: vi.fn(() => false),46getUserMessageForFetcherError: vi.fn(() => ''),47fetchWithPagination: vi.fn(),48} as unknown as IFetcherService;4950const githubSession = {51accessToken: 'github-token',52} as AuthenticationSession;53const authenticationService = {54_serviceBrand: undefined,55getGitHubSession: vi.fn(async () => githubSession),56getCopilotToken: vi.fn(async () => ({ token: 'copilot-token', endpoints: { api: 'https://api.github.test/' } })),57} as unknown as IAuthenticationService;58const logService = {59_serviceBrand: undefined,60trace: vi.fn(),61debug: vi.fn(),62info: vi.fn(),63warn: vi.fn(),64error: vi.fn(),65show: vi.fn(),66createSubLogger: () => logService,67withExtraTarget: () => logService,68} as unknown as ILogService;6970const client = new MissionControlApiClient(authenticationService, fetcherService, logService);7172await client.createSession(1, 2, 'task-1', {});73await client.submitEvents('mc-session', [], []);74await client.getPendingCommands('mc-session');75await client.deleteSession('mc-session');7677expect(requests).toHaveLength(4);78expect(requests.map(({ options }) => options.headers?.['Copilot-Integration-Id'])).toEqual([79INTEGRATION_ID,80INTEGRATION_ID,81INTEGRATION_ID,82INTEGRATION_ID,83]);84expect(requests.map(({ url }) => url)).toEqual([85'https://api.github.test/agents/sessions',86'https://api.github.test/agents/sessions/mc-session/events',87'https://api.github.test/agents/sessions/mc-session/commands',88'https://api.github.test/agents/sessions/mc-session',89]);90});91});929394