
    jKj                        d dl Z d dlZd dlZd dlZd dlmZ d dlmZmZ d dlmZ d dl	m	Z	 d dl
mZ d dlmZmZmZ d dlmZmZ d d	lmZ d d
lmZ d dlmZmZ d dlmZ d dlmZ  G d dej:                        Zy)    N)datetime)UUIDuuid4)mock)parameterizedClient)get_context_session_idnew_contextset_context_session)APIErrorGetResponse)capture_message_only_logs)FAKE_TEST_API_KEY)FeatureFlagLegacyFlagMetadata)VERSION)tagc                   >   e Zd Zed        Zed        Zd Zd Zd Z e	j                  g d      d        Zd Zd	 Zd
 Zd Z e	j                  dddifddddfddddfg      d        Zd Zd Z ej(                  d      d        Z ej(                  d      d        Z ej(                  d      d        Zd Zd Zd  Zd! Zd" Zd# Zd$ Zd% Z e	j                  g d&      d'        Z  e	j                  g d&      d(        Z!d) Z"d* Z#d+ Z$d, Z%d- Z&d. Z'd/ Z(d0 Z)d1 Z* ej(                  d      d2        Z+ ej(                  d      d3        Z, ej(                  d      d4        Z- ej(                  d      d5        Z. ej(                  d      d6        Z/ ej(                  d      d7        Z0 e	j                  g d8      d9        Z1 ej(                  d      d:        Z2 ej(                  d      d;        Z3 ej(                  d      d<        Z4 ej(                  d      d=        Z5 ej(                  d      d>        Z6 ej(                  d      d?        Z7 ej(                  d      d@        Z8 ej(                  d      dA        Z9dB Z:dC Z;dD Z<dE Z=dF Z>dG Z?dH Z@dI ZAdJ ZBdK ZCdL ZDdM ZE e	j                  dNdOi i fdPdQdRddSdTdRddSdTfdU eF eG             i i fdVdWi i fdXdYi i fdZd[i i fg      d\        ZHd] ZId^ ZJ e	j                  d_d`dadbdcidbdcifdddedfdgdhdidjgdkdldgifdmdndodpddRdqddrdpddRdqddrfdsdtdudvdpddwdpdxdydzdvdpddwdpdxdydzfg      d{        ZK e	j                  d|d}d~ddd}dd~ifdd~dvdddd~dvdfdi ddi fddd~ddddd~dfg      d        ZLd ZMd ZNd ZOd ZPd ZQd ZRd ZSd ZTd ZUd ZVd ZWd ZXd ZYd ZZ ej(                  d      d        Z[d Z\d Z]d Z^d Z_ ej(                  d      d        Z` ej(                  d       ej(                  d      d               Za ej(                  d      d        Zb e	j                  dddgi dgfdddgi dgfddgi dfddgi dfddgi dfg       ej(                  d      d               Zc ej(                  d      d        Zd ej(                  d      d        Ze e	j                  g d      d        Zf ej(                  d      d        Zgd Zh e	j                  ddg      d        Zid Zjd Zkd Zl ej(                  d       ej(                  d      d               Zm ej(                  d       ej(                  d      d               Zn ej(                  d      d        Zod Zpd Zq ej(                  d      d        Zr ej(                  d      d        Zs ej(                  d      d        Ztd Zu ej(                  d evdī      ū      dƄ        Zw ej(                  d evdǫ      ū      dȄ        Zxy)
TestClientc                     t        j                  d      | _        t        j                  d      | _        | j                  j	                          | j                  j	                          y )Nposthog.client.batch_postposthog.consumer.batch_post)r   patchclient_post_patcherconsumer_post_patcherstartclss    i/Users/ahmed/devFolder/Ultron/claude-voice/.venv/lib/python3.12/site-packages/posthog/test/test_client.py
setUpClasszTestClient.setUpClass   sL     #'**-H"I$(JJ/L$M!%%'!!'')    c                 l    | j                   j                          | j                  j                          y N)r   stopr   r   s    r    tearDownClasszTestClient.tearDownClass   s&    $$&!!&&(r"   c                 ,    t        d||       d| _        y)zMark the failure handlerFAILTN)printfailed)selfebatchs      r    set_failzTestClient.set_fail#   s    far"   c                 R    d| _         t        t        | j                        | _        y )NFon_error)r*   r	   r   r.   clientr+   s    r    setUpzTestClient.setUp(   s    .Gr"   c                 8    | j                  t        t               y r$   )assertRaises	TypeErrorr	   r3   s    r    test_requires_api_keyz TestClient.test_requires_api_key,   s    )V,r"   ))	valid_keyz 
phc_validkey	 phc_validkeyFF)whitespace_only 
	  TT)empty_stringr=   r=   TTc                 \   t         j                  j                  t        j                  d      5 }t        |d      }d d d        | j                  j                  |       | j                  |j                  |       |rj                  d       y j                          y # 1 sw Y   fxY w)NerrorFsendzFapi_key is empty after trimming whitespace; check your project API key)
r   r   objectr	   logassertEqualapi_keydisabledassert_called_once_withassert_not_called)r+   _raw_api_keyexpected_api_keyexpected_disabledexpect_error_log
mock_errorr2   s           r    test_trims_api_key_whitespacez(TestClient.test_trims_api_key_whitespace/   s     ZZvzz73zKe4F 4 	)9:*;<..X ((* 43s   B""B+c                     t        t        ddd      }| j                  |j                  d       | j                  |j                  d       | j                  |j                         y )Nz 
https://eu.posthog.com/	 r<   F)hostpersonal_api_keyrB   zhttps://eu.posthog.com/zhttps://eu.i.posthog.com)r	   r   rE   raw_hostrR   assertIsNonerS   r+   r2   s     r    /test_trims_host_and_personal_api_key_whitespacez:TestClient.test_trims_host_and_personal_api_key_whitespaceE   sX    0%	
 	*CD&@A&112r"   c                 b    t        dd      }| j                  |j                  dd             y )Nr=   FrA   eventdistinct_idrZ   )r	   rU   capturerV   s     r    &test_client_with_empty_api_key_is_noopz1TestClient.test_client_with_empty_api_key_is_noopQ   s)    '&..m.LMr"   c                 |    t         j                  j                          t         j                  j                          y r$   )r	   _client_registryclear_duplicate_client_warningsr3   s    r     _reset_duplicate_client_registryz+TestClient._reset_duplicate_client_registryV   s&    %%'))//1r"   c                    | j                          | j                  | j                          d}t        |f}t        j                  d      5  t        j                  d      5  t        j                  j                  t        j                  d      5 }t        t        |      }t        t        |      }t        t        |      }| j                  ||       | j                  ||       |j                  d       |j                          |j                          |j                          | j                  |t        j                         | j                  |t        j                         t        t        |      }t        t        |      }| j                  |j                  d       |j                          |j                          d d d        d d d        d d d        y # 1 sw Y   xY w# 1 sw Y   xY w# 1 sw Y   y xY w)Nhttps://us.i.posthog.composthog.client.atexit.registerposthog.client.Consumer.startwarningrR   zMultiple active PostHog clients detected for the same project API key and host. Reuse one Posthog instance per app or process when possible to avoid competing background queues and missed shutdown flushes. Multiple clients are supported when intentional.   )rb   
addCleanupr   r   r   rC   r	   rD   assertIsNotrH   shutdownassertNotInr_   ra   rE   
call_count)	r+   rR   registry_keymock_warningfirstsecondthirdfourthfifths	            r    ;test_warns_once_on_duplicate_async_client_same_key_and_hostzFTestClient.test_warns_once_on_duplicate_async_client_same_key_and_hostZ   sz   --/==>))40 JJ78JJ67JJfjj)4,48E-D9F,48EUF+VU+00$ NNOONN\6+B+BC\6+L+LM-D9F,48E\44a8OONN; 5 8 9844 87 98s=   G8!/G,D7G G,G8 G)%G,,G5	1G88Hdifferent_hostrR   zhttps://two.example.com	sync_modehttps://one.example.comT)rR   rx   send_disabledF)rR   rB   c                 N   | j                          | j                  | j                          t        j                  d      5  t        j                  d      5  t        j                  j	                  t
        j                  d      5 }t        t        d      }t        t        fi |}| j                  ||       |j                          |j                          |j                          d d d        d d d        d d d        y # 1 sw Y   xY w# 1 sw Y   xY w# 1 sw Y   y xY w)Nre   rf   rg   ry   rh   )rb   rj   r   r   rC   r	   rD   r   rk   rI   rl   )r+   rJ   duplicate_kwargsrp   rq   	duplicates         r    Ctest_duplicate_client_warning_allows_intentional_multi_client_caseszNTestClient.test_duplicate_client_warning_allows_intentional_multi_client_cases   s     	--/==> JJ78JJ67JJfjj)4,3LME0E4DEIUI.**,NN  5 8 9844 87 98s=   D/DA$D*D2DDDD	DD$c                    t        j                         | j                  _        t        j                         | j                  j                  j                  _        t        t        j                        5 }| j                  j                  dd       d d d        | j                  j                         j                         d       y # 1 sw Y   8xY w)NlevelrZ   zflag-keyzD[PostHog] [FEATURE FLAGS] Using stale cached value for flag flag-key)r   Mockr2   
flag_cacheget_stale_cached_flagreturn_valuer   loggingINFO_get_stale_flag_fallbackrE   getvaluestripr+   logss     r    2test_message_only_info_logs_include_posthog_prefixz=TestClient.test_message_only_info_logs_include_posthog_prefix   s    !%DHIIK44A&W\\:dKK00
K ; 	MMO!!#R	
 ;:s   5C		Cc                    t        t        j                        5 }| j                  j                  j                  d       d d d        | j                  j                         j                         d       y # 1 sw Y   8xY w)Nr   z[PostHog] already prefixed)	r   r   ERRORr2   rD   r@   rE   r   r   r   s     r    ?test_message_only_logs_do_not_duplicate_existing_posthog_prefixzJTestClient.test_message_only_logs_do_not_duplicate_existing_posthog_prefix   sW    &W]];tKKOO!!">? < 	..02NO <;s   &A88Bzposthog.client.getc                     t        ddd      }|j                          |j                          | j                  |j                  g        | j                  |j                         y )Nr=   testF)rS   rB   )r	   load_feature_flagsrI   rE   feature_flagsrU   poller)r+   	patch_getr2   s      r    0test_disabled_client_does_not_load_feature_flagsz;TestClient.test_disabled_client_does_not_load_feature_flags   sP    V%@!!###%--r2&--(r"   posthog.client.flagsc                    t        dd      }| j                  |j                  d      d   i        | j                  |j                  d      i        | j                  |j	                  d      i        | j                  |j                  d      i i d       | j                  |j                  ddd	             |j                          y )
Nr=   FrA   rZ   flagsfeatureFlagsfeatureFlagPayloadsrY   TrZ   send_feature_flags)	r	   rE   get_flags_decisionget_feature_variantsget_feature_payloadsget_feature_flags_and_payloadsrU   r\   rI   r+   patch_flagsr2   s      r    0test_disabled_client_does_not_get_flags_decisionz;TestClient.test_disabled_client_does_not_get_flags_decision   s    '22=A'JBO44]CRH44]CRH11-@;	
 	NN7RVNW	
 	%%'r"   c                    t        dd      |_        t        t        d      dfdi fdfdi fd	fd
i fdfdi i dfg}|D ]8  \  }}}| j	                  |      5  | j                   |       |       d d d        : y # 1 sw Y   ExY w)N  UnauthorizedFrA   r   c                  ,     j                  d      d   S )NrZ   r   )r   r2   s   r    <lambda>zRTestClient.test_client_flag_helpers_return_defaults_on_api_error.<locals>.<lambda>   s    11-@Ir"   r   c                  &     j                  d      S NrZ   )r   r   s   r    r   zRTestClient.test_client_flag_helpers_return_defaults_on_api_error.<locals>.<lambda>       33MBr"   r   c                  &     j                  d      S r   )r   r   s   r    r   zRTestClient.test_client_flag_helpers_return_defaults_on_api_error.<locals>.<lambda>   r   r"   r   c                  &     j                  d      S r   )r   r   s   r    r   zRTestClient.test_client_flag_helpers_return_defaults_on_api_error.<locals>.<lambda>   s    ==mLr"   r   method)r   side_effectr	   r   subTestrE   )r+   r   
test_casesmethod_namecall_helperexpectedr2   s         @r    5test_client_flag_helpers_return_defaults_on_api_errorz@TestClient.test_client_flag_helpers_return_defaults_on_api_error   s    "*3"?)6 %I 'B 'B 1L!#B?!

