66import { expect } from 'chai' ;
77import * as path from 'path' ;
88import { SemVer } from 'semver' ;
9+ import * as sinon from 'sinon' ;
910import { anyString , anything , instance , mock , verify , when } from 'ts-mockito' ;
1011import { Uri } from 'vscode' ;
1112import { IWorkspaceService } from '../../../client/common/application/types' ;
@@ -28,6 +29,8 @@ import {
2829import { InterpreterHelper } from '../../../client/interpreter/helpers' ;
2930import { InterpreterService } from '../../../client/interpreter/interpreterService' ;
3031import { EnvironmentType , PythonEnvironment } from '../../../client/pythonEnvironments/info' ;
32+ import * as Telemetry from '../../../client/telemetry' ;
33+ import { EventName } from '../../../client/telemetry/constants' ;
3134
3235/* eslint-disable @typescript-eslint/no-explicit-any */
3336
@@ -42,7 +45,9 @@ suite('Interpreters - Auto Selection', () => {
4245 let helper : IInterpreterHelper ;
4346 let proxy : IInterpreterAutoSelectionProxyService ;
4447 let interpreterService : IInterpreterService ;
48+ let sendTelemetryEventStub : sinon . SinonStub ;
4549 let options : GetInterpreterOptions [ ] = [ ] ;
50+ let telemetryEvents : { eventName : string ; properties : Record < string , unknown > } [ ] = [ ] ;
4651 class InterpreterAutoSelectionServiceTest extends InterpreterAutoSelectionService {
4752 public initializeStore ( resource : Resource ) : Promise < void > {
4853 return super . initializeStore ( resource ) ;
@@ -93,17 +98,27 @@ suite('Interpreters - Auto Selection', () => {
9398 } as PythonEnvironment ,
9499 ] ) ;
95100 } ) ;
101+
102+ sendTelemetryEventStub = sinon
103+ . stub ( Telemetry , 'sendTelemetryEvent' )
104+ . callsFake ( ( eventName : string , _ , properties : Record < string , unknown > ) => {
105+ const telemetry = { eventName, properties } ;
106+ telemetryEvents . push ( telemetry ) ;
107+ } ) ;
96108 } ) ;
97109
98110 teardown ( ( ) => {
111+ sinon . restore ( ) ;
112+ Telemetry . _resetSharedProperties ( ) ;
99113 options = [ ] ;
114+ telemetryEvents = [ ] ;
100115 } ) ;
101116
102117 test ( 'Instance is registered in proxy' , ( ) => {
103118 verify ( proxy . registerInstance ! ( autoSelectionService ) ) . once ( ) ;
104119 } ) ;
105120
106- suite ( 'When using locator-based auto-selection' , ( ) => {
121+ suite ( 'Test locator-based auto-selection method ' , ( ) => {
107122 let workspacePath : string ;
108123 let resource : Uri ;
109124 let eventFired : boolean ;
@@ -284,6 +299,102 @@ suite('Interpreters - Auto Selection', () => {
284299 verify ( interpreterService . getInterpreters ( resource , anything ( ) ) ) . once ( ) ;
285300 expect ( options ) . to . deep . equal ( [ { ignoreCache : false } ] , 'getInterpreters options are different' ) ;
286301 } ) ;
302+
303+ test ( 'Telemetry event is sent with useCachedInterpreter set to false if auto-selection has not been run before' , async ( ) => {
304+ const interpreterComparer = new EnvironmentTypeComparer ( instance ( helper ) ) ;
305+
306+ when ( interpreterService . getInterpreters ( resource , anything ( ) ) ) . thenCall ( ( ) =>
307+ Promise . resolve ( [
308+ {
309+ envType : EnvironmentType . Conda ,
310+ envPath : path . join ( 'some' , 'conda' , 'env' ) ,
311+ version : { major : 3 , minor : 7 , patch : 2 } ,
312+ } as PythonEnvironment ,
313+ {
314+ envType : EnvironmentType . Pipenv ,
315+ envPath : path . join ( 'some' , 'pipenv' , 'env' ) ,
316+ version : { major : 3 , minor : 10 , patch : 0 } ,
317+ } as PythonEnvironment ,
318+ ] ) ,
319+ ) ;
320+
321+ autoSelectionService = new InterpreterAutoSelectionServiceTest (
322+ instance ( workspaceService ) ,
323+ instance ( stateFactory ) ,
324+ instance ( fs ) ,
325+ instance ( interpreterService ) ,
326+ interpreterComparer ,
327+ instance ( proxy ) ,
328+ instance ( helper ) ,
329+ ) ;
330+
331+ autoSelectionService . initializeStore = ( ) => Promise . resolve ( ) ;
332+
333+ await autoSelectionService . autoSelectInterpreter ( resource ) ;
334+
335+ verify ( interpreterService . getInterpreters ( resource , anything ( ) ) ) . once ( ) ;
336+ sinon . assert . calledOnce ( sendTelemetryEventStub ) ;
337+ expect ( telemetryEvents ) . to . deep . equal (
338+ [
339+ {
340+ eventName : EventName . PYTHON_INTERPRETER_AUTO_SELECTION ,
341+ properties : { useCachedInterpreter : false } ,
342+ } ,
343+ ] ,
344+ 'Telemetry event properties are different' ,
345+ ) ;
346+ } ) ;
347+
348+ test ( 'Telemetry event is sent with useCachedInterpreter set to true if auto-selection has been run before' , async ( ) => {
349+ const interpreterComparer = new EnvironmentTypeComparer ( instance ( helper ) ) ;
350+
351+ when ( interpreterService . getInterpreters ( resource , anything ( ) ) ) . thenCall ( ( ) =>
352+ Promise . resolve ( [
353+ {
354+ envType : EnvironmentType . Conda ,
355+ envPath : path . join ( 'some' , 'conda' , 'env' ) ,
356+ version : { major : 3 , minor : 7 , patch : 2 } ,
357+ } as PythonEnvironment ,
358+ {
359+ envType : EnvironmentType . Pipenv ,
360+ envPath : path . join ( 'some' , 'pipenv' , 'env' ) ,
361+ version : { major : 3 , minor : 10 , patch : 0 } ,
362+ } as PythonEnvironment ,
363+ ] ) ,
364+ ) ;
365+
366+ autoSelectionService = new InterpreterAutoSelectionServiceTest (
367+ instance ( workspaceService ) ,
368+ instance ( stateFactory ) ,
369+ instance ( fs ) ,
370+ instance ( interpreterService ) ,
371+ interpreterComparer ,
372+ instance ( proxy ) ,
373+ instance ( helper ) ,
374+ ) ;
375+
376+ autoSelectionService . initializeStore = ( ) => Promise . resolve ( ) ;
377+
378+ await autoSelectionService . autoSelectInterpreter ( resource ) ;
379+
380+ await autoSelectionService . autoSelectInterpreter ( resource ) ;
381+
382+ verify ( interpreterService . getInterpreters ( resource , anything ( ) ) ) . once ( ) ;
383+ sinon . assert . calledTwice ( sendTelemetryEventStub ) ;
384+ expect ( telemetryEvents ) . to . deep . equal (
385+ [
386+ {
387+ eventName : EventName . PYTHON_INTERPRETER_AUTO_SELECTION ,
388+ properties : { useCachedInterpreter : false } ,
389+ } ,
390+ {
391+ eventName : EventName . PYTHON_INTERPRETER_AUTO_SELECTION ,
392+ properties : { useCachedInterpreter : true } ,
393+ } ,
394+ ] ,
395+ 'Telemetry event properties are different' ,
396+ ) ;
397+ } ) ;
287398 } ) ;
288399
289400 test ( 'Initialize the store' , async ( ) => {
0 commit comments