
    $/i                        d dl Zd dlmc mZ d dlZd dlZd dlZd dl	Z	d dl
Z
d dlmZmZmZmZ d dlmZmZmZ d dlmZ d dlmZ ej0                  j                  d        Zej0                  j                  d        Zej0                  j                  d        Zej0                  j                  d	        Zej0                  j                  d
        Zej0                  j                  d        Zej0                  j                  d        Zej0                  j                  d        Z ej0                  j                  d        Z!ej0                  j                  d        Z"e#dk(  r ejH                  e%g       yy)    N)	MagicMockpatch	mock_open	AsyncMock)BrowserControllerBrowserConfigNavigationStatus)GeminiLiveSession)VisionWorkerc                    K   t        t        d            } | j                          d{    | j                  s-| j	                          d{    t        j                  d       | j                  j                         dk(  rB| j                  j                  r,| j                  j                  j	                          d{    	 | j                  d       d{   }|j                  }t        j                  }||k7  }|st        j                  d|fd	||f      d
t        j                          v st        j"                  |      rt        j$                  |      nd
t        j$                  |      dt        j                          v st        j"                  t              rt        j$                  t              ndt        j$                  |      dz  }dd|iz  }t'        t        j(                  |            dx}x}}| j	                          d{    y7 7 7 ]7 F# t*        $ r Y 0w xY w7  # | j	                          d{  7   w xY ww)z0Verify session recovery if Chrome.exe is killed.TheadlessconfigNzInit failed
Playwrightzhttps://example.com)!=)zK%(py2)s
{%(py2)s = %(py0)s.status
} != %(py6)s
{%(py6)s = %(py4)s.SUCCESS
}resr	   )py0py2py4py6zassert %(py8)spy8)r   r   
initialize_active_backendclosepytestskipname_browsernavigatestatusr	   SUCCESS
@pytest_ar_call_reprcompare@py_builtinslocals_should_repr_global_name	_safereprAssertionError_format_explanation	Exception)
controllerr   @py_assert1@py_assert5@py_assert3@py_format7@py_format9s          ;/mnt/e/genesis-system/tests/chaos/test_system_resilience.pytest_chrome_kill_recoveryr3      s     #-*FGJ



!!!%%Z-=-=-?'?'?]A[ !!&&(L8%%..,,55;;===!
 ''(=>>zz5-555z55555z5555555s555s555z555555-555-55555555555    + "'?
 > ?   	!j   s   *IH#IHA3IH!I
H' H$D#H' IH6II!I$H' '	H30H8 2H33H8 6I8IIIIc                     K   t               } t        j                         | _        t        j                         | _        d}|s8ddt        j                  |      iz  }t        t        j                  |            d}yw)z(Verify automatic audio output switching.Tassert %(py1)spy1N)
r
   asyncioget_running_looploopQueueaudio_queuer#   r(   r)   r*   session@py_assert0@py_format2s      r2   test_audio_focus_swapr@   *   sK       !G++-GL!--/G K4KKK4KKKKKKs   A<A>c                  ~  K   t               } t        j                         | _        t        j                         | _        t        j                  | d      5 }| j                  dddd       d}|s8ddt        j                  |      iz  }t        t        j                  |            d}ddd       y# 1 sw Y   yxY ww)z9Verify graceful degradation when mic unplugged (IOError).r;   Ni   Tr5   r6   )r
   r7   r8   r9   r:   r;   r   object_input_callbackr#   r(   r)   r*   )r=   mock_qr>   r?   s       r2   test_mic_unplug_handlingrE   9   s       !G++-GL!--/G 
g}	- dD$7tt	  s   AB=AB1(	B=1B:6B=c                  4  K   t        dt              5  	 t        d       ddd       d}|s8dd	t        j                  |      iz  }t        t        j                  |            d}y# t        $ r ddl} | j	                  d       Y gw xY w# 1 sw Y   lxY ww)
z-Verify system doesn't crash if print() fails.zbuiltins.printside_effectTestr   NzTest LogTr5   r6   )	r   BlockingIOErrorprintlogginginfor#   r(   r)   r*   )rL   r>   r?   s      r2   test_stdout_block_handlingrN   H   su      
_	= 
		&M
 K4KKK4KKKKKK  	 LL$	
 
s9   BBA(AB(B	BB		BBBc                     K   yw)z#Verify retry logic if 9222 blocked.N rP       r2   test_debug_port_blockrR   Z   s      	s   c            	        K   t               } t        j                         | _        t        j                         | _        t        dt        d            5  t        dt        j                  ddd            5  	 d	d	d	       d	d	d	       d
}|s8ddt        j                  |      iz  }t        t        j                  |            d	}y	# 1 sw Y   PxY w# 1 sw Y   TxY ww)z0Verify handling of corrupt context.json updates.zbuiltins.openz{ invalid json)	read_dataz	json.loadmsgdocr   rG   NTr5   r6   )r
   r7   r8   r9   Event_conductor_stop_flagr   r   jsonJSONDecodeErrorr#   r(   r)   r*   r<   s      r2   test_corrupt_context_jsonr[   a   s       !G++-GL#*==?G  
	4D E	F ;D,@,@q,QR 	
 	 K4KKK4KKKKKK	 	 s7   AC!#C8C	:CAC!	C	CCC!c                  R  K   t               } t        d      | j                  _        t	        |       }d }||_        d|_        t        j                  |j                               }||_
        t        j                  d       d{    |j                          d{    g }|j                  } |       }|}|s|j                  } |       }	|	}|s@ddt        j                          v st#        j$                  |      rt#        j&                  |      ndt#        j&                  |      t#        j&                  |      dz  }
