a
    `gc2                     @   s   d 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 ddl	m
Z
 ddlZddlmZ ddlmZ eeZdd Zd	d
 Zejdrdd Zndd Zejdddd Zejdd ZG dd dZdd Ze e!e dddZ"e e!e dddZ#dS ) zLangSmith Pytest hooks.    N)defaultdict)Lock)utils)testc                 C   sD   z"|  dd}|jddddd W n ty>   td Y n0 d	S )
zaSet a boolean flag for LangSmith output.

    Skip if --langsmith-output is already defined.
    	langsmithZ	LangSmith--langsmith-output
store_trueFz'Use LangSmith output (requires 'rich').)actiondefaulthelpzCLangSmith output flag cannot be added because it's already defined.N)ZgetgroupZ	addoption
ValueErrorloggerwarning)parsergroup r   e/var/www/html/cobodadashboardai.evdpl.com/venv/lib/python3.9/site-packages/langsmith/pytest_plugin.pypytest_addoption   s    
r   c                    s^   t  fdddD rZt  fdddD s8 dd t  fddd	D sZ dd
 dS )zHandle output arguments.c                 3   s   | ]}| v V  qd S Nr   ).0optargsr   r   	<genexpr>*       z&_handle_output_args.<locals>.<genexpr>)r   c                 3   s   | ]}| v V  qd S r   r   r   ar   r   r   r   ,   r   )-qqr   r   c                 3   s   | ]}| v V  qd S r   r   r   r   r   r   r   /   r   )-sz--capture=nor   N)anyinsertr   r   r   r   _handle_output_args(   s
    r!   z7.c                 C   s   t | dS )zCCall immediately after command line options are parsed (pytest v7).Nr!   )configr   r   r   r   pytest_cmdline_preparse5   s    r$   c                 C   s   t |  dS )zHandle args in pytest v8+.Nr"   r   r   r   r   pytest_load_initial_conftests;   s    r%   T)Zhookwrapperc                 c   s   |  d}|r|r|jni }| j}tf i ||| _t| dd}|durd| jvr|| jd< t| j| jjd | jj	| jj
| jjd| _dV  dS )zEApply LangSmith tracking to tests marked with @pytest.mark.langsmith.r   _requestNrequest)r'   )argnamesinitialnamesnames_closurename2fixturedefs)Zget_closest_markerkwargsobjls_testgetattrZfuncargstypeZ_fixtureinfor(   r)   r*   r+   )itemmarkerr,   Zoriginal_funcZrequest_objr   r   r   pytest_runtest_call@   s    


r3   c                 C   s   | drdS dS )z7Remove the short test-status character outputs ("./F").r   ) r4   r4   N)	getoption)reportr#   r   r   r   pytest_report_teststatusX   s    
r7   c                   @   s^   e Zd ZdZdd Zdd Zdd Zdd	 Zd
d Zdd Z	e
dddZdd Zdd ZdS )LangSmithPluginz'Plugin for rendering LangSmith results.c                 C   sp   ddl m} ddlm} tt| _i | _i | _t	 | _
| | _||  | jdd| _| j  | jjd dS )zInitialize.r   )Console)Live
   )consolerefresh_per_secondzCollecting tests...N)rich.consoler9   Z	rich.liver:   r   listtest_suitestest_suite_urlsprocess_statusr   status_lockr<   generate_tableslivestartprint)selfr9   r:   r   r   r   __init__d   s    

zLangSmithPlugin.__init__c                 C   s&   t  | _|jD ]}| j|j qdS )zHCall after collection phase is completed and session.items is populated.N)setZcollected_nodeidsitemsaddnodeid)rH   sessionr1   r   r   r   pytest_collection_finishv   s    
z(LangSmithPlugin.pytest_collection_finishc                 C   s   | j | | dS )z&Group a test case with its test suite.N)r@   append)rH   Z
test_suite
process_idr   r   r   add_process_to_test_suite|   s    z)LangSmithPlugin.add_process_to_test_suitec                 C   s  | j s| jjd | j | j |i }|drRi |di |d|d< |drzi |di |d|d< |dri |di |d|d< |dri |di |d|d< i ||| j |< W d   n1 s0    Y  | j|   dS )zUpdate test results.zRunning tests...feedbackinputsreference_outputsoutputsN)	rB   rE   r<   rG   rC   getpopupdaterD   )rH   rQ   statusZcurrent_statusr   r   r   update_process_status   s<    







0z%LangSmithPlugin.update_process_statusc                 C   s   |  |ddi dS )z/Initialize live display when first test starts.rZ   runningN)r[   )rH   rM   r   r   r   pytest_runtest_logstart   s    z'LangSmithPlugin.pytest_runtest_logstartc                 C   s<   ddl m} g }| jD ]}| |}|| q|| }|S )u   Generate a collection of tables—one per suite.

        Returns a 'Group' object so it can be rendered simultaneously by Rich Live.
        r   )Group)r>   r^   r@   _generate_tablerP   )rH   r^   Ztables
suite_nametabler   r   r   r   rD      s    

zLangSmithPlugin.generate_tables)r`   c                    st  ddl m} j| }d| dj|  d}||dd}|d |d	 |d
 |d |d |d |d td}td}t }g }	tt}
