Path: blob/main/extensions/copilot/src/platform/authentication/test/node/authentication.spec.ts
13405 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 { afterEach, beforeEach, expect, suite, test } from 'vitest';6import { Event } from '../../../../util/vs/base/common/event';7import { DisposableStore } from '../../../../util/vs/base/common/lifecycle';8import { IConfigurationService } from '../../../configuration/common/configurationService';9import { ICAPIClientService } from '../../../endpoint/common/capiClient';10import { IDomainService } from '../../../endpoint/common/domainService';11import { IEnvService } from '../../../env/common/envService';12import { ILogService } from '../../../log/common/logService';13import { IFetcherService } from '../../../networking/common/fetcherService';14import { ITelemetryService } from '../../../telemetry/common/telemetry';15import { createPlatformServices } from '../../../test/node/services';16import { StaticGitHubAuthenticationService } from '../../common/staticGitHubAuthenticationService';17import { CopilotToken, createTestExtendedTokenInfo } from '../../common/copilotToken';18import { ICopilotTokenStore } from '../../common/copilotTokenStore';19import { FixedCopilotTokenManager } from '../../node/copilotTokenManager';2021suite('AuthenticationService', function () {22let disposables: DisposableStore;23// These will be used to test the authentication service, but eventually these will24// be folded into the authentication service itself.25let copilotTokenManager: FixedCopilotTokenManager;26let authenticationService: StaticGitHubAuthenticationService;2728const testToken = 'tid=test';2930beforeEach(async () => {31disposables = new DisposableStore();32const accessor = disposables.add(createPlatformServices().createTestingAccessor());33copilotTokenManager = new FixedCopilotTokenManager(34testToken,35accessor.get(ILogService),36accessor.get(ITelemetryService),37accessor.get(ICAPIClientService),38accessor.get(IDomainService),39accessor.get(IFetcherService),40accessor.get(IEnvService)41);42authenticationService = new StaticGitHubAuthenticationService(43() => testToken,44accessor.get(ILogService),45accessor.get(ICopilotTokenStore),46copilotTokenManager,47accessor.get(IConfigurationService)48);49disposables.add(authenticationService);50});5152afterEach(() => {53disposables.dispose();54});5556test('Can get anyGitHubToken', async () => {57const token = await authenticationService.getGitHubSession('any', { silent: true });58expect(token?.accessToken).toBe(testToken);59expect(authenticationService.anyGitHubSession?.accessToken).toBe(testToken);60});6162test('Can get permissiveGitHubToken', async () => {63const token = await authenticationService.getGitHubSession('permissive', { silent: true });64expect(token?.accessToken).toBe(testToken);65expect(authenticationService.permissiveGitHubSession?.accessToken).toBe(testToken);66});6768test('Can get copilotToken', async () => {69const token = await authenticationService.getCopilotToken();70expect(token.token).toBe(testToken);71expect(authenticationService.copilotToken?.token).toBe(testToken);72});7374test('Emits onDidAuthenticationChange when a Copilot Token change is notified', async () => {75const promise = Event.toPromise(authenticationService.onDidAuthenticationChange);76const newToken = 'tid=new';77authenticationService.setCopilotToken(new CopilotToken(createTestExtendedTokenInfo({78token: newToken,79username: 'fake',80copilot_plan: 'unknown',81})));82await promise;83expect(authenticationService.copilotToken?.token).toBe(newToken);84});8586test.skip('Emits onDidAuthenticationChange when a Copilot Token change is notified from the manager', async () => {87const promise = Event.toPromise(authenticationService.onDidAuthenticationChange);88const newToken = 'tid=new';89copilotTokenManager.completionsToken = newToken;90await promise;91expect(authenticationService.copilotToken?.token).toBe(newToken);92});93});949596