|
| 1 | +// Copyright (c) Microsoft Corporation. |
| 2 | +// Licensed under the MIT license. |
| 3 | + |
| 4 | +import * as assert from "assert"; |
| 5 | +import { StatsbeatFeaturesManager } from "../../../../src/shared/util/statsbeatFeaturesManager"; |
| 6 | +import { StatsbeatFeature, StatsbeatInstrumentation } from "../../../../src/shim/types"; |
| 7 | + |
| 8 | +describe("shared/util/StatsbeatFeaturesManager", () => { |
| 9 | + let originalEnv: NodeJS.ProcessEnv; |
| 10 | + |
| 11 | + beforeEach(() => { |
| 12 | + // Save original environment |
| 13 | + originalEnv = { ...process.env }; |
| 14 | + // Clear the AZURE_MONITOR_STATSBEAT_FEATURES environment variable before each test |
| 15 | + delete process.env["AZURE_MONITOR_STATSBEAT_FEATURES"]; |
| 16 | + }); |
| 17 | + |
| 18 | + afterEach(() => { |
| 19 | + // Restore original environment |
| 20 | + process.env = originalEnv; |
| 21 | + }); |
| 22 | + |
| 23 | + describe("initialize", () => { |
| 24 | + it("should initialize environment variable with default values when not set", () => { |
| 25 | + StatsbeatFeaturesManager.getInstance().initialize(); |
| 26 | + |
| 27 | + const envValue = process.env["AZURE_MONITOR_STATSBEAT_FEATURES"]; |
| 28 | + assert.ok(envValue, "AZURE_MONITOR_STATSBEAT_FEATURES should be set after initialization"); |
| 29 | + |
| 30 | + const config = JSON.parse(envValue); |
| 31 | + assert.strictEqual(config.instrumentation, StatsbeatInstrumentation.NONE, "instrumentation should default to NONE"); |
| 32 | + assert.strictEqual(config.feature, StatsbeatFeature.SHIM, "feature should default to SHIM"); |
| 33 | + }); |
| 34 | + |
| 35 | + it("should not overwrite existing environment variable", () => { |
| 36 | + const existingValue = JSON.stringify({ |
| 37 | + instrumentation: StatsbeatInstrumentation.MONGODB, |
| 38 | + feature: StatsbeatFeature.LIVE_METRICS |
| 39 | + }); |
| 40 | + process.env["AZURE_MONITOR_STATSBEAT_FEATURES"] = existingValue; |
| 41 | + |
| 42 | + StatsbeatFeaturesManager.getInstance().initialize(); |
| 43 | + |
| 44 | + assert.strictEqual(process.env["AZURE_MONITOR_STATSBEAT_FEATURES"], existingValue, "existing value should not be overwritten"); |
| 45 | + }); |
| 46 | + }); |
| 47 | + |
| 48 | + describe("enableFeature", () => { |
| 49 | + it("should enable MULTI_IKEY feature using bitmap", () => { |
| 50 | + StatsbeatFeaturesManager.getInstance().initialize(); |
| 51 | + StatsbeatFeaturesManager.getInstance().enableFeature(StatsbeatFeature.MULTI_IKEY); |
| 52 | + |
| 53 | + const envValue = process.env["AZURE_MONITOR_STATSBEAT_FEATURES"]; |
| 54 | + assert.ok(envValue, "environment variable should be set"); |
| 55 | + |
| 56 | + const config = JSON.parse(envValue); |
| 57 | + assert.ok((config.feature & StatsbeatFeature.MULTI_IKEY) !== 0, "MULTI_IKEY feature should be enabled"); |
| 58 | + assert.ok((config.feature & StatsbeatFeature.SHIM) !== 0, "SHIM feature should remain enabled"); |
| 59 | + }); |
| 60 | + |
| 61 | + it("should enable CUSTOMER_STATSBEAT feature using bitmap", () => { |
| 62 | + StatsbeatFeaturesManager.getInstance().initialize(); |
| 63 | + StatsbeatFeaturesManager.getInstance().enableFeature(StatsbeatFeature.CUSTOMER_STATSBEAT); |
| 64 | + |
| 65 | + const envValue = process.env["AZURE_MONITOR_STATSBEAT_FEATURES"]; |
| 66 | + assert.ok(envValue, "environment variable should be set"); |
| 67 | + |
| 68 | + const config = JSON.parse(envValue); |
| 69 | + assert.ok((config.feature & StatsbeatFeature.CUSTOMER_STATSBEAT) !== 0, "CUSTOMER_STATSBEAT feature should be enabled"); |
| 70 | + assert.ok((config.feature & StatsbeatFeature.SHIM) !== 0, "SHIM feature should remain enabled"); |
| 71 | + }); |
| 72 | + |
| 73 | + it("should enable multiple features using bitmap", () => { |
| 74 | + StatsbeatFeaturesManager.getInstance().initialize(); |
| 75 | + StatsbeatFeaturesManager.getInstance().enableFeature(StatsbeatFeature.MULTI_IKEY); |
| 76 | + StatsbeatFeaturesManager.getInstance().enableFeature(StatsbeatFeature.LIVE_METRICS); |
| 77 | + |
| 78 | + const envValue = process.env["AZURE_MONITOR_STATSBEAT_FEATURES"]; |
| 79 | + assert.ok(envValue, "environment variable should be set"); |
| 80 | + |
| 81 | + const config = JSON.parse(envValue); |
| 82 | + assert.ok((config.feature & StatsbeatFeature.MULTI_IKEY) !== 0, "MULTI_IKEY feature should be enabled"); |
| 83 | + assert.ok((config.feature & StatsbeatFeature.LIVE_METRICS) !== 0, "LIVE_METRICS feature should be enabled"); |
| 84 | + assert.ok((config.feature & StatsbeatFeature.SHIM) !== 0, "SHIM feature should remain enabled"); |
| 85 | + }); |
| 86 | + }); |
| 87 | + |
| 88 | + describe("disableFeature", () => { |
| 89 | + it("should disable specific feature using bitmap", () => { |
| 90 | + StatsbeatFeaturesManager.getInstance().initialize(); |
| 91 | + StatsbeatFeaturesManager.getInstance().enableFeature(StatsbeatFeature.MULTI_IKEY); |
| 92 | + StatsbeatFeaturesManager.getInstance().enableFeature(StatsbeatFeature.LIVE_METRICS); |
| 93 | + |
| 94 | + // Disable only MULTI_IKEY |
| 95 | + StatsbeatFeaturesManager.getInstance().disableFeature(StatsbeatFeature.MULTI_IKEY); |
| 96 | + |
| 97 | + const envValue = process.env["AZURE_MONITOR_STATSBEAT_FEATURES"]; |
| 98 | + assert.ok(envValue, "environment variable should be set"); |
| 99 | + |
| 100 | + const config = JSON.parse(envValue); |
| 101 | + assert.strictEqual((config.feature & StatsbeatFeature.MULTI_IKEY), 0, "MULTI_IKEY feature should be disabled"); |
| 102 | + assert.ok((config.feature & StatsbeatFeature.LIVE_METRICS) !== 0, "LIVE_METRICS feature should remain enabled"); |
| 103 | + assert.ok((config.feature & StatsbeatFeature.SHIM) !== 0, "SHIM feature should remain enabled"); |
| 104 | + }); |
| 105 | + }); |
| 106 | + |
| 107 | + describe("isFeatureEnabled", () => { |
| 108 | + it("should correctly detect enabled features", () => { |
| 109 | + StatsbeatFeaturesManager.getInstance().initialize(); |
| 110 | + |
| 111 | + assert.ok(StatsbeatFeaturesManager.getInstance().isFeatureEnabled(StatsbeatFeature.SHIM), "SHIM should be enabled by default"); |
| 112 | + assert.ok(!StatsbeatFeaturesManager.getInstance().isFeatureEnabled(StatsbeatFeature.MULTI_IKEY), "MULTI_IKEY should not be enabled by default"); |
| 113 | + |
| 114 | + StatsbeatFeaturesManager.getInstance().enableFeature(StatsbeatFeature.MULTI_IKEY); |
| 115 | + assert.ok(StatsbeatFeaturesManager.getInstance().isFeatureEnabled(StatsbeatFeature.MULTI_IKEY), "MULTI_IKEY should be enabled after enableFeature"); |
| 116 | + }); |
| 117 | + }); |
| 118 | + |
| 119 | + describe("instrumentation management", () => { |
| 120 | + it("should enable and disable instrumentation features", () => { |
| 121 | + StatsbeatFeaturesManager.getInstance().initialize(); |
| 122 | + |
| 123 | + assert.ok(!StatsbeatFeaturesManager.getInstance().isInstrumentationEnabled(StatsbeatInstrumentation.MONGODB), "MONGODB should not be enabled by default"); |
| 124 | + |
| 125 | + StatsbeatFeaturesManager.getInstance().enableInstrumentation(StatsbeatInstrumentation.MONGODB); |
| 126 | + assert.ok(StatsbeatFeaturesManager.getInstance().isInstrumentationEnabled(StatsbeatInstrumentation.MONGODB), "MONGODB should be enabled after enableInstrumentation"); |
| 127 | + |
| 128 | + StatsbeatFeaturesManager.getInstance().disableInstrumentation(StatsbeatInstrumentation.MONGODB); |
| 129 | + assert.ok(!StatsbeatFeaturesManager.getInstance().isInstrumentationEnabled(StatsbeatInstrumentation.MONGODB), "MONGODB should be disabled after disableInstrumentation"); |
| 130 | + }); |
| 131 | + }); |
| 132 | + |
| 133 | + describe("error handling", () => { |
| 134 | + it("should handle malformed JSON in environment variable", () => { |
| 135 | + process.env["AZURE_MONITOR_STATSBEAT_FEATURES"] = "invalid json"; |
| 136 | + |
| 137 | + // Should not throw and should return default values |
| 138 | + assert.ok(!StatsbeatFeaturesManager.getInstance().isFeatureEnabled(StatsbeatFeature.MULTI_IKEY), "should handle malformed JSON gracefully"); |
| 139 | + |
| 140 | + // Should be able to enable features despite malformed initial value |
| 141 | + StatsbeatFeaturesManager.getInstance().enableFeature(StatsbeatFeature.MULTI_IKEY); |
| 142 | + assert.ok(StatsbeatFeaturesManager.getInstance().isFeatureEnabled(StatsbeatFeature.MULTI_IKEY), "should be able to enable features after handling malformed JSON"); |
| 143 | + }); |
| 144 | + }); |
| 145 | +}); |
0 commit comments