. 3=.Kh[1  9 21 3=11s    BB	c                 8    | j                   j                          y r$   )r2   flushr3   s    r    test_empty_flushzTestClient.test_empty_flush   s    r"   c                 D   t        t        dd      }|j                  j                  ddi       t	        j
                         }| j                  dd      5 }|j                  d	
       d d d        | j                  t	        j
                         |z
  d       | j                  |j                  j                                | j                  dj                  d          |j                  j                          |j                  j                          y # 1 sw Y   xY w)NFr   rB   threadrY   stuckposthogWARNINGr   g{Gz?timeout_seconds   zflush timed out)r	   r   queueputtime	monotonic
assertLogsr   
assertLessassertFalseemptyassertInoutput
get_nowait	task_done)r+   r2   r   r   s       r    4test_flush_timeout_returns_when_queue_does_not_drainz?TestClient.test_flush_timeout_returns_when_queue_does_not_drain   s    )a@'7+, __Yi_8DLLL. 9 	(50!4++-.'Q8!  98s   DDc                 $   t        t        dd      }|j                  j                  ddi       t        j
                  j                  |j                  j                  dt        d            5  | j                  d	d
      5 }|j                  d       d d d        d d d        | j                  dj                  d          |j                  j                          |j                  j                          y # 1 sw Y   exY w# 1 sw Y   ixY w)NFr   r   rY   r   waitboomr   r   r   r   r   r   zerror flushing queue)r	   r   r   r   r   r   rC   all_tasks_doneRuntimeErrorr   r   r   r   r   r   r+   r2   r   s      r    /test_flush_logs_and_returns_on_unexpected_errorz:TestClient.test_flush_logs_and_returns_on_unexpected_error   s    )a@'7+,ZZLL''$V,  

 ':dQ/ ;
 	,dkk!n=!  ;:
 
s$   /DC:D:D	?DDc                    t        j                  d      5 }t        t        | j                  d      }|j                  dd      }| j                  |       | j                  | j                         |j                          |j                  d   d   }|d	   }| j                  |d
   d       | j                  t        |d   t                     | j                  |j                  d             | j                  |d   d       | j                  |d   d   d       | j                  |d   d   t                | j                  |d   d   d       |d   d   t         j"                  k(  sJ |d   d   t         j"                  k(  sJ |d   d   t         j"                  k(  sJ |d   d   t         j"                  k(  sJ 	 d d d        y # 1 sw Y   y xY w)Nr   Tr1   rx   python test eventrZ   r[   r   r-   r   rY   	timestampuuid
properties$libposthog-python$lib_version
$is_server$python_runtime$python_version$os$os_version)r   r   r	   r   r.   r\   assertIsNotNoner   r*   assert_called_once	call_argsrE   
assertTrue
isinstancestrgetr   ANYr+   	mock_postr2   msg_uuid
batch_datamsgs         r    test_basic_capturezTestClient.test_basic_capture  s   ZZ34	-QUVF~~&9}~UH  *T[[) ((*",,Q/8JQ-CS\+>?OOJs;'7=>  1S/?S.v68HIS.~>HS.|<dC|$%67488CCC|$%67488CCC|$U+txx777|$]3txx???- 544s   F'GGc                 r   t        j                  d      5 }t        t        | j                  dd      }|j                  dd       | j                  | j                         |j                  d   d	   d
   }| j                  |d   d   d       | j                  d|d          d d d        y # 1 sw Y   y xY w)Nr   TF)r1   rx   	is_serverr   rZ   r[   r   r-   r   r   r   r   r   )r   r   r	   r   r.   r\   r   r*   r   rE   rm   r+   r   r2   r   s       r    *test_capture_omits_is_server_when_disabledz5TestClient.test_capture_omits_is_server_when_disabled%  s    ZZ34	!	F NN.MNJT[[)%%a(1!4CS.v68HI\3|+<= 544s   BB--B6c                 L   t        j                  d      5 }t        t        | j                  dddi      }|j                  dd       | j                  | j                         |j                  d	   d
   d   }| j                  |d   d   d       d d d        y # 1 sw Y   y xY w)Nr   Tr   F)r1   rx   super_propertiesr   rZ   r[   r   r-   r   r   )
r   r   r	   r   r.   r\   r   r*   r   rE   r   s       r    1test_is_server_not_overridden_by_super_propertiesz<TestClient.test_is_server_not_overridden_by_super_properties4  s    ZZ34	!".!6	F NN.MNJT[[)%%a(1!4CS.|<dC 544s   A;BB#c                    t        j                  d      5 }t        t        | j                  d      }t        t                     }|j                  dd|      }| j                  ||       | j                  | j                         |j                          |j                  d   d   }|d	   }| j                  |d
   d       | j                  t        |d   t
                     | j                  |d   |       | j                  |d   d       | j                  |d   d   d       | j                  |d   d   t               d d d        y # 1 sw Y   y xY w)Nr   Tr   r   rZ   rZ   r   r   r-   r   rY   r   r   r   r   r   r   )r   r   r	   r   r.   r   r   r\   rE   r   r*   r   r   r   r   r   )r+   r   r2   r   r   r   r   s          r    test_basic_capture_with_uuidz'TestClient.test_basic_capture_with_uuidB  s1   ZZ34	-QUVFuw<D~~#T & H Xt,T[[) ((*",,Q/8JQ-CS\+>?OOJs;'7=>S[$/S/?S.v68HIS.~>H' 544s   D-EEc                    t        j                  d      5 }t        t        | j                  d      }t        d      }|j                  dd|      }| j                  |t        |             | j                  | j                         |j                          |j                  d   d	   d
   }| j                  |d   t        |             d d d        y # 1 sw Y   y xY w)Nr   Tr   z$00000000-0000-4000-8000-000000000002r   rZ   r   r   r-   r   r   )r   r   r	   r   r.   r   r\   rE   r   r   r*   r   r   )r+   r   r2   r   r   r   s         r    #test_basic_capture_with_uuid_objectz.TestClient.test_basic_capture_with_uuid_objectX  s    ZZ34	-QUVF>?D~~#T & H Xs4y1T[[)((*%%a(1!4CS[#d)4 544s   B5CC))zempty stringr=   )zinvalid stringz
