@@ -17,9 +17,12 @@ import { getHashedPercentageForObjIds } from '../../../../flagsmith-engine/utils
1717import { getEvaluationContext } from '../../../../flagsmith-engine/evaluationContext/mappers.js' ;
1818import {
1919 EvaluationContext ,
20+ InSegmentCondition ,
2021 SegmentCondition ,
22+ SegmentCondition1 ,
2123 SegmentContext
2224} from '../../../../flagsmith-engine/evaluationContext/evaluationContext.types.js' ;
25+ import { SegmentConditionModel } from '../../../../flagsmith-engine/segments/models.js' ;
2326
2427// todo: work out how to implement this in a test function or before hook
2528vi . mock ( '../../../../flagsmith-engine/utils/hashing' , ( ) => ( {
@@ -203,6 +206,98 @@ describe('getIdentitySegments integration', () => {
203206 } ) ;
204207} ) ;
205208
209+ describe ( 'IN operator' , ( ) => {
210+ const mockContext : EvaluationContext = {
211+ environment : { key : 'env' , name : 'test' } ,
212+ identity : {
213+ key : 'test-user' ,
214+ identifier : 'test' ,
215+ traits : { name : 'test' }
216+ } ,
217+ segments : { } ,
218+ features : { }
219+ } ;
220+
221+ test . each ( [
222+ // Array of strings
223+ [
224+ {
225+ property : '$.identity.identifier' ,
226+ operator : CONDITION_OPERATORS . IN ,
227+ value : [ 'test' , 'john-doe' ]
228+ } ,
229+ true
230+ ] ,
231+ [
232+ {
233+ property : '$.identity.identifier' ,
234+ operator : CONDITION_OPERATORS . IN ,
235+ value : [ 'john-doe' ]
236+ } ,
237+ false
238+ ] ,
239+
240+ // JSON encoded
241+ [
242+ {
243+ property : '$.identity.identifier' ,
244+ operator : CONDITION_OPERATORS . IN ,
245+ value : '["test", "john-doe"]'
246+ } ,
247+ true
248+ ] ,
249+ [
250+ {
251+ property : '$.identity.identifier' ,
252+ operator : CONDITION_OPERATORS . IN ,
253+ value : '["john-doe"]'
254+ } ,
255+ false
256+ ] ,
257+
258+ // Legacy value string to split
259+ [
260+ {
261+ property : '$.identity.identifier' ,
262+ operator : CONDITION_OPERATORS . IN ,
263+ value : 'test,john-doe'
264+ } ,
265+ true
266+ ] ,
267+ [
268+ {
269+ property : '$.identity.identifier' ,
270+ operator : CONDITION_OPERATORS . IN ,
271+ value : 'john-doe'
272+ } ,
273+ false
274+ ] ,
275+ // Fails because the value is split in middle
276+ [
277+ {
278+ property : '$.identity.identifier' ,
279+ operator : CONDITION_OPERATORS . IN ,
280+ value : 'te,st,john-doe'
281+ } ,
282+ false
283+ ] ,
284+
285+ // Edge cases
286+ [ { property : '$.identity.identifier' , operator : CONDITION_OPERATORS . IN , value : '' } , false ] ,
287+ [ { property : '$.identity.identifier' , operator : CONDITION_OPERATORS . IN , value : [ ] } , false ] ,
288+ [
289+ { property : '$.identity.identifier' , operator : CONDITION_OPERATORS . IN , value : '[]' } ,
290+ false
291+ ]
292+ ] as Array < [ SegmentCondition | InSegmentCondition , boolean ] > ) (
293+ 'evaluates IN condition %j to %s' ,
294+ ( condition : SegmentCondition | InSegmentCondition , expected : boolean ) => {
295+ const result = traitsMatchSegmentCondition ( condition , 'segment' , mockContext ) ;
296+ expect ( result ) . toBe ( expected ) ;
297+ }
298+ ) ;
299+ } ) ;
300+
206301describe ( 'evaluateIdentityInSegment' , ( ) => {
207302 const mockContext : EvaluationContext = {
208303 environment : { key : 'env' , name : 'test' } ,
@@ -361,7 +456,11 @@ describe('percentage split operator', () => {
361456 ] ) ( 'percentage %d with threshold %d returns %s' , ( hashedValue , threshold , expected ) => {
362457 const mockHashFn = getHashedPercentageForObjIds ;
363458 mockHashFn . mockReturnValue ( hashedValue ) ;
364- const condition = { property : 'any' , operator : 'PERCENTAGE_SPLIT' , value : threshold } ;
459+ const condition = {
460+ property : 'any' ,
461+ operator : 'PERCENTAGE_SPLIT' ,
462+ value : threshold . toString ( )
463+ } as SegmentCondition1 | InSegmentCondition ;
365464 const result = traitsMatchSegmentCondition ( condition , 'seg1' , mockContext ) ;
366465
367466 expect ( result ) . toBe ( expected ) ;
0 commit comments