|
| 1 | +// Copyright (c) Microsoft Corporation. All rights reserved. |
| 2 | +// Licensed under the MIT License. |
| 3 | + |
| 4 | +import { assert } from 'chai'; |
| 5 | +import * as sinon from 'sinon'; |
| 6 | +import * as typemoq from 'typemoq'; |
| 7 | +import { Extension, WorkspaceConfiguration } from 'vscode'; |
| 8 | +import * as extensionsApi from '../../client/common/vscodeApis/extensionsApi'; |
| 9 | +import * as workspaceApis from '../../client/common/vscodeApis/workspaceApis'; |
| 10 | +import { |
| 11 | + _resetEnvExtensionDecisionCache, |
| 12 | + ENVS_EXTENSION_ID, |
| 13 | + getEnvExtensionDecisionTelemetry, |
| 14 | + useEnvExtension, |
| 15 | +} from '../../client/envExt/api.internal'; |
| 16 | + |
| 17 | +function configuration(resolvedSetting: boolean): WorkspaceConfiguration { |
| 18 | + const config = typemoq.Mock.ofType<WorkspaceConfiguration>(); |
| 19 | + config.setup((c) => c.get<boolean>('useEnvironmentsExtension', false)).returns(() => resolvedSetting); |
| 20 | + return config.object; |
| 21 | +} |
| 22 | + |
| 23 | +suite('Python Environments integration decision telemetry', () => { |
| 24 | + let getConfiguration: sinon.SinonStub; |
| 25 | + let getExtension: sinon.SinonStub; |
| 26 | + |
| 27 | + setup(() => { |
| 28 | + _resetEnvExtensionDecisionCache(); |
| 29 | + getConfiguration = sinon.stub(workspaceApis, 'getConfiguration').returns(configuration(false)); |
| 30 | + getExtension = sinon.stub(extensionsApi, 'getExtension').returns(undefined); |
| 31 | + }); |
| 32 | + |
| 33 | + teardown(() => { |
| 34 | + _resetEnvExtensionDecisionCache(); |
| 35 | + sinon.restore(); |
| 36 | + }); |
| 37 | + |
| 38 | + const cases = [ |
| 39 | + { |
| 40 | + name: 'extension unavailable', |
| 41 | + available: false, |
| 42 | + resolvedSetting: true, |
| 43 | + decision: false, |
| 44 | + reason: 'extensionUnavailable', |
| 45 | + }, |
| 46 | + { |
| 47 | + name: 'resolved setting false', |
| 48 | + available: true, |
| 49 | + resolvedSetting: false, |
| 50 | + decision: false, |
| 51 | + reason: 'resolvedSettingFalse', |
| 52 | + }, |
| 53 | + { |
| 54 | + name: 'integration enabled', |
| 55 | + available: true, |
| 56 | + resolvedSetting: true, |
| 57 | + decision: true, |
| 58 | + reason: 'enabled', |
| 59 | + }, |
| 60 | + ] as const; |
| 61 | + |
| 62 | + cases.forEach(({ name, available, resolvedSetting, decision, reason }) => { |
| 63 | + test(`captures ${name}`, () => { |
| 64 | + getConfiguration.returns(configuration(resolvedSetting)); |
| 65 | + getExtension.returns(available ? ({} as Extension<unknown>) : undefined); |
| 66 | + |
| 67 | + assert.strictEqual(useEnvExtension(), decision); |
| 68 | + assert.deepEqual(getEnvExtensionDecisionTelemetry(), { |
| 69 | + envsDecisionReason: reason, |
| 70 | + envsAvailableToHostNow: available, |
| 71 | + envsResolvedSettingNow: resolvedSetting, |
| 72 | + envsCachedDecision: decision, |
| 73 | + }); |
| 74 | + sinon.assert.alwaysCalledWithExactly(getExtension, ENVS_EXTENSION_ID); |
| 75 | + }); |
| 76 | + }); |
| 77 | + |
| 78 | + test('reports live inputs without changing a stale cached decision', () => { |
| 79 | + assert.isFalse(useEnvExtension()); |
| 80 | + getConfiguration.returns(configuration(true)); |
| 81 | + getExtension.returns({} as Extension<unknown>); |
| 82 | + |
| 83 | + assert.deepEqual(getEnvExtensionDecisionTelemetry(), { |
| 84 | + envsDecisionReason: 'extensionUnavailable', |
| 85 | + envsAvailableToHostNow: true, |
| 86 | + envsResolvedSettingNow: true, |
| 87 | + envsCachedDecision: false, |
| 88 | + }); |
| 89 | + assert.isFalse(useEnvExtension()); |
| 90 | + }); |
| 91 | + |
| 92 | + test('does not initialize the cached decision when telemetry is requested', () => { |
| 93 | + getConfiguration.returns(configuration(true)); |
| 94 | + getExtension.returns({} as Extension<unknown>); |
| 95 | + |
| 96 | + assert.deepEqual(getEnvExtensionDecisionTelemetry(), { |
| 97 | + envsDecisionReason: undefined, |
| 98 | + envsAvailableToHostNow: true, |
| 99 | + envsResolvedSettingNow: true, |
| 100 | + envsCachedDecision: undefined, |
| 101 | + }); |
| 102 | + }); |
| 103 | +}); |
0 commit comments