not-a-uuid)zshort string1234)integer{   c                 L   t        j                  d      5 }t        t        | j                  d      }| j                  dd      5 }|j                  dd	      }d d d        | j                         t        |       |j                          |j                  d
   d   d   }| j                  |d   |       | j                  |d   t                     | j                  t        fdj                   D                     d d d        y # 1 sw Y   xY w# 1 sw Y   y xY w)Nr   Tr   r   r   r   r   rZ   r   r   r-   r   r   c              3   D   K   | ]  }d |v xr
 d|v xr d|v   yw)zInvalid event uuid z2Expected a valid UUID string or uuid.UUID instancez Falling back to a generated UUIDN ).0messageinvalid_uuids     r    	<genexpr>zbTestClient.test_capture_with_invalid_uuid_logs_and_falls_back_to_generated_uuid.<locals>.<genexpr>  sM       $/ *,)9:gE FLPWWF:gEF $/s    )r   r   r	   r   r.   r   r\   r   r   r   r   rE   assertNotEqualr   r   anyr   )r+   _namer  r   r2   r   r   r   s     `     r    Dtest_capture_with_invalid_uuid_logs_and_falls_back_to_generated_uuidzOTestClient.test_capture_with_invalid_uuid_logs_and_falls_back_to_generated_uuidf  s    ZZ34	-QUVF':d!>>'] *  ;
   *N((*%%a(1!4CS[(3FS->?OO  $(;;	  54:: 54s$   0DDB)DD	DD#c                 L   t        j                  d      5 }t        t        dd      }| j	                  dd      5  |j                  dd|	      }d d d        | j                         t        |       |j                          d d d        y # 1 sw Y   >xY w# 1 sw Y   y xY w)
Nr   T)debugrx   r   r   r   r   rZ   r   )	r   r   r	   r   r   r\   r   r   r   )r+   r  r  r   r2   r   s         r    2test_capture_with_invalid_uuid_falls_back_in_debugz=TestClient.test_capture_with_invalid_uuid_falls_back_in_debug  s     ZZ34	-TTJF':!>>'] *  ;
   *N((* 54:: 54s"   &BB4BB	BB#c                 \   t        j                  d      5 }t        t        | j                  d      }|j                  dd      }| j                  |       | j                  | j                         |j                          |j                  d   d   }|d	   }| j                  |d
   d       | j                  t        |d   t                     | j                  |d   d       | j                  |d   d   d       | j                  |d   d   t               d d d        y # 1 sw Y   y xY w)Nr   T)project_api_keyr1   rx   r   rZ   r[   r   r-   r   rY   r   r   r   r   r   )r   r   r	   r   r.   r\   r   r   r*   r   r   rE   r   r   r   r   r   s         r    'test_basic_capture_with_project_api_keyz2TestClient.test_basic_capture_with_project_api_key  s   ZZ34	 1F ~~&9}~UH  *T[[) ((*",,Q/8JQ-CS\+>?OOJs;'7=>S/?S.v68HIS.~>H) 544s   DD""D+c                 p   t        j                  d      5 }t        t        ddid      }|j	                  dd      }| j                  |       |j                          |j                  d	   d
   }|d   }| j                  |d   d       | j                  |d   d   d       d d d        y # 1 sw Y   y xY w)Nr   sourcez	repo-nameTr   rx   r   rZ   r[   r   r-   r   rY   r   )	r   r   r	   r   r\   r   r   r   rE   r   s         r    test_basic_super_propertiesz&TestClient.test_basic_super_properties  s    ZZ34	!"*K!8F ~~&9}~UH  * ((*",,Q/8JQ-CS\+>?S.x8+F! 544s   BB,,B5c                 |   t         j                  j                  t        dd       5 }| j                  }t        d      }|j                  |d       | j                  |j                         |j                  }| j                  |d   d   d       | j                  |d   d   d       d d d        y # 1 sw Y   y xY w	Nr\   r   test exceptionrZ   r[   r   
$exceptionr   r   r   rC   r	   r2   	Exceptioncapture_exceptionr   calledr   rE   r+   patch_capturer2   	exceptioncapture_calls        r    test_basic_capture_exceptionz'TestClient.test_basic_capture_exception      ZZvytD[[F!"23I$$YM$JOOM001(22L\!_Q/>\!_];]K EDD   BB22B;c                 |   t         j                  j                  t        dd       5 }| j                  }t        d      }|j                  |d       | j                  |j                         |j                  }| j                  |d   d   d       | j                  |d   d   d       d d d        y # 1 sw Y   y xY wr  r!  r%  s        r    -test_basic_capture_exception_with_distinct_idz8TestClient.test_basic_capture_exception_with_distinct_id  r*  r+  c                    t         j                  j                  t        dd       5 }t        t        | j
                  d      }t        d      }|j                  |d       | j                  |j                         |j                  }| j                  |d   d   d	       | j                  |d
   d   d       d d d        y # 1 sw Y   y xY w)Nr\   r  zhttps://aloha.comr1   rR   r  rZ   r[   r   r   r   r   r   rC   r	   r   r.   r"  r#  r   r$  r   rE   )r+   r&  r2   r'  calls        r    9test_basic_capture_exception_with_correct_host_generationzDTestClient.test_basic_capture_exception_with_correct_host_generation  s    ZZvytD!DMM@SF ""23I$$YM$JOOM001 **DT!WQZ6T!W]3]C EDD   BCCc                    t         j                  j                  t        dd       5 }t        t        | j
                  d      }t        d      }|j                  |d       | j                  |j                         |j                  }| j                  |d   d   d	       | j                  |d
   d   d       d d d        y # 1 sw Y   y xY w)Nr\   r  https://app.posthog.comr/  r  rZ   r[   r   r   r   r0  r%  s        r    Jtest_basic_capture_exception_with_correct_host_generation_for_server_hostszUTestClient.test_basic_capture_exception_with_correct_host_generation_for_server_hosts  s     ZZvytD!.F
 ""23I$$YM$JOOM001(22L\!_Q/>\!_];]K EDDr3  c                    t         j                  j                  t        dd       5 }| j                  }	 t        d      # t
        $ r |j                  d d       Y nw xY w| j                  |j                         |j                  }t        |       | j                  |d   d   d       | j                  |d   d   d       | j                  |d   d	   d
   d   d   d   d       | j                  |d   d	   d
   d   d   d   d       | j                  |d   d	   d
   d   d   d        | j                  |d   d	   d
   d   d   d       | j                  |d   d	   d
   d   d   d       | j                  |d   d	   d
   d   d   d   d       | j                  |d   d	   d
   d   d   d   d   d   d       | j                  |d   d	   d
   d   d   d   d   d   d       | j                  |d   d	   d
   d   d   d   d   d   d       | j                  |d   d	   d
   d   d   d   d   d   d       d d d        y # 1 sw Y   y xY w)Nr\   r  r  rZ   r[   r   r   r   r   z$exception_list	mechanismtypegenerichandledTmoduler"  value
stacktracerawframesfilenamezposthog/test/test_client.pyfunction4test_basic_capture_exception_with_no_exception_givenzposthog.test.test_clientin_app)r   r   rC   r	   r2   r"  r#  r   r$  r   r)   rE   )r+   r&  r2   r(  s       r    rC  z?TestClient.test_basic_capture_exception_with_no_exception_given  s   ZZvytD[[FJ 011 J((=(IJ OOM001(22L,\!_];]K\!_Q/>Q-.?@CKP 	 Q-.?@CKP 	 Q-.?@CHMt Q-.?@CFK[ Q-.?@CGL  Q-.?@CLQ 	 Q-.?@CLQ! .	 Q-.?@CLQ! G	 Q-.?@CLQ +	 Q-.?@CLQ 	u EDDs)   IA  AIAGIIc                 x   t         j                  j                  t        dd       5 }| j	                  dd      5 }| j
                  }|j                  d        | j                  |j                         | j                  |j                  d   d       d d d        d d d        y # 1 sw Y   xY w# 1 sw Y   y xY w)Nr\   r  r   r   r   r   z<WARNING:posthog:[PostHog] No exception information available)r   r   rC   r	   r   r2   r#  r   r$  rE   r   )r+   r&  r   r2   s       r    8test_basic_capture_exception_with_no_exception_happeningzCTestClient.test_basic_capture_exception_with_no_exception_happening;  s    ZZvytD)<((.  !5!56  KKNR = ED<< EDs#   B0AB$B0$B-	)B00B9c                     t        t        d      }| j                  dd      5 }|j                  t	        d      d       | j                  |j                  d	   d
       d d d        y # 1 sw Y   y xY w)NT)log_captured_exceptionsr   r   r   r  rZ   r[   r   z5ERROR:posthog:[PostHog] test exception
NoneType: None)r	   r   r   r#  r"  rE   r   r   s      r    (test_capture_exception_logs_when_enabledz3TestClient.test_capture_exception_logs_when_enabledG  si    )4H__Yg_6$$$*+ %  A X	 766s   <A**A3c                 X   dddii|_         t        j                  d      5 }t        t        | j
                  t        d      }|j                  ddd	      }| j                  |       | j                  | j                         |j                          |j                  d
   d   }|d   }| j                  |d   d       | j                  t        |d   t                     | j                  |j!                  d             | j                  |d   d       | j                  |d   d   d       | j                  |d   d   t"               | j                  |d   d   d       | j                  |d   d   dg       | j                  |j$                  d
       d d d        y # 1 sw Y   y xY w)Nr   beta-featurerandom-variantr   Tr1   rS   rx   r   rZ   r   r   r-   r   rY   r   r   r   r   r   r   $feature/beta-feature$active_feature_flagsr   r   r   r	   r   r.   r\   r   r   r*   r   r   rE   r   r   r   r   r   rn   r+   r   r   r2   r   r   r   s          r    %test_basic_capture_with_feature_flagsz0TestClient.test_basic_capture_with_feature_flagsQ  s   $2^EU4V#W ZZ34	!!2	F ~~#SW & H   *T[[) ((*",,Q/8JQ-CS\+>?OOJs;'7=>  1S/?S.v68HIS.~>HL!"9:<L L!"9:^<L [33Q7? 544s   E6F  F)c                    dddii|_         dddddd	d
dddgddddigdddddddddddddgidddiddd}ddd dd!dd"gd
d#gddgd$d%id&d'}ddd(dg d)dgd$d%id&d'}t        j                  d*      5 }t        t        | j
                  t        d+      }|||g|_        |j                  d,d-d.      }| j                  |       | j                  | j                         |j                          |j                  d   d/   }|d)   }	| j                  |	d0   d,       | j                  t        |	d1   t                      | j                  |	j#                  d2             | j                  |	d-   d-       | j                  |	d3   d4   d5       | j                  |	d3   d6   t$               | j                  |	d3   d7   d       | j                  |	d3   d8   d9       | j                  |	d3   d:   dg       d;|	d3   vsJ | j                  |j&                  d)       d d d        t        j                  d*      5 }t        t        | j
                  t        d+      }g |_        |j                  d,d-<      }| j                  |       | j                  | j                         |j                          |j                  d   d/   }|d)   }	d;|	d3   vsJ d7|	d3   vsJ d8|	d3   vsJ d:|	d3   vsJ 	 d d d        y # 1 sw Y   xY w# 1 sw Y   y xY w)=Nr   rK  rL  r   Beta Featurebeta-feature-localTd   emailpersontest@posthog.comexactkeyr9  r=  operatorr   rollout_percentager_  2   variantsfirst-variantFirst Variantr\  namer_  second-variantSecond Variant   third-variantThird Variantsome-payloadajsonrb  ri  groupsmultivariatepayloadsidre  r\  activer_  filtersperson-flagregionUSAr\  r]  r=  r9  true,  rp  rr  rt  re  r\  ru  rv  z
false-flagr   r   rM  r   rZ   r   r-   rY   r   r   r   r   r   r   $feature/beta-feature-localz$feature/false-flagFrO  rN  r[   r   r   r   r	   r   r.   r   r\   r   r   r*   r   r   rE   r   r   r   r   r   rn   )
r+   r   multivariate_flag
basic_flag
false_flagr   r2   r   r   r   s
             r    7test_basic_capture_with_locally_evaluated_feature_flagszBTestClient.test_basic_capture_with_locally_evaluated_feature_flagsv  s   $2^EU4V#W  "'"% (/(0);,3	' /2
 -b" #2$324 $4$424 $3$324!!( &4&)6]I(/
b "  (0,3*/(0	' /2
 $SM

. " ')./ $SM

  ZZ34	!!2	F %6z:#NF ~~#SW & H   *T[[) ((*",,Q/8JQ-CS\+>?OOJs;'7=>  1S/?S.v68HIS.~>HL!"?@/ S./DEuML!"9:=Q<R +#l2CCCC[33Q7G 5L ZZ34	!!2	F $&F ~~&9}~UH  *T[[) ((*",,Q/8JQ-C*#l2CCCC0L8IIII(L0AAAA*#l2CCCC) 54M 54L 54s   :F!K58B3L5K>L
c                    dddd}t        d|d         |_        t        t        d      }| j	                  d	d
      5 }|j                          | j                  |j                  g        | j                  |j                  i        | j                  |j                  i        | j                  |j                  i        | j                  d|j                  d          d d d        y # 1 sw Y   y xY w)Nquota_limitedz1You have exceeded your feature flag request quotapayment_required)r9  detailcodei  r  r   rS   r   r   r   z#PostHog feature flags quota limitedr   r   r   r	   r   r   _load_feature_flagsrE   r   feature_flags_by_keygroup_type_mappingcohortsr   r   )r+   r   mock_responser2   r   s        r    %test_load_feature_flags_quota_limitedz0TestClient.test_load_feature_flags_quota_limited  s     $I&

 !)mH.E F	)FC__Yi_8D&&(V1126V88"=V66;V^^R0MM?QP 988s   B C((C1c                 R   t        dd      |_        t        t        d      }| j	                  dd      5 }|j                          | j                  |j                  g        | j                  |j                  i        | j                  |j                  i        | j                  |j                  i        | j                  d|j                  d          | j                  d	|j                  d          | j                  d
|j                  d          d d d        y # 1 sw Y   y xY w)Nr   r   r   r  r   r   r   r   r  rS   r  )r+   r   r2   r   s       r    $test_load_feature_flags_unauthorizedz/TestClient.test_load_feature_flags_unauthorized  s     (n =	)FC__Yg_6$&&(V1126V88"=V66;V^^R0MM.$++a.9MM+T[[^<MM,dkk!n= 766s   CDD&c                 $   dddii|_         dddddd	d
dddgddddigdddddddddddddgidddiddd}ddd dd!dd"gd
d#gddgd$d%id&d'}t        j                  d(      5 }t        t        | j
                  t        d)      }||g|_        |j                  d*d+d,d-id.      }| j                  |       | j                  | j                         |j                          |j                  d   d/   }|d0   }| j                  |d1   d*       | j                  t        |d2   t                      | j                  |j#                  d3             | j                  |d+   d+       | j                  |d4   d5   d6       | j                  |d4   d7   t$               | j                  |d4   d,   d-       | j                  |d4   d8   dg       d9|d4   vsJ d:|d4   vsJ | j                  |j&                  d0       d d d        y # 1 sw Y   y xY w);Nr   rK  rL  r   rT  rU  TrV  rW  rX  rY  rZ  r[  r^  r_  r`  ra  rb  rc  rd  rf  rg  rh  ri  rj  rk  rl  rm  rn  ro  rs  rw  rx  ry  rz  r{  r|  r}  r~  r   rM  r   rZ   r  zmy-custom-variant)rZ   r   r   r-   r   rY   r   r   r   r   r   r   rO  rN  z$feature/person-flagr  )	r+   r   r  r  r   r2   r   r   r   s	            r    +test_dont_override_capture_with_local_flagsz6TestClient.test_dont_override_capture_with_local_flags/  s   $2^EU4V#W  "'"% (/(0);,3	' /2
 -b" #2$324 $4$424 $3$324!!( &4&)6]I(/
b "  (0,3*/(0	' /2
 $SM

. ZZ34	!!2	F %6z#BF ~~#)9;NO#'	 & H   *T[[) ((*",,Q/8JQ-CS\+>?OOJs;'7=>  1S/?S.v68HIS.~>HL!"?@BU L!"9:=Q<R +#l2CCCC)\1BBBB[33Q7M 544s   )FHHc                    dddddi|_         t        j                  d      5 }t        t        | j
                  t        d      }|j                  dd	d
      }| j                  |       | j                  | j                         |j                          |j                  d   d   }|d   }| j                  |d   d       | j                  t        |d   t                     | j                  |j!                  d             | j                  |d	   d	       | j                  |d   d          | j                  |d   d   d       | j                  |d   d   t"               | j                  |d   d   d       | j                  |d   d   d       | j                  |d   d   ddg       | j                  |j$                  d       |j'                  ddddd	i i i dd 
       d d d        y # 1 sw Y   y xY w)Nr   rL  TFrK  alpha-featureoff-featurer   rM  r   rZ   r   r   r-   r   rY   r   r   r   $geoip_disabler   r   r   rN  $feature/alpha-featurerO  rK  r  
