
    ږJh*                        d dl mZ d dlmZ d dlmZmZ d dlZd dlZd dl	Z	d dl
Z
d dlZ e
j                  e
j                  d        e
j                  e      Z	 eredk(  rej#                  d       dZn ee	      Zej'                  d
       dedee   fdZdedee   fdZdedefdZddedee   dee   fdZy# e$ r Zej#                  de        dZY dZ[PdZ[ww xY w)    )OpenAI)OPENAI_API_KEY)OptionalListNz4%(asctime)s - %(name)s - %(levelname)s - %(message)s)levelformatyour_openai_api_key_hereuA   ⚠️ Invalid or missing OPENAI_API_KEY in environment variables)api_keyz&OpenAI client initialized successfullyz"Error initializing OpenAI client: 	image_urlreturnc                     	 t        j                  |       }t        j                  |j                        j                  d      S # t        $ r"}t        j                  d|        Y d}~yd}~ww xY w)z Encode image from URL to base64.utf-8zError encoding image from URL: N)	requestsgetbase64	b64encodecontentdecode	Exceptionloggererror)r   responsees      6D:\Projects\AI-TestCaseGenerator\ai\image_generator.pyencode_image_from_urlr      s^    <<	* 0 0188AA 6qc:;s   AA 	A0A++A0
image_pathc                    	 t        | d      5 }t        j                  |j                               j	                  d      cddd       S # 1 sw Y   yxY w# t
        $ r"}t        j                  d|        Y d}~yd}~ww xY w)z'Encode image from local path to base64.rbr   Nz Error encoding image from path: )openr   r   readr   r   r   r   )r   
image_filer   s      r   encode_image_from_pathr"   #   sr    *d# 	Gz##JOO$56==gF	G 	G 	G 7s;<s4   A 2A
 	A 
AA A 	BA<<B	test_typec                     ddddddddddddd}|j                  | |       }dd	d
dddd
dddddddddddddddddd}|j                  |i       }|st        j                  d|  d| d       |S )z.Get the configuration for a specific test typedashboard_functionaldashboard_negativedashboard_uidashboard_uxdashboard_compatibilitydashboard_performance)r%   r&   r'   r(   r)   r*   zFunctional - Positive TestszFunctional - Negative TestzUI TestszUX TestszCompatibility TestszPerformance TestsTC_FUNCzEfunctional test cases focusing on valid inputs and expected behaviors   )prefixdescriptioncountTC_NEGzNnegative test cases focusing on invalid inputs, error handling, and edge casesTC_UIz4UI test cases focusing on visual elements and layout   TC_UXzDuser experience test cases focusing on user interaction and workflow	TC_COMPATz@compatibility test cases across different browsers and platformsTC_PERFz@performance test cases focusing on load times and responsiveness)r%   r&   r'   r(   r)   r*   zUnknown test type: z (mapped to ))r   r   warning)r#   form_to_internalinternal_typebase_configsconfigs        r   get_test_type_configr<   ,   s     !72&&#<!8'=&:""84" %((I>M  b!
 k
 Q
 a
 "]$
  ]"
5LB mR0F,YK|M?RSTUM    selected_typesc                 <   | st         j                  d       y|rt        |      dk(  rt         j                  d       yt        "d}t         j                  |       t	        |      t         j                  d|        t         j                  d|         g d}	 t        j                  j                  |       s%d	|  }t         j                  |       t        |      t        |       }|st         j                  d
       yg }|D ]:  }t         j                  d|        t        |      }|st         j                  d|        Bd|d    d| d|d    d|d    d	}d}	d}
|D ]  }|	r n	 t         j                  d| d| d       t        j                  j                  j                  |ddddd|dddd | id!gdgd"d#$      }|j                   d   j"                  j$                  j'                         }	|	r6t         j                  d%| d&|        d'| d(|	 }|j)                  |        nt         j                  d)| d&|         |	s|
rd,}d-|
v sd.|
v r=d/| d+|
 }t         j                  |       t         j                  d0       t	        d1      d2|
