Path: blob/main/extensions/copilot/src/extension/onboardDebug/test/node/debuggableCommandIdentifier.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*--------------------------------------------------------------------------------------------*/4/* eslint-disable local/code-no-unused-expressions */567import { SinonStub, stub } from 'sinon';8import { afterEach, beforeEach, describe, expect, it } from 'vitest';9import { ConfigKey, IConfigurationService } from '../../../../platform/configuration/common/configurationService';10import { ITestingServicesAccessor } from '../../../../platform/test/node/services';11import { TestWorkspaceService } from '../../../../platform/test/node/testWorkspaceService';12import { IWorkspaceService } from '../../../../platform/workspace/common/workspaceService';13import { CancellationToken } from '../../../../util/vs/base/common/cancellation';14import { basename } from '../../../../util/vs/base/common/path';15import { URI } from '../../../../util/vs/base/common/uri';16import { IInstantiationService } from '../../../../util/vs/platform/instantiation/common/instantiation';17import { createExtensionUnitTestingServices } from '../../../test/node/services';18import { DebuggableCommandIdentifier } from '../../node/debuggableCommandIdentifier';19import { ILanguageToolsProvider } from '../../node/languageToolsProvider';2021describe('DebuggableCommandIdentifier', () => {22let accessor: ITestingServicesAccessor;23let debuggableCommandIdentifier: DebuggableCommandIdentifier;24let getToolsForLanguages: SinonStub;2526const setConfigEnabled = (enabled: boolean) =>27accessor.get(IConfigurationService).setConfig(ConfigKey.TerminalToDebuggerEnabled, enabled);2829beforeEach(() => {30getToolsForLanguages = stub().resolves({ ok: true, commands: ['mytool'] });31const testingServiceCollection = createExtensionUnitTestingServices();32testingServiceCollection.define(ILanguageToolsProvider, {33_serviceBrand: undefined,34getToolsForLanguages35});36accessor = testingServiceCollection.createTestingAccessor();3738setConfigEnabled(true);39debuggableCommandIdentifier = accessor40.get(IInstantiationService)41.createInstance(DebuggableCommandIdentifier);42});4344afterEach(() => {45debuggableCommandIdentifier.dispose();46});4748it('should return false if globally disabled', async () => {49setConfigEnabled(false);50const result = await debuggableCommandIdentifier.isDebuggable(undefined, 'node index.js', CancellationToken.None);51expect(result).to.be.false;52});5354it('should return true for well-known commands', async () => {55const result = await debuggableCommandIdentifier.isDebuggable(undefined, 'node index.js', CancellationToken.None);56expect(result).to.be.true;57});5859it('should return false for unknown commands', async () => {60const result = await debuggableCommandIdentifier.isDebuggable(undefined, 'mytool', CancellationToken.None);61expect(result).to.be.false;62expect(getToolsForLanguages.called).to.be.false;63});6465it('should return true for locals', async () => {66const result = await debuggableCommandIdentifier.isDebuggable(undefined, 'mytool', CancellationToken.None);67expect(result).to.be.false;68expect(getToolsForLanguages.called).to.be.false;69});7071// todo@connor4312: these work on macos locally but fail in CI, look into it72it.skip('should return true if referencing an absolute path', async () => {73const result = await debuggableCommandIdentifier.isDebuggable(undefined, __filename, CancellationToken.None);74expect(result).to.be.true;75});7677// todo@connor4312: these work on macos locally but fail in CI, look into it78it.skip('should return true if referencing a relative path in a cwd', async () => {79const result = await debuggableCommandIdentifier.isDebuggable(URI.file(__dirname), basename(__filename), CancellationToken.None);80expect(result).to.be.true;81});8283it('should not call the model tools for known languages', async () => {84(accessor.get(IWorkspaceService) as TestWorkspaceService)85.didOpenTextDocumentEmitter.fire({ languageId: 'javascript' } as any);8687const result = await debuggableCommandIdentifier.isDebuggable(undefined, 'othertool hello', CancellationToken.None);88expect(result).to.be.false;89expect(getToolsForLanguages.callCount).to.equal(0);90});9192it('should return true for model provided commands', async () => {93(accessor.get(IWorkspaceService) as TestWorkspaceService)94.didOpenTextDocumentEmitter.fire({ languageId: 'mylang' } as any);95const result = await debuggableCommandIdentifier.isDebuggable(undefined, 'mytool hello', CancellationToken.None);96expect(result).to.be.true;97expect(getToolsForLanguages.calledWith(['mylang'])).to.be.true;9899// should not call again because no new langauge was seen:100const result2 = await debuggableCommandIdentifier.isDebuggable(undefined, 'othertool hello', CancellationToken.None);101expect(result2).to.be.false;102expect(getToolsForLanguages.callCount).to.equal(1);103});104105it('returns treatment value 1', async () => {106accessor.get(IConfigurationService).setConfig(ConfigKey.Advanced.TerminalToDebuggerPatterns, ['othert']);107const result = await debuggableCommandIdentifier.isDebuggable(undefined, 'othert hello', CancellationToken.None);108expect(result).to.be.true;109});110111it('return treatment value 2', async () => {112accessor.get(IConfigurationService).setConfig(ConfigKey.Advanced.TerminalToDebuggerPatterns, ['!mytool']);113const result = await debuggableCommandIdentifier.isDebuggable(undefined, 'mytool hello', CancellationToken.None);114expect(result).to.be.false;115});116117// it('should return true for commands matching specific treatment', async () => {118// (configurationService.getConfig as sinon.SinonStub).returns(['node']);119// const result = await debuggableCommandIdentifier.isDebuggable(undefined, 'node index.js', CancellationToken.None);120// expect(result).to.be.true;121// });122123// it('should return false for commands matching specific exclusion', async () => {124// (configurationService.getConfig as sinon.SinonStub).returns(['!node']);125// const result = await debuggableCommandIdentifier.isDebuggable(undefined, 'node index.js', CancellationToken.None);126// expect(result).to.be.false;127// });128129// it('should query language model for unknown commands', async () => {130// (context.globalState.get as sinon.SinonStub).returns({ languages: ['unknown'], commands: [] });131// (instantiationService.createInstance().getToolsForLanguages as sinon.SinonStub).resolves({ commands: ['unknowncommand'], ok: true });132// const result = await debuggableCommandIdentifier.isDebuggable(undefined, 'unknowncommand', CancellationToken.None);133// expect(result).to.be.true;134// });135});136137138