random_keyrd      timeoutmax_retriesrZ   rp  person_propertiesgroup_propertiesgeoip_disable	device_idr   r   r   r	   r   r.   r\   r   r   r*   r   r   rE   r   r   r   r   r   rn   assert_called_withrQ  s          r    9test_basic_capture_with_feature_flags_returns_active_onlyzDTestClient.test_basic_capture_with_feature_flags_returns_active_only  s     0!%$$
  ZZ34	!!2	F ~~#SW & H   *T[[) ((*",,Q/8JQ-CS\+>?OOJs;'7=>  1S/?OOC-.>?@S.v68HIS.~>HL!"9:<L S./GH$OL!"9:1
 [33Q7***)"$!#" + G 544s   GG--G6))defaultr   )rG   r   )two_retriesri   c                    t        j                  d      5 }i i d|_        t        t        |t              }|j                  d       d d d        | j                  j                  j                  d   |       y # 1 sw Y   3xY w)Nr   r   )!feature_flags_request_max_retriesrS   rZ   r  )	r   r   r   r	   r   get_all_flagsrE   r   kwargs)r+   r  expected_max_retriesr   r2   s        r    3test_feature_flags_request_max_retries_is_forwardedz>TestClient.test_feature_flags_request_max_retries_is_forwarded  sy     ZZ./;8:SU'VK$!2F!2F   / 0 	!!((79M	
 0/s   2A::Bc                    dddddi|_         t        j                  d      5 }t        t        d| j
                  t        ddd	      }|j                  d
ddd      }| j                  |       | j                  | j                         |j                          |j                  d   d   }|d   }| j                  |d   d
       | j                  t        |d   t                     | j                  |j!                  d             | j                  d|d   v       | j                  |d   d       | j                  |d   d   d       | j                  |d   d   t"               | j                  |d   d   d       | j                  |d   d   d       | j                  |d   d   ddg       | j                  |j$                  d       |j'                  dddddi i i dd 
       d d d        y # 1 sw Y   y xY w) Nr   rL  TFr  r   r5     )rR   r1   rS   disable_geoip%feature_flags_request_timeout_secondsrx   r   rZ   )rZ   r   r  r   r-   r   rY   r   r   r  r   r   r   r   rN  r  rO  rK  r  r  rd   r  r  rQ  s          r    Itest_basic_capture_with_feature_flags_and_disable_geoip_returns_correctlyzTTestClient.test_basic_capture_with_feature_flags_and_disable_geoip_returns_correctly  s   
  0!%$$
  ZZ34	!.!2"68F ~~#)#'#	 & H   *T[[) ((*",,Q/8JQ-CS\+>?OOJs;'7=>  1OO,C4EEFS/?S.v68HIS.~>HL!"9:<L S./GH$OL!"9:1
 [33Q7***)"$!## + S 544s   GG00G9c                 N   dddii|_         t        j                  d      5 }t        t        | j
                  t        d      }|j                  ddd	
      }| j                  |       | j                  | j                         |j                          |j                  d   d   }|d   }| j                  |d   d       | j                  t        |d   t                     | j                  |j!                  d             | j                  |d   d       | j                  |d   d   d       | j                  |d   d   t"               | j                  d|d   v       | j                  d|d   v       | j                  |j$                  d       d d d        y # 1 sw Y   y xY w)Nr   rK  rL  r   TrM  r   rZ   Fr   r   r-   r   rY   r   r   r   r   r   r   rN  rO  rP  rQ  s          r    Ctest_basic_capture_with_feature_flags_switched_off_doesnt_send_themzNTestClient.test_basic_capture_with_feature_flags_switched_off_doesnt_send_them6  s    %3^EU4V#W ZZ34	!!2	F ~~#SX & H   *T[[) ((*",,Q/8JQ-CS\+>?OOJs;'7=>  1S/?S.v68HIS.~>HOO33|;LLMOO33|;LLM[33Q77 544s   E1FF$c                    dddii|_         dddddd	digd
ddddddddgidd}dddddd	digid}t        j                  d      5 }t        t        | j
                  t        d      }||g|_        |j                  ddd      }| j                  |       | j                  | j                         |j                          |j                  d   d   }|d   }| j                  |d    d       | j                  |d   d       | j                  d!|d"          | j                  d#|d"          | j                  d$|d"          | j                  |j                  d       d%d%d%       y%# 1 sw Y   y%xY w)&zTTest that send_feature_flags=False with local evaluation enabled does NOT send flagsr   rK  remote-variantr   rT  rU  TrV  r_  ra  rb  rc  r`  rd  rf  rg  rp  rq  rs  ri   Simple Flagsimple-flagrp  r~  r   rM  r   rZ   Fr   r-   r   rY   r  r   $feature/simple-flagrO  N)r   r   r   r	   r   r.   r   r\   r   r   r*   r   r   rE   rm   rn   )	r+   r   r  simple_flagr   r2   r   r   r   s	            r    Qtest_capture_with_send_feature_flags_false_and_local_evaluation_doesnt_send_flagsz\TestClient.test_capture_with_send_feature_flags_false_and_local_evaluation_doesnt_send_flagsY  s   
 %3^EU4V#W  "'"% -c #2$324 $4$424!!
: ! ,c
 ZZ34	!!2	F %6{#CF ~~#)#( & H
   *T[[) ((*",,Q/8JQ-CS\+>?S/? :C<MN3S5FG4c,6GH [33Q7? 544s   	DE))E2c                 r   dddii|_         dddddd	digd
ddddddddgidd}dddddd	digid}t        j                  d      5 }t        t        | j
                  t        d      }||g|_        |j                  ddd      }| j                  |       | j                  | j                         |j                          |j                  d   d   }|d   }| j                  |d   d       | j                  |d   d       | j                  d |d!          | j                  d"|d!          | j                  |d!   d"   d       |d!   d#   }	| j                  d|	       | j                  d|	       | j                  d$|d!          | j                  |j                   d       d%d%d%       y%# 1 sw Y   y%xY w)&zaTest that send_feature_flags=True with local evaluation enabled uses local flags without API callr   remote-flagr  r   rT  rU  TrV  r_  ra  rb  rc  r`  rd  rf  rg  r  rs  ri   r  r  rp  r~  r   rM  r   rZ   r   r-   r   rY   r  r   r  rO  $feature/remote-flagN)r   r   r   r	   r   r.   r   r\   r   r   r*   r   r   rE   r   rm   rn   )
r+   r   r  r  r   r2   r   r   r   active_flagss
             r    Otest_capture_with_send_feature_flags_true_and_local_evaluation_uses_local_flagszZTestClient.test_capture_with_send_feature_flags_true_and_local_evaluation_uses_local_flags  s   
 %3]DT4U#V  "'"% -c #2$324 $4$424!!
: ! ,c
 ZZ34	!!2	F %6{#CF ~~#)#' & H
   *T[[) ((*",,Q/8JQ-CS\+>?S/? MM7\9JKMM0#l2CDS./EFM |,-DELMM.=MM-6 3S5FG [33Q7O 544s   	EF--F6c                 &   t        j                  d      5 }t        t        | j                  t        d      }ddddddd	gd
dgidg|_        dddid}|j                  dd|      }| j                  |       | j                  | j                         |j                          |j                          |j                  d   d   }|d   }| j                  |d   d   d       | j                  |d   d   dg       ddd       y# 1 sw Y   yxY w)zWTest that SendFeatureFlagsOptions with only_evaluate_locally=True uses local evaluationr   TrM  r   z
local-flagrp  rx  US)r\  r=  rV  r^  )rt  r\  ru  rv  only_evaluate_locallyr  
test eventrZ   r   r-   r   r   z$feature/local-flagrO  N)r   r   r	   r   r.   r   r\   r   r   r*   rI   r   r   rE   )r+   r   r   r2   send_optionsr   r   r   s           r    Gtest_capture_with_send_feature_flags_options_only_evaluate_locally_truezRTestClient.test_capture_with_send_feature_flags_options_only_evaluate_locally_true  s8   
 ZZ34	!!2	F '" 7?$/O.P69# 	$F " *.&.%5L
 ~~-L & H   *T[[) ))+ ((*",,Q/8JQ-CS./DEtLS./FG,X] 544s   C(DDc                 j   dddii|_         t        j                  d      5 }t        t        | j
                  t        d      }ddd	id
ddiid}|j                  ddd
di|      }| j                  |       | j                  | j                         |j                          |j                  d   }| j                  |d   dd	i       | j                  |d   d
ddii       |j                          |j                  d   d   }|d   }| j                  |d   d   d       ddd       y# 1 sw Y   yxY w)z[Test that SendFeatureFlagsOptions with only_evaluate_locally=False forces remote evaluationr   r  zremote-valuer   TrM  Fplanpremiumcompanyr9  
enterpriser  r  r  r  rZ   acme)rZ   rp  r   r   r  r  r-   r   r   r  Nr   r   r   r	   r   r.   r\   r   r   r*   r   r   rE   	r+   r   r   r2   r  r   r   r   r   s	            r    Htest_capture_with_send_feature_flags_options_only_evaluate_locally_falsezSTestClient.test_capture_with_send_feature_flags_options_only_evaluate_locally_false:  sX   
 %3]N4S#T ZZ34	!!2	F */&,i%8%.0F$GL ~~)!6*#/	 & H   *T[[) **,#--a0IY':;fi=PQ,-	FL;Q/R
 ((*",,Q/8JQ-CS./EFWK 544s   C?D))D2c                 &   dddii|_         t        j                  d      5 }t        t        | j
                  t        d      }ddd	ii}|j                  d
d|      }| j                  |       | j                  | j                         |j                          |j                  d   }| j                  |d   dd	i       |j                          |j                  d   d   }|d   }| j                  |d   d   d       ddd       y# 1 sw Y   yxY w)z]Test that SendFeatureFlagsOptions without only_evaluate_locally defaults to remote evaluationr   zdefault-flagzdefault-valuer   TrM  r  subscriptionpror  rZ   r   r   r-   r   r   z$feature/default-flagNr  r  s	            r    =test_capture_with_send_feature_flags_options_default_behaviorzHTestClient.test_capture_with_send_feature_flags_options_default_behaviorh  s   
 %3^_4U#V ZZ34	!!2	F $ne%<L ~~-L & H   *T[[) **,#--a0IY':;ne=TU ((*",,Q/8JQ-CL!"9:O; 544s   CDDc                    dddii|_         t        j                  d      5 }t        t        | j
                  t        d      }dddid	}	 t        d
      # t        $ r}|j                  |d|      }Y d}~nd}~ww xY w| j                  |       | j                  | j                         |j                          |j                  d   }| j                  |d   ddi       |j                          |j                  d   d   }|d   }	| j                  |	d   d       | j                  |	d   d   d       ddd       y# 1 sw Y   yxY w)zATest that capture_exception also supports SendFeatureFlagsOptionsr   zexception-flagTr   rM  F	user_typeadminr  zTest exceptionrZ   r   Nr   r  r-   r   rY   r   r   z$feature/exception-flag)r   r   r   r	   r   r.   
