package testutil import ( ld "github.com/launchdarkly/go-server-sdk/v6" "github.com/launchdarkly/go-server-sdk/v6/ldcomponents" "github.com/launchdarkly/go-server-sdk/v6/testhelpers/ldtestdata" ff "edge-infra.dev/pkg/lib/featureflag" ) // TestFlag is a simplistic representation of an LD feature flag targeting rule. If user is empty, // the variation will apply for all users. type TestFlag struct { FlagContext ff.Contextualizer Name string Value bool } // MustInitTestFeatureFlags is meant for use in test scenarios only. // // It sets a custom data source which will force the featureflag client to be recreated. func MustInitTestFeatureFlags(flagsAndValues map[string]bool) { td := ldtestdata.DataSource() for feat, enabled := range flagsAndValues { td.Update(td.Flag(feat).VariationForAll(enabled)) } var testcfg ld.Config testcfg.Events = ldcomponents.NoEvents() testcfg.DataSource = td ff.SetConfig(testcfg) } // MustInitTestFeatureFlagsForContexts is meant for use in test scenarios only. Parameter flags // is a set of targeting rules used to configure flags for specific Contexts instead of all. func MustInitTestFeatureFlagsForContexts(flags []TestFlag) { td := ldtestdata.DataSource() for _, flag := range flags { fbldr := td.Flag(flag.Name) if flag.FlagContext.Kind() == "" { fbldr = fbldr.VariationForAll(flag.Value) } else { fbldr = fbldr.VariationForKey(flag.FlagContext.Kind(), flag.FlagContext.Key(), flag.Value).On(true).FallthroughVariation(false) } td.Update(fbldr) } var testcfg ld.Config testcfg.Events = ldcomponents.NoEvents() testcfg.DataSource = td ff.SetConfig(testcfg) }