@@ -29,6 +29,83 @@ class DevCycleContextMapperTest {
2929 assertEquals(" user-123" , jsonMap[" userId" ])
3030 }
3131
32+ @Test
33+ fun `maps userId attribute to user ID when no targeting key` () {
34+ val context = ImmutableContext (
35+ attributes = mutableMapOf (
36+ " userId" to Value .String (" user-from-userId" )
37+ )
38+ )
39+
40+ val result = DevCycleContextMapper .evaluationContextToDevCycleUser(context)
41+
42+ assertNotNull(result)
43+ val jsonMap = convertToJsonMap(result!! )
44+ assertEquals(" user-from-userId" , jsonMap[" userId" ])
45+ assertEquals(false , jsonMap[" isAnonymous" ]) // User should be identified
46+ }
47+
48+ @Test
49+ fun `maps user_id attribute to user ID when no targeting key or userId` () {
50+ val context = ImmutableContext (
51+ attributes = mutableMapOf (
52+ " user_id" to Value .String (" user-from-user_id" )
53+ )
54+ )
55+
56+ val result = DevCycleContextMapper .evaluationContextToDevCycleUser(context)
57+
58+ assertNotNull(result)
59+ val jsonMap = convertToJsonMap(result!! )
60+ assertEquals(" user-from-user_id" , jsonMap[" userId" ])
61+ assertEquals(false , jsonMap[" isAnonymous" ]) // User should be identified
62+ }
63+
64+ @Test
65+ fun `prioritizes targeting key over userId and user_id attributes` () {
66+ val context = ImmutableContext (
67+ targetingKey = " targeting-key-user" ,
68+ attributes = mutableMapOf (
69+ " userId" to Value .String (" userId-attribute" ),
70+ " user_id" to Value .String (" user_id-attribute" )
71+ )
72+ )
73+
74+ val result = DevCycleContextMapper .evaluationContextToDevCycleUser(context)
75+
76+ assertNotNull(result)
77+ val jsonMap = convertToJsonMap(result!! )
78+ assertEquals(" targeting-key-user" , jsonMap[" userId" ])
79+
80+ // userId and user_id should not appear in custom data when targeting key is used
81+ val customData = jsonMap[" customData" ] as ? Map <* , * >
82+ assertNull(customData?.get(" userId" ))
83+ assertNull(customData?.get(" user_id" ))
84+ }
85+
86+ @Test
87+ fun `prioritizes user_id over userId attribute when no targeting key` () {
88+ val context = ImmutableContext (
89+ attributes = mutableMapOf (
90+ " userId" to Value .String (" userId-attribute" ),
91+ " user_id" to Value .String (" user_id-attribute" )
92+ )
93+ )
94+
95+ val result = DevCycleContextMapper .evaluationContextToDevCycleUser(context)
96+
97+ assertNotNull(result)
98+ val jsonMap = convertToJsonMap(result!! )
99+ // Should use user_id value as it has higher priority than userId (matching Java SDK)
100+ assertEquals(" user_id-attribute" , jsonMap[" userId" ])
101+ assertEquals(false , jsonMap[" isAnonymous" ]) // User should be identified
102+
103+ // Neither should appear in custom data when used for user ID
104+ val customData = jsonMap[" customData" ] as ? Map <* , * >
105+ assertNull(customData?.get(" userId" ))
106+ assertNull(customData?.get(" user_id" ))
107+ }
108+
32109 @Test
33110 fun `maps standard attributes correctly` () {
34111 val context = ImmutableContext (
0 commit comments