
    5i-                         d Z ddlZddlZddlmZ  G d d      Z G d d      Z G d d	      Z G d
 d      Z G d d      Z	 G d d      Z
 G d d      Zy)z
E2E API Integration Tests for ReceptionistAI.

Tests the FastAPI backend endpoints based on:
- /mnt/e/genesis-system/RECEPTIONISTAI/QUICK_START.md
- localhost:8000/docs (FastAPI auto-generated docs)
    N)expectc                   "    e Zd ZdZd Zd Zd Zy)TestAPIHealthz"API health and availability tests.c                 d    t        j                  | dd      }|j                  dk(  sJ d       y)z Verify API server is accessible.
/v1/health   timeout   zAPI health check failedNrequestsgetstatus_codeselfapi_urlresponses      +/mnt/e/genesis-system/tests/e2e/test_api.pytest_api_server_runningz%TestAPIHealth.test_api_server_running   s3    <<7): 6B##s*E,EE*    c                     t        j                  | dd      }|j                         }d|v sJ d       |d   dv sJ d|d           y)	z*Verify health endpoint returns valid JSON.r   r   r	   statusz&Health response missing 'status' field)healthyokUnexpected status: N)r   r   json)r   r   r   datas       r   test_health_endpoint_responsez+TestAPIHealth.test_health_endpoint_response   s]    <<7): 6B}}4I!IIH~!22Z6I$x.IY4ZZ2r   c                 d    t        j                  | dd      }|j                  dk(  sJ d       y)z#Verify OpenAPI docs are accessible.z/docsr   r	   r   zAPI docs not accessibleNr   r   s      r   test_api_docs_availablez%TestAPIHealth.test_api_docs_available   s3    <<7)5 11=##s*E,EE*r   N)__name__
__module____qualname____doc__r   r   r     r   r   r   r      s    ,F
[Fr   r   c                   (    e Zd ZdZd Zd Zd Zd Zy)TestConversationEndpointsz!Test conversation/chat endpoints.c                     dddd}ddd}t        j                  | d||d	
      }|j                  dv sJ d|j                          |j                         }d|v sd|v sJ d       yy)z)Test POST /v1/conversation/text endpoint.bus_20260215092501_4ecb2567What are your hours?test_session_001business_idmessage
session_idapplication/jsonHgw_live_GIZxDFqNWku4yCPuUpt9mhO8dVTr49tcPrLQdBVWWUXmVt0l4Iiwufpprb3Ju2uLzContent-Typez	X-API-Key/v1/conversation/text
   r   headersr
   r      r   r   r.   z Response missing expected fieldsN)r   postr   r   )r   r   payloadr6   r   r   s         r   test_text_conversation_endpointz9TestConversationEndpoints.test_text_conversation_endpoint&   s     9-,
 /c

 ==i,-	
 ##z1_5HI]I]H^3__1 }}T!Y$%6Z8ZZ6%6!r   c                 r    dddd}ddd}t        j                  | d||d	
      }|j                  dv sJ y)z3Test conversation with phone number (lead capture).r)   z&My name is John, call me on 0412345678test_session_002r,   r0   r1   r2   r3   r4   r5   r7   Nr   r9   r   r   r   r:   r6   r   s        r    test_conversation_with_lead_dataz:TestConversationEndpoints.test_conversation_with_lead_data@   s]     9?,
 /c

 ==i,-	
 ##z111r   c                     dddd}ddd}t        j                  | d||d	
      }|j                  dv sJ d|j                          y)z0Test API handles invalid business ID gracefully.invalid_business_idTest messagetest_session_003r,   r0   r1   r2   r3   r4   r5   )  i    zExpected error, got Nr>   r?   s        r   test_invalid_business_idz2TestConversationEndpoints.test_invalid_business_idV   sq     1%,
 /c

 ==i,-	
 ##6e:NxOcOcNd8ee6r   c                     dddd}t        j                  | d|d      }|j                  dv sJ d	|j                          y
)z!Test API requires authentication.r)   rC   test_session_004r,   r3   r4   )r   r
   )i  i  rF   zExpected auth error, got Nr>   )r   r   r:   r   s       r   test_missing_api_keyz.TestConversationEndpoints.test_missing_api_keym   s`     9%,
 ==i,-
 ##6j:ST\ThThSi8jj6r   N)r!   r"   r#   r$   r;   r@   rG   rJ   r%   r   r   r'   r'   #   s    +[42,f.kr   r'   c                       e Zd ZdZd Zd Zy)TestCORSHeadersz/Test CORS configuration for widget integration.c                     dddd}t        j                  | d|d      }d|j                  v sJ d	       |j                  j                  d
      sJ d       y)z&Verify CORS headers are set correctly.http://localhost:8888POSTzcontent-type,x-api-key)OriginzAccess-Control-Request-MethodzAccess-Control-Request-Headersr3   r   r6   r
   Access-Control-Allow-Originz CORS Allow-Origin header missingzAccess-Control-Allow-Methodsz!CORS Allow-Methods header missingN)r   optionsr6   r   r   r   r6   r   s       r   test_cors_headers_presentz)TestCORSHeaders.test_cors_headers_present   st     .-3.F
 ##i,-
 -0@0@@dBdd@##$BChEhhCr   c                     ddi}t        j                  | d|d      }|j                  j                  d      }|dv s
J d|        y	)
z Verify widget origin is allowed.rP   rN   r   r   rQ   rR   )*rN   zOrigin not allowed: N)r   r   r6   )r   r   r6   r   allowed_origins        r   test_cors_allows_widget_originz.TestCORSHeaders.test_cors_allows_widget_origin   sh     -
 <<iz"
 "))--.KL!??hCWXfWgAhh?r   N)r!   r"   r#   r$   rU   rY   r%   r   r   rL   rL      s    9i&ir   rL   c                   "    e Zd ZdZd Zd Zd Zy)TestErrorHandlingz'Test API error handling and validation.c                     ddd}t        j                  | dd|d      }|j                  dk(  sJ d	|j                          y
)z Test API handles malformed JSON.r0   r1   r2   r3   zinvalid json {{{r   )r   r6   r
   rF   zExpected 422, got Nr>   rT   s       r   test_malformed_jsonz%TestErrorHandling.test_malformed_json   s_     /c
 ==i,-#	
 ##s*W.@AUAU@V,WW*r   c                 z    ddi}ddd}t        j                  | d||d      }|j                  d	k(  sJ d
       y)z#Test API validates required fields.r/   test_session_005r0   r1   r2   r3   r   r5   rF   zShould validate required fieldsNr>   r?   s        r   test_missing_required_fieldsz.TestErrorHandling.test_missing_required_fields   s`     ,
 /c

 ==i,-	
 ##s*M,MM*r   c                 |    dddd}ddd}t        j                  | d||d	
      }|j                  dv sJ d       y)z Test API handles empty messages.r)    test_session_006r,   r0   r1   r2   r3   r   r5   )rE   rF   zShould reject empty messageNr>   r?   s        r   test_empty_messagez$TestErrorHandling.test_empty_message   sc     9,
 /c

 ==i,-	
 ##z1P3PP1r   N)r!   r"   r#   r$   r]   r`   rd   r%   r   r   r[   r[      s    1X"N*Qr   r[   c                       e Zd ZdZd Zd Zy)TestAPIPerformancez(API response time and performance tests.c                     ddl }|j                         }t        j                  | dd      }|j                         |z
  }|j                  dk(  sJ |dk  sJ d|d	d
       y)z$Health check should respond quickly.r   Nr   r   r	   r   g      ?zHealth check took .2fzs (target: <1s))timer   r   r   )r   r   ri   
