diff --git a/.github/workflows/sdk-spec.yml b/.github/workflows/sdk-spec.yml index b0176d8..99f9981 100644 --- a/.github/workflows/sdk-spec.yml +++ b/.github/workflows/sdk-spec.yml @@ -11,8 +11,8 @@ permissions: jobs: sdk-spec: # Keep the ref in `uses:` and `spec-ref` in sync. - uses: Flagsmith/sdk-spec/.github/workflows/run-spec.yml@v0.9.0 + uses: Flagsmith/sdk-spec/.github/workflows/run-spec.yml@v0.10.0 with: - spec-ref: v0.9.0 + spec-ref: v0.10.0 tags: core events local-evaluation language: ruby diff --git a/lib/flagsmith/sdk/models/flags.rb b/lib/flagsmith/sdk/models/flags.rb index 2177e61..a93d0f8 100644 --- a/lib/flagsmith/sdk/models/flags.rb +++ b/lib/flagsmith/sdk/models/flags.rb @@ -92,6 +92,7 @@ def from_api(json_flag_data) value: json_flag_data.fetch(:feature_state_value) { json_flag_data[:value] }, feature_name: json_flag_data.dig(:feature, :name), feature_id: json_flag_data.dig(:feature, :id), + reason: json_flag_data[:reason], variant: json_flag_data[:variant], experiment: Experiment.from_api(json_flag_data[:metadata]) ) diff --git a/sdk-spec/steps.rb b/sdk-spec/steps.rb index a38be88..faca7e1 100644 --- a/sdk-spec/steps.rb +++ b/sdk-spec/steps.rb @@ -7,7 +7,7 @@ require 'flagsmith' def as_spec_flag(flag) - { feature: flag.feature_name, enabled: flag.enabled, value: flag.value } + { feature: flag.feature_name, enabled: flag.enabled, value: flag.value, reason: flag.reason } end def as_spec_flags(flags) diff --git a/spec/sdk/fixtures/flags.json b/spec/sdk/fixtures/flags.json index 633b24f..33cce9f 100644 --- a/spec/sdk/fixtures/flags.json +++ b/spec/sdk/fixtures/flags.json @@ -15,7 +15,8 @@ "enabled": false, "environment": 2310, "identity": null, - "feature_segment": null + "feature_segment": null, + "reason": "STATIC" }, { "id": 8712, @@ -33,7 +34,8 @@ "enabled": true, "environment": 2310, "identity": null, - "feature_segment": null + "feature_segment": null, + "reason": "STATIC" }, { "id": 8724, @@ -51,7 +53,8 @@ "enabled": false, "environment": 2310, "identity": null, - "feature_segment": null + "feature_segment": null, + "reason": "STATIC" }, { "id": 8766, @@ -69,6 +72,7 @@ "enabled": true, "environment": 2310, "identity": null, - "feature_segment": 335 + "feature_segment": 335, + "reason": "STATIC" } ] diff --git a/spec/sdk/fixtures/identities.json b/spec/sdk/fixtures/identities.json index 7f18f47..967c307 100644 --- a/spec/sdk/fixtures/identities.json +++ b/spec/sdk/fixtures/identities.json @@ -16,7 +16,8 @@ "enabled": false, "environment": 2310, "identity": null, - "feature_segment": null + "feature_segment": null, + "reason": "STATIC" }, { "id": 8712, @@ -34,7 +35,8 @@ "enabled": true, "environment": 2310, "identity": null, - "feature_segment": null + "feature_segment": null, + "reason": "STATIC" }, { "id": 8766, @@ -52,7 +54,8 @@ "enabled": true, "environment": 2310, "identity": null, - "feature_segment": 335 + "feature_segment": 335, + "reason": "TARGETING_MATCH; segment=admins" } ], "traits": [ diff --git a/spec/sdk/flagsmith_spec.rb b/spec/sdk/flagsmith_spec.rb index d65b793..fb46389 100644 --- a/spec/sdk/flagsmith_spec.rb +++ b/spec/sdk/flagsmith_spec.rb @@ -26,6 +26,37 @@ end end + describe 'flag reasons' do + it 'returns the reason Flagsmith evaluated each environment flag to' do + # Given / When + flags = subject.get_environment_flags + + # Then + expect(flags.get_flag(:feature_one).reason).to eq('STATIC') + expect(flags.get_flag(:feature_two).reason).to eq('STATIC') + end + + it 'returns the reason Flagsmith evaluated each identity flag to' do + # Given / When + flags = subject.get_identity_flags(user_id) + + # Then + expect(flags.get_flag(:feature_two).reason).to eq('STATIC') + expect(flags.get_flag(:feature_three).reason).to eq('TARGETING_MATCH; segment=admins') + end + + it 'returns a nil reason when Flagsmith does not send one' do + # Given + json_flag_data = { feature: { id: 1, name: 'banner' }, feature_state_value: 'hello', enabled: true } + + # When + flag = Flagsmith::Flags::Flag.from_api(json_flag_data) + + # Then + expect(flag.reason).to be_nil + end + end + describe '#feature_enabled?' do it 'checks a specific feature' do flags = subject.get_environment_flags