@@ -6,7 +6,8 @@ import * as chai from "chai";
66import chaiAsPromised from "chai-as-promised" ;
77import { featureFlagContentType } from "@azure/app-configuration" ;
88import { load } from "../src/index.js" ;
9- import { mockAppConfigurationClientGetSnapshot , mockAppConfigurationClientListConfigurationSettingsForSnapshot , createMockedConnectionString , createMockedEndpoint , createMockedFeatureFlag , createMockedEnhancedFeatureFlag , createMockedKeyValue , mockAppConfigurationClientListConfigurationSettings , mockFeatureFlagClientListFeatureFlags , restoreMocks , sleepInMs } from "./utils/testHelper.js" ;
9+ import { convert } from "../src/featureManagement/featureFlagConverter.js" ;
10+ import { mockAppConfigurationClientGetSnapshot , mockAppConfigurationClientListConfigurationSettingsForSnapshot , createMockedConnectionString , createMockedEndpoint , createMockedFeatureFlag , createMockedEnhancedFeatureFlag , createMockedKeyValue , mockAppConfigurationClientListConfigurationSettings , mockFeatureFlagClientListFeatureFlags , restoreMocks , sleepInMs , expectEnhancedFeatureFlagJsonError } from "./utils/testHelper.js" ;
1011chai . use ( chaiAsPromised ) ;
1112const expect = chai . expect ;
1213
@@ -567,6 +568,7 @@ describe("enhanced feature flags", function () {
567568 name : "Microsoft.Targeting" ,
568569 parameters : {
569570 Audience : JSON . stringify ( audience ) ,
571+ JsonArray : " [\"one\",\"two\"] " ,
570572 PlainText : "not-json" ,
571573 Percentage : "50"
572574 }
@@ -583,8 +585,43 @@ describe("enhanced feature flags", function () {
583585 . find ( ff => ff . id === "Targeted" ) ;
584586 const parameters = featureFlag . conditions . client_filters [ 0 ] . parameters ;
585587 expect ( parameters . Audience ) . deep . equals ( audience ) ;
588+ expect ( parameters . JsonArray ) . deep . equals ( [ "one" , "two" ] ) ;
586589 expect ( parameters . PlainText ) . equals ( "not-json" ) ;
587- expect ( parameters . Percentage ) . equals ( 50 ) ;
590+ expect ( parameters . Percentage ) . equals ( "50" ) ;
591+ } ) ;
592+
593+ it ( "should throw for invalid JSON in enhanced feature flag filter parameters" , ( ) => {
594+ const enhancedFeatureFlag = createMockedEnhancedFeatureFlag ( "InvalidParameter" , {
595+ conditions : {
596+ filters : [ {
597+ name : "CustomFilter" ,
598+ parameters : { Value : "{not-json}" }
599+ } ]
600+ }
601+ } ) ;
602+
603+ expectEnhancedFeatureFlagJsonError ( ( ) => convert ( enhancedFeatureFlag ) , "InvalidParameter" ) ;
604+ } ) ;
605+
606+ it ( "should parse enhanced feature flag variants based on content type" , ( ) => {
607+ const enhancedFeatureFlag = createMockedEnhancedFeatureFlag ( "VariantContentType" , {
608+ variants : [
609+ { name : "Json" , value : "{\"color\":\"blue\"}" , contentType : "application/json" } ,
610+ { name : "Text" , value : "{\"color\":\"blue\"}" , contentType : "text/plain" }
611+ ]
612+ } ) ;
613+
614+ const featureFlag = convert ( enhancedFeatureFlag ) ;
615+ expect ( featureFlag . variants ?. [ 0 ] . configuration_value ) . deep . equals ( { color : "blue" } ) ;
616+ expect ( featureFlag . variants ?. [ 1 ] . configuration_value ) . equals ( "{\"color\":\"blue\"}" ) ;
617+ } ) ;
618+
619+ it ( "should throw for invalid JSON in an enhanced feature flag variant" , ( ) => {
620+ const enhancedFeatureFlag = createMockedEnhancedFeatureFlag ( "InvalidVariant" , {
621+ variants : [ { name : "Json" , value : "{not-json}" , contentType : "application/json" } ]
622+ } ) ;
623+
624+ expectEnhancedFeatureFlagJsonError ( ( ) => convert ( enhancedFeatureFlag ) , "InvalidVariant" ) ;
588625 } ) ;
589626
590627 it ( "should let an enhanced feature flag supersede a feature flag with the same name" , async ( ) => {
@@ -647,7 +684,7 @@ describe("enhanced feature flags", function () {
647684 expect ( featureFlag . allocation . percentile [ 0 ] . variant ) . equals ( "On" ) ;
648685 // telemetry enabled => metadata populated with the feature flag reference and allocation id
649686 expect ( featureFlag . telemetry . metadata ) . not . undefined ;
650- expect ( featureFlag . telemetry . metadata . FeatureFlagReference ) . contains ( ".appconfig.featureflag/ Variant" ) ;
687+ expect ( featureFlag . telemetry . metadata . FeatureFlagReference ) . equals ( ` ${ createMockedEndpoint ( ) } /ff/ Variant` ) ;
651688 expect ( featureFlag . telemetry . metadata . AllocationId ) . not . undefined ;
652689 } ) ;
653690
0 commit comments