
    `%Bj                     h    d dl Zd dlmc mZ d dlmZ d dlm	Z	 d Z
d Zd Zd Zd Zd	 Zd
 Zd ZdS )    N)Path)	AlertPlanc                 .   ddl m} |                     dt          |dz                       |                     dd           |                                }|j        }d}||u }|st          j        d|fd	||f          d
t          j
                    v st          j        |          rt          j        |          nd
t          j        |          t          j        |          dz  }dd|iz  }t          t          j        |                    d x}x}}|j        }d }||u }|st          j        d|fd||f          d
t          j
                    v st          j        |          rt          j        |          nd
t          j        |          t          j        |          dz  }dd|iz  }t          t          j        |                    d x}x}}|j        }d }||u }|st          j        d|fd||f          d
t          j
                    v st          j        |          rt          j        |          nd
t          j        |          t          j        |          dz  }dd|iz  }t          t          j        |                    d x}x}}d S )Nr   TelegramAlertConfigCTB_TELEGRAM_ENV_FILEzmissing.envCTB_TELEGRAM_ALERTSFraisingisz/%(py2)s
{%(py2)s = %(py0)s.enabled
} is %(py5)scfgpy0py2py5assert %(py7)spy7)z/%(py2)s
{%(py2)s = %(py0)s.chat_id
} is %(py5)s)z1%(py2)s
{%(py2)s = %(py0)s.thread_id
} is %(py5)s)telegram_alertsr   setenvstrdelenvfrom_envenabled
@pytest_ar_call_reprcompare@py_builtinslocals_should_repr_global_name	_safereprAssertionError_format_explanationchat_id	thread_id)	monkeypatchtmp_pathr   r   @py_assert1@py_assert4@py_assert3@py_format6@py_format8s	            O/home/agent/projects/CryptoTradingBot/Crypto_Agent/tests/test_telegram_paper.py&test_telegram_config_defaults_disabledr.      s    333333.H}4L0M0MNNN,e<<<

&
&
(
(C;%;%;%33;%;$;$;$33;$= D =D          =D            3     3      =    D                                 c                    ddl m} |                     dd           |                     dd           |                     dd           |                     d	d
           |                                }|j        }d}||u }|st          j        d|fd||f          dt          j                    v st          j	        |          rt          j
        |          ndt          j
        |          t          j
        |          dz  }dd|iz  }t          t          j        |                    d x}x}}|j        }d}||k    }|st          j        d|fd||f          dt          j                    v st          j	        |          rt          j
        |          ndt          j
        |          t          j
        |          dz  }dd|iz  }t          t          j        |                    d x}x}}|j        }d}||k    }|st          j        d|fd||f          dt          j                    v st          j	        |          rt          j
        |          ndt          j
        |          t          j
        |          dz  }dd|iz  }t          t          j        |                    d x}x}}|j        }d}||k    }|st          j        d|fd||f          dt          j                    v st          j	        |          rt          j
        |          ndt          j
        |          t          j
        |          dz  }dd|iz  }t          t          j        |                    d x}x}}d S )Nr   r   r	   trueCTB_TELEGRAM_BOT_TOKENtokenCTB_TELEGRAM_CHAT_ID-100123CTB_TELEGRAM_THREAD_ID6579Tr   r   r   r   r   r   ==z1%(py2)s
{%(py2)s = %(py0)s.bot_token
} == %(py5)sz/%(py2)s
{%(py2)s = %(py0)s.chat_id
} == %(py5)s  z1%(py2)s
{%(py2)s = %(py0)s.thread_id
} == %(py5)s)r   r   r   r   r   r   r   r   r   r    r!   r"   r#   	bot_tokenr$   r%   )r&   r   r   r(   r)   r*   r+   r,   s           r-   $test_telegram_config_reads_topic_envr?      s   333333,f555/999-y999/888

&
&
(
(C;$;$;$33;$=#G#=G##########=G############3#####3######=####G###########################;#)#;)##########;)############3#####3######;####)###########################= D =D          =D            3     3      =    D                             r/   c                 t   ddl m} |dz  }|                    dd           |                     dt	          |                     |                     dd	
           |                     dd	
           |                     dd	
           |                     dd	
           |                                }|j        }d}||u }|st          j	        d|fd||f          dt          j                    v st          j        |          rt          j        |          ndt          j        |          t          j        |          dz  }dd|iz  }	t          t          j        |	                    d x}x}}|j        }d}||k    }|st          j	        d|fd||f          dt          j                    v st          j        |          rt          j        |          ndt          j        |          t          j        |          dz  }dd|iz  }	t          t          j        |	                    d x}x}}|j        }d}||k    }|st          j	        d|fd||f          dt          j                    v st          j        |          rt          j        |          ndt          j        |          t          j        |          dz  }dd|iz  }	t          t          j        |	                    d x}x}}|j        }d}||k    }|st          j	        d|fd||f          dt          j                    v st          j        |          rt          j        |          ndt          j        |          t          j        |          dz  }dd|iz  }	t          t          j        |	                    d x}x}}d S )Nr   r   zcrypto_agent_telegram.envztCTB_TELEGRAM_ALERTS=true
CTB_TELEGRAM_BOT_TOKEN=file-token
CTB_TELEGRAM_CHAT_ID=-100456
CTB_TELEGRAM_THREAD_ID=6579
zutf-8)encodingr   r2   Fr
   r4   r6   r	   Tr   r   r   r   r   r   z
file-tokenr8   r:   z-100456r;   r<   r=   )r   r   
write_textr   r   r   r   r   r   r   r   r   r    r!   r"   r#   r>   r$   r%   )
r&   r'   r   env_filer   r(   r)   r*   r+   r,   s
             r-   -test_telegram_config_can_load_secret_env_filerD   "   sZ   33333355H  S  ^e  f  f  f.H>>>/???-u===/???,e<<<

&
&
(
(C;$;$;$33;$=(L(=L((((((((((=L((((((((((((3(((((3((((((=((((L(((((((((((((((((((((((((((;#)#;)##########;)############3#####3######;####)###########################= D =D          =D            3     3      =    D                             r/   c                  ^   ddl m} m}  |t          d           | d                    }|j        }d}||u }|st          j        d|fd||f          d	t          j                    v st          j	        |          rt          j
        |          nd	t          j
        |          t          j
        |          d
z  }dd|iz  }t          t          j        |                    d x}x}}|j        }d}||k    }|st          j        d|fd||f          d	t          j                    v st          j	        |          rt          j
        |          nd	t          j
        |          t          j
        |          d
z  }dd|iz  }t          t          j        |                    d x}x}}d S )Nr   r   send_telegram_alerthellotextF)r   r   z,%(py2)s
{%(py2)s = %(py0)s.sent
} is %(py5)sresultr   r   r   disabledr8   z.%(py2)s
{%(py2)s = %(py0)s.reason
} == %(py5)sr   r   rG   r   sentr   r   r   r   r    r!   r"   r#   reason)r   rG   rL   r(   r)   r*   r+   r,   s           r-   5test_telegram_disabled_returns_skipped_without_senderrR   5   s   HHHHHHHH  !8!8!8:M:MV[:\:\:\]]F;%;%;%66;%=&J&=J&&&&&&&&&&=J&&&&&&&&&&&&6&&&&&6&&&&&&=&&&&J&&&&&&&&&&&&&&&&&&&&&&&&&&&&&r/   c                  n   ddl m} m}  | dddd           } |t          d          |d	 
          }|j        }d}||u }|st          j        d|fd||f          dt          j                    v st          j	        |          rt          j
        |          ndt          j
        |          t          j
        |          dz  }dd|iz  }t          t          j        |                    d x}x}}|j        }d}||k    }|st          j        d|fd||f          dt          j                    v st          j	        |          rt          j
        |          ndt          j
        |          t          j
        |          dz  }dd|iz  }t          t          j        |                    d x}x}}d S )Nr   rF   Tr3   r5   r   r>   r$   r%   rH   rI   c                  
    ddiS )NokT )_args_kwargss     r-   <lambda>zAtest_telegram_send_requires_crypto_topic_thread.<locals>.<lambda>C   s    aegk`l r/   senderFr   rK   rL   r   r   r   missing_thread_idr8   rN   rO   )	r   rG   r   rL   r(   r)   r*   r+   r,   s	            r-   /test_telegram_send_requires_crypto_topic_threadr^   >   s   HHHHHHHH