ValueErrorr#  r   r   r*   r   r   rE   )
r+   r   r   r2   r  r,   r   r   r   r   s
             r    6test_capture_exception_with_send_feature_flags_optionszATestClient.test_capture_exception_with_send_feature_flags_options  sT    %35Et4L#M ZZ34	!!2	F */&17%;L
 !122 !33=\ 4 
   *T[[) **,#--a0IY':;k7=ST ((*",,Q/8JQ-CS\<8S./HI4PE 544s0   )D?A	A=A83D?8A==B9D??Ec                    t        j                  d      5 }t        t        | j                  d      }|j                  dd      }| j                  |       | j                  | j                         |j                          |j                  d   d   }|d	   }| j                  |d
   d       d d d        y # 1 sw Y   y xY w)Nr   Tr   r   l   IqKr[   r   r-   r   rZ   157963456373623802)r   r   r	   r   r.   r\   r   r   r*   r   r   rE   r   s         r    test_stringifies_distinct_idz'TestClient.test_stringifies_distinct_id  s     ZZ34	-QUVF~~#1C & H   *T[[) ((*",,Q/8JQ-CS/1EF 544s   BB77C c                    t        j                  d      5 }t        t        | j                  d      }|j                  ddddit        dd	d
      d      }| j                  |d       |j                          |j                  d   d   }|d   }| j                  |d   d       | j                  |d   d   d       | j                  |d   d       | j                  |d   d   d       | j                  |d   d   t               | j                  |d   d       | j                  |d   d       | j                  d|d   v       d d d        y # 1 sw Y   y xY w)Nr   Tr   r   rZ   propertyr=    	   r  $00000000-0000-4000-8000-000000000001rZ   r   r   r   r   r-   r   r   2014-09-03T00:00:00+00:00r   rY   r   r   r   r   $groups)r   r   r	   r   r.   r\   r   rE   r   r   r   r   r   s         r    test_advanced_capturez TestClient.test_advanced_capture  sZ   ZZ34	-QUVF~~#)&0"4A.; & H X'MN ((*",,Q/8JQ-CS-/JKS.z:GDS\+>?S.v68HIS.~>HS[*PQS/?OOIS->>?1 544s   D/EEc                 d   t        j                  d      5 }t        t        | j                  d      }|j                  ddddd	      }| j                  |       |j                          |j                  d
   d   }|d   }| j                  |d   d   ddd       d d d        y # 1 sw Y   y xY w)Nr   Tr   
test_eventrZ   id:5app.posthog.comr  instance)rZ   rp  r   r-   r   r   r  
r   r   r	   r   r.   r\   r   r   r   rE   r   s         r    test_groups_capturezTestClient.test_groups_capture  s    ZZ34	-QUVF~~)#)7HI & H   * ((*",,Q/8JQ-CL!),"0AB 544s   BB&&B/c                 >   t        j                  d      5 }t        t        | j                  d      }|j                  dddi      }| j                  |       | j                  | j                         |j                          |j                  d   d	   }|d
   }| j                  |d   d   d       | j                  t        |d   t                     | j                  |j                  d             | j                  |d   d       d d d        y # 1 sw Y   y xY w)Nr   Tr   rZ   traitr=  rZ   r   r   r-   r   $setr   r   )r   r   r	   r   r.   setr   r   r*   r   r   rE   r   r   r   r   r   s         r    test_basic_setzTestClient.test_basic_set  s    ZZ34	-QUVFzz)w6H " H   *T[[) ((*",,Q/8JQ-CS[17;OOJs;'7=>  1S/?! 544   C4DDc           
         t        j                  d      5 }t        t        | j                  d      }|j                  dddit        ddd	      d
      }| j                  |d
       |j                          |j                  d   d   }|d   }| j                  |d   d       | j                  |d   d   d       | j                  |d   d   d       | j                  |d   d   t               | j                  t        |d   t                     | j                  |d   d
       | j                  |d   d       d d d        y # 1 sw Y   y xY w)Nr   Tr   rZ   r  r=  r  r  r  r  r  r   r-   r   r   r  r  r   r   r   r   r   )r   r   r	   r   r.   r  r   rE   r   r   r   r   r   r   r   s         r    test_advanced_setzTestClient.test_advanced_set  sC   ZZ34	-QUVFzz)#W-"4A.;	 " H X'MN ((*",,Q/8JQ-CS-/JKS[17;S.v68HIS.~>HOOJs;'7=>S[*PQS/?- 544   D%EEc                 >   t        j                  d      5 }t        t        | j                  d      }|j                  dddi      }| j                  |       | j                  | j                         |j                          |j                  d   d	   }|d
   }| j                  |d   d   d       | j                  t        |d   t                     | j                  |j                  d             | j                  |d   d       d d d        y # 1 sw Y   y xY w)Nr   Tr   rZ   r  r=  r  r   r-   r   	$set_oncer   r   )r   r   r	   r   r.   set_oncer   r   r*   r   r   rE   r   r   r   r   r   s         r    test_basic_set_oncezTestClient.test_basic_set_once'  s    ZZ34	-QUVF)w6H ' H   *T[[) ((*",,Q/8JQ-CS-g6@OOJs;'7=>  1S/?! 544r  c           
         t        j                  d      5 }t        t        | j                  d      }|j                  dddit        ddd	      d
      }| j                  |d
       |j                          |j                  d   d   }|d   }| j                  |d   d       | j                  |d   d   d       | j                  |d   d   d       | j                  |d   d   t               | j                  t        |d   t                     | j                  |d   d
       | j                  |d   d       d d d        y # 1 sw Y   y xY w)Nr   Tr   rZ   r  r=  r  r  r  r  r  r   r-   r   r   r  r  r   r   r   r   r   )r   r   r	   r   r.   r  r   rE   r   r   r   r   r   r   r   s         r    test_advanced_set_oncez!TestClient.test_advanced_set_once:  sD   ZZ34	-QUVF)#W-"4A.;	 ' H X'MN ((*",,Q/8JQ-CS-/JKS-g6@S.v68HIS.~>HOOJs;'7=>S[*PQS/?- 544r  c                    t        j                  d      5 }t        t        | j                  d      }|j                  dd      }| j                  |       |j                          |j                  d   d   }|d   }| j                  |d	   d
       | j                  |d   ddi dt        ddd       | j                  t        |d   t                     | j                  |j                  d             d d d        y # 1 sw Y   y xY w)Nr   Tr   organizationr  r   r-   r   rY   $groupidentifyr   r   z$group_type
$group_keyz
$group_setr   r   r  r   r   r   r   r   r	   r   r.   group_identifyr   r   r   rE   r   r   r   r   r   r   s         r    test_basic_group_identifyz$TestClient.test_basic_group_identifyS  s    ZZ34	-QUVF,,^VDH  * ((*",,Q/8JQ-CS\+;<L!#1"("$,$+&*"& OOJs;'7=>  13 544   CC>>Dc                 B   t        j                  d      5 }t        t        | j                  d      }|j                  ddd      }| j                  |       |j                          |j                  d   d	   }|d
   }| j                  |d   d       | j                  |d   d       | j                  |d   ddi dt        ddd       | j                  t        |d   t                     | j                  |j                  d             d d d        y # 1 sw Y   y xY w)Nr   Tr   r  r  rZ   r[   r   r-   r   rY   r  r   r   r  r   r   r  r   s         r    *test_basic_group_identify_with_distinct_idz5TestClient.test_basic_group_identify_with_distinct_ido  s   ZZ34	-QUVF,,M - H   * ((*",,Q/8JQ-CS\+;<S/?L!#1"("$,$+&*"& OOJs;'7=>  17 544s   C6DDc                    t        j                  d      5 }t        t        | j                  d      }|j                  ddddit        dd	d
      d      }| j                  |d       |j                          |j                  d   d   }|d   }| j                  |d   d       | j                  |d   ddddidt        ddd       | j                  |d   d       d d d        y # 1 sw Y   y xY w)Nr   Tr   r  r  r  r=  r  r  r  r  )r   r   r   r-   r   rY   r  r   r   r  r   r  r   r   r	   r   r.   r  r   rE   r   r   r   r   s         r    test_advanced_group_identifyz'TestClient.test_advanced_group_identify  s   ZZ34	-QUVF,,'""4A.; - H X'MN ((*",,Q/8JQ-CS\+;<L!#1"(#*G"4,$+&*"& S-/JK= 544s   CC%%C.c                    t        j                  d      5 }t        t        | j                  d      }|j                  ddddit        dd	d
      dd      }| j                  |d       |j                          |j                  d   d   }|d   }| j                  |d   d       | j                  |d   d       | j                  |d   ddddidt        ddd       | j                  |d   d       d d d        y # 1 sw Y   y xY w)Nr   Tr   r  r  r  r=  r  r  r  r  rZ   )r   r   rZ   r   r-   r   rY   r  r   r   r  r   r  r  r   s         r    -test_advanced_group_identify_with_distinct_idz8TestClient.test_advanced_group_identify_with_distinct_id  s   ZZ34	-QUVF,,'""4A.;) - H X'MN ((*",,Q/8JQ-CS\+;<S/?L!#1"(#*G"4,$+&*"& S-/JKC 544s   CC;;Dc                    t        j                  d      5 }t        t        | j                  d      }|j                  dd      }| j                  |       | j                  | j                         |j                          |j                  d   d   }|d   }| j                  |d	   d   d       | j                  |d	   d
   d       d d d        y # 1 sw Y   y xY w)Nr   Tr   
previousIdrZ   r   r-   r   r   alias)r   r   r	   r   r.   r  r   r   r*   r   r   rE   r   s         r    test_basic_aliaszTestClient.test_basic_alias  s    ZZ34	-QUVF||L-@H  *T[[) ((*",,Q/8JQ-CS.}=|LS.w7G 544s   B2CCbasic_session_idztest-session-123 session_id_with_other_propertiesztest-session-456custom_valuezhttps://example.com)custom_prop$process_person_profile$current_urlsession_id_uuid_formatsession_id_numeric_string
1234567890session_id_empty_stringr=   session_id_with_special_charszsession-123_test.idc                    t        j                  d      5 }t        t        | j                  d      }d|i|}|j                  dd|      }| j                  |       | j                  | j                         |j                          |j                  d   d	   }	|	d
   }
| j                  |
d   d       | j                  |
d   d       | j                  |
d   d   |       | j                  |
d   d   d       | j                  |
d   d   t               |j                         D ]  \  }}| j                  |
d   |   |        	 d d d        y # 1 sw Y   y xY w)Nr   Tr   $session_idr   rZ   r  r   r-   r   rY   r   r   r   r   )r   r   r	   r   r.   r\   r   r   r*   r   r   rE   r   items)r+   	test_name
session_idadditional_propertiesexpected_propertiesr   r2   r   r   r   r   r\  r=  s                r    'test_capture_with_session_id_variationsz2TestClient.test_capture_with_session_id_variations  sW   6 ZZ34	-QUVF'M7LMJ~~#: & H   *T[[) ((*",,Q/8JQ-CS\+>?S/?S.}=zJS.v68HIS.~>H 2779
U  \!23!7? :/ 544s   D0EEc           	         t        j                  d      5 }t        t        | j                  d      }d}|j                  ddd|idd	d
      }| j                  |       |j                          |j                  d   d   }|d   }| j                  |d   d   |       | j                  |d   d   dd	d
       d d d        y # 1 sw Y   y xY w)Nr   Tr   zgroup-session-101r  rZ   r"  r  r  r  )rZ   r   rp  r   r-   r   r   r  r  r+   r   r2   r%  r   r   r   s          r    %test_session_id_preserved_with_groupsz0TestClient.test_session_id_preserved_with_groups  s    ZZ34	-QUVF,J~~)):6#)7HI	 & H   * ((*",,Q/8JQ-CS.}=zJL!),"0AB' 544s   B$CCc                    t        j                  d      5 }t        t        | j                  d      }d}|j                  dd|dd	      }| j                  |       |j                          |j                  d
   d   }|d   }| j                  |d   d   |       | j                  |d   d   d       d d d        y # 1 sw Y   y xY w)Nr   Tr   zanonymous-session-202anonymous_eventrZ   F)r"  r  r  r   r-   r   r   r"  r  r  r*  s          r    $test_session_id_with_anonymous_eventz/TestClient.test_session_id_with_anonymous_event.  s    ZZ34	-QUVF0J~~!)#-/4 & H   * ((*",,Q/8JQ-CS.}=zJS./HI5Q+ 544s   BB==Cscreen_eventz$screenzspecial-session-505z$screen_name
HomeScreensurvey_eventzsurvey sentzsurvey-session-606
survey_123q1z#How likely are you to recommend us?)rt  question)
$survey_idz$survey_questionsr5  complex_properties_eventcomplex_eventzmixed-session-707zhttps://example.com/page*   )r  r  custom_propertynumeric_propertyboolean_propertycsp_violationz$csp_violationzcsp-session-789z1.0zMozilla/5.0 Test Agentzhttps://malicious.com/script.jsz
script-src)z$csp_versionr  r  z$raw_user_agentz$csp_document_urlz$csp_blocked_urlz$csp_violated_directivec                 V   t        j                  d      5 }t        t        | j                  d      }d|i|}|j                  |d|      }	| j                  |	       |j                          |j                  d   d   }
|
d	   }| j                  |d
   |       | j                  |d   d   |       |j                         D ]  \  }}| j                  |d   |   |        | j                  |d   d   d       | j                  |d   d   t               d d d        y # 1 sw Y   y xY w)Nr   Tr   r"  rZ   r  r   r-   r   rY   r   r   r   r   )r   r   r	   r   r.   r\   r   r   r   rE   r#  r   )r+   r$  
