@@ -890,6 +890,143 @@ def test_capture_with_send_feature_flags_options_default_behavior(
890890 msg ["properties" ]["$feature/default-flag" ], "default-value"
891891 )
892892
893+ @mock .patch ("posthog.client.flags" )
894+ def test_capture_with_send_feature_flags_flag_keys_filter (self , patch_flags ):
895+ """Test that flag_keys filters the feature flags that are evaluated"""
896+ with mock .patch ("posthog.client.batch_post" ) as mock_post :
897+ client = Client (
898+ FAKE_TEST_API_KEY ,
899+ on_error = self .set_fail ,
900+ personal_api_key = FAKE_TEST_API_KEY ,
901+ sync_mode = True ,
902+ )
903+
904+ # Set up multiple local flags
905+ client .feature_flags = [
906+ {
907+ "id" : 1 ,
908+ "key" : "flag-one" ,
909+ "active" : True ,
910+ "filters" : {
911+ "groups" : [
912+ {
913+ "properties" : [],
914+ "rollout_percentage" : 100 ,
915+ }
916+ ],
917+ },
918+ },
919+ {
920+ "id" : 2 ,
921+ "key" : "flag-two" ,
922+ "active" : True ,
923+ "filters" : {
924+ "groups" : [
925+ {
926+ "properties" : [],
927+ "rollout_percentage" : 100 ,
928+ }
929+ ],
930+ },
931+ },
932+ {
933+ "id" : 3 ,
934+ "key" : "flag-three" ,
935+ "active" : True ,
936+ "filters" : {
937+ "groups" : [
938+ {
939+ "properties" : [],
940+ "rollout_percentage" : 100 ,
941+ }
942+ ],
943+ },
944+ },
945+ ]
946+
947+ # Only evaluate flag-one and flag-three
948+ send_options = {
949+ "only_evaluate_locally" : True ,
950+ "flag_keys" : ["flag-one" , "flag-three" ],
951+ }
952+
953+ msg_uuid = client .capture (
954+ "test event" , distinct_id = "distinct_id" , send_feature_flags = send_options
955+ )
956+
957+ self .assertIsNotNone (msg_uuid )
958+ self .assertFalse (self .failed )
959+
960+ # Check the message only includes flag-one and flag-three
961+ mock_post .assert_called_once ()
962+ batch_data = mock_post .call_args [1 ]["batch" ]
963+ msg = batch_data [0 ]
964+
965+ # Should have flag-one and flag-three, but not flag-two
966+ self .assertEqual (msg ["properties" ]["$feature/flag-one" ], True )
967+ self .assertEqual (msg ["properties" ]["$feature/flag-three" ], True )
968+ self .assertNotIn ("$feature/flag-two" , msg ["properties" ])
969+
970+ # Active flags should only include flag-one and flag-three
971+ self .assertEqual (
972+ sorted (msg ["properties" ]["$active_feature_flags" ]),
973+ ["flag-one" , "flag-three" ],
974+ )
975+
976+ @mock .patch ("posthog.client.flags" )
977+ def test_capture_with_send_feature_flags_flag_keys_remote_evaluation (
978+ self , patch_flags
979+ ):
980+ """Test that flag_keys filters remote evaluation results"""
981+ # Mock remote flags response with multiple flags
982+ patch_flags .return_value = {
983+ "featureFlags" : {
984+ "remote-flag-one" : "value-one" ,
985+ "remote-flag-two" : "value-two" ,
986+ "remote-flag-three" : "value-three" ,
987+ }
988+ }
989+
990+ with mock .patch ("posthog.client.batch_post" ) as mock_post :
991+ client = Client (
992+ FAKE_TEST_API_KEY ,
993+ on_error = self .set_fail ,
994+ sync_mode = True ,
995+ )
996+
997+ # Only evaluate remote-flag-one and remote-flag-three
998+ send_options = {
999+ "flag_keys" : ["remote-flag-one" , "remote-flag-three" ],
1000+ }
1001+
1002+ msg_uuid = client .capture (
1003+ "test event" , distinct_id = "distinct_id" , send_feature_flags = send_options
1004+ )
1005+
1006+ self .assertIsNotNone (msg_uuid )
1007+ self .assertFalse (self .failed )
1008+
1009+ # Verify flags() was called
1010+ patch_flags .assert_called_once ()
1011+
1012+ # Check the message only includes remote-flag-one and remote-flag-three
1013+ mock_post .assert_called_once ()
1014+ batch_data = mock_post .call_args [1 ]["batch" ]
1015+ msg = batch_data [0 ]
1016+
1017+ # Should have remote-flag-one and remote-flag-three, but not remote-flag-two
1018+ self .assertEqual (msg ["properties" ]["$feature/remote-flag-one" ], "value-one" )
1019+ self .assertEqual (
1020+ msg ["properties" ]["$feature/remote-flag-three" ], "value-three"
1021+ )
1022+ self .assertNotIn ("$feature/remote-flag-two" , msg ["properties" ])
1023+
1024+ # Active flags should only include remote-flag-one and remote-flag-three
1025+ self .assertEqual (
1026+ sorted (msg ["properties" ]["$active_feature_flags" ]),
1027+ ["remote-flag-one" , "remote-flag-three" ],
1028+ )
1029+
8931030 @mock .patch ("posthog.client.flags" )
8941031 def test_capture_exception_with_send_feature_flags_options (self , patch_flags ):
8951032 """Test that capture_exception also supports SendFeatureFlagsOptions"""
@@ -2185,6 +2322,7 @@ def test_parse_send_feature_flags_method(self):
21852322 "only_evaluate_locally" : None ,
21862323 "person_properties" : None ,
21872324 "group_properties" : None ,
2325+ "flag_keys" : None ,
21882326 }
21892327 self .assertEqual (result , expected )
21902328
@@ -2195,6 +2333,7 @@ def test_parse_send_feature_flags_method(self):
21952333 "only_evaluate_locally" : None ,
21962334 "person_properties" : None ,
21972335 "group_properties" : None ,
2336+ "flag_keys" : None ,
21982337 }
21992338 self .assertEqual (result , expected )
22002339
@@ -2203,13 +2342,15 @@ def test_parse_send_feature_flags_method(self):
22032342 "only_evaluate_locally" : True ,
22042343 "person_properties" : {"plan" : "premium" },
22052344 "group_properties" : {"company" : {"type" : "enterprise" }},
2345+ "flag_keys" : ["beta-feature" , "my-flag" ],
22062346 }
22072347 result = client ._parse_send_feature_flags (options )
22082348 expected = {
22092349 "should_send" : True ,
22102350 "only_evaluate_locally" : True ,
22112351 "person_properties" : {"plan" : "premium" },
22122352 "group_properties" : {"company" : {"type" : "enterprise" }},
2353+ "flag_keys" : ["beta-feature" , "my-flag" ],
22132354 }
22142355 self .assertEqual (result , expected )
22152356
@@ -2221,6 +2362,7 @@ def test_parse_send_feature_flags_method(self):
22212362 "only_evaluate_locally" : None ,
22222363 "person_properties" : {"user_id" : "123" },
22232364 "group_properties" : None ,
2365+ "flag_keys" : None ,
22242366 }
22252367 self .assertEqual (result , expected )
22262368
@@ -2231,6 +2373,7 @@ def test_parse_send_feature_flags_method(self):
22312373 "only_evaluate_locally" : None ,
22322374 "person_properties" : None ,
22332375 "group_properties" : None ,
2376+ "flag_keys" : None ,
22342377 }
22352378 self .assertEqual (result , expected )
22362379
0 commit comments