|j)                  |
       |sd	dt        j                          v st#        j$                  |      rt#        j&                  |      ndt#        j&                        t#        j&                  	      d
z  }|j)                  |       t#        j*                  |d      i z  }dd|iz  }t-        t#        j.                  |            dx}x}x}x}x}}	y7 7 w)z)Verify VisionWorker doesn't hang forever.T)return_valuec                  J   K   t        j                  d       d {    y7 w)N   rQ   )r7   sleeprP   rQ   r2   slow_capturez-test_screenshot_timeout.<locals>.slow_capture{   s      mmA 	s   #!#g?Nz=%(py6)s
{%(py6)s = %(py4)s
{%(py4)s = %(py2)s.cancelled
}()
}task)r   r   r   z<%(py12)s
{%(py12)s = %(py10)s
{%(py10)s = %(py8)s.done
}()
})r   py10py12   zassert %(py15)spy15)r   r   r   is_aliver   _capture_frame
is_runningr7   create_task_loop_taskr`   stop	cancelleddoner%   r&   r#   r'   r(   append_format_boolopr)   r*   )	mock_ctrlworkerra   rb   r-   r/   r.   r>   @py_assert9@py_assert11r0   @py_format13@py_format14@py_format16s                 r2   test_screenshot_timeoutry   s   sY     I)2)EI&)$F )FF v||~.DFL
--

++-*4>>*>**tyy*y{*{******4***4***>************t***t***y***{************* s%   BH'H!H'H$FH'$H'c                    K   t        t        d            } dfd}|| _        t        d      D ]  }| j                  s| j                          ! d}|k(  }|st	        j
                  d|fd|f      d	t        j                         v st	        j                        rt	        j                        nd	t	        j                  |      d
z  }dd|iz  }t        t	        j                  |            dx}}yw)zVerify yield on flooded input.Tr   r   r   c                       dz   y )Nre   rP   )counts   r2   on_activityz)test_input_dos_flood.<locals>.on_activity   s    
rQ   i  )==)z%(py0)s == %(py3)sr|   r   py3assert %(py5)spy5N)r   r   on_user_activityranger#   r$   r%   r&   r'   r(   r)   r*   )cr}   _@py_assert2r-   @py_format4@py_format6r|   s          @r2   test_input_dos_floodr      s     
 	!=>AE %A 4[ ! !
 5D=5D55Ds   ?C<B9C<c                    K   d} t        t        |       }|sddt        j                         v st	        j
                  t               rt	        j                  t               nddt        j                         v st	        j
                  t              rt	        j                  t              ndt	        j                  |       t	        j                  |      dz  }t        t	        j                  |            dx} }yw)zVerify clean shutdown.SIGINTz5assert %(py5)s
{%(py5)s = %(py0)s(%(py1)s, %(py3)s)
}hasattrsignal)r   r6   r   r   N)	r   r   r%   r&   r#   r'   r(   r)   r*   )r   @py_assert4r   s      r2   test_sigint_handlingr      s      $$768$$$$$$$$7$$$7$$$$$$6$$$6$$$8$$$$$$$$$$s   C8C:c                  t  K   t               } t        t        d            }t        |      }d}| |u}|st	        j
                  d|fd| |f      dt        j                         v st	        j                  |       rt	        j                  |       ndt	        j                  |      dz  }d	d
|iz  }t        t	        j                  |            dx}}d}||u}|st	        j
                  d|fd||f      dt        j                         v st	        j                  |      rt	        j                  |      ndt	        j                  |      dz  }d	d
|iz  }t        t	        j                  |            dx}}d}||u}|st	        j
                  d|fd||f      dt        j                         v st	        j                  |      rt	        j                  |      ndt	        j                  |      dz  }d	d
|iz  }t        t	        j                  |            dx}}|j                          d{    y7 w)z Verify combined stress scenario.Tr   r   N)is not)z%(py0)s is not %(py3)sr=   r   r   r   r,   rs   )r
   r   r   r   r#   r$   r%   r&   r'   r(   r)   r*   r   )r=   r,   rs   r   r-   r   r   s          r2   test_gold_run_chaos_combor      sF      !G"-*FGJ*%F7$7$77$!!:T!!!!:T!!!!!!:!!!:!!!T!!!!!!!6666



s   H.H80H61H8__main__)&builtinsr%   _pytest.assertion.rewrite	assertionrewriter#   r   pytest_asyncior7   r   rY   unittest.mockr   r   r   r   core.browser_controllerr   r   r	   core.voice.gemini_live_sessionr
   core.vision_workerr   markr3   r@   rE   rN   rR   r[   ry   r   r   r   __name__main__file__rP   rQ   r2   <module>r      su          @ @ V V < + ! !8      " 	 	  " + +8  , % %  ( zFKK
 rQ   