Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions .github/workflows/sdk-spec.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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: nodejs
16 changes: 13 additions & 3 deletions sdk-spec/steps.mts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,8 @@ function asSpecFlags(flags: Flags) {
return flags.allFlags().map(flag => ({
feature: flag.featureName,
enabled: flag.enabled,
value: flag.value
value: flag.value,
reason: flag.reason
}));
}

Expand Down Expand Up @@ -75,8 +76,17 @@ export async function getExperimentFlag(
traits: Record<string, FlagsmithValue>
) {
const flag = await flagsmith.getExperimentFlag(feature, identifier, traits);
const featureName = flag instanceof Flag ? flag.featureName : feature; // A default flag has no name of its own
return [{ feature: featureName, enabled: flag.enabled, value: flag.value }];
if (flag instanceof Flag) {
return [
{
feature: flag.featureName,
enabled: flag.enabled,
value: flag.value,
reason: flag.reason
}
];
}
return [{ feature, enabled: flag.enabled, value: flag.value }]; // A default flag has no name nor reason of its own
}

// When the application tracks event "{event}" for identity "{identifier}" with value {value} and metadata:
Expand Down
3 changes: 2 additions & 1 deletion sdk/models.ts
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,8 @@ export class Flag extends BaseFlag {
*/
featureName: string;
/**
* The reason for this feature, unique per Flagsmith project.
* Why the flag was evaluated to its value, e.g. `STATIC`, `DEFAULT`,
* `TARGETING_MATCH; segment=premium_users` or `SPLIT; weight=50`.
*/
reason?: string;
/**
Expand Down
12 changes: 11 additions & 1 deletion tests/sdk/flagsmith-environment-flags.test.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import Flagsmith from '../../sdk/index.js';
import { fetch, environmentJSON, flagsJSON } from './fetchMock.js';
import { environmentModel, flagsmith } from './utils.js';
import { DefaultFlag } from '../../sdk/models.js';
import { DefaultFlag, Flag } from '../../sdk/models.js';
import { getUserAgent } from '../../sdk/utils.js';

vi.mock('../../sdk/polling_manager');
Expand Down Expand Up @@ -181,3 +181,13 @@ test('test_local_evaluation', async () => {
expect(flag.value).not.toBe(defaultFlag.value);
expect(flag.value).toBe('some-value');
});

test('getEnvironmentFlags local evaluation sets the STATIC reason, ignoring segments that need an identity', async () => {
const flg = flagsmith({ environmentKey: 'ser.key', enableLocalEvaluation: true });

const flags = await flg.getEnvironmentFlags();
const flag = flags.getFlag('some_feature') as Flag;

expect(flag.value).toBe('some-value');
expect(flag.reason).toBe('STATIC');
});
22 changes: 21 additions & 1 deletion tests/sdk/flagsmith-identity-flags.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import {
transientIdentityJSON,
badFetch
} from './utils.js';
import { DefaultFlag } from '../../sdk/models.js';
import { DefaultFlag, Flag } from '../../sdk/models.js';
import { getUserAgent } from '../../sdk/utils.js';

vi.mock('../../sdk/polling_manager');
Expand Down Expand Up @@ -335,3 +335,23 @@ test('getIdentityFlags fails if API call failed and no default flag handler was
'getIdentityFlags failed and no default flag handler was provided'
);
});

test('getIdentityFlags local evaluation sets the reason of a segment override the traits match', async () => {
const flg = flagsmith({ environmentKey: 'ser.key', enableLocalEvaluation: true });

const flags = await flg.getIdentityFlags('identifier', { age: 30 });
const flag = flags.getFlag('some_feature') as Flag;

expect(flag.value).toBe('segment_override');
expect(flag.reason).toBe('TARGETING_MATCH; segment=regular_segment');
});

test('getIdentityFlags local evaluation sets the DEFAULT reason when no segment override matches', async () => {
const flg = flagsmith({ environmentKey: 'ser.key', enableLocalEvaluation: true });

const flags = await flg.getIdentityFlags('identifier', { age: 50 });
const flag = flags.getFlag('some_feature') as Flag;

expect(flag.value).toBe('some-value');
expect(flag.reason).toBe('DEFAULT');
});
Loading