|
| 1 | +// Copyright (c) Microsoft Corporation. |
| 2 | +// Licensed under the MIT license. |
| 3 | + |
| 4 | +import * as sinon from "sinon"; |
| 5 | +import * as fs from "node:fs/promises"; |
| 6 | +import { FeatureManager, ConfigurationObjectFeatureFlagProvider } from "@microsoft/feature-management"; |
| 7 | +import { createTelemetryPublisher as createNodeTelemetryPublisher } from "@microsoft/feature-management-applicationinsights-node"; |
| 8 | +import { createTelemetryPublisher as createBrowserTelemetryPublisher } from "@microsoft/feature-management-applicationinsights-browser"; |
| 9 | +import { FILE_PATH, SAMPLE_JSON_KEY, TESTS_JSON_KEY, validateFeatureEvaluation, validateTelemetry, FeatureFlagTest } from "./utils.js"; |
| 10 | +import { ApplicationInsights } from "@microsoft/applicationinsights-web"; |
| 11 | +import applicationInsights from "applicationinsights"; |
| 12 | + |
| 13 | +// For telemetry validation |
| 14 | +let eventNameToValidate; |
| 15 | +let eventPropertiesToValidate; |
| 16 | + |
| 17 | +applicationInsights.setup("DUMMY-CONNECTION-STRING").start(); |
| 18 | +sinon.stub(applicationInsights.defaultClient, "trackEvent").callsFake((event) => { |
| 19 | + eventNameToValidate = event.name; |
| 20 | + eventPropertiesToValidate = event.properties; |
| 21 | +}); |
| 22 | + |
| 23 | +const appInsights = new ApplicationInsights({ config: { connectionString: "DUMMY-CONNECTION-STRING" }}); |
| 24 | +sinon.stub(appInsights, "trackEvent").callsFake((event, customProperties) => { |
| 25 | + eventNameToValidate = event.name; |
| 26 | + eventPropertiesToValidate = customProperties; |
| 27 | +}); |
| 28 | + |
| 29 | +async function runTestWithNodePackage(testName: string) { |
| 30 | + const config = JSON.parse(await fs.readFile(FILE_PATH + testName + SAMPLE_JSON_KEY, "utf8")); |
| 31 | + const testcases: FeatureFlagTest[] = JSON.parse(await fs.readFile(FILE_PATH + testName + TESTS_JSON_KEY, "utf8")); |
| 32 | + const ffProvider = new ConfigurationObjectFeatureFlagProvider(config); |
| 33 | + const fm = new FeatureManager(ffProvider, { onFeatureEvaluated: createNodeTelemetryPublisher(applicationInsights.defaultClient) }); |
| 34 | + |
| 35 | + for (const testcase of testcases) { |
| 36 | + const featureFlagName = testcase.FeatureFlagName; |
| 37 | + const context = { userId: testcase.Inputs?.User, groups: testcase.Inputs?.Groups }; |
| 38 | + await fm.getVariant(featureFlagName, context); |
| 39 | + validateTelemetry(testcase, undefined, eventNameToValidate, eventPropertiesToValidate); |
| 40 | + validateFeatureEvaluation(testcase, fm); |
| 41 | + } |
| 42 | +} |
| 43 | + |
| 44 | +async function runTestWithBrowserPackage(testName: string) { |
| 45 | + const config = JSON.parse(await fs.readFile(FILE_PATH + testName + SAMPLE_JSON_KEY, "utf8")); |
| 46 | + const testcases: FeatureFlagTest[] = JSON.parse(await fs.readFile(FILE_PATH + testName + TESTS_JSON_KEY, "utf8")); |
| 47 | + const ffProvider = new ConfigurationObjectFeatureFlagProvider(config); |
| 48 | + const fm = new FeatureManager(ffProvider, { onFeatureEvaluated: createBrowserTelemetryPublisher(appInsights) }); |
| 49 | + |
| 50 | + for (const testcase of testcases) { |
| 51 | + const featureFlagName = testcase.FeatureFlagName; |
| 52 | + const context = { userId: testcase.Inputs?.User, groups: testcase.Inputs?.Groups }; |
| 53 | + await fm.getVariant(featureFlagName, context); |
| 54 | + validateTelemetry(testcase, undefined, eventNameToValidate, eventPropertiesToValidate); |
| 55 | + validateFeatureEvaluation(testcase, fm); |
| 56 | + } |
| 57 | +} |
| 58 | + |
| 59 | +describe("telemetry with provider and node package", function () { |
| 60 | + it("should pass BasicTelemetry test", async () => { |
| 61 | + await runTestWithNodePackage("BasicTelemetry"); |
| 62 | + }); |
| 63 | +}); |
| 64 | + |
| 65 | +describe("telemetry with provider and browser package", function () { |
| 66 | + it("should pass BasicTelemetry test", async () => { |
| 67 | + await runTestWithBrowserPackage("BasicTelemetry"); |
| 68 | + }); |
| 69 | +}); |
0 commit comments