-
Notifications
You must be signed in to change notification settings - Fork 405
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
chore: telemetry service refactor (#5226)
* feat: telemetryProvider * fix: typos * chore: compatible with old e4d telemetryService usage * chore: unit tests for telemetry * chore: lint * chore: polish getInstance * chore: header * chore: header * chore: remove change in prettierrc * chore: rename TelemetryProvider * chore: use getInstance from TelemetryService instead * chore: revert change in configUtil * chore: revert change in prettierrc * chore: update telemetry reporter name * chore: add explanation * chore: unit test * chore: save pack name in constants * fix: delete unused env * Update packages/salesforcedx-utils-vscode/src/telemetry/telemetry.ts Co-authored-by: peternhale <[email protected]> --------- Co-authored-by: peternhale <[email protected]>
- Loading branch information
1 parent
9a4bae6
commit 5cde4b6
Showing
5 changed files
with
173 additions
and
39 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
94 changes: 94 additions & 0 deletions
94
packages/salesforcedx-utils-vscode/test/jest/telemetry/telemetry.test.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,94 @@ | ||
/* | ||
* Copyright (c) 2023, salesforce.com, inc. | ||
* All rights reserved. | ||
* Licensed under the BSD 3-Clause license. | ||
* For full license text, see LICENSE.txt file in the repo root or https://opensource.org/licenses/BSD-3-Clause | ||
*/ | ||
import { TelemetryService } from '../../../src'; | ||
import { SFDX_CORE_EXTENSION_NAME } from '../../../src/constants'; | ||
import { TelemetryServiceProvider } from '../../../src/telemetry/telemetry'; | ||
|
||
describe('Telemetry', () => { | ||
describe('Telemetry Provider', () => { | ||
afterEach(() => { | ||
// Clear instances after each test to avoid state leakage. | ||
TelemetryServiceProvider.instances.clear(); | ||
}); | ||
it('getInstance should return a TelemetryService instance for core extension when no name is provided', () => { | ||
const instance = TelemetryServiceProvider.getInstance(); | ||
expect(instance).toBeInstanceOf(TelemetryService); | ||
expect( | ||
TelemetryServiceProvider.instances.has(SFDX_CORE_EXTENSION_NAME) | ||
).toBeTruthy(); | ||
}); | ||
|
||
it('getInstance should return the same TelemetryService instance for core extension on subsequent calls', () => { | ||
const firstInstance = TelemetryServiceProvider.getInstance(); | ||
const secondInstance = TelemetryServiceProvider.getInstance(); | ||
expect(secondInstance).toBe(firstInstance); | ||
}); | ||
|
||
it('getInstance should return a TelemetryService instance for a named extension', () => { | ||
const extensionName = 'someExtension'; | ||
const instance = TelemetryServiceProvider.getInstance(extensionName); | ||
expect(instance).toBeInstanceOf(TelemetryService); | ||
expect( | ||
TelemetryServiceProvider.instances.has(extensionName) | ||
).toBeTruthy(); | ||
}); | ||
|
||
it('getInstance should return the same TelemetryService instance for a named extension on subsequent calls', () => { | ||
const extensionName = 'someExtension'; | ||
const firstInstance = TelemetryServiceProvider.getInstance(extensionName); | ||
const secondInstance = | ||
TelemetryServiceProvider.getInstance(extensionName); | ||
expect(secondInstance).toBe(firstInstance); | ||
}); | ||
|
||
it('getInstance should return different instances for different extension names', () => { | ||
const firstExtensionName = 'extensionOne'; | ||
const secondExtensionName = 'extensionTwo'; | ||
const firstInstance = | ||
TelemetryServiceProvider.getInstance(firstExtensionName); | ||
const secondInstance = | ||
TelemetryServiceProvider.getInstance(secondExtensionName); | ||
expect(firstInstance).not.toBe(secondInstance); | ||
}); | ||
}); | ||
|
||
describe('Telemetry Service', () => { | ||
it('getInstance should return the core instance if no extension name provided', () => { | ||
const firstInstance = TelemetryService.getInstance(); | ||
const secondInstance = TelemetryServiceProvider.getInstance( | ||
SFDX_CORE_EXTENSION_NAME | ||
); | ||
expect(firstInstance).toBe(secondInstance); | ||
}); | ||
it('getInstance should return the same TelemetryService instance for a named extension on subsequent calls', () => { | ||
const extensionName = 'someExtension'; | ||
const firstInstance = TelemetryService.getInstance(extensionName); | ||
const secondInstance = | ||
TelemetryServiceProvider.getInstance(extensionName); | ||
expect(secondInstance).toBe(firstInstance); | ||
}); | ||
}); | ||
|
||
describe('getTelemetryReporterName', () => { | ||
let telemetryService: TelemetryService; | ||
beforeEach(() => { | ||
telemetryService = new TelemetryService(); | ||
}); | ||
|
||
it('should return "salesforcedx-vscode" when extensionName starts with "salesforcedx-vscode"', () => { | ||
telemetryService.extensionName = 'salesforcedx-vscode-core'; | ||
const result = telemetryService.getTelemetryReporterName(); | ||
expect(result).toBe('salesforcedx-vscode'); | ||
}); | ||
|
||
it('should return the actual extensionName when it does not start with "salesforcedx-vscode"', () => { | ||
telemetryService.extensionName = 'salesforcedx-einstein-gpt'; | ||
const result = telemetryService.getTelemetryReporterName(); | ||
expect(result).toBe(telemetryService.extensionName); | ||
}); | ||
}); | ||
}); |