o
    û›hA=  ã                   @   sZ  d dl Z d dlmZmZmZmZmZmZmZm	Z	m
Z
mZmZ d dlmZ d dlmZmZ d dlmZ d dlmZ d dlmZ d dlmZ d d	lmZ d d
lmZ d dlm Z m!Z! dd„ Z"dd„ Z#zddl$m%Z% W n e&ys   dZ%Y nw e 'd¡e j(j)dd„ ƒƒZ*e j( +dddg¡dd„ ƒZ,e j( +dddg¡dd„ ƒZ-e j( +dg d ¢¡d!d"„ ƒZ.e j( +dg d#¢¡d$d%„ ƒZ/e j( +dg d&¢¡d'd(„ ƒZ0e j( +dg d)¢¡d*d+„ ƒZ1e j( +d,d-d.g¡d/d0„ ƒZ2d1d2„ Z3d3d4„ Z4d5d6„ Z5e j(j)d7d8„ ƒZ6e j(j)d9d:„ ƒZ7d;d<„ Z8d=d>„ Z9e j:d?d@„ ƒZ;dAdB„ Z<dCdD„ Z=dEdF„ Z>dGdH„ Z?dIdJ„ Z@dKdL„ ZAdMdN„ ZBdOdP„ ZCdQdR„ ZDdS )Sé    N)ÚacceptÚaccept_or_jump_to_endÚaccept_tokenÚaccept_characterÚaccept_wordÚaccept_and_keep_cursorÚdiscardÚllm_autosuggestionÚNavigableAutoSuggestFromHistoryÚswap_autosuggestion_upÚswap_autosuggestion_down)Ú	skip_over)Úcreate_ipython_shortcutsÚreset_search_buffer)Ú
decorators)ÚInMemoryHistory)ÚBuffer)ÚDocument)ÚAutoSuggestFromHistory)ÚDEFAULT_BUFFER)ÚpatchÚMockc                  C   sR   dd l m  m  m}  tjtdd�� | j W d   ƒ d S 1 s"w   Y  d S )Nr   z8\.12.+accept_or_jump_to_end©Úmatch)Ú'IPython.terminal.shortcuts.auto_suggestZterminalÚ	shortcutsÚauto_suggestÚpytestZwarnsÚDeprecationWarningZaccept_in_vi_insert_mode)Ziptsa© r   ú[c:\Users\shubh\Videos\agent\nova\Lib\site-packages\IPython/terminal/tests/test_shortcuts.pyÚtest_deprected   s   "ÿr!   c                 C   sF   t ƒ }t ƒ |_t ƒ |j_| |j_||j_||jj_t| |d�|j_|S )N)ÚtextÚcursor_position)r   Úcurrent_bufferÚ
suggestionr"   r#   r   Údocument)r"   Úcursorr%   Úeventr   r   r    Ú
make_event#   s   

r)   é   )Ú	FIBONACCIZ
jupyter_aic                  Ã   sr   �t ƒ } tƒ }| |_d|_d}t|t|ƒdƒ}tg d�|jjj	j
_t|ƒI d H  |jjjtt|ƒd … ks7J ‚d S )Nz;IPython.terminal.tests.fake_llm.FibonacciCompletionProviderzdef fibÚ )Úreturn_value)r
   Úget_ipythonr   Zllm_provider_classr)   Úlenr   r$   ÚhistoryÚshellZhistory_managerZ	get_ranger	   r%   r"   r+   )ÚproviderÚipr"   r(   r   r   r    Útest_llm_autosuggestion4   s   €"r4   ztext, suggestion, expected)r,   údef out(tag: str, n=50):r5   )údef úout(tag: str, n=50):r7   c                 C   sL   t | t| ƒ|ƒ}|j}tƒ |_t|ƒ |jjsJ ‚|jjd |fks$J ‚d S ©Nr   )r)   r/   r$   r   Úinsert_textr   ÚcalledÚ	call_args)r"   r%   Úexpectedr(   Úbufferr   r   r    Útest_acceptB   s   r>   ztext, suggestion)r,   r5   )r6   r7   c                 C   sD   t | t| ƒ|ƒ}|j}tƒ |_t|ƒ |jjrJ ‚|jd u s J ‚d S ©N)r)   r/   r$   r   r9   r   r:   r%   )r"   r%   r(   r=   r   r   r    Útest_discardR   s   r@   z text, cursor, suggestion, called))Ú123456é   Ú	123456789T)rA   é   rC   F)z123456   
