
    ϴhm!                         S r SSKrSSKJs  Jr  SSKrSSKrSSK	J
r
Jr  SSKJr  SSKJr  SSKJr  SSKJr  SSKJrJrJr  SS	KJrJrJrJrJrJr  \" 5       r " S
 S\5      r g)z*
Unit tests for streams app Celery tasks.
    N)patch	MagicMock)TestCase)get_user_model)timezone)Retry)ChannelStreamSession
HLSSegment)start_stream_capturestop_stream_captureprocess_new_segmentstream_health_checkcleanup_old_segmentssend_stream_status_notificationc                      \ rS rSrSrS rS r\" S5      \" S5      S 5       5       r\" S5      S 5       r	\" S	5      \" S5      S
 5       5       r
S r\" S5      S 5       r\" S5      S 5       r\" S5      S 5       r\" S5      S 5       r\" S5      \" S5      \" S5      S 5       5       5       r\" S5      S 5       rS rSrg)StreamTasksTest   z+Test cases for stream-related Celery tasks.c                     [         R                  R                  SSSS9U l        [        R
                  " 5       U l        [        R                  R                  SSSU R                  U R                  S9U l	        g )	Ntestuserztest@example.comtestpass123)usernameemailpasswordzTest Channelztest-channelzhttps://example.com/stream.m3u8)nameslughls_urloutput_directory
created_by)
Userobjectscreate_userusertempfilemkdtemptemp_dirr	   createchannelselfs    IC:\Users\brahi\OneDrive\Desktop\Code\src\apps\streams\tests\test_tasks.pysetUpStreamTasksTest.setUp   sj    LL,,$" - 
	
 !((*--5!]]yy . 
    c                 B    [         R                  " U R                  SS9  g )NT)ignore_errors)shutilrmtreer&   r)   s    r+   tearDownStreamTasksTest.tearDown)   s    dmm48r.   z8apps.streams.services.StreamCaptureService.start_capturez9apps.notifications.tasks.send_notification_via_rule.delayc                    [        5       nSUl        X2l        [        [	        U R
                  R                  5      5      nU R                  US   5        U R                  US   S5        UR                  5         UR                  5         g)z*Test successful stream capture start task.ztest-session-idsuccess