dgy\`
a
a
aC  !8!8!8#FlFlmmmF;%;%;%66;%=///=///////////=/////////////6/////6//////=//////////////////////////////////r/   c                     ddl m} m} g fd} | dddd          } |t          d	
          ||          }|j        }d}||u }|st          j        d|fd||f          dt          j                    v st          j	        |          rt          j
        |          ndt          j
        |          t          j
        |          dz  }dd|iz  }	t          t          j        |	                    d x}x}}|j        }d}||k    }|st          j        d|fd||f          dt          j                    v st          j	        |          rt          j
        |          ndt          j
        |          t          j
        |          dz  }dd|iz  }	t          t          j        |	                    d x}x}}d         d         d         }
d}|
|k    }|slt          j        d|fd|
|f          t          j
        |
          t          j
        |          dz  }dd|iz  }t          t          j        |                    d x}
x}}d         d         d         }
d}|
|k    }|slt          j        d|fd|
|f          t          j
        |
          t          j
        |          dz  }dd|iz  }t          t          j        |                    d x}
x}}d}
d         d         }t          |          }|
|v}|st          j        d|fd|
|f          t          j
        |
          dt          j                    v st          j	        t                    rt          j
        t                    ndt          j
        |          t          j
        |          dz  }	d d!|	iz  }t          t          j        |                    d x}
x}x}}d}
d         d         }|
|v }|slt          j        d"|fd#|
|f          t          j
        |
          t          j
        |          dz  }dd|iz  }t          t          j        |                    d x}
x}}d S )$Nr   rF   c                 B                         | ||f           dddidS )NT
message_id{   )rV   rL   )append)urlpayloadtimeoutcallss      r-   fake_senderzLtest_telegram_sender_payload_uses_thread_and_no_secrets.<locals>.fake_senderN   s/    c7G,---|S&9:::r/   Tzsecret-tokenr5   r<   rT   zCrypto Bot ENTRY BTCrI   r[   r   rK   rL   r   r   r   rV   r8   rN      r$   z%(py1)s == %(py4)spy1py4assert %(py6)spy6message_thread_id)not in)z4%(py1)s not in %(py7)s
{%(py7)s = %(py3)s(%(py5)s)
}r   )rl   py3r   r   zassert %(py9)spy9)in)z%(py1)s in %(py4)s)r   r   rG   r   rP   r   r   r   r   r    r!   r"   r#   rQ   r   )r   rG   rh   r   rL   r(   r)   r*   r+   r,   @py_assert0@py_assert2@py_format5@py_format7@py_assert6@py_format10rg   s                   @r-   7test_telegram_sender_payload_uses_thread_and_no_secretsr{   I   sL   HHHHHHHHE; ; ; ; ; 
dnicg
h
h
hC  0F!G!G!GU`aaaF;$;$;$66;$= D =D          =D            6     6      =    D                           8A;y!.Y.!Y..........!Y.....!....Y...........................8A;*+3t3+t3333333333+t33333+3333t3333333333333333333333333331U1Xa[1[!1!11>!11111111111>!111111>1111111111111111111111[1111!111111111111111111111111111111(U1Xa[(>[((((((((((>[(((((>(((([(((((((((((((((((((((((((((((r/   c           	         ddl m} | dz  } ||          }|                    dd           |                    ddddd	          }|                    ddd
ddddi           |d         }d}||k    }|slt	          j        d|fd||f          t	          j        |          t	          j        |          dz  }dd|iz  }	t          t	          j        |	                    d x}x}}|j	        d         d         }d}||k    }|slt	          j        d|fd||f          t	          j        |          t	          j        |          dz  }dd|iz  }	t          t	          j        |	                    d x}x}}|j
        }t          |          }
d}|
|k    }|st	          j        d|fd|
|f          dt          j                    v st	          j        t                    rt	          j        t                    nddt          j                    v st	          j        |          rt	          j        |          ndt	          j        |          t	          j        |
          t	          j        |          dz  }dd|iz  }t          t	          j        |                    d x}x}
x}}|j        } |            }|sdd t          j                    v st	          j        |          rt	          j        |          nd t	          j        |          t	          j        |          d!z  }t          t	          j        |                    d x}}d S )"Nr   PaperExchangepaper_state.json   zBTC/USDC:USDCmarketbuyg      ?g      Y@sellg     [@
reduceOnlyT)paramsstatusrV   r8   rj   rk   rn   ro   BTC	contracts   )zL%(py5)s
{%(py5)s = %(py0)s(%(py3)s
{%(py3)s = %(py1)s.orders
})
} == %(py8)slenexchange)r   rl   rr   r   py8zassert %(py10)spy10zAassert %(py4)s
{%(py4)s = %(py2)s
{%(py2)s = %(py0)s.exists
}()
}path)r   r   rm   )paper_tradingr~   set_leveragecreate_orderr   r   r!   r"   r#   	positionsordersr   r   r   r    exists)r'   r~   r   r   rL   ru   r*   rv   rw   rx   r)   @py_assert7ry   @py_format9@py_format11r(   s                   r-   0test_paper_exchange_records_orders_and_positionsr   ]   s   ++++++((D}T""H!_---""?HeS%PPF/8VS%Q]_cPdeee(#t#t##########t#########t###########################e$[16Q61Q66666666661Q6666616666Q666666666666666666666666666$3$1$1$$$$$$$$$$1$$$$$$$$$$$$3$$$$$3$$$$$$$$$$$$$x$$$$$x$$$$$$$$$$$$$$1$$$$$$$$$$$$$$$$$$$$$$$$$$$$$;;;===44;=r/   c                    ddl m}  || dz            }|                    ddddd           |                                }dddd	d
dg}||k    }|st	          j        d|fd||f          dt          j                    v st	          j        |          rt	          j	        |          ndt	          j	        |          dz  }dd|iz  }t          t	          j        |                    d x}}d S )Nr   r}   r   zETH/USDC:USDCr   r   g       @g      I@ri   long)symbolr   
entryPriceleveragesider8   )z%(py0)s == %(py3)sr   )r   rr   zassert %(py5)sr   )r   r~   r   fetch_positionsr   r   r   r   r    r!   r"   r#   )r'   r~   r   r   rv   r(   @py_format4r+   s           r-   6test_paper_exchange_fetch_positions_matches_ccxt_shaper   m   s   ++++++}X(::;;H/8UCFFF((**I "   9         9                                               r/   )builtinsr   _pytest.assertion.rewrite	assertionrewriter   pathlibr   alertingr   r.   r?   rD   rR   r^   r{   r   r   rW   r/   r-   <module>r      s                             	! 	! 	!! ! ! ! ! !&' ' '0 0 0) ) )(       r/   