789rB   rC   Tc                 C   sB   t | ||ƒ}tƒ |j_t|ƒ |r|jj ¡  dS |jj ¡  dS )z?
    test that autosuggest is only applied at end of line.
    N)r)   r   r$   r9   r   Zassert_calledZassert_not_called)r"   r'   r%   r:   r(   r   r   r    Útest_autosuggest_at_EOLb   s   
rE   )©r,   r5   r6   ©Údúef out(tag: str, n=50):zef )úde úf out(tag: str, n=50):úf ©Údefz out(tag: str, n=50):ú )r6   r7   zout()zdef ozut(tag: str, n=50):zut()zdef ouzt(tag: str, n=50):zt()zdef outz(tag: str, n=50):ú()zdef out(ztag: str, n=50):ztag: )z	def out(tzag: str, n=50):zag: )z
def out(tazg: str, n=50):zg: )zdef out(tagz: str, n=50):z: )zdef out(tag:z str, n=50):rO   )zdef out(tag: zstr, n=50):zstr, )zdef out(tag: sz
tr, n=50):ztr, )zdef out(tag: stz	r, n=50):zr, )zdef out(tag: strz, n=50):z, n)zdef out(tag: str,z n=50):z n)zdef out(tag: str, zn=50):zn=)zdef out(tag: str, nz=50):ú=)zdef out(tag: str, n=z50):z50))zdef out(tag: str, n=5z0):z0))zdef out(tag: str, n=50ú):rR   )zdef out(tag: str, n=50)ú:rS   c                 C   óL   t | t| ƒ|ƒ}tƒ |j_t|ƒ |jjjsJ ‚|jjjd |fks$J ‚d S r8   )r)   r/   r   r$   r9   r   r:   r;   ©r"   r%   r<   r(   r   r   r    Útest_autosuggest_tokeny   s
   
rV   ))r,   r5   rH   )rH   rI   Úe)rJ   rK   ÚfrM   c                 C   rT   r8   )r)   r/   r   r$   r9   r   r:   r;   rU   r   r   r    Útest_accept_characterž   s
   

rY   )rF   rG   )ÚderK   rL   rM   )r6   r7   z	out(tag: c                 C   rT   r8   )r)   r/   r   r$   r9   r   r:   r;   rU   r   r   r    Útest_accept_word¯   s
   
r[   z"text, suggestion, expected, cursor)r,   r5   r5   r   )r6   r7   r7   é   c                 C   sV   t | ||ƒ}|j}tƒ |_t|ƒ |jjsJ ‚|jjd |fks"J ‚|j|ks)J ‚d S r8   )r)   r$   r   r9   r   r:   r;   r#   )r"   r%   r<   r'   r(   r=   r   r   r    Útest_accept_and_keep_cursorÂ   s   r]   c                  C   sl   d} t | t| ƒdƒ}tƒ |j_tdƒ�}t|ƒ |jjjrJ ‚|js$J ‚W d   ƒ d S 1 s/w   Y  d S )Nr5   r,   z?prompt_toolkit.key_binding.bindings.named_commands.forward_word)r)   r/   r   r$   r9   r   r   r:   )Úfullr(   Zforward_wordr   r   r    Útest_autosuggest_token_emptyÓ   s   
ÿ"ûr_   c                  C   sf   t ƒ } d| jj_t ƒ | j_t ƒ }d|jj_t ƒ |jj_t| ƒ | jj 	¡  t|ƒ |jjj 
t¡ d S )Nz	some textr,   )r   r$   r&   r"   ÚresetZappZlayoutZfocusr   Zassert_called_onceZassert_called_once_withr   )Zevent_with_textZevent_emptyr   r   r    Útest_reset_search_bufferà   s   


