a
    ”˜ëjƒ2  ã                   @   s0  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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¡r d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%e&e&e'e" e&d!œd"d#„Z(ee"d$œd%d&„Z)dS )'zLangSmith Pytest hooks.é    N)Údefaultdict)ÚLock)ÚAny)Ú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   úV/mnt/bigstor/www/usecases/.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   0   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_preparse6   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r^d| jvr^|| jd< |duržd| jjvrž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funcargsZ_fixtureinfor)   Útyper*   r+   r,   )ÚitemÚmarkerr-   Zoriginal_funcZrequest_objr   r   r   Úpytest_runtest_callA   s     


ür4   c                 C   s   |  d¡rdS dS )z7Remove the short test-status character outputs ("./F").r   )Ú r5   r5   N)Ú	getoption)Úreportr$   r   r   r   Úpytest_report_teststatusZ   s    
r8   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é
   )ÚconsoleZrefresh_per_secondzCollecting tests...N)Úrich.consoler:   Z	rich.liver;   r   ÚlistÚtest_suitesÚtest_suite_urlsÚprocess_statusr   Ústatus_lockr=   Úgenerate_tablesÚliveÚstartÚprint)Úselfr:   r;   r   r   r   Ú__init__f   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   Úsessionr2   r   r   r   Úpytest_collection_finishx   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   st   | j s| jj d¡ | j�6 | j  |i ¡}t||g d¢d�| j |< W d  ƒ n1 sV0    Y  | j |  ¡ ¡ dS )zUpdate test results.zRunning tests...)ÚfeedbackÚinputsÚreference_outputsÚoutputs)ÚunpackN)	rB   rE   r=   rG   rC   ÚgetÚ_merge_statusesÚupdaterD   )rH   rQ   ÚstatusZcurrent_statusr   r   r   Úupdate_process_status‚   s    ý*z%LangSmithPlugin.update_process_statusc                 C   s   |   |ddi¡ dS )z/Initialize live display when first test starts.r[   Ú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)ra   c                    sl  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ø|
|  |¡ qøtt|d›d�ƒ|ƒ}tt| 
dd¡ƒ|ƒ}q¾tdd„ | ¡ D ƒƒ}tdd„ | ¡ D ƒƒ}|| �r¾|||  }|dk�rždnd}d|› d |d!›d"|› d �}nd#}|	�rât|	ƒ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)ÚtitleZtitle_justifyZTestZInputszRef outputsZOutputsZStatusZFeedbackZDurationr[   Údurationc                    s   i | ]}|ˆ j | “qS r   )rB   )r   Úpid)rH   r   r   Ú
<dictcomp>»   r   z3LangSmithPlugin._generate_table.<locals>.<dictcomp>Zend_timeÚ
start_timerS   z.2fÚsZqueuedc                 s   s   | ]}|  d ¡dkV  qdS )r[   ÚpassedN©rX   ©r   rj   r   r   r   r   Å   r   z2LangSmithPlugin._generate_table.<locals>.<genexpr>c                 s   s   | ]}|  d ¡dkV  qdS )r[   ÚfailedNrl   rm   r   r   r   r   Æ   r   é   ZgreenZ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--é   é   ZyellowZcyan)r]   rk   rn   ÚskippedZwhitec                 3   s:   | ]2\}}t |ˆ d �› dt|tƒr*t|ƒn|› �V  qdS )©Úmax_lenrs   N)Ú_abbreviateÚ
isinstanceÚboolÚintrv   )Úmax_dynamic_col_widthr   r   r   ç   s   ÿrT   rU   rV   r|   Nr5   z[bold]Averages[/bold])Z
rich.tablerc   r@   rA   Z
add_columnru   Útimer   r?   rK   rX   rP   r   Úfloatr�   r€   Úmaxrt   ÚvaluesÚjoinr=   ÚwidthÚ_dumps_with_fallbackZadd_rowÚ_abbreviate_test_nameÚstrr~   )rH   ra   rc   Zprocess_idsre   rb   Z
max_statusZmax_durationÚnowZ	durationsZnumeric_feedbacksZsuite_statusesrg   r[   rf   rw   rx   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$   Z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   r9   c   s   h
r9   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   Z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_liner6   Ú	importlibÚutilÚ	find_specr   ÚosÚenvironrX   Úls_utilsZtest_tracking_is_disabledr‘   Úregisterr9   r�   r�   )r$   Úmsgr   r   r   r’     s$    ÿ
ÿÿÿr’   )Úxr}   Úreturnc                 C   s(   t | ƒ|kr | d |d … d S | S d S )Né   ú...)ru   )r¢   r}   r   r   r   r~   :  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...::)ru   Úsplit)r¦   r}   Úfiler   Zfile_lenr   r   r   rŠ   A  s    rŠ   )rZ   ÚcurrentrW   r£   c                C   s`   |D ]N}|   |d ¡ }r| |i ¡}t|tƒrJt|tƒrJi |¥|¥||< q|||< qi |¥| ¥S r   )ÚpoprX   r   Údict)rZ   r©   rW   ÚpathZpath_updateZpath_currentr   r   r   rY   L  s    
rY   )r.   r£   c                 C   s&   zt  | ¡W S  ty    Y dS 0 d S )NZunserializable)ÚjsonÚdumpsÚ	Exception)r.   r   r   r   r‰   W  s    r‰   )*r˜   Úimportlib.utilrš   r­   Úloggingr�   rƒ   Úcollectionsr   Ú	threadingr   Útypingr   Zpytestr   r   rŸ   Zlangsmith.testing._internalr   r/   Ú	getLoggerr•   r   r   r"   Ú__version__Ú
startswithr%   r&   Zhookimplr4   r8   r9   r’   r‹   r�   r~   rŠ   r«   r?   rY   r‰   r   r   r   r   Ú<module>   s8   




 9