1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
|
"""Tests for Telegram notification service."""
import uuid
from decimal import Decimal
from unittest.mock import AsyncMock, MagicMock, patch
import pytest
from shared.models import Signal, Order, OrderSide, OrderType, OrderStatus, Position
from shared.notifier import TelegramNotifier
class TestTelegramNotifierEnabled:
"""Test the enabled property."""
def test_disabled_when_no_token(self):
notifier = TelegramNotifier(bot_token="", chat_id="123")
assert notifier.enabled is False
def test_enabled_with_token(self):
notifier = TelegramNotifier(bot_token="fake-token", chat_id="123")
assert notifier.enabled is True
def test_disabled_when_token_is_empty_string(self):
notifier = TelegramNotifier(bot_token="", chat_id="")
assert notifier.enabled is False
class TestTelegramNotifierSend:
"""Test send method."""
@pytest.mark.asyncio
async def test_send_does_nothing_when_disabled(self):
notifier = TelegramNotifier(bot_token="", chat_id="123")
# Should not raise or do anything
await notifier.send("test message")
@pytest.mark.asyncio
async def test_send_posts_to_api(self):
notifier = TelegramNotifier(bot_token="fake-token", chat_id="12345")
mock_response = AsyncMock()
mock_response.status = 200
mock_response.json = AsyncMock(return_value={"ok": True})
mock_response.__aenter__ = AsyncMock(return_value=mock_response)
mock_response.__aexit__ = AsyncMock(return_value=False)
mock_session = AsyncMock()
mock_session.post = MagicMock(return_value=mock_response)
with patch.object(notifier, "_session", mock_session):
await notifier.send("Hello, world!")
mock_session.post.assert_called_once()
call_args = mock_session.post.call_args
assert "fake-token" in call_args[0][0]
assert call_args[1]["json"]["chat_id"] == "12345"
assert call_args[1]["json"]["text"] == "Hello, world!"
assert call_args[1]["json"]["parse_mode"] == "HTML"
@pytest.mark.asyncio
async def test_send_with_custom_parse_mode(self):
notifier = TelegramNotifier(bot_token="fake-token", chat_id="12345")
mock_response = AsyncMock()
mock_response.status = 200
mock_response.json = AsyncMock(return_value={"ok": True})
mock_response.__aenter__ = AsyncMock(return_value=mock_response)
mock_response.__aexit__ = AsyncMock(return_value=False)
mock_session = AsyncMock()
mock_session.post = MagicMock(return_value=mock_response)
with patch.object(notifier, "_session", mock_session):
await notifier.send("test", parse_mode="Markdown")
call_args = mock_session.post.call_args
assert call_args[1]["json"]["parse_mode"] == "Markdown"
class TestTelegramNotifierFormatters:
"""Test message formatting methods."""
@pytest.mark.asyncio
async def test_send_signal_formats_message(self):
notifier = TelegramNotifier(bot_token="fake-token", chat_id="123")
signal = Signal(
strategy="rsi_strategy",
symbol="BTCUSDT",
side=OrderSide.BUY,
price=Decimal("50000.00"),
quantity=Decimal("0.01"),
reason="RSI oversold",
)
with patch.object(notifier, "send", new_callable=AsyncMock) as mock_send:
await notifier.send_signal(signal)
mock_send.assert_called_once()
msg = mock_send.call_args[0][0]
assert "BUY" in msg
assert "rsi_strategy" in msg
assert "BTCUSDT" in msg
assert "50000.00" in msg
assert "0.01" in msg
assert "RSI oversold" in msg
@pytest.mark.asyncio
async def test_send_order_formats_message(self):
notifier = TelegramNotifier(bot_token="fake-token", chat_id="123")
order = Order(
signal_id=str(uuid.uuid4()),
symbol="ETHUSDT",
side=OrderSide.SELL,
type=OrderType.LIMIT,
price=Decimal("3000.50"),
quantity=Decimal("1.5"),
status=OrderStatus.FILLED,
)
with patch.object(notifier, "send", new_callable=AsyncMock) as mock_send:
await notifier.send_order(order)
mock_send.assert_called_once()
msg = mock_send.call_args[0][0]
assert "FILLED" in msg
assert "ETHUSDT" in msg
assert "SELL" in msg
assert "3000.50" in msg
assert "1.5" in msg
@pytest.mark.asyncio
async def test_send_error_formats_message(self):
notifier = TelegramNotifier(bot_token="fake-token", chat_id="123")
with patch.object(notifier, "send", new_callable=AsyncMock) as mock_send:
await notifier.send_error("Connection failed", service="executor")
mock_send.assert_called_once()
msg = mock_send.call_args[0][0]
assert "Connection failed" in msg
assert "executor" in msg
@pytest.mark.asyncio
async def test_send_daily_summary_formats_message(self):
notifier = TelegramNotifier(bot_token="fake-token", chat_id="123")
positions = [
Position(
symbol="BTCUSDT",
quantity=Decimal("0.1"),
avg_entry_price=Decimal("50000"),
current_price=Decimal("51000"),
),
]
with patch.object(notifier, "send", new_callable=AsyncMock) as mock_send:
await notifier.send_daily_summary(
positions=positions,
total_value=Decimal("5100.00"),
daily_pnl=Decimal("100.00"),
)
mock_send.assert_called_once()
msg = mock_send.call_args[0][0]
assert "BTCUSDT" in msg
assert "5100.00" in msg
assert "100.00" in msg
class TestTelegramNotifierClose:
"""Test close method."""
@pytest.mark.asyncio
async def test_close_closes_session(self):
notifier = TelegramNotifier(bot_token="fake-token", chat_id="123")
mock_session = AsyncMock()
notifier._session = mock_session
await notifier.close()
mock_session.close.assert_called_once()
|