session_idN)	r   idreturn_valuer   strr(   
assertTrueassertEqualassert_called_once)r*   mock_notificationmock_capturemock_sessionresults        r+   !test_start_stream_capture_success1StreamTasksTest.test_start_stream_capture_success,   sn    
 !{+$0!%c$,,//&:;y)*-/@A''),,.r.   c                     SUl         [        [        U R                  R                  5      5      nU R                  US   5        U R                  SU5        g)z"Test stream capture start failure.Nr6   error)r9   r   r:   r(   r8   assertFalseassertIn)r*   r?   rA   s      r+   !test_start_stream_capture_failure1StreamTasksTest.test_start_stream_capture_failure<   sD     %)!%c$,,//&:;	*+gv&r.   z7apps.streams.services.StreamCaptureService.stop_capturec                 B   [         R                  R                  U R                  S[        R
                  " 5       S9nSUl        [        [        U R                  R                  5      5      nU R                  US   5        UR                  5         UR                  5         g)z)Test successful stream capture stop task.activer(   status
started_atTr6   N)r
   r!   r'   r(   r   nowr9   r   r:   r8   r;   r=   )r*   r>   	mock_stopsessionrA   s        r+    test_stop_stream_capture_success0StreamTasksTest.test_stop_stream_capture_successF   s|    
  ''..LL||~ / 
 "&	$S%9:y)*$$&,,.r.   c                     [        [        U R                  R                  5      5      nU R	                  US   5        U R                  SUS   R                  5       5        g)z,Test stopping stream with no active session.r6   zno active sessionrE   N)r   r:   r(   r8   rF   rG   lower)r*   rA   s     r+   *test_stop_stream_capture_no_active_session:StreamTasksTest.test_stop_stream_capture_no_active_sessionY   sH    $S%9:	*+)6'?+@+@+BCr.   z6apps.jingles.services.JingleDetector.process_detectionc                    [         R                  R                  U R                  S[        R
                  " 5       S9n[        R                  R                  USSSSS9n[        5       nSUl        XAl	        [        [        UR                  5      5      nU R                  US	   5        U R                  US
   5        UR                  5         g)z2Test processing new segment with jingle detection.rK   rL      segment_001.ts/tmp/segment_001.ts      @rQ   sequence_numberfilename	file_pathdurationzdetection-idr6   detection_foundN)r
   r!   r'   r(   r   rO   r   r   r8   r9   r   r:   r;   r=   )r*   mock_processrQ   segmentmock_detectionrA   s         r+   'test_process_new_segment_with_detection7StreamTasksTest.test_process_new_segment_with_detection`   s      ''..LL||~ / 
 $$++%+ , 
 #*$2!$S_5y)*012'')r.   c                 X   [         R                  R                  U R                  S[        R
                  " 5       S9n[        R                  R                  USSSSS9nSUl        [        [        UR                  5      5      nU R                  US	   5        U R                  US
   5        g)z.Test processing new segment without detection.rK   rL   rY   rZ   r[   r\   r]   Nr6   rb   )r
   r!   r'   r(   r   rO   r   r9   r   r:   r8   r;   rF   )r*   rc   rQ   rd   rA   s        r+   %test_process_new_segment_no_detection5StreamTasksTest.test_process_new_segment_no_detection}   s      ''..LL||~ / 
 $$++%+ , 
 %)!$S_5y)* 123r.   zpsutil.process_iterc                 F   [         R                  R                  U R                  SS[        R
                  " 5       S9n[        5       nSSS.Ul        U/Ul        [        5       nU R                  US   5        U R                  US   S5        U R                  US	   S
5        g)z.Test stream health check with healthy streams.rK   i90  r(   rM   
process_idrN   ffmpeg)pidr   r6   healthy_streamsrY   unhealthy_streamsr   N)r
   r!   r'   r(   r   rO   r   infor9   r   r;   r<   )r*   mock_process_iterrQ   rc   rA   s        r+    test_stream_health_check_healthy0StreamTasksTest.test_stream_health_check_healthy   s      ''..LL||~	 / 
 !{$)8<*6&$&y)* 12A6 34a8r.   c                 t   [         R                  R                  U R                  SS[        R
                  " 5       S9n/ Ul        [        5       nU R                  US   5        U R                  US   S5        U R                  US   S5        UR                  5         U R                  UR                  S	5        g
)z-Test stream health check with dead processes.rK   i rl   r6   rp   r   rq   rY   failedN)r
   r!   r'   r(   r   rO   r9   r   r;   r<   refresh_from_dbrM   )r*   rs   rQ   rA   s       r+   "test_stream_health_check_unhealthy2StreamTasksTest.test_stream_health_check_unhealthy   s      ''..LL||~	 / 
 *,&$&y)* 12A6 34a8 	!2r.   z
os.listdirzos.path.getmtimez	os.removec                 D   SS/Ul         [        R                  " 5       R                  5       S-
  Ul         [	        [        U R                  R                  5      SS9nU R                  US   5        U R                  US   S5        U R                  UR                  S5        g	)
zTest cleanup old segments task.rZ   zsegment_002.tsi   rY   )max_age_hoursr6   files_cleaned   N)r9   r   rO   	timestampr   r:   r(   r8   r;   r<   
call_count)r*   mock_removemock_getmtimemock_listdirrA   s        r+   test_cleanup_old_segments_task.StreamTasksTest.test_cleanup_old_segments_task   s     &67G$H!%-\\^%=%=%?$%F"%c$,,//&:!Ly)*0!4//3r.   c                    [         R                  R                  U R                  S[        R
                  " 5       S9n[        5       nU R                  US   5        U R                  US   S5        UR                  5         g)z(Test sending stream status notification.rK   rL   r6   notifications_sentrY   N)
r
   r!   r'   r(   r   rO   r   r;   r<   r=   )r*   r>   rQ   rA   s       r+   $test_send_stream_status_notification4StreamTasksTest.test_send_stream_status_notification   so      ''..LL||~ / 
 12y)* 45q9,,.r.   c                    [        S5       n[        S5      Ul        U R                  [        5         [	        [        U R                  R                  5      5        SSS5        SSS5        g! , (       d  f       N= f! , (       d  f       g= f)z%Test task retry mechanism on failure.z'apps.streams.models.Channel.objects.getzDatabase errorN)r   	Exceptionside_effectassertRaisesr   r:   r(   r8   )r*   mock_gets     r+   test_task_retry_on_failure*StreamTasksTest.test_task_retry_on_failure   s_    <=#,-=#>H  ""9-$S%9: .	 >= .-	 >=s"   &A=)A,A=,
A:	6A==
B)r(   r&   r#   N)__name__
__module____qualname____firstlineno____doc__r,   r3   r   rB   rH   rR   rV   rf   ri   rt   ry   r   r   r   __static_attributes__ r.   r+   r   r      sB   5
 9 EF
FG/ H G/ EF' G' DE
FG/ H F/"D CD* E*8 CD4 E4.  !9 "9*  !3 "3. <

;
4   
4 FG/ H/;r.   r   )!r   builtins@py_builtins_pytest.assertion.rewrite	assertionrewrite
@pytest_arr$   r1   unittest.mockr   r   django.testr   django.contrib.authr   django.utilsr   celery.exceptionsr   apps.streams.modelsr	   r
   r   apps.streams.tasksr   r   r   r   r   r   r    r   r   r.   r+   <module>r      sM       *   . ! # B B 
 S;h S;r.   