|
| 1 | +// Copyright (c) Microsoft Corporation. |
| 2 | +// Licensed under the MIT license. |
| 3 | + |
| 4 | +import { setTimeout as delay } from "node:timers/promises"; |
| 5 | +import { assert } from "chai"; |
| 6 | +import { DefaultAzureCredential } from "@azure/identity"; |
| 7 | +import { load } from "../../src/index.js"; |
| 8 | +import type { AzureAppConfiguration } from "../../src/index.js"; |
| 9 | +import { createNotModifiedResponseTracker, INTEGRATION_TEST_ENDPOINT as endpoint, IntegrationTestContext, REFRESH_INTERVAL_IN_MS, refreshUntil } from "./helpers.js"; |
| 10 | + |
| 11 | +interface FeatureFlag { |
| 12 | + id: string; |
| 13 | + enabled: boolean; |
| 14 | + conditions: { |
| 15 | + client_filters: unknown[]; |
| 16 | + requirement_type: "Any" | "All"; |
| 17 | + }; |
| 18 | +} |
| 19 | + |
| 20 | +function getFeatureFlags(settings: AzureAppConfiguration): FeatureFlag[] { |
| 21 | + const featureManagement = settings.get<{ feature_flags: FeatureFlag[] }>("feature_management"); |
| 22 | + if (featureManagement === undefined) { |
| 23 | + throw new Error("Expected the provider to return feature_management.feature_flags."); |
| 24 | + } |
| 25 | + assert.hasAllKeys(featureManagement, ["feature_flags"]); |
| 26 | + assert.isArray(featureManagement.feature_flags); |
| 27 | + return featureManagement.feature_flags; |
| 28 | +} |
| 29 | + |
| 30 | +describe("integration: feature flags", function () { |
| 31 | + let credential: DefaultAzureCredential; |
| 32 | + let context: IntegrationTestContext; |
| 33 | + |
| 34 | + before(() => { |
| 35 | + credential = new DefaultAzureCredential(); |
| 36 | + }); |
| 37 | + |
| 38 | + beforeEach(() => { |
| 39 | + context = new IntegrationTestContext(endpoint, credential); |
| 40 | + console.info(`Integration test key prefix: ${context.keyPrefix}`); |
| 41 | + }); |
| 42 | + |
| 43 | + afterEach(async () => { |
| 44 | + await context.cleanup(); |
| 45 | + }); |
| 46 | + |
| 47 | + it("loads only flags matching the selected IDs and label", async () => { |
| 48 | + await context.setSetting("message", "Ordinary value"); |
| 49 | + await context.setFeatureFlag("selected-enabled", true, { label: "production" }); |
| 50 | + await context.setFeatureFlag("selected-enabled", false, { label: "development" }); |
| 51 | + await context.setFeatureFlag("selected-disabled", false, { label: "production" }); |
| 52 | + await context.setFeatureFlag("excluded", true, { label: "production" }); |
| 53 | + |
| 54 | + const settings = await load(endpoint, credential, { |
| 55 | + selectors: [{ keyFilter: context.key("*") }], |
| 56 | + featureFlagOptions: { |
| 57 | + enabled: true, |
| 58 | + selectors: [{ keyFilter: context.key("selected-*"), labelFilter: "production" }] |
| 59 | + } |
| 60 | + }); |
| 61 | + |
| 62 | + assert.sameDeepMembers(getFeatureFlags(settings), [ |
| 63 | + { |
| 64 | + id: context.key("selected-enabled"), |
| 65 | + enabled: true, |
| 66 | + conditions: { client_filters: [], requirement_type: "Any" } |
| 67 | + }, |
| 68 | + { |
| 69 | + id: context.key("selected-disabled"), |
| 70 | + enabled: false, |
| 71 | + conditions: { client_filters: [], requirement_type: "Any" } |
| 72 | + } |
| 73 | + ]); |
| 74 | + assert.equal(settings.get(context.key("message")), "Ordinary value"); |
| 75 | + assert.sameMembers([...settings.keys()], [context.key("message"), "feature_management"]); |
| 76 | + }); |
| 77 | + |
| 78 | + it("refreshes changed, added, and deleted flags independently of ordinary settings", async () => { |
| 79 | + await context.setSetting("message", "Initial value"); |
| 80 | + await context.setFeatureFlag("updated", true); |
| 81 | + await context.setFeatureFlag("removed", false); |
| 82 | + const notModified = createNotModifiedResponseTracker(); |
| 83 | + const settings = await load(endpoint, credential, { |
| 84 | + selectors: [{ keyFilter: context.key("*") }], |
| 85 | + clientOptions: { |
| 86 | + additionalPolicies: [{ policy: notModified.policy, position: "perCall" }] |
| 87 | + }, |
| 88 | + featureFlagOptions: { |
| 89 | + enabled: true, |
| 90 | + selectors: [{ keyFilter: context.key("*") }], |
| 91 | + refresh: { enabled: true, refreshIntervalInMs: REFRESH_INTERVAL_IN_MS } |
| 92 | + } |
| 93 | + }); |
| 94 | + assert.sameDeepMembers(getFeatureFlags(settings), [ |
| 95 | + { |
| 96 | + id: context.key("updated"), |
| 97 | + enabled: true, |
| 98 | + conditions: { client_filters: [], requirement_type: "Any" } |
| 99 | + }, |
| 100 | + { |
| 101 | + id: context.key("removed"), |
| 102 | + enabled: false, |
| 103 | + conditions: { client_filters: [], requirement_type: "Any" } |
| 104 | + } |
| 105 | + ]); |
| 106 | + assert.equal(settings.get(context.key("message")), "Initial value"); |
| 107 | + |
| 108 | + let notifications = 0; |
| 109 | + const listener = settings.onRefresh(() => notifications++); |
| 110 | + try { |
| 111 | + await context.setSetting("message", "Updated ordinary value"); |
| 112 | + await context.setFeatureFlag("updated", false); |
| 113 | + await context.setFeatureFlag("added", true); |
| 114 | + await context.deleteFeatureFlag("removed"); |
| 115 | + |
| 116 | + await refreshUntil(settings, () => { |
| 117 | + const flags = getFeatureFlags(settings); |
| 118 | + return flags.some(flag => flag.id === context.key("updated") && !flag.enabled) && |
| 119 | + flags.some(flag => flag.id === context.key("added") && flag.enabled) && |
| 120 | + !flags.some(flag => flag.id === context.key("removed")); |
| 121 | + }, "feature flag updates, additions, and deletions"); |
| 122 | + |
| 123 | + assert.sameDeepMembers(getFeatureFlags(settings), [ |
| 124 | + { |
| 125 | + id: context.key("updated"), |
| 126 | + enabled: false, |
| 127 | + conditions: { client_filters: [], requirement_type: "Any" } |
| 128 | + }, |
| 129 | + { |
| 130 | + id: context.key("added"), |
| 131 | + enabled: true, |
| 132 | + conditions: { client_filters: [], requirement_type: "Any" } |
| 133 | + } |
| 134 | + ]); |
| 135 | + assert.equal(settings.get(context.key("message")), "Initial value"); |
| 136 | + assert.equal(notifications, 1); |
| 137 | + |
| 138 | + const previousNotModifiedCount = notModified.count; |
| 139 | + await delay(REFRESH_INTERVAL_IN_MS); |
| 140 | + await settings.refresh(); |
| 141 | + assert.isAbove(notModified.count, previousNotModifiedCount); |
| 142 | + assert.equal(notifications, 1); |
| 143 | + } finally { |
| 144 | + listener.dispose(); |
| 145 | + } |
| 146 | + }); |
| 147 | +}); |
0 commit comments