j/                         v sd3|
j/                         v r=d4| d+|
 }t         j                  |       t         j                  d5       t	        d6      d7| d8|
 }t         j                  |       t	        d9|
       t         j                  d:|        = |st         j                  d;       yt         j                  d<t        |       d=       d(d(j1                  |      z   S # t*        $ r4}t-        |      }
t         j                  d*| d| d+|
        Y d}~dd}~ww xY w# t*        $ r?}t         j                  d>t-        |       d?@       t	        dAt-        |             d}~ww xY w)Bz9Generate test cases from an image using OpenAI Vision APIz/No image path provided for test case generationNr   z/No test types selected for test case generationzJOpenAI client not initialized. Please check your API key in the .env file.z,Generating test cases from image for types: zImage path: )zgpt-4ozgpt-4-vision-previewzgpt-4-turbo-previewzImage file not found at path: zFailed to encode imagez#Starting generation for test type: zSkipping unknown test type: z4
            Analyze the image and generate EXACTLY r/    z' test cases.
            Focus ONLY on r.   zM.

            Use this EXACT format for each test case:

            Title: r-   ab  _[Number]_[Brief_Title]
            Scenario: [Detailed scenario description based on the image]
            Steps to reproduce:
            1. [Step 1]
            2. [Step 2]
            ...
            Expected Result: [What should happen]
            Actual Result: [To be filled during execution]
            Priority: [High/Medium/Low]
            z1Sending request to OpenAI Vision API using model z for z test casessystemzDYou are a QA engineer generating test cases from the provided image.)roler   usertext)typerD   r   urlzdata:image/jpeg;base64,)rE   r   i  gffffff?)modelmessages
max_tokenstemperaturezSuccessfully generated z test cases using model zTEST TYPE: z

zReceived empty response for zError using model z test cases:  model_not_foundinvalid_request_errorz!Error with all OpenAI models for zXPlease check if your OpenAI account has access to GPT-4 models with vision capabilities.ziThe OpenAI model required for image processing is not available. Please check your OpenAI account access.authorizationzapi keyzAuthorization error for z6Please check your OpenAI API key in the configuration.z;OpenAI API authorization failed. Please check your API key.zError generating z test cases with all models: zFailed to generate test cases: z$Completed generation for test type: z!Failed to generate any test casesz&Successfully generated test cases for z test typesz(Error in generate_test_case_from_image: T)exc_infoz*Failed to generate test cases from image: )r   r   lenclient
ValueErrorinfoospathexistsFileNotFoundErrorr"   r<   r7   chatcompletionscreatechoicesmessager   stripappendr   strlowerjoin)r   r>   	error_msgvision_modelsbase64_imageall_test_casesr#   r;   prompt
test_cases
last_errorrG   r   test_cases_with_headerr   s                  r   generate_test_case_from_imagerj   h   s   FGS0A5FG ~`	Y##
KK>~>NOP
KK,zl+, NMxPww~~j)8EILL##I..-j9LL12' ^	LIKK=i[IJ))4F!=i[IJ44:7O3DAi[ Q!-01 2 8$% 	&F" JJ ' -)KK"STYSZZ_`i_jju vw  &{{66==# )1-q
 )/-3V$D0;,15L\N3[6*%&,""$ $($'+  >  H0 "*!1!1!!4!<!<!D!D!J!J!LJ!&=i[H`af`g$hi3>ykj\1Z.&--.DE)Ei[Phinho'pqQ-` *	$
26MQ[6["CI;m\f[g hILL+LL!{|$  &Q  R  R$
(8(8(::i:K[K[K]>]":9+]S]R^ _ILL+LL!YZ$%bcc"3I;>[\f[g hILL+$'Fzl%STTKK>ykJK}^	L@ LL<=<S=P<QQ\]^N333? ! !$QJNN%7weI;m\f[g#hiB  P?AxHSWXEc!fXNOOPs^   A&O A+O /CN4O 6NDO 5O 	O)OO OO 	P:PP)N)openair   config.settingsr   typingr   r   r   r   rT   loggingsysbasicConfigINFO	getLogger__name__r   r   rQ   rS   r   r   r_   r   r"   dictr<   rj    r=   r   <module>rv      s    * !   	  
   ',,/e f			8	$	^/IIXY/<=
S Xc] s x} :C :D :xNPc NP49 NPX`adXe NPe  
LL5aS9:Fs   5B? ?C$CC$