Path: blob/main/src/vs/workbench/api/test/browser/extHostCommands.test.ts
5237 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 assert from 'assert';6import { ExtHostCommands } from '../../common/extHostCommands.js';7import { MainThreadCommandsShape } from '../../common/extHost.protocol.js';8import { CommandsRegistry } from '../../../../platform/commands/common/commands.js';9import { SingleProxyRPCProtocol } from '../common/testRPCProtocol.js';10import { mock } from '../../../../base/test/common/mock.js';11import { NullLogService } from '../../../../platform/log/common/log.js';12import { IExtHostTelemetry } from '../../common/extHostTelemetry.js';13import { ensureNoDisposablesAreLeakedInTestSuite } from '../../../../base/test/common/utils.js';1415suite('ExtHostCommands', function () {16ensureNoDisposablesAreLeakedInTestSuite();1718test('dispose calls unregister', function () {1920let lastUnregister: string;2122const shape = new class extends mock<MainThreadCommandsShape>() {23override $registerCommand(id: string): void {24//25}26override $unregisterCommand(id: string): void {27lastUnregister = id;28}29};3031const commands = new ExtHostCommands(32SingleProxyRPCProtocol(shape),33new NullLogService(),34new class extends mock<IExtHostTelemetry>() {35override onExtensionError(): boolean {36return true;37}38}39);40commands.registerCommand(true, 'foo', (): any => { }).dispose();41assert.strictEqual(lastUnregister!, 'foo');42assert.strictEqual(CommandsRegistry.getCommand('foo'), undefined);4344});4546test('dispose bubbles only once', function () {4748let unregisterCounter = 0;4950const shape = new class extends mock<MainThreadCommandsShape>() {51override $registerCommand(id: string): void {52//53}54override $unregisterCommand(id: string): void {55unregisterCounter += 1;56}57};5859const commands = new ExtHostCommands(60SingleProxyRPCProtocol(shape),61new NullLogService(),62new class extends mock<IExtHostTelemetry>() {63override onExtensionError(): boolean {64return true;65}66}67);68const reg = commands.registerCommand(true, 'foo', (): any => { });69reg.dispose();70reg.dispose();71reg.dispose();72assert.strictEqual(unregisterCounter, 1);73});7475test('execute with retry', async function () {7677let count = 0;7879const shape = new class extends mock<MainThreadCommandsShape>() {80override $registerCommand(id: string): void {81//82}83override async $executeCommand<T>(id: string, args: any[], retry: boolean): Promise<T | undefined> {84count++;85assert.strictEqual(retry, count === 1);86if (count === 1) {87assert.strictEqual(retry, true);88throw new Error('$executeCommand:retry');89} else {90assert.strictEqual(retry, false);91// eslint-disable-next-line local/code-no-any-casts92return <any>17;93}94}95};9697const commands = new ExtHostCommands(98SingleProxyRPCProtocol(shape),99new NullLogService(),100new class extends mock<IExtHostTelemetry>() {101override onExtensionError(): boolean {102return true;103}104}105);106107const result: number = await commands.executeCommand('fooo', [this, true]);108assert.strictEqual(result, 17);109assert.strictEqual(count, 2);110});111112test('onCommand:abc activates extensions when executed from command palette, but not when executed programmatically with vscode.commands.executeCommand #150293', async function () {113114const activationEvents: string[] = [];115116const shape = new class extends mock<MainThreadCommandsShape>() {117override $registerCommand(id: string): void {118//119}120override $fireCommandActivationEvent(id: string): void {121activationEvents.push(id);122}123};124const commands = new ExtHostCommands(125SingleProxyRPCProtocol(shape),126new NullLogService(),127new class extends mock<IExtHostTelemetry>() {128override onExtensionError(): boolean {129return true;130}131}132);133134commands.registerCommand(true, 'extCmd', (args: any): any => args);135136const result: unknown = await commands.executeCommand('extCmd', this);137assert.strictEqual(result, this);138assert.deepStrictEqual(activationEvents, ['extCmd']);139});140});141142143