fdd|D }|	 D ]\}}|
d||
d| }|	| |
di 	 D ]&\}}t|tttfr|
| | qtt|dd|}tt|
dd|}qtdd | D }tdd | D }|| r|||  }|dkrdnd}d| d |d!d"| d }nd#}|	rt|	t|	 dd}nd$}|
rd%d&d |
	 D }nd'}t|t|}jj||  d(  t d) |	 D ]\}}d*ddd+d,
|
ddd-}|
d||
d| }d% fd.d|
di 	 D }t|
d/i }t|
d0i }t|
d1i }|tt| d2t| d2t| d2t| d2  d3 d| d |
dd d"| d ||dd q>|d4d4d4d4d4d4d4 |d5d4d4d4||| |S )6zGenerate results table.r   )TablezTest Suite: [bold]z$[/bold]
LangSmith URL: [bright_cyan]z[/bright_cyan]left)titletitle_justifyZTestZInputszRef outputsZOutputsStatusZFeedbackZDurationrZ   durationc                    s   i | ]}| j | qS r   )rB   )r   pid)rH   r   r   
<dictcomp>   r   z3LangSmithPlugin._generate_table.<locals>.<dictcomp>end_time
start_timerS   z.2fsZqueuedc                 s   s   | ]}| d dkV  qdS )rZ   passedNrW   r   rl   r   r   r   r      r   z2LangSmithPlugin._generate_table.<locals>.<genexpr>c                 s   s   | ]}| d dkV  qdS )rZ   failedNrn   ro   r   r   r   r      r      greenred[]z.0%z[/z
Passed: --z--s
c                 s   s,   | ]$\}}| d t |t|  V  qdS ): N)sumlenr   kvr   r   r   r      s   z--      yellowcyan)r\   rm   rp   Zskippedwhitec                 3   s:   | ]2\}}t | d  dt|tr*t|n| V  qdS )max_lenrw   N)_abbreviate
isinstanceboolintrz   )max_dynamic_col_widthr   r   r      s   rT   rU   rV   r   Nr4   z[bold]Averages[/bold])Z
rich.tablerb   r@   rA   
add_columnry   timer   r?   rK   rW   rP   r   floatr   r   maxrx   valuesjoinr<   widthjsondumpsadd_row_abbreviate_test_namestrr   )rH   r`   rb   Zprocess_idsrd   ra   Z
max_statusZmax_durationnowZ	durationsZnumeric_feedbacksZsuite_statusesrh   rZ   rg   r{   r|   Zpassed_countZfailed_countZratecolorZaggregate_statusZaggregate_durationZaggregate_feedbackZstatus_colorrS   rT   rU   rV   r   )r   rH   r   r_      s    

















zLangSmithPlugin._generate_tablec                 C   s&   d|j _|jd}|r"dd |_dS )z9Disable warning reporting and show no warnings in output.Fzwarnings-pluginc                  _   s   d S r   r   )r   r,   r   r   r   <lambda>  r   z2LangSmithPlugin.pytest_configure.<locals>.<lambda>N)optionshowwarningspluginmanagerZ
get_pluginZwarning_summary)rH   r#   reporterr   r   r   pytest_configure  s    z LangSmithPlugin.pytest_configurec                 C   s   | j   | j jd dS )z3Stop Rich Live rendering at the end of the session.z
Finishing up...N)rE   stopr<   rG   )rH   rN   r   r   r   pytest_sessionfinish!  s    
z$LangSmithPlugin.pytest_sessionfinishN)__name__
__module____qualname____doc__rI   rO   rR   r[   r]   rD   r   r_   r   r   r   r   r   r   r8   a   s   f
r8   c                 C   sv   |  dd | drrtjds.d}t|tjdrFd}t|t	
 rZd}t|| jt d	 d
| j_dS )z Register the 'langsmith' marker.markersz/langsmith: mark test to be tracked in LangSmithr   richzoMust have 'rich' installed to use --langsmith-output. Please install with: `pip install -U 'langsmith[pytest]'`ZPYTEST_XDIST_TESTRUNUIDzq--langsmith-output not supported with pytest-xdist. Please remove the '--langsmith-output' option or '-n' option.z--langsmith-output not supported when env varLANGSMITH_TEST_TRACKING='false'. Please remove the'--langsmith-output' option or enable test tracking.Zlangsmith_output_pluginFN)Zaddinivalue_liner5   	importlibutil	find_specr   osenvironrW   ls_utilsZtest_tracking_is_disabledr   registerr8   r   r   )r#   msgr   r   r   r   '  s$    
r   )xr   returnc                 C   s(   t | |kr | d |d  d S | S d S )N   ...)ry   )r   r   r   r   r   r   F  s    r   )	test_namer   r   c                 C   sr   t | |krj| d\}}t d| |kr@d||d  d   S |t d|  }d|| d   d | S | S d S )Nz::z.py::r   r   z...::)ry   split)r   r   filer   Zfile_lenr   r   r   r   M  s    r   )$r   importlib.utilr   r   loggingr   r   collectionsr   	threadingr   Zpytestr   r   r   Zlangsmith.testing._internalr   r.   	getLoggerr   r   r   r!   __version__
startswithr$   r%   Zhookimplr3   r7   r8   r   r   r   r   r   r   r   r   r   <module>   s2   




 G