start_timer   response_times         r   test_health_check_fastz)TestAPIPerformance.test_health_check_fast   sk    YY[
<<7): 6B		j0##s***s"[&8s8K?$[["r   c                     ddl }dddd}ddd	}|j                         }t        j                  | d
||d      }|j                         |z
  }|j                  dv sJ |dk  sJ d|dd       y)z<Conversation endpoint should respond within reasonable time.r   Nr)   r*   test_session_perfr,   r0   r1   r2   r3      r5   r7   g      $@zResponse took rh   zs (target: <10s))ri   r   r9   r   )r   r   ri   r:   r6   rj   r   rk   s           r   test_conversation_response_timez2TestAPIPerformance.test_conversation_response_time   s     9--
 /c

 YY[
==i,-	
 		j0##z111t#Y~mC5HHX%YY#r   N)r!   r"   r#   r$   rl   rp   r%   r   r   rf   rf      s    2	\Zr   rf   c                   T    e Zd ZdZej
                  j                  d      d        Zy)TestRateLimitingz-Test rate limiting behavior (if implemented).z(Rate limiting may not be implemented yet)reasonc                     ddd}dddd}g }t        d      D ]9  }t        j                  | d	||d
      }|j                  |j                         ; d|v sJ d       y)z$Test that rate limiting is enforced.r0   r1   r2   r)   Testtest_rate_limitr,   d   r3   r   r5   i  zRate limiting not enforcedN)ranger   r9   appendr   )r   r   r6   r:   	responsesiresps          r   test_rate_limit_enforcementz,TestRateLimiting.test_rate_limit_enforcement  s    
 /c
 9+
 	s 	/A==)01	D T--.	/ i=!==r   N)r!   r"   r#   r$   pytestmarkskipr}   r%   r   r   rr   rr     s+    7[[GH> I>r   rr   c                       e Zd ZdZd Zy)TestSessionManagementz&Test session handling and persistence.c                     d}ddd}dd|d}t        j                  | d||d	
      }|j                  dv sJ dd|d}t        j                  | d||d	
      }|j                  dv sJ y)z/Test messages in same session maintain context.test_session_continuityr0   r1   r2   r)   zMy name is Alicer,   r3   r4   r5   r7   zWhat was my name again?Nr>   )r   r   r/   r6   payload1	response1payload2	response2s           r   test_same_session_continuityz2TestSessionManagement.test_same_session_continuity6  s    .
 /c
 9)$
 MMi,-	
	 $$
222 90$
 MMi,-	
	 $$
222r   N)r!   r"   r#   r$   r   r%   r   r   r   r   3  s
    0'3r   r   )r$   r~   r   playwright.sync_apir   r   r'   rL   r[   rf   rr   r   r%   r   r   <module>r      sl      &F F,Zk Zkz#i #iL>Q >QB(Z (ZV> >B*3 *3r   