-
Notifications
You must be signed in to change notification settings - Fork 8.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[EBT] Core Context Providers (#130785)
- Loading branch information
Showing
63 changed files
with
2,033 additions
and
448 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
/* | ||
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one | ||
* or more contributor license agreements. Licensed under the Elastic License | ||
* 2.0 and the Server Side Public License, v 1; you may not use this file except | ||
* in compliance with, at your election, the Elastic License 2.0 or the Server | ||
* Side Public License, v 1. | ||
*/ | ||
|
||
import { AnalyticsClient } from '@kbn/analytics-client'; | ||
import { Subject } from 'rxjs'; | ||
|
||
export const analyticsClientMock: jest.Mocked<AnalyticsClient> = { | ||
optIn: jest.fn(), | ||
reportEvent: jest.fn(), | ||
registerEventType: jest.fn(), | ||
registerContextProvider: jest.fn(), | ||
removeContextProvider: jest.fn(), | ||
registerShipper: jest.fn(), | ||
telemetryCounter$: new Subject(), | ||
shutdown: jest.fn(), | ||
}; | ||
|
||
jest.doMock('@kbn/analytics-client', () => ({ | ||
createAnalytics: () => analyticsClientMock, | ||
})); |
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,93 @@ | ||
/* | ||
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one | ||
* or more contributor license agreements. Licensed under the Elastic License | ||
* 2.0 and the Server Side Public License, v 1; you may not use this file except | ||
* in compliance with, at your election, the Elastic License 2.0 or the Server | ||
* Side Public License, v 1. | ||
*/ | ||
|
||
import { firstValueFrom, Observable } from 'rxjs'; | ||
import { analyticsClientMock } from './analytics_service.test.mocks'; | ||
import { coreMock, injectedMetadataServiceMock } from '../mocks'; | ||
import { AnalyticsService } from './analytics_service'; | ||
|
||
describe('AnalyticsService', () => { | ||
let analyticsService: AnalyticsService; | ||
beforeEach(() => { | ||
jest.clearAllMocks(); | ||
analyticsService = new AnalyticsService(coreMock.createCoreContext()); | ||
}); | ||
test('should register some context providers on creation', async () => { | ||
expect(analyticsClientMock.registerContextProvider).toHaveBeenCalledTimes(3); | ||
await expect( | ||
firstValueFrom(analyticsClientMock.registerContextProvider.mock.calls[0][0].context$) | ||
).resolves.toMatchInlineSnapshot(` | ||
Object { | ||
"branch": "branch", | ||
"buildNum": 100, | ||
"buildSha": "buildSha", | ||
"isDev": true, | ||
"isDistributable": false, | ||
"version": "version", | ||
} | ||
`); | ||
await expect( | ||
firstValueFrom(analyticsClientMock.registerContextProvider.mock.calls[1][0].context$) | ||
).resolves.toEqual({ session_id: expect.any(String) }); | ||
await expect( | ||
firstValueFrom(analyticsClientMock.registerContextProvider.mock.calls[2][0].context$) | ||
).resolves.toEqual({ | ||
preferred_language: 'en-US', | ||
preferred_languages: ['en-US', 'en'], | ||
user_agent: expect.any(String), | ||
}); | ||
}); | ||
|
||
test('setup should expose all the register APIs, reportEvent and opt-in', () => { | ||
const injectedMetadata = injectedMetadataServiceMock.createSetupContract(); | ||
expect(analyticsService.setup({ injectedMetadata })).toStrictEqual({ | ||
registerShipper: expect.any(Function), | ||
registerContextProvider: expect.any(Function), | ||
removeContextProvider: expect.any(Function), | ||
registerEventType: expect.any(Function), | ||
reportEvent: expect.any(Function), | ||
optIn: expect.any(Function), | ||
telemetryCounter$: expect.any(Observable), | ||
}); | ||
}); | ||
|
||
test('setup should register the elasticsearch info context provider (undefined)', async () => { | ||
const injectedMetadata = injectedMetadataServiceMock.createSetupContract(); | ||
analyticsService.setup({ injectedMetadata }); | ||
await expect( | ||
firstValueFrom(analyticsClientMock.registerContextProvider.mock.calls[3][0].context$) | ||
).resolves.toMatchInlineSnapshot(`undefined`); | ||
}); | ||
|
||
test('setup should register the elasticsearch info context provider (with info)', async () => { | ||
const injectedMetadata = injectedMetadataServiceMock.createSetupContract(); | ||
injectedMetadata.getElasticsearchInfo.mockReturnValue({ | ||
cluster_name: 'cluster_name', | ||
cluster_uuid: 'cluster_uuid', | ||
cluster_version: 'version', | ||
}); | ||
analyticsService.setup({ injectedMetadata }); | ||
await expect( | ||
firstValueFrom(analyticsClientMock.registerContextProvider.mock.calls[3][0].context$) | ||
).resolves.toMatchInlineSnapshot(` | ||
Object { | ||
"cluster_name": "cluster_name", | ||
"cluster_uuid": "cluster_uuid", | ||
"cluster_version": "version", | ||
} | ||
`); | ||
}); | ||
|
||
test('setup should expose only the APIs report and opt-in', () => { | ||
expect(analyticsService.start()).toStrictEqual({ | ||
reportEvent: expect.any(Function), | ||
optIn: expect.any(Function), | ||
telemetryCounter$: expect.any(Observable), | ||
}); | ||
}); | ||
}); |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
/* | ||
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one | ||
* or more contributor license agreements. Licensed under the Elastic License | ||
* 2.0 and the Server Side Public License, v 1; you may not use this file except | ||
* in compliance with, at your election, the Elastic License 2.0 or the Server | ||
* Side Public License, v 1. | ||
*/ | ||
|
||
import { getSessionId } from './get_session_id'; | ||
|
||
describe('getSessionId', () => { | ||
test('should return a session id', () => { | ||
const sessionId = getSessionId(); | ||
expect(sessionId).toStrictEqual(expect.any(String)); | ||
}); | ||
|
||
test('calling it twice should return the same value', () => { | ||
const sessionId1 = getSessionId(); | ||
const sessionId2 = getSessionId(); | ||
expect(sessionId2).toStrictEqual(sessionId1); | ||
}); | ||
}); |
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,20 @@ | ||
/* | ||
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one | ||
* or more contributor license agreements. Licensed under the Elastic License | ||
* 2.0 and the Server Side Public License, v 1; you may not use this file except | ||
* in compliance with, at your election, the Elastic License 2.0 or the Server | ||
* Side Public License, v 1. | ||
*/ | ||
|
||
import { v4 } from 'uuid'; | ||
|
||
/** | ||
* Returns a session ID for the current user. | ||
* We are storing it to the sessionStorage. This means it remains the same through refreshes, | ||
* but it is not persisted when closing the browser/tab or manually navigating to another URL. | ||
*/ | ||
export function getSessionId(): string { | ||
const sessionId = sessionStorage.getItem('sessionId') ?? v4(); | ||
sessionStorage.setItem('sessionId', sessionId); | ||
return sessionId; | ||
} |
Oops, something went wrong.