ra   c                  C   sD   t ƒ } tƒ }| |_tƒ }tƒ |_t|ƒdu sJ ‚t|ƒdu s J ‚dS )zHEnsure that swapping autosuggestions does not break with other providersN)r   r.   r   r   r   r$   r   r   )r2   r3   r(   r   r   r    Útest_other_providersð   s   rb   c                  ƒ   s.  �t ƒ ‰tg d¢d�} t| d�‰ tƒ }ˆ|_|  ¡ 2 z3 d H W }q6 dˆ _dˆ _t}t	}t
ƒ }ˆ |_‡ ‡fdd„}|ƒ jdksAJ ‚||ƒ |ƒ jd	ksMJ ‚||ƒ |ƒ jd
ksYJ ‚||ƒ |ƒ jdkseJ ‚||ƒ |ƒ jd
ksqJ ‚||ƒ |ƒ jd	ks}J ‚||ƒ |ƒ jdks‰J ‚||ƒ |ƒ jd
ks•J ‚d S )N)Zvery_aÚveryZvery_bÚvery_c©Zhistory_strings©r0   é   rc   c                     ó   ˆ  ˆ ˆ j¡} | ˆ _| S r?   ©Úget_suggestionr&   r%   ©r%   ©r=   r2   r   r    rj     ó   z/test_navigable_provider.<locals>.get_suggestionÚ_cÚ_bÚ_a©r
   r   r   r.   r   Úloadr#   r"   r   r   r   r$   ©r0   r3   Ú_ÚupZdownr(   rj   r   rl   r    Útest_navigable_providerû   s>   €
ÿrv   c                  ƒ   sæ   �t ƒ ‰tddgd�} t| d�‰ tƒ }ˆ|_|  ¡ 2 z3 d H W }q6 dˆ _dˆ _t}t	}t
ƒ }ˆ |_‡ ‡fdd„}|ƒ jd	ksAJ ‚||ƒ |ƒ jd
ksMJ ‚||ƒ |ƒ jdksYJ ‚||ƒ |ƒ jd
kseJ ‚||ƒ |ƒ jd	ksqJ ‚d S )Nzvery_a
very_brd   re   rf   rg   rc   c                     rh   r?   ri   rk   rl   r   r    rj   C  rm   zAtest_navigable_provider_multiline_entries.<locals>.get_suggestionrn   ro   rp   rq   rs   r   rl   r    Ú)test_navigable_provider_multiline_entries0  s2   €
ÿrw   c                  C   s   t ƒ } tƒ | _| S r?   )r   r   Údefault_buffer)Úsessionr   r   r    Úcreate_session_mockW  s   rz   c                  C   sº   t ƒ } d| _tƒ }|  |¡ | jdksJ ‚|jj ¡  | jdks"J ‚tƒ }|  |¡ d| _| jdks4J ‚|jj ¡  | jdksAJ ‚d| _|  ¡  |jj ¡  |jj ¡  | jdks[J ‚d S )Nr*   r   é   rD   )r
   Z
skip_linesrz   Úconnectrx   Zon_text_insertÚfireZ
disconnect)r2   Z	session_1Z	session_2r   r   r    Ú"test_navigable_provider_connection]  s$   

r~   c                  c   s8   � t ƒ } tƒ | _t| ƒ| j_z	| V  W d | _d S d | _w r?   )r.   r   Úpt_appr   Úkey_bindings)r3   r   r   r    Úipython_with_promptw  s   €r�   c                    s   t ƒ }‡ fdd„|jjjD ƒS )Nc                    s   g | ]	}|j ˆ kr|‘qS r   )Úhandler)Ú.0Zbinding©Úcommandr   r    Ú
<listcomp>„  s
    
