|
| 1 | +import { AITestClass } from "@microsoft/ai-test-framework"; |
| 2 | +import { AppInsightsCore as AIInternalCore, AppInsightsExtCore, EventLatency, FullVersionString, IChannelControls, IExtendedConfiguration, IExtendedTelemetryItem, IPropertyStorageOverride, blockDynamicConversion } from '../../../../src/index'; |
| 3 | +import dynamicProto from "@microsoft/dynamicproto-js"; |
| 4 | + |
| 5 | +export class CoreTest extends AITestClass { |
| 6 | + |
| 7 | + private channelExtension: IChannelControls; |
| 8 | + private channelExtensionWithVer: IChannelControls; |
| 9 | + |
| 10 | + public testInitialize() { |
| 11 | + // As the class is using dynamicProto we need to construct at least 1 instance |
| 12 | + // before we can override/replace any prototype method as they are not populated |
| 13 | + // until the 1st instance is created. |
| 14 | + this._disableDynProtoBaseFuncs(dynamicProto); // We need to disable the useBaseInst performance setting as the sinon spy fools the internal logic and the spy is bypassed |
| 15 | + // As we are now using the module as is we need to reach inside the current instance to get the correct instance of applicationinsights-core-js |
| 16 | + QUnit.assert.notEqual(Object.getPrototypeOf(new AIInternalCore()).initialize, undefined, 'The prototype method must exist for the instance'); |
| 17 | + QUnit.assert.notEqual(AIInternalCore.prototype.initialize, undefined, 'The prototype method must exist'); |
| 18 | + |
| 19 | + this.channelExtension = blockDynamicConversion({ |
| 20 | + pause: () => { }, |
| 21 | + resume: () => { }, |
| 22 | + teardown: () => { }, |
| 23 | + flush: (async: any, callBack: any) => { }, |
| 24 | + processTelemetry: (env: any) => { }, |
| 25 | + setNextPlugin: (next: any) => { }, |
| 26 | + initialize: (config: any, core: any, extensions: any) => { }, |
| 27 | + identifier: "testChannel", |
| 28 | + priority: 501 |
| 29 | + }); |
| 30 | + |
| 31 | + this.channelExtensionWithVer = blockDynamicConversion({ |
| 32 | + pause: () => { }, |
| 33 | + resume: () => { }, |
| 34 | + teardown: () => { }, |
| 35 | + flush: (async: any, callBack: any) => { }, |
| 36 | + processTelemetry: (env: any) => { }, |
| 37 | + setNextPlugin: (next: any) => { }, |
| 38 | + initialize: (config: any, core: any, extensions: any) => { }, |
| 39 | + identifier: "testChannel", |
| 40 | + priority: 501, |
| 41 | + version: "channelVer" |
| 42 | + }); |
| 43 | + } |
| 44 | + |
| 45 | + public registerTests() { |
| 46 | + this.testCase({ |
| 47 | + name: "initialize test", |
| 48 | + test: () => { |
| 49 | + this._disableDynProtoBaseFuncs(dynamicProto); // We need to disable the useBaseInst performance setting as the sinon spy fools the internal logic and the spy is bypassed |
| 50 | + |
| 51 | + let coreTrackSpy = this.sandbox.spy(AIInternalCore.prototype, 'initialize'); |
| 52 | + var core: AppInsightsExtCore = new AppInsightsExtCore(); |
| 53 | + |
| 54 | + // As we are now using the module as is we need to reach inside the current instance to get the correct instance of applicationinsights-core-js |
| 55 | + QUnit.assert.notEqual(AIInternalCore.prototype.initialize, Object.getPrototypeOf(core).initialize, 'The prototype method must not be the same as the current instance'); |
| 56 | + |
| 57 | + var testPropertyStorageOverride: IPropertyStorageOverride = { |
| 58 | + setProperty: (key: string, value: string) => { |
| 59 | + |
| 60 | + }, |
| 61 | + getProperty: (key: string) => { |
| 62 | + return 'test property'; |
| 63 | + } |
| 64 | + }; |
| 65 | + var config: IExtendedConfiguration = { |
| 66 | + instrumentationKey: 'testIkey', |
| 67 | + propertyStorageOverride: testPropertyStorageOverride |
| 68 | + }; |
| 69 | + core.initialize(config, [this.channelExtension]); |
| 70 | + QUnit.assert.equal(coreTrackSpy.called, true, "Expecting AI initialize was called"); |
| 71 | + var actualConfig: IExtendedConfiguration = coreTrackSpy.getCall(0).args[0]; |
| 72 | + QUnit.assert.equal(actualConfig.endpointUrl, "https://browser.events.data.microsoft.com/OneCollector/1.0/"); |
| 73 | + QUnit.assert.equal(actualConfig.instrumentationKey, "testIkey"); |
| 74 | + if (actualConfig.propertyStorageOverride) { |
| 75 | + QUnit.assert.ok(actualConfig.propertyStorageOverride.getProperty('testKey') === 'test property'); |
| 76 | + } |
| 77 | + QUnit.assert.equal(core.getWParam(), 0); |
| 78 | + } |
| 79 | + }); |
| 80 | + |
| 81 | + this.testCase({ |
| 82 | + name: "Dynamic Config Test", |
| 83 | + useFakeTimers: true, |
| 84 | + test: () => { |
| 85 | + this._disableDynProtoBaseFuncs(dynamicProto); // We need to disable the useBaseInst performance setting as the sinon spy fools the internal logic and the spy is bypassed |
| 86 | + |
| 87 | + let coreTrackSpy = this.sandbox.spy(AIInternalCore.prototype, 'initialize'); |
| 88 | + let config: IExtendedConfiguration = {}; |
| 89 | + let expectedIkey: string = "test"; |
| 90 | + let expectedEndpointUrl: string = "https://browser.events.data.microsoft.com/OneCollector/1.0/"; |
| 91 | + let expectedPropertyStorageOverride: IPropertyStorageOverride; |
| 92 | + let core: AppInsightsExtCore = new AppInsightsExtCore(); |
| 93 | + |
| 94 | + let onChangeCalled = 0; |
| 95 | + let handler = core.onCfgChange((details) => { |
| 96 | + onChangeCalled ++; |
| 97 | + QUnit.assert.equal(expectedIkey, details.cfg.instrumentationKey, `onChangeCalled ${onChangeCalled} times: Expect the iKey to be set`); |
| 98 | + QUnit.assert.equal(expectedEndpointUrl, details.cfg.endpointUrl, `onChangeCalled ${onChangeCalled} times: Expect the endpoint to be set`); |
| 99 | + QUnit.assert.deepEqual(expectedPropertyStorageOverride, details.cfg.propertyStorageOverride, `onChangeCalled ${onChangeCalled} times: Expect the propertyStorageOverride to be set`); |
| 100 | + }); |
| 101 | + |
| 102 | + // Check defaults |
| 103 | + config = {instrumentationKey: expectedIkey} |
| 104 | + core.initialize(config, [this.channelExtension]); |
| 105 | + QUnit.assert.equal(coreTrackSpy.called, true, "Expecting AI initialize was called"); |
| 106 | + QUnit.assert.equal(1, onChangeCalled, "oncfgChange should be called 1 times"); |
| 107 | + |
| 108 | + // Check config changes |
| 109 | + expectedPropertyStorageOverride = { |
| 110 | + setProperty: (key: string, value: string) => { |
| 111 | + }, |
| 112 | + getProperty: (key: string) => { |
| 113 | + return 'test property'; |
| 114 | + } |
| 115 | + }; |
| 116 | + expectedEndpointUrl = "https://testendpoint.com"; |
| 117 | + expectedIkey = "test1"; |
| 118 | + core.config.instrumentationKey = expectedIkey; |
| 119 | + core.config.endpointUrl = expectedEndpointUrl; |
| 120 | + core.config.propertyStorageOverride = expectedPropertyStorageOverride; |
| 121 | + this.clock.tick(1); |
| 122 | + QUnit.assert.equal(onChangeCalled, 2, "onConfigChange Called 2 times"); |
| 123 | + |
| 124 | + } |
| 125 | + }); |
| 126 | + |
| 127 | + |
| 128 | + this.testCase({ |
| 129 | + name: "track test", |
| 130 | + test: () => { |
| 131 | + this._disableDynProtoBaseFuncs(dynamicProto); // We need to disable the useBaseInst performance setting as the sinon spy fools the internal logic and the spy is bypassed |
| 132 | + let coreTrackSpy = this.sandbox.spy(AIInternalCore.prototype, 'track'); |
| 133 | + var core: AppInsightsExtCore = new AppInsightsExtCore(); |
| 134 | + |
| 135 | + QUnit.assert.notEqual(AIInternalCore.prototype.track, Object.getPrototypeOf(core).track, 'The prototype method must not be the same as the current instance'); |
| 136 | + |
| 137 | + var config: IExtendedConfiguration = { |
| 138 | + instrumentationKey: 'testIkey' |
| 139 | + }; |
| 140 | + core.initialize(config, [this.channelExtension]); |
| 141 | + var telemetryItem: IExtendedTelemetryItem = { |
| 142 | + name: 'testEvent', |
| 143 | + baseType: 'testEventBaseType' |
| 144 | + }; |
| 145 | + core.track(telemetryItem); |
| 146 | + QUnit.assert.equal(coreTrackSpy.called, true, "Expecting the AI Core was called"); |
| 147 | + var actualEvent: IExtendedTelemetryItem = coreTrackSpy.getCall(0).args[0]; |
| 148 | + QUnit.assert.equal(actualEvent.name, "testEvent"); |
| 149 | + QUnit.assert.equal(actualEvent.latency, EventLatency.Normal); |
| 150 | + QUnit.assert.ok(actualEvent.ext['sdk']['ver'].indexOf('1DS-Web-JS') > -1); |
| 151 | + QUnit.assert.equal(actualEvent.ext['sdk']['ver'], FullVersionString, actualEvent.ext['sdk']['ver']); |
| 152 | + QUnit.assert.equal(isNaN(actualEvent.timings.trackStart as number), false); |
| 153 | + QUnit.assert.equal(actualEvent.baseData['properties']['version'], '', actualEvent.baseData['properties']['version']); |
| 154 | + } |
| 155 | + }); |
| 156 | + |
| 157 | + this.testCase({ |
| 158 | + name: "track test with provided item properties version", |
| 159 | + test: () => { |
| 160 | + this._disableDynProtoBaseFuncs(dynamicProto); // We need to disable the useBaseInst performance setting as the sinon spy fools the internal logic and the spy is bypassed |
| 161 | + let coreTrackSpy = this.sandbox.spy(AIInternalCore.prototype, 'track'); |
| 162 | + var core: AppInsightsExtCore = new AppInsightsExtCore(); |
| 163 | + |
| 164 | + QUnit.assert.notEqual(AIInternalCore.prototype.track, Object.getPrototypeOf(core).track, 'The prototype method must not be the same as the current instance'); |
| 165 | + |
| 166 | + var config: IExtendedConfiguration = { |
| 167 | + instrumentationKey: 'testIkey' |
| 168 | + }; |
| 169 | + |
| 170 | + core.initialize(config, [this.channelExtensionWithVer]); |
| 171 | + var telemetryItem: IExtendedTelemetryItem = { |
| 172 | + name: 'testEvent', |
| 173 | + baseType: 'testEventBaseType', |
| 174 | + baseData:{} |
| 175 | + }; |
| 176 | + telemetryItem.baseData['properties'] = {version:'version1'}; |
| 177 | + core.track(telemetryItem); |
| 178 | + QUnit.assert.equal(coreTrackSpy.called, true, "Expecting the AI Core was called"); |
| 179 | + var actualEvent: IExtendedTelemetryItem = coreTrackSpy.getCall(0).args[0]; |
| 180 | + QUnit.assert.equal(actualEvent.name, "testEvent"); |
| 181 | + QUnit.assert.ok(actualEvent.ext['sdk']['ver'].indexOf('1DS-Web-JS') > -1); |
| 182 | + QUnit.assert.equal(actualEvent.ext['sdk']['ver'], FullVersionString, actualEvent.ext['sdk']['ver']); |
| 183 | + QUnit.assert.equal(actualEvent.baseData['properties']['version'], 'version1', actualEvent.baseData['properties']['version']); |
| 184 | + } |
| 185 | + }); |
| 186 | + |
| 187 | + this.testCase({ |
| 188 | + name: "track test with provided plugin version and no item properties version", |
| 189 | + test: () => { |
| 190 | + this._disableDynProtoBaseFuncs(dynamicProto); // We need to disable the useBaseInst performance setting as the sinon spy fools the internal logic and the spy is bypassed |
| 191 | + let coreTrackSpy = this.sandbox.spy(AIInternalCore.prototype, 'track'); |
| 192 | + var core: AppInsightsExtCore = new AppInsightsExtCore(); |
| 193 | + |
| 194 | + QUnit.assert.notEqual(AIInternalCore.prototype.track, Object.getPrototypeOf(core).track, 'The prototype method must not be the same as the current instance'); |
| 195 | + |
| 196 | + var config: IExtendedConfiguration = { |
| 197 | + instrumentationKey: 'testIkey' |
| 198 | + }; |
| 199 | + core.initialize(config, [this.channelExtensionWithVer]); |
| 200 | + var telemetryItem: IExtendedTelemetryItem = { |
| 201 | + name: 'testEvent', |
| 202 | + baseType: 'testEventBaseType' |
| 203 | + }; |
| 204 | + core.track(telemetryItem); |
| 205 | + QUnit.assert.equal(coreTrackSpy.called, true, "Expecting the AI Core was called"); |
| 206 | + var actualEvent: IExtendedTelemetryItem = coreTrackSpy.getCall(0).args[0]; |
| 207 | + QUnit.assert.equal(actualEvent.name, "testEvent"); |
| 208 | + QUnit.assert.ok(actualEvent.ext['sdk']['ver'].indexOf('1DS-Web-JS') > -1); |
| 209 | + QUnit.assert.equal(actualEvent.ext['sdk']['ver'], FullVersionString, actualEvent.ext['sdk']['ver']); |
| 210 | + QUnit.assert.equal(actualEvent.baseData['properties']['version'], 'testChannel=channelVer', actualEvent.baseData['properties']['version']); |
| 211 | + } |
| 212 | + }); |
| 213 | + |
| 214 | + this.testCase({ |
| 215 | + name: "Check inheritence implementation", |
| 216 | + test: () => { |
| 217 | + var core1: AppInsightsExtCore = new AppInsightsExtCore(); |
| 218 | + var core2: AppInsightsExtCore = new AppInsightsExtCore(); |
| 219 | + |
| 220 | + // Make sure the 2 initialize methods are actually different function instances |
| 221 | + QUnit.assert.ok(!core1.hasOwnProperty("initialize"), "initialize should not be an instance function"); |
| 222 | + QUnit.assert.ok(!core2.hasOwnProperty("initialize"), "initialize should not be an instance function"); |
| 223 | + QUnit.assert.equal(core1.initialize, core2.initialize, "initialize function should be the same (i.e. prototype based methods)"); |
| 224 | + |
| 225 | + QUnit.assert.ok(!core1.hasOwnProperty("track"), "track should not be an instance function"); |
| 226 | + QUnit.assert.ok(!core2.hasOwnProperty("track"), "track should not be an instance function"); |
| 227 | + QUnit.assert.equal(core1.track, core2.track, "track function should be the same (i.e. prototype based methods)"); |
| 228 | + } |
| 229 | + }); |
| 230 | + } |
| 231 | +} |
0 commit comments