event_namer%  r&  expected_additional_propertiesr   r2   r   r   r   r   r\  r=  s                 r    *test_session_id_with_different_event_typesz5TestClient.test_session_id_with_different_event_typesF  s3   T ZZ34	-QUVF'M7LMJ~~* & H   * ((*",,Q/8JQ-CS\:6S.}=zJ =BBD
U  \!23!7? E S.v68HIS.~>H1 544s   D DD($super_properties_override_session_idzsuper-sessionr   )r"  r  zevent-session-808r  no_super_properties_conflict)r  versionzevent-session-909empty_super_propertieszevent-session-111(super_properties_with_other_dollar_propszhttps://super.com)r  r  zevent-session-222c                    t        j                  d      5 }t        t        |d      }|j	                  ddd|i      }| j                  |       |j                          |j                  d   d	   }	|	d
   }
| j                  |
d   d   |       |j                         D ]  \  }}| j                  |
d   |   |        	 d d d        y # 1 sw Y   y xY w)Nr   Tr  r  rZ   r"  r  r   r-   r   r   )
r   r   r	   r   r\   r   r   r   rE   r#  )r+   r$  r   event_session_idexpected_session_idexpected_super_propsr   r2   r   r   r   r\  r=  s                r    0test_session_id_with_super_properties_variationsz;TestClient.test_session_id_with_super_properties_variations  s    R ZZ34	!4DPTF ~~))+;< & H   * ((*",,Q/8JQ-CS.}=?RS 388:
U  \!23!7? ;+ 544s   B)C		Cc                     | j                   }t        d      D ]  }|j                  ddddi        |j                          | j	                  |j
                  j                                y )N  rY   rZ   r  r=  r  )r2   ranger\   r   r   r   r   )r+   r2   is      r    
test_flushzTestClient.test_flush  sZ    tANN]?Q    	**,-r"   c                 :   | j                   }t        d      D ]  }|j                  ddddi        |j                          | j	                  |j
                  j                                |j                  D ]!  }| j                  |j                                # y )NrL  r  rZ   r  r=  r  )
r2   rM  r\   rl   r   r   r   	consumersr   is_alive)r+   r2   rN  consumers       r    test_shutdownzTestClient.test_shutdown  s    tANN-WgDV    	 	**,-((HX..01 )r"   c                     t        t        dd      }dh|j                  d<   |j                          | j	                  t        |j                        d       y )NFr   r   )flagTr	  user)r	   r   #distinct_ids_feature_flags_reportedrl   rE   lenrV   s     r    5test_shutdown_clears_feature_flag_called_dedupe_cachez@TestClient.test_shutdown_clears_feature_flag_called_dedupe_cache  sI    )a@>P=Q226:VGGH!Lr"   c                     t        t        dd      }t        j                  j	                  |d      5 }|j                          d d d        j                  d        y # 1 sw Y   xY w)NFr   r   r   r   )r	   r   r   r   rC   rl   rH   )r+   r2   
mock_flushs      r    %test_shutdown_flushes_without_timeoutz0TestClient.test_shutdown_flushes_without_timeout  sR    )a@ZZvw/:OO 0 	**4*@ 0/s   AA(c                 j   t        j                  d      5 }t        t        d      }|j	                  dd      }| j                  |j                         | j                  |j                  j                                | j                  |       |j                          d d d        y # 1 sw Y   y xY w)Nr   Trx   r  rZ   r[   )r   r   r	   r   r\   r   rQ  r   r   r   r   r   )r+   r   r2   r   s       r    test_synchronouszTestClient.test_synchronous  s    ZZ34	->F~~l~NHV--.OOFLL..01  * ((* 544s   B
B))B2c                     t        t        d      }|j                          t        d      D ]  }|j	                  dd        |j	                  dd      }| j                  |       y )Nr   )max_queue_size
   r  rZ   r[   )r	   r   joinrM  r\   rU   )r+   r2   rN  r   s       r    test_overflowzTestClient.test_overflow#  sX    )!<rANN<]NC  >>,M>J(#r"   c                     t        d       y )Nunicode_keyr   r3   s    r    test_unicodezTestClient.test_unicode/  s
    }r"   c                     | j                   j                  dd       | j                   j                          | j                  | j                         y )Nzpython eventi  r[   )r2   r\   r   r   r*   r3   s    r    test_numeric_distinct_idz#TestClient.test_numeric_distinct_id2  s;    N=%r"   c                     t        dd       y )Nbad_keyTr  r   r3   s    r    
test_debugzTestClient.test_debug7  s    y%r"   c                     t        t        | j                  d      }t        d      D ]  }|j	                  ddddi        |j                          | j                  | j                         y )	NT)r1   gziprc  rY   rZ   r  r=  r  )r	   r   failrM  r\   r   r   r*   )r+   r2   rJ   s      r    	test_gzipzTestClient.test_gzip:  s]    )DIIDIrANN]?Q    	%r"   c                 P    t        t         j                  dd      } fd}t        j                  d|      5 }t        d      D ]  }|j                  dd	d
di        t        j                  d        j                  |j                  d       d d d        y # 1 sw Y   y xY w)Nrc  r  )r1   flush_atflush_intervalc                  B    j                  t        |d         d       y )Nr-   rc  )rE   rY  )argsr  r+   s     r    mock_post_fnz;TestClient.test_user_defined_flush_at.<locals>.mock_post_fnH  s    S126r"   r   r      rY   rZ   r  r=  r  r   ri   )r	   r   rq  r   r   rM  r\   r   sleeprE   rn   )r+   r2   rx  r   rJ   s   `    r    test_user_defined_flush_atz%TestClient.test_user_defined_flush_atC  s    		Bq
	7
 ZZ)|
2YGWCU    JJqMY1115
 
 
s   ABB%c                     t        t        d      }|j                  D ]  }| j                  |j                  d         y )Nrc  r  r	   r   rQ  rE   r  r+   r2   rS  s      r    test_user_defined_timeoutz$TestClient.test_user_defined_timeoutW  s5    )26((HX--r2 )r"   c                 |    t        t              }|j                  D ]  }| j                  |j                  d         y )N   r~  r  s      r    test_default_timeout_15z"TestClient.test_default_timeout_15\  s3    )*((HX--r2 )r"   c                     t        t        | j                  d      }|j                  dd      }|j	                          | j                  |       | j                  | j                         y )NTr1   rG   r   rZ   r[   )r	   r   r.   r\   r   rU   r   r*   )r+   r2   r   s      r    test_disabledzTestClient.test_disableda  sQ    )DMMDQ>>"5=>Q(#%r"   c                    t        t        | j                  d      }|j                  dd      }| j	                  |       |j                          |j                  dd      }| j	                  |       |j                          |j                  d      }| j	                  |       |j                          |j                  dd      }| j	                  |       |j                          |j                  d      }| j                  |d d d       |j                          | j                  |j                  j                                y )NTr  rK  12345r\  r   )r	   r   r.   get_feature_flagrU   rI   feature_enabledr  get_feature_flag_payloadget_all_flags_and_payloadsrE   r   r   r   )r+   r   r2   responses       r     test_disabled_with_feature_flagsz+TestClient.test_disabled_with_feature_flagsh  s   )DMMDQ**>7C(#%%')).'B(#%%'''0(#%%'225'B(#%%'44W=DQU#VW%%' 	**,-r"   c                    t        j                  d      5 }t        t        | j                  dd      }|j                  dd      }| j                  |       | j                  | j                         |j                          |j                  d   d	   }|d
   }| j                  |d   d       d|_        |j                  dd      }| j                  |       | j                  | j                         d d d        y # 1 sw Y   y xY w)Nr   FT)r1   rG   rx   r   rZ   r[   r   r-   r   rY   )r   r   r	   r   r.   r\   r   r   r*   r   r   rE   rG   rU   r   s         r    test_enabled_to_disabledz#TestClient.test_enabled_to_disabled  s    ZZ34	!	F ~~&9}~UH  *T[[) ((*",,Q/8JQ-CS\+>?"FO~~&9}~UHh'T[[)+ 544r
  c                 X   t        j                  d      5 }t        t        | j                  dd      }|j                  dd      }| j                  |       |j                          |j                  d   d   }|d	   }| j                  |d
   d   d       d d d        y # 1 sw Y   y xY w)Nr   Tr1   r  rx   r   rZ   r[   r   r-   r   r   r  r  )r+   r   r2   r   r   capture_msgs         r    $test_disable_geoip_default_on_eventsz/TestClient.test_disable_geoip_default_on_events  s    ZZ34	!"	F ~~&9}~UH  * ((*",,Q/8J$Q-K[67GH$O 544s   BB  B)c                 0   t        j                  d      5 }t        t        | j                  dd      }|j                  ddddd	      }| j                  |       |j                  d
dddid	      }| j                  |       | j                  |j                  d       |j                  d   d   d   }|d   }| j                  |d   d   d       |j                  d   d   d   }|d   }| j                  d|d   vd       d d d        y # 1 sw Y   y xY w)Nr   FTr  rZ   bd)rl  c)rZ   r   r  rY   r  r=  ri   r   r   r-   r   r  )r   r   r	   r   r.   r  r   r\   rE   rn   call_args_list)r+   r   r2   r   	set_batchr  
page_batchidentify_msgs           r    %test_disable_geoip_override_on_eventsz0TestClient.test_disable_geoip_override_on_events  s8   ZZ34	!#	F zz)!$3/" " H
   *~~)#W-#	 & H   * Y1115 "003A6w?I#A,K[67GH$O #11!4Q7@J%a=L-\,5OOQUVC 544s   C-DDc                 V   t        j                  d      5 }t        t        | j                  dd      }|j                  ddd      }| j                  |       |j                          |j                  d   d	   }|d
   }| j                  d|d   v       d d d        y # 1 sw Y   y xY w)Nr   Tr  r   rZ   F)rZ   r  r   r-   r   r  r   )
r   r   r	   r   r.   r\   r   r   r   r   r   s         r    2test_disable_geoip_method_overrides_init_on_eventsz=TestClient.test_disable_geoip_method_overrides_init_on_events  s    ZZ34	!"	F ~~#e & H   * ((*",,Q/8JQ-COO,C4EEF! 544s   B BB(c                    dddddi|_         t        t        | j                  d      }|j	                  ddd	       |j                  dd
dddi i i dd dg       |j                          |j                  ddd	       |j                  dd
dddi i i dd dg       |j                          |j                  d       |j                  dd
dddi i i dd 
       y )Nr   rL  TFr  )r1   r  r  some_id)r  rd   r  r   	r  r  rZ   rp  r  r  r  r  flag_keys_to_evaluatefeature_enabled_distinct_idall_flags_payloads_idr  )	r   r	   r   r.   r  r  
reset_mockr  r  r   s      r    $test_disable_geoip_default_on_decidez/TestClient.test_disable_geoip_default_on_decide  s/     0!%$$
  )DMMQVWitL&&&! #/. 	' 	
 	 7t 	 	
 	&&&5 #/. 	' 	
 	 ))*AB&&&/  	' 	
