Skip to content

Commit a357194

Browse files
talissoncostaclaude
andcommitted
test: add tests for TraitConfig format in local evaluation
Add tests verifying that segment matching works correctly when traits are passed using the TraitConfig format ({ value, transient }) in local evaluation mode. These tests expose a bug where TraitConfig objects are passed to the engine without unwrapping the actual value, causing segment rules to never match. Tests added: - getIdentityFlags with plain traits matches segment (baseline) - getIdentityFlags with TraitConfig format matches segment - getIdentityFlags with mixed trait formats matches segment - getIdentitySegments with TraitConfig format matches segment Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
1 parent 8c15261 commit a357194

1 file changed

Lines changed: 70 additions & 0 deletions

File tree

tests/sdk/flagsmith-identity-flags.test.ts

Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -209,6 +209,76 @@ test('test_identity_with_transient_traits', async () => {
209209
expect(identityFlags[0].featureName).toBe('some_feature');
210210
});
211211

212+
test('test_get_identity_flags_local_evaluation_with_plain_traits_matches_segment', async () => {
213+
const identifier = 'identifier';
214+
// Plain trait format: age=30 should match segment rule "age LESS_THAN 40"
215+
const traits = { age: 30 };
216+
217+
const flg = flagsmith({
218+
environmentKey: 'ser.key',
219+
enableLocalEvaluation: true
220+
});
221+
222+
const flags = await flg.getIdentityFlags(identifier, traits);
223+
224+
// Should get segment override value, not the default
225+
expect(flags.getFeatureValue('some_feature')).toBe('segment_override');
226+
expect(flags.isFeatureEnabled('some_feature')).toBe(false);
227+
});
228+
229+
test('test_get_identity_flags_local_evaluation_with_trait_config_matches_segment', async () => {
230+
const identifier = 'identifier';
231+
// TraitConfig format: same trait value wrapped with transient metadata
232+
const traits = { age: { value: 30, transient: true } };
233+
234+
const flg = flagsmith({
235+
environmentKey: 'ser.key',
236+
enableLocalEvaluation: true
237+
});
238+
239+
const flags = await flg.getIdentityFlags(identifier, traits);
240+
241+
// Should get segment override value — same result as plain trait format
242+
expect(flags.getFeatureValue('some_feature')).toBe('segment_override');
243+
expect(flags.isFeatureEnabled('some_feature')).toBe(false);
244+
});
245+
246+
test('test_get_identity_flags_local_evaluation_with_mixed_trait_formats_matches_segment', async () => {
247+
const identifier = 'identifier';
248+
// Mix of plain and TraitConfig formats
249+
const traits = {
250+
age: { value: 30, transient: true },
251+
some_other_trait: 'plain_value'
252+
};
253+
254+
const flg = flagsmith({
255+
environmentKey: 'ser.key',
256+
enableLocalEvaluation: true
257+
});
258+
259+
const flags = await flg.getIdentityFlags(identifier, traits);
260+
261+
// Should get segment override value
262+
expect(flags.getFeatureValue('some_feature')).toBe('segment_override');
263+
expect(flags.isFeatureEnabled('some_feature')).toBe(false);
264+
});
265+
266+
test('test_get_identity_segments_with_trait_config_matches_segment', async () => {
267+
const identifier = 'identifier';
268+
// TraitConfig format should work for getIdentitySegments too
269+
const traits = { age: { value: 30, transient: true } };
270+
271+
const flg = flagsmith({
272+
environmentKey: 'ser.key',
273+
enableLocalEvaluation: true
274+
});
275+
276+
const segments = await flg.getIdentitySegments(identifier, traits);
277+
278+
expect(segments).toHaveLength(1);
279+
expect(segments[0].name).toBe('regular_segment');
280+
});
281+
212282
test('getIdentityFlags fails if API call failed and no default flag handler was provided', async () => {
213283
const flg = flagsmith({
214284
fetch: badFetch

0 commit comments

Comments
 (0)