|
35 | 35 | expect(fetched.value).to eq(false) |
36 | 36 | end |
37 | 37 | end |
| 38 | + |
| 39 | + context "when attached to configuration" do |
| 40 | + it "emits PROVIDER_CONFIGURATION_CHANGED event" do |
| 41 | + config = instance_double("OpenFeature::SDK::Configuration") |
| 42 | + allow(config).to receive(:dispatch_provider_event) |
| 43 | + provider.send(:attach, config) |
| 44 | + |
| 45 | + provider.add_flag(flag_key: "new_flag", value: "new_value") |
| 46 | + |
| 47 | + expect(config).to have_received(:dispatch_provider_event).with( |
| 48 | + provider, |
| 49 | + OpenFeature::SDK::ProviderEvent::PROVIDER_CONFIGURATION_CHANGED, |
| 50 | + flags_changed: ["new_flag"] |
| 51 | + ) |
| 52 | + end |
| 53 | + end |
| 54 | + |
| 55 | + context "when not attached to configuration" do |
| 56 | + it "does not emit events" do |
| 57 | + expect { provider.add_flag(flag_key: "new_flag", value: "new_value") }.not_to raise_error |
| 58 | + end |
| 59 | + end |
| 60 | + end |
| 61 | + |
| 62 | + describe "#update_flags" do |
| 63 | + it "replaces all flags" do |
| 64 | + provider.update_flags({"new_key" => "new_value"}) |
| 65 | + |
| 66 | + fetched = provider.fetch_string_value(flag_key: "new_key", default_value: "fallback") |
| 67 | + expect(fetched.value).to eq("new_value") |
| 68 | + |
| 69 | + old_fetched = provider.fetch_string_value(flag_key: "str", default_value: "fallback") |
| 70 | + expect(old_fetched.value).to eq("fallback") |
| 71 | + expect(old_fetched.error_code).to eq(OpenFeature::SDK::Provider::ErrorCode::FLAG_NOT_FOUND) |
| 72 | + end |
| 73 | + |
| 74 | + context "when attached to configuration" do |
| 75 | + it "emits PROVIDER_CONFIGURATION_CHANGED event with all flag keys" do |
| 76 | + config = instance_double("OpenFeature::SDK::Configuration") |
| 77 | + allow(config).to receive(:dispatch_provider_event) |
| 78 | + provider.send(:attach, config) |
| 79 | + |
| 80 | + provider.update_flags({"flag_a" => true, "flag_b" => false}) |
| 81 | + |
| 82 | + expect(config).to have_received(:dispatch_provider_event).with( |
| 83 | + provider, |
| 84 | + OpenFeature::SDK::ProviderEvent::PROVIDER_CONFIGURATION_CHANGED, |
| 85 | + flags_changed: contain_exactly("flag_a", "flag_b") |
| 86 | + ) |
| 87 | + end |
| 88 | + end |
| 89 | + |
| 90 | + context "when not attached to configuration" do |
| 91 | + it "does not emit events" do |
| 92 | + expect { provider.update_flags({"flag_a" => true}) }.not_to raise_error |
| 93 | + end |
| 94 | + end |
| 95 | + end |
| 96 | + |
| 97 | + describe "context callbacks (callable flag values)" do |
| 98 | + it "calls a Proc flag value with evaluation_context" do |
| 99 | + context = OpenFeature::SDK::EvaluationContext.new(targeting_key: "user-123") |
| 100 | + flag_proc = proc { |ctx| ctx.targeting_key == "user-123" } |
| 101 | + provider = described_class.new({"dynamic_flag" => flag_proc}) |
| 102 | + |
| 103 | + fetched = provider.fetch_boolean_value(flag_key: "dynamic_flag", default_value: false, evaluation_context: context) |
| 104 | + |
| 105 | + expect(fetched.value).to eq(true) |
| 106 | + expect(fetched.reason).to eq(OpenFeature::SDK::Provider::Reason::TARGETING_MATCH) |
| 107 | + end |
| 108 | + |
| 109 | + it "calls a lambda flag value with evaluation_context" do |
| 110 | + context = OpenFeature::SDK::EvaluationContext.new(targeting_key: "user-456") |
| 111 | + flag_lambda = ->(ctx) { "hello-#{ctx.targeting_key}" } |
| 112 | + provider = described_class.new({"greeting" => flag_lambda}) |
| 113 | + |
| 114 | + fetched = provider.fetch_string_value(flag_key: "greeting", default_value: "default", evaluation_context: context) |
| 115 | + |
| 116 | + expect(fetched.value).to eq("hello-user-456") |
| 117 | + expect(fetched.reason).to eq(OpenFeature::SDK::Provider::Reason::TARGETING_MATCH) |
| 118 | + end |
| 119 | + |
| 120 | + it "returns STATIC reason for non-callable values" do |
| 121 | + fetched = provider.fetch_string_value(flag_key: "str", default_value: "fallback") |
| 122 | + |
| 123 | + expect(fetched.value).to eq("testing") |
| 124 | + expect(fetched.reason).to eq(OpenFeature::SDK::Provider::Reason::STATIC) |
| 125 | + end |
38 | 126 | end |
39 | 127 |
|
40 | 128 | describe "#fetch_boolean_value" do |
|
0 commit comments