r"   zposthog.client.Pollerc                     d }||j                   j                  _        t        t        d      }ddig|_        | j                  |j                  dd             y )Nc                      t        d      )Nzhttp exception)r"  r	  r"   r    raise_effectz9TestClient.test_call_identify_fails.<locals>.raise_effect	  s    ,--r"   r   r  r\  examplerZ   )r   
raiseErrorr   r	   r   r   r   r  )r+   r   patch_pollerr  r2   s        r    test_call_identify_failsz#TestClient.test_call_identify_fails	  sV    	. 9E	))5)FC!&	 23//	=IJr"   c                    dddddi|_         t        t        d| j                  d      }|j	                  dd	d
ddddidddii       |j                  ddddd	d
ddddid
ddddiddd dg       |j                          |j	                  dd	d
ddddidddii       |j                  ddddd	d
ddddiddiddiddd dg       |j                          |j                  d	i d d        |j                  ddddd	i i i dd 
       y )Nr   rL  TFr  zhttp://app2.posthog.com)rR   r1   r  r  r  r  r  r  x1y1r  xy)rp  r  r  r  r   )r  r  r  r  rZ   overridegroup_overrider  )r   r	   r   r.   r  r  r  r  r   s      r    *test_default_properties_get_added_properlyz5TestClient.test_default_properties_get_added_properly'	  s     0!%$$
  *]]	
 	%3DE#Tl'#s4 	  	
 	&&%!%3DE#Tl*0s;)+<=  #/. 	' 	
" 	 %3DE,j9 "2 	  
	
 	&&%!%3DE,j9(*:;)+<=  #/. 	' 	
" 	 ))bD4 	* 	
 	&&%!  	' 	
r"   r  r  r  r  r  Nr  r   c                     dddii|_         t        t        | j                        } t	        ||      |ddi di |i ddd	}|r||d
<    |j
                  	 	 dddd| y)zJTest that device_id is properly passed to the flags request when provided.r   rK  rL  r0   r  ztest-device-123r  T)rZ   rp  r  r  r  r  r  r  r   )r  r  N)r  rd   )r   r	   r   r.   getattrr  )r+   r   method_argsexpected_person_propsexpected_flag_keysr   r2   expected_calls           r    )test_device_id_is_passed_to_flags_requestz4TestClient.test_device_id_is_passed_to_flags_request}	  s    F %3^EU4V#W )DMMBJ8IJ %!6 "!*
 5GM12&&&&	
 		

 	
r"   c                    ddl m}m} dddii|_        t	        t
        | j                        } |       5   |d       |j                  dd	       |j                  dd
ddd	i i i dddg       ddd       |j                           |       5   |d       |j                  dd	d       |j                  dd
ddd	i i i dddg       ddd       y# 1 sw Y   cxY w# 1 sw Y   yxY w)zWTest that device_id from context is used in flags request when not explicitly provided.r   )r   set_context_device_idr   rK  rL  r0   zcontext-device-idr  r  rd   r  r   Tr  Nzexplicit-device-id)r  )
posthog.contextsr   r  r   r	   r   r.   r  r  r  )r+   r   r   r  r2   s        r    4test_device_id_from_context_is_used_in_flags_requestz?TestClient.test_device_id_from_context_is_used_in_flags_request	  s    	H  0$
 
 ]]
 ]!"56##L)<***%"$!#"-'3n +  $ 	 ]!"56##i3G $  ***%"$!#".'3n +  ]' ]& ]s   8C:CCC%c                    dddii|_         t        t        | j                        }|j	                         5  |j                  d       |j                  dd       d d d        |j                  ddd	d
di i i dddg       y # 1 sw Y   'xY w)Nr   rK  rL  r0   zclient-context-device-idr  r  rd   r  r   Tr  )r   r	   r   r.   r   r  r  r  r   s      r    :test_client_set_context_device_id_is_used_in_flags_requestzETestClient.test_client_set_context_device_id_is_used_in_flags_request	  s      0$
 
 ]]

 !(()CD##L)< " 	&&&! 0#/. 	' 	
	 "!s   $BB
))
macOSdarwinr     rc  
MockPython3.8.10zMac OS X10.15.7Nmac_ver)r  r=   r=   )
Windowswin32r  r  r  r  10N	win32_ver)r  r=   r=   r=   )
Linuxlinuxr  r  r  r  z20.04UbuntuNNc                 &   t        j                  d      5 }t        j                  d      5 }||j                  _        ||_        ||_        |	r|
t        ||	      _        |dk(  rTt        j                  d      5 }d|i|j                  _        |xs d|j                  _        ddl	m
}  |       }d	d	d	       nddl	m
}  |       }||||d
}|dk(  r||d<   |k(  sJ 	 d	d	d	       d	d	d	       y	# 1 sw Y   3xY w# 1 sw Y   xY w# 1 sw Y   y	xY w)zATest that we can mock platform and sys for testing system_contextzposthog.utils.platformzposthog.utils.sysr  zposthog.utils.distrorC  r=   r   )system_contextN)r   r   r   r   z
$os_distro)r   r   python_implementationr   version_infoplatformr  infore  posthog.utilsr  )r+   r  sys_platformr  expected_runtimeexpected_versionexpected_osexpected_os_versionexpected_os_distroplatform_methodplatform_returnmock_platformmock_sysmock_distror  contextexpected_contexts                    r    test_mock_system_contextz#TestClient.test_mock_system_context
  s    n ZZ01]/0HCS33@(4%$0! # &5 %"  7*$:;{9BDW8X((58J8Pb((5@"0"2 <; =,.G (8'7&#6	$   7*5G$\2"2222M 1 21 <; 10 21s;   DAC;96C//.C;D/C84C;;D	 DDc                 .   ddddddiddd|_         t        t              }d	}d
di}ddi}|j                  |||      }|t	        dddd t        d            t	        ddd d t        d             t	        ddd d t        d             ddddk(  sJ y )NrL  TFr  rK  z{"some": "data"}ztest-id)r   r   errorsWhileComputingFlags	requestIdtest_distinct_idtest_group_typetest_group_idtest_property
test_value)payload)r\  enabledvariantreasonmetadatar  r  )r   r  r  )r   r	   r   r   r   r   )r+   r   r2   rZ   rp  r  r  s          r    2test_get_decide_returns_normalized_decide_responsez=TestClient.test_get_decide_returns_normalized_decide_responsel
  s     !1!%$
 %34F#G)."	$
  )*(#_5,l;,,[&BST +& ,/ 2! "-'  / $"  +%! / $ ': */"? 
  
  	
  
r"   c                    t        j                  d      5 }t        t        | j                  d      }t               5  t        d       |j                  ddddi	      }| j                  |       |j                          |j                  d
   d   }|d   }| j                  |d   d   d       d d d        d d d        y # 1 sw Y   xY w# 1 sw Y   y xY w)Nr   Tr   context-session-123r  rZ   r  r=  r  r   r-   r   r   r"  )r   r   r	   r   r.   r   r   r\   r   r   r   rE   r   s         r    %test_set_context_session_with_capturez0TestClient.test_set_context_session_with_capture
  s    ZZ34	-QUVF#$9:!>>  - -w7 *  $$X. ,,.&003G<
 m  %m46K!  54 54s#   'CA2C /C C		CC)r   )scopedc                     t        j                  d      5 }t        t         j                  d       fd|dk(  r#j                  d      5          }d d d        n!j                  d      fd       } |       } j                         |j                          |j                  d   d	   }|d
   } j                  |d   d        j                  |d   d   d        j                  |d   d   d        j                  |d   d   d        j                  |d   d   dg        j                  j                         i        d d d        y # 1 sw Y   xY w# 1 sw Y   y xY w)Nr   Tr   c                       j                  dd        j                  d        j                  d       j                   j	                         ddi        j                  dddi      S )	N
client_tag	tag-valuecontext-userr  r  r  r=  )r   )r   identify_contextr   rE   get_tagsr\   )r2   r+   s   r    capture_in_contextzSTestClient.test_client_context_helpers_apply_to_capture.<locals>.capture_in_context
  sk    

<5''7**+@A  !2\;4OP~~  -w7 &  r"   r   freshc                               S r$   r	  )r  s   r    scoped_capturezOTestClient.test_client_context_helpers_apply_to_capture.<locals>.scoped_capture
  s    -//r"   r   r-   r   rZ   r  r   r  r  r  r=  r"  r  $context_tags)r   r   r	   r   r.   r   r  r   r   r   rE   assertCountEqualr   )	r+   context_helperr   r   r  r   r   r  r2   s	   `      @@r    ,test_client_context_helpers_apply_to_capturez7TestClient.test_client_context_helpers_apply_to_capture
  si   ZZ34	-QUVF
 .''d'313H 43 T*0 +0 *+  *((*",,Q/8JQ-CS/@S.|<kJS.}=wGS.}=?TU!!#l"3O"D|nUV__.3I 54  43! 54s$   :E(EC8E(E%	!E((E1c                 f   t        j                  d      5 }t        t        | j                  d      j                  d      fd       }t        j                   |             }| j                  |       |j                          |j                  d   d   }|d   }| j                  |d	   d
       | j                  |d   d   d       | j                  |d   d   d       | j                  |d   d   dg       | j                  j                         i        d d d        y # 1 sw Y   y xY w)Nr   Tr   r  c                     K    j                  dd        j                  d        j                  d       t        j                  d       d {     j                  d      S 7 w)Nasync_scoped_tagasync-scoped-valueasync-scoped-userasync-scoped-session-123r   async_scoped_event)r   r  r   asynciorz  r\   r   s   r    r  z\TestClient.test_client_scoped_context_helpers_apply_to_capture_async.<locals>.scoped_capture
  s\     

-/CD''(;<**+EFmmA&&&~~&:;; 's   AA(A&A(r   r-   r   rZ   r  r   r  r  r"  r  r  )r   r   r	   r   r.   r  r  runr   r   r   rE   r  r   )r+   r   r  r   r   r   r2   s         @r    9test_client_scoped_context_helpers_apply_to_capture_asynczDTestClient.test_client_scoped_context_helpers_apply_to_capture_async
  s+   ZZ34	-QUVF]]]&< '< {{>#34H  *((*",,Q/8JQ-CS/1DEL!"457K L!-02L !!L!/25G4H V__.39 544s   DD''D0c                    t        j                  d      5 }t        t        | j                  d      }t               5  t        d       t               dd}|j                  dd|	      }| j                  |       |j                          |j                  d
   d   }|d   }| j                  |d   d   d       d d d        d d d        y # 1 sw Y   xY w# 1 sw Y   y xY w)Nr   Tr   zpage-explicit-session-789landing)r"  	page_typez$pagerZ   r  r   r-   r   r   r"  )r   r   r	   r   r.   r   r   r
   r\   r   r   r   rE   )r+   r   r2   r   r   r   r   s          r    6test_set_context_session_with_page_explicit_propertieszATestClient.test_set_context_session_with_page_explicit_properties   s    ZZ34	-QUVF#$?@ $:#;!*
 ">>: *  $$X. ,,.&003G<
 m  %m46Q%  54 54s#   'CA=C:CC	CC c           	         ddl m}m} t        j                  d      5 }t        t        | j                  d      } |       5   |d       |j                  ddd	d
d      }| j                  |       |j                          |j                  d   d   }|d   }| j                  |d   d   d	       ddd       ddd       y# 1 sw Y   xY w# 1 sw Y   yxY w)zETest that explicit session ID overrides context session ID in capturer   )r   r   r   Tr   zcontext-session-overrider  rZ   zexplicit-session-overrider=  )r"  r  r  r   r-   r   r"  N)r  r   r   r   r   r	   r   r.   r\   r   r   r   rE   )r+   r   r   r   r2   r   r   r   s           r    ,test_set_context_session_override_in_capturez7TestClient.test_set_context_session_override_in_capture  s    EZZ34	-QUVF#$>?!>>  -'B'.  *  $$X. ,,.&003G<
 m  %m46Q'  54 54s$   $CA0C2CC	CCc                 N   t        dddddgi i dd      |_        t        t        d	d
      }|j	                          |j                          |j                          | j                  t        |j                        d       | j                  |j                  d   d   d       y)zGTest that when enable_local_evaluation=False, the poller is not startedr   rT  rK  Trt  re  r\  ru  r   r  r  "test-etag"dataetagtest-personal-keyFrS   enable_local_evaluationr   r\  N)