þz,find_bindings_by_command.<locals>.<listcomp>)r.   r   r€   Zbindings)r…   r3   r   r„   r    Úfind_bindings_by_command‚  s   
þr‡   c                 C   s  t tƒ}t|ƒdksJ ‚dg d¢dœg| _t tƒ}t|ƒdks!J ‚t|d jƒg d¢ks.J ‚t|d jƒt|d jƒks>J ‚|d j|d jksJJ ‚dddœg| _t tƒ}t|ƒdks]J ‚t|d jƒg d¢ksjJ ‚t|d jƒt|d jƒkszJ ‚|d j|d jks†J ‚d S )Nr*   ú!IPython:auto_suggest.accept_token)ÚaÚbÚc©r…   Únew_keysr   Úalways)r…   Z
new_filter)r‡   r   r/   r   ÚlistÚkeysÚfilter)r�   ÚoriginalÚmatchedr   r   r    Útest_modify_unique_shortcut‹  s    ÿ ÿ r”   c                 C   s`   t tƒ}t|ƒdksJ ‚dg dœg| _t tƒ}t|ƒdksJ ‚g | _t tƒ}t|ƒdks.J ‚d S )Nr*   rˆ   rŒ   r   )r‡   r   r/   r   ©r�   r“   r   r   r    Útest_disable_shortcut¢  s   ÿr–   c                 C   s    t tƒ}dd„ |D ƒ}|h d£ksJ ‚tjtdd�� ddgdœg| _W d   ƒ n1 s-w   Y  ddgd	d
œg| _t tƒ}dd„ |D ƒ}|h d£ksNJ ‚d S )Nc                 S   ó   h | ]}|j d  ’qS ©r   ©r�   ©rƒ   Úmr   r   r    Ú	<setcomp>³  ó    z4test_modify_shortcut_with_filters.<locals>.<setcomp>>   ú)Ú}ú'ú]ú"zMultiple shortcuts matchingr   úIPython:auto_match.skip_overÚxrŒ   z6focused_insert & auto_match & followed_by_single_quote)r…   r�   Zmatch_filterc                 S   r—   r˜   r™   rš   r   r   r    rœ   Ã  r�   >   rž   r¤   rŸ   r¡   r¢   )r‡   r   r   ÚraisesÚ
ValueErrorr   )r�   r“   Zmatched_keysr   r   r    Ú!test_modify_shortcut_with_filters±  s   
ÿÿýÿr§   c                   C   s   d S r?   r   r   r   r   r    Úexample_commandÇ  s   r¨   c                 C   sr   t tƒ}t|ƒdksJ ‚tjtdd�� ddgdœg| _W d   ƒ n1 s&w   Y  t tƒ}t|ƒdks7J ‚d S )Nr   zexample_command is not a knownr   r¨   r¤   rŒ   )r‡   r¨   r/   r   r¥   r¦   r   r•   r   r   r    Ú!test_add_shortcut_for_new_commandË  s   
ÿÿr©   c                 C   sH   t jtdd�� ddgdgdœg| _W d   ƒ d S 1 sw   Y  d S )NzNo shortcuts matchingr   r£   r¤   Úy)r…   Z
match_keysr�   )r   r¥   r¦   r   )r�   r   r   r    Útest_modify_shortcut_failure×  s   ýÿ"ÿr«   c                 C   s¢   t tƒ}t|ƒdksJ ‚tjtdd�� dg ddœg| _W d   ƒ n1 s&w   Y  ddgddœg| _t tƒ}t|ƒdks@J ‚g | _t tƒ}t|ƒdksOJ ‚d S )	Nrg   z"Cannot add a shortcut without keysr   r£   T©r…   r�   Úcreater¤   rB   )r‡   r   r/   r   r¥   r¦   r   r•   r   r   r    Ú&test_add_shortcut_for_existing_commandâ  s   
ÿÿÿr®   c                  C   s<   t ƒ } | jd u s
J ‚ddgddœg}|| _| j|ksJ ‚d S )Nr£   r¤   Tr¬   )r.   r   r   )Úipythonr   r   r   r    Ú)test_setting_shortcuts_before_pt_app_initö  s   ÿr°   )Er   r   r   r   r   r   r   r   r   r	   r
   r   r   Z%IPython.terminal.shortcuts.auto_matchr   ZIPython.terminal.shortcutsr   r   ZIPython.testingr   ÚdecZprompt_toolkit.historyr   Zprompt_toolkit.bufferr   Zprompt_toolkit.documentr   Zprompt_toolkit.auto_suggestr   Zprompt_toolkit.enumsr   Zunittest.mockr   r   r!   r)   Zfake_llmr+   ÚImportErrorZskip_withoutÚmarkÚasyncior4   Zparametrizer>   r@   rE   rV   rY   r[   r]   r_   ra   rb   rv   rw   rz   r~   Zfixturer�   r‡   r”   r–   r§   r¨   r©   r«   r®   r°   r   r   r   r    Ú<module>   s¤    4ÿþþ
	þþ
	þ
þ
þ
	þ
þþ


4
&

	