r   r   r	   r   r   rI   r   rE   rY  r   r+   r   r  r2   s       r    2test_enable_local_evaluation_false_disables_pollerz=TestClient.test_enable_local_evaluation_false_disables_poller6  s     "-   .-"&	 ') "
	  0$)
 	!!# 	&&($$&V112A6--a07Hr"   c                 N   t        dddddgi i dd      |_        t        t        d	d
      }|j	                          |j                          |j                          | j                  t        |j                        d       | j                  |j                  d   d   d       y)zLTest that when enable_local_evaluation=True (default), the poller is startedr   rT  rK  Tr  r  r  r  r!  r"  r   r\  N)	r   r   r	   r   r   r   rE   rY  r   r$  s       r    /test_enable_local_evaluation_true_starts_pollerz:TestClient.test_enable_local_evaluation_true_starts_poller\  s     "-   .-"&	 ') "
	  0$(
 	!!# 	'')$$&V112A6--a07Hr"   zposthog.client.remote_configc                     ddi|_         t        t        dd      }|j                  d      }| j	                  |ddi       |j                  dt        |j                  d|j                         y)	zJTest that get_remote_config_payload works without local evaluation enabledr   r  r!  Fr"  	test-flagr}  N)r   r	   r   get_remote_config_payloadrE   rH   rR   r  )r+   patch_remote_configr2   results       r    3test_get_remote_config_payload_works_without_pollerz>TestClient.test_get_remote_config_payload_works_without_poller  sx     -3I+>(0$)
 11+>&)!4533KK@@ 	4 	
r"   c                 j    t        t        d      }|j                  d      }| j                  |       y)z=Test that get_remote_config_payload requires personal API keyF)r#  r)  N)r	   r   r*  rU   )r+   r2   r,  s      r    8test_get_remote_config_payload_requires_personal_api_keyzCTestClient.test_get_remote_config_payload_requires_personal_api_key  s2    $)

 11+>&!r"   c                 H   t        t        d      }|j                  d      }dddddd}| j                  ||       |j                  d      }dddddd}| j                  ||       dddidd	d
iid}|j                  |      }ddddidd	d
iidd}| j                  ||       dddii}|j                  |      }ddddiddd}| j                  ||       |j                  i       }dddddd}| j                  ||       | j	                  t
              5 }|j                  d       ddd       | j                  dt        j                               | j	                  t
              5 }|j                  d       ddd       | j                  dt        |j                               | j	                  t
              5 }|j                  d       ddd       | j                  dt        |j                               y# 1 sw Y   xY w# 1 sw Y   xY w# 1 sw Y   GxY w)z0Test the _parse_send_feature_flags helper methodTr_  N)should_sendr  r  r  flag_keys_filterFr  r  r  r9  r  r  r  user_id123invalidz#Invalid type for send_feature_flagsr  )	r	   r   _parse_send_feature_flagsrE   r6   r7   r   r   r'  )r+   r2   r,  r   optionscms         r    $test_parse_send_feature_flags_methodz/TestClient.test_parse_send_feature_flags_method  sC   )T: 11$7%)!% $ $
 	* 11%8 %)!% $ $
 	* &*"()!4!*V\,B C

 11':%)"()!4!*V\,B C $
 	* 'E(:;11':%)"+U!3 $ $
 	* 11"5%)!% $ $
 	* y)R,,Y7 *;S=NOy)R,,S1 *;S=NOy)R,,T2 *;S=NO *) *) *)s$   H ,H H H	HH!c                    ddddi|_         t        j                  d      5 }t        t        | j
                  t        d      }dd	gd
did}|j                  dd|      }| j                  |       | j                  | j                         |j                          |j                  d   }| j                  |d   dd	g       | j                  |d   d
di       |j                          |j                  d   d   }|d   }| j                  |d   d   d       | j                  |d   d   d       | j                  d|d          ddd       y# 1 sw Y   yxY w)zVTest that SendFeatureFlagsOptions with flag_keys_filter only evaluates specified flagsr   value1value3)flag1flag3r   TrM  r=  r>  r  r  )r2  r  r  rZ   r   r   r  r  r-   r   r   z$feature/flag1z$feature/flag3z$feature/flag2N)r   r   r   r	   r   r.   r\   r   r   r*   r   r   rE   rm   r  s	            r    5test_capture_with_send_feature_flags_flag_keys_filterz@TestClient.test_capture_with_send_feature_flags_flag_keys_filter  sw   
 !!$
  ZZ34	!!2	F &-g$6&4e%<L
 ~~-L & H   *T[[) **,#--a0IY'>?'7ASTY':;ne=TU ((*",,Q/8JQ-CS./?@(KS./?@(K-s</@AE 544s   D$EEr   c                 f   t        t        dd      }ddddddg dd	d
gdd	dddgid	diddg|_        |j                  ddd      }| j	                  |       | j                  |j                  d       | j                  |j                         d	       | j                  |j                  d       y)zZTest that get_feature_flag_result returns a FeatureFlagResult when payload is empty stringtest_personal_api_keyTrS   rx   r   z	Test flagr)  FNzempty-variant)r   r_  r  ra  zEmpty VariantrV  rd  r=   ro  )rt  re  r\  is_simple_flagru  r_  rv  	test-userr  )	r	   r   r   get_feature_flag_resultr   rE   r\  	get_valuer  r+   patch_batch_postr2   r,  s       r    6test_get_feature_flag_result_with_empty_string_payloadzATestClient.test_get_feature_flag_result_with_empty_string_payload  s     4
 #""'&* +-26'6 #'6(769%% "1" 5# 
> //D 0 

 	V$[1))+_=,r"   c                    t        t        dd      }dddddg dd	gd
dddgiddidddddddg dd	gd
dddgiddiddg|_        |j                  dd      }| j	                  |d   d   d       | j	                  |d   d   d       | j                  d|d          | j	                  |d   d   d       | j	                  |d   d   d       y)zNTest that get_all_flags_and_payloads includes flags with empty string payloadsrA  TrB  r   zFlag with empty payloadzempty-payload-flagFvariant1)r   r  ra  rV  )r\  r_  r=   ro  )rt  re  r\  rC  ru  rv  ri   zFlag with normal payloadznormal-payload-flagvariant2znormal payloadrD  rE  r   r   N)r	   r   r   r  rE   r   rH  s       r    1test_get_all_flags_and_payloads_with_empty_stringz<TestClient.test_get_all_flags_and_payloads_with_empty_stringQ  sM    4
 1+"'.0ZHI"Zs%S$T% ",R 0 2,"'.0ZHI"Zs%S$T% ",-= > 
> 22t 3 

 	/0DEzR/0EF
S 	*F3H,IJ 567KLbQ()*?@BR	
r"   c                 l   t        j                  d      5 }t        t        | j                  d      }t               5  t        dd       |j                  dd       d d d        |j                  d	   d
   }|d   }| j                  |d   d   dg       d d d        y # 1 sw Y   BxY w# 1 sw Y   y xY w)Nr   Tr   
random_tagi90  r   rZ   r[   r   r-   r   r   r  )
r   r   r	   r   r.   r   r   r\   r   rE   )r+   r   r2   r   r   s        r    test_context_tags_addedz"TestClient.test_context_tags_added  s    ZZ34	-QUVFL%(2N  #,,Q/8JQ-CS.?,P 54  54s"   'B* B8B*B'	#B**B3zposthog.client.Client._enqueuezUnexpected errorr   c                    t        d      }ddgi fdg dddidfd	g dddidfd
ddgi fdddgi fg}|D ]G  \  }}}| j                  |      5  t        ||      } ||i |}| j                  |d       ddd       I y# 1 sw Y   TxY w)z=Test that all decorated methods handle exceptions gracefully.test-keyr\   r  r  some-idrl  r  r  r  r  
group-type	group-keyr  new-idr   N)r	   r   r  rE   )	r+   mock_enqueuer2   r   r   rw  r  r   r,  s	            r    test_methods_handle_exceptionsz)TestClient.test_methods_handle_exceptions  s    
 
# +B	#sLMYsCjQRk:B?y(+R0

 *4%Kv[1 500  . 21 *411s   'A>>B	Expected errorc                    t        dd      }ddgi fdg ddd	id
fdg ddd	id
fdddgi fdddgi fg}|D ]x  \  }}}| j                  |      5  t        ||      }| j                  t              5 } ||i | ddd       | j                  t        j                        d       ddd       z y# 1 sw Y   9xY w# 1 sw Y   xY w)z6Test that methods re-raise exceptions when debug=True.rS  Trm  r\   r  r  rT  rl  r  r  r  r  rU  rV  r  rW  r   NrZ  )r	   r   r  r6   r"  rE   r   r'  )	r+   rX  r2   r   r   rw  r  r   r8  s	            r    $test_debug_flag_re_raises_exceptionsz/TestClient.test_debug_flag_re_raises_exceptions  s    
 
$/ +B	#sLMYsCjQRk:B?y(+R0

 *4%Kv[1 5&&y1RD+F+ 2  R\\!24DE	 21 *4 21 21s$   "B=0	B19-B=1B:6B==C	)y__name__
__module____qualname__classmethodr!   r&   r.   r4   r8   r   expandrP   rW   r]   rb   rv   r~   r   r   r   r   r   r   r   r   r   r   r   r   r   r  r  r  r  r  r  r)  r-  r2  r6  rC  rF  rI  rR  r  r  r  r  r  r  r  r  r  r  r  r  r  r  r  r  r  r  r  r  r  r	  r  r  r  r  r   r   r(  r+  r.  r@  rJ  rO  rT  rZ  r]  r`  re  rh  rj  rn  rr  r{  r  r  r  r  r  r  r  r  r  r  r  r  r  r  r  r  r  r	  r  r  r  r%  r'  r-  r/  r9  r?  rJ  rN  rQ  r"  rY  r\  r	  r"   r    r   r      s-
   * * ) )
H- ]	
++
3N
2&P ](ABC#<4PQ'@%PQ	
!!(

P TZZ$%) &) TZZ&'( (( TZZ&': (:>!!"@2>DI,5 ]	
2 ]	

+
+I.G&	L	LDL"@D
 TZZ&'"8 ("8H TZZ&'SD (SDj TZZ$%Q &Q$ TZZ$%> &> TZZ&'p8 (p8d TZZ&'7 (7r ]	


" TZZ&'? (?B TZZ&' 8 ( 8D TZZ&'O8 (O8b TZZ&'W8 (W8r TZZ&'2Y (2Yh TZZ&'+X (+XZ TZZ&'% (%N TZZ&'&Q (&QPG$@6*@&@2@&@2282<LB"LHH ]  !3R<2"#1/4$9 $2/4$9 &s57|R<(,B?&B3,.CRL)	
0@10@:2R0 ] %.. $".#1VW* |, +#$>/3'5(*(, %?/3'5(*(,(   !$)$>/4'?)C(I/; %*$>/4'?)C(I/;Q?	
AD IEAD ID ] 7 /6B#6" /!e4##!e4 )## ;!4G##!4G/	
 B@C B@@.2MA
+
$&
&&6(3
3
& TZZ&'. (.4*0P""WHG& TZZ&'5
 (5
n TZZ'(TZZ$%K & )K TZZ&'S
 (S
j ] #y)	 "y)	 -	 yk2t4!I;D9+	
2 TZZ&'
 (34
B TZZ&'3 (3j TZZ&'
 (
: ]&	
(R53S(R53n TZZ&'3
 (3
j. ]+[9:%4 ;%4N4>2: TZZ'(TZZ$%"I & )"IH TZZ'(TZZ$%I & )IB TZZ./
 0
,	"LP\ TZZ&',B (,B\ TZZ+,0- -0-d TZZ+,5
 -5
n
Q TZZ(i@R6S//$ TZZ(i@P6QFFr"   r   )r   r  r   unittestr   r   r   r   r   r   posthog.clientr	   r  r
   r   r   posthog.requestr   r   posthog.test.logging_helpersr   posthog.test.test_utilsr   posthog.typesr   r   posthog.versionr   r   TestCaser   r	  r"   r    <module>rj     sN           ' ! U U 1 B 5 9 #  i2F"" i2Fr"   