pybinbot 2.1.4__tar.gz → 2.1.7__tar.gz

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (84) hide show
  1. {pybinbot-2.1.4/pybinbot.egg-info → pybinbot-2.1.7}/PKG-INFO +1 -1
  2. {pybinbot-2.1.4 → pybinbot-2.1.7}/pybinbot/__init__.py +8 -0
  3. {pybinbot-2.1.4 → pybinbot-2.1.7}/pybinbot/apis/binbot/base.py +9 -0
  4. {pybinbot-2.1.4 → pybinbot-2.1.7}/pybinbot/models/autotrade_settings.py +0 -4
  5. pybinbot-2.1.7/pybinbot/models/fastest_movers.py +26 -0
  6. {pybinbot-2.1.4 → pybinbot-2.1.7}/pybinbot/shared/telegram_delivery.py +57 -28
  7. {pybinbot-2.1.4 → pybinbot-2.1.7/pybinbot.egg-info}/PKG-INFO +1 -1
  8. {pybinbot-2.1.4 → pybinbot-2.1.7}/pybinbot.egg-info/SOURCES.txt +2 -0
  9. {pybinbot-2.1.4 → pybinbot-2.1.7}/pyproject.toml +1 -1
  10. pybinbot-2.1.7/tests/test_fastest_movers.py +89 -0
  11. {pybinbot-2.1.4 → pybinbot-2.1.7}/tests/test_telegram_delivery.py +62 -0
  12. {pybinbot-2.1.4 → pybinbot-2.1.7}/LICENSE +0 -0
  13. {pybinbot-2.1.4 → pybinbot-2.1.7}/MANIFEST.in +0 -0
  14. {pybinbot-2.1.4 → pybinbot-2.1.7}/README.md +0 -0
  15. {pybinbot-2.1.4 → pybinbot-2.1.7}/pybinbot/apis/binance/base.py +0 -0
  16. {pybinbot-2.1.4 → pybinbot-2.1.7}/pybinbot/apis/binance/exceptions.py +0 -0
  17. {pybinbot-2.1.4 → pybinbot-2.1.7}/pybinbot/apis/binbot/exceptions.py +0 -0
  18. {pybinbot-2.1.4 → pybinbot-2.1.7}/pybinbot/apis/coingecko.py +0 -0
  19. {pybinbot-2.1.4 → pybinbot-2.1.7}/pybinbot/apis/kucoin/base.py +0 -0
  20. {pybinbot-2.1.4 → pybinbot-2.1.7}/pybinbot/apis/kucoin/exceptions.py +0 -0
  21. {pybinbot-2.1.4 → pybinbot-2.1.7}/pybinbot/apis/kucoin/futures.py +0 -0
  22. {pybinbot-2.1.4 → pybinbot-2.1.7}/pybinbot/apis/kucoin/market.py +0 -0
  23. {pybinbot-2.1.4 → pybinbot-2.1.7}/pybinbot/apis/kucoin/orders.py +0 -0
  24. {pybinbot-2.1.4 → pybinbot-2.1.7}/pybinbot/apis/kucoin/rest.py +0 -0
  25. {pybinbot-2.1.4 → pybinbot-2.1.7}/pybinbot/models/__init__.py +0 -0
  26. {pybinbot-2.1.4 → pybinbot-2.1.7}/pybinbot/models/bot.py +0 -0
  27. {pybinbot-2.1.4 → pybinbot-2.1.7}/pybinbot/models/bot_base.py +0 -0
  28. {pybinbot-2.1.4 → pybinbot-2.1.7}/pybinbot/models/cvd.py +0 -0
  29. {pybinbot-2.1.4 → pybinbot-2.1.7}/pybinbot/models/deal.py +0 -0
  30. {pybinbot-2.1.4 → pybinbot-2.1.7}/pybinbot/models/derivatives.py +0 -0
  31. {pybinbot-2.1.4 → pybinbot-2.1.7}/pybinbot/models/grid_ladder.py +0 -0
  32. {pybinbot-2.1.4 → pybinbot-2.1.7}/pybinbot/models/order.py +0 -0
  33. {pybinbot-2.1.4 → pybinbot-2.1.7}/pybinbot/models/routes.py +0 -0
  34. {pybinbot-2.1.4 → pybinbot-2.1.7}/pybinbot/models/signals.py +0 -0
  35. {pybinbot-2.1.4 → pybinbot-2.1.7}/pybinbot/models/symbol.py +0 -0
  36. {pybinbot-2.1.4 → pybinbot-2.1.7}/pybinbot/py.typed +0 -0
  37. {pybinbot-2.1.4 → pybinbot-2.1.7}/pybinbot/shared/__init__.py +0 -0
  38. {pybinbot-2.1.4 → pybinbot-2.1.7}/pybinbot/shared/beta.py +0 -0
  39. {pybinbot-2.1.4 → pybinbot-2.1.7}/pybinbot/shared/breadth.py +0 -0
  40. {pybinbot-2.1.4 → pybinbot-2.1.7}/pybinbot/shared/cache.py +0 -0
  41. {pybinbot-2.1.4 → pybinbot-2.1.7}/pybinbot/shared/candles.py +0 -0
  42. {pybinbot-2.1.4 → pybinbot-2.1.7}/pybinbot/shared/enums.py +0 -0
  43. {pybinbot-2.1.4 → pybinbot-2.1.7}/pybinbot/shared/handlers.py +0 -0
  44. {pybinbot-2.1.4 → pybinbot-2.1.7}/pybinbot/shared/heikin_ashi.py +0 -0
  45. {pybinbot-2.1.4 → pybinbot-2.1.7}/pybinbot/shared/indicators.py +0 -0
  46. {pybinbot-2.1.4 → pybinbot-2.1.7}/pybinbot/shared/logging_config.py +0 -0
  47. {pybinbot-2.1.4 → pybinbot-2.1.7}/pybinbot/shared/maths.py +0 -0
  48. {pybinbot-2.1.4 → pybinbot-2.1.7}/pybinbot/shared/price_crossings.py +0 -0
  49. {pybinbot-2.1.4 → pybinbot-2.1.7}/pybinbot/shared/timestamps.py +0 -0
  50. {pybinbot-2.1.4 → pybinbot-2.1.7}/pybinbot/shared/types.py +0 -0
  51. {pybinbot-2.1.4 → pybinbot-2.1.7}/pybinbot/shared/utils.py +0 -0
  52. {pybinbot-2.1.4 → pybinbot-2.1.7}/pybinbot/streaming/binance/async_socket_client.py +0 -0
  53. {pybinbot-2.1.4 → pybinbot-2.1.7}/pybinbot/streaming/binance/socket_client.py +0 -0
  54. {pybinbot-2.1.4 → pybinbot-2.1.7}/pybinbot/streaming/binance/socket_manager.py +0 -0
  55. {pybinbot-2.1.4 → pybinbot-2.1.7}/pybinbot/streaming/kucoin/kucoin_async_client.py +0 -0
  56. {pybinbot-2.1.4 → pybinbot-2.1.7}/pybinbot.egg-info/dependency_links.txt +0 -0
  57. {pybinbot-2.1.4 → pybinbot-2.1.7}/pybinbot.egg-info/requires.txt +0 -0
  58. {pybinbot-2.1.4 → pybinbot-2.1.7}/pybinbot.egg-info/top_level.txt +0 -0
  59. {pybinbot-2.1.4 → pybinbot-2.1.7}/setup.cfg +0 -0
  60. {pybinbot-2.1.4 → pybinbot-2.1.7}/tests/test_beta.py +0 -0
  61. {pybinbot-2.1.4 → pybinbot-2.1.7}/tests/test_binbot_api.py +0 -0
  62. {pybinbot-2.1.4 → pybinbot-2.1.7}/tests/test_bot_futures_leverage.py +0 -0
  63. {pybinbot-2.1.4 → pybinbot-2.1.7}/tests/test_breadth.py +0 -0
  64. {pybinbot-2.1.4 → pybinbot-2.1.7}/tests/test_candles.py +0 -0
  65. {pybinbot-2.1.4 → pybinbot-2.1.7}/tests/test_closed_bot_status.py +0 -0
  66. {pybinbot-2.1.4 → pybinbot-2.1.7}/tests/test_coingecko.py +0 -0
  67. {pybinbot-2.1.4 → pybinbot-2.1.7}/tests/test_conditional_entry.py +0 -0
  68. {pybinbot-2.1.4 → pybinbot-2.1.7}/tests/test_conditional_futures_orders.py +0 -0
  69. {pybinbot-2.1.4 → pybinbot-2.1.7}/tests/test_cvd_api.py +0 -0
  70. {pybinbot-2.1.4 → pybinbot-2.1.7}/tests/test_deal_model.py +0 -0
  71. {pybinbot-2.1.4 → pybinbot-2.1.7}/tests/test_derivatives_api.py +0 -0
  72. {pybinbot-2.1.4 → pybinbot-2.1.7}/tests/test_futures_place_order.py +0 -0
  73. {pybinbot-2.1.4 → pybinbot-2.1.7}/tests/test_grid_ladder.py +0 -0
  74. {pybinbot-2.1.4 → pybinbot-2.1.7}/tests/test_handlers.py +0 -0
  75. {pybinbot-2.1.4 → pybinbot-2.1.7}/tests/test_heikin_ashi.py +0 -0
  76. {pybinbot-2.1.4 → pybinbot-2.1.7}/tests/test_indicators.py +0 -0
  77. {pybinbot-2.1.4 → pybinbot-2.1.7}/tests/test_kucoin_api.py +0 -0
  78. {pybinbot-2.1.4 → pybinbot-2.1.7}/tests/test_logging_config.py +0 -0
  79. {pybinbot-2.1.4 → pybinbot-2.1.7}/tests/test_matching_engine.py +0 -0
  80. {pybinbot-2.1.4 → pybinbot-2.1.7}/tests/test_maths.py +0 -0
  81. {pybinbot-2.1.4 → pybinbot-2.1.7}/tests/test_price_crossings.py +0 -0
  82. {pybinbot-2.1.4 → pybinbot-2.1.7}/tests/test_symbol_model.py +0 -0
  83. {pybinbot-2.1.4 → pybinbot-2.1.7}/tests/test_timestamps.py +0 -0
  84. {pybinbot-2.1.4 → pybinbot-2.1.7}/tests/test_utils.py +0 -0
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.4
2
2
  Name: pybinbot
3
- Version: 2.1.4
3
+ Version: 2.1.7
4
4
  Summary: Utility functions for the binbot project.
5
5
  Author-email: Carlos Wu <carkodw@gmail.com>
6
6
  Requires-Python: >=3.15
@@ -121,6 +121,11 @@ from pybinbot.models.signals import (
121
121
  )
122
122
  from pybinbot.models.symbol import SymbolModel
123
123
  from pybinbot.models.cvd import CvdAggregateResponse, CvdPoint, CvdSymbolResponse
124
+ from pybinbot.models.fastest_movers import (
125
+ FastestMoverEntry,
126
+ FastestMoversResponse,
127
+ FastestMoversSnapshot,
128
+ )
124
129
  from pybinbot.models.routes import (
125
130
  GainerLoserEntry,
126
131
  GainersLosersSeriesResponse,
@@ -211,6 +216,9 @@ __all__ = [
211
216
  "GainerLoserEntry",
212
217
  "GainersLosersSeriesResponse",
213
218
  "GainersLosersSnapshot",
219
+ "FastestMoverEntry",
220
+ "FastestMoversResponse",
221
+ "FastestMoversSnapshot",
214
222
  "MarketBreadthSeries",
215
223
  "MarketBreadthSeriesResponse",
216
224
  "CvdPoint",
@@ -29,6 +29,7 @@ from pybinbot import (
29
29
  from datetime import datetime, timezone
30
30
  from dateutil.parser import parse
31
31
  from pybinbot.models.cvd import CvdAggregateResponse, CvdSymbolResponse
32
+ from pybinbot.models.fastest_movers import FastestMoversResponse, FastestMoversSnapshot
32
33
 
33
34
  logger = logging.getLogger(__name__)
34
35
 
@@ -81,6 +82,7 @@ class BinbotApi:
81
82
  self.bb_gainers_losers_series_url = (
82
83
  f"{bb_base_url}/charts/gainers-losers-series"
83
84
  )
85
+ self.bb_fastest_movers_url = f"{bb_base_url}/charts/fastest_movers"
84
86
  self.bb_signals_url = f"{bb_base_url}/signals"
85
87
  self.bb_grid_ladders_url = f"{bb_base_url}/grid-ladders"
86
88
  self.bb_grid_ladder_calculate_url = f"{bb_base_url}/grid-ladders/calculate"
@@ -306,6 +308,13 @@ class BinbotApi:
306
308
  return []
307
309
  return GainersLosersSeriesResponse.model_validate(response).data
308
310
 
311
+ async def get_fastest_movers(self, limit: int = 10) -> FastestMoversSnapshot:
312
+ """Return one-hour price-move rankings with three-hour and 24h context."""
313
+ response = await self.fetch(
314
+ url=self.bb_fastest_movers_url, params={"limit": limit}
315
+ )
316
+ return FastestMoversResponse.model_validate(response).data
317
+
309
318
  async def get_cvd_symbol(self, symbol: str, limit: int = 15) -> CvdSymbolResponse:
310
319
  """Read oldest-first one-minute contract CVD, including coverage flags.
311
320
 
@@ -4,7 +4,6 @@ from pydantic import BaseModel, ConfigDict, Field
4
4
  from pybinbot.models.routes import StandardResponse
5
5
  from pybinbot.shared.enums import (
6
6
  AutotradeSettingsDocument,
7
- BinanceKlineIntervals,
8
7
  CloseConditions,
9
8
  ExchangeId,
10
9
  )
@@ -12,9 +11,6 @@ from pybinbot.shared.enums import (
12
11
 
13
12
  class AutotradeSettingsSchema(BaseModel):
14
13
  id: AutotradeSettingsDocument = AutotradeSettingsDocument.settings
15
- candlestick_interval: BinanceKlineIntervals = Field(
16
- default=BinanceKlineIntervals.fifteen_minutes,
17
- )
18
14
  close_condition: CloseConditions = Field(
19
15
  default=CloseConditions.dynamic_trailing,
20
16
  )
@@ -0,0 +1,26 @@
1
+ """One-hour KuCoin futures rankings served by Binbot's charts API."""
2
+
3
+ from pydantic import AwareDatetime, BaseModel
4
+
5
+ from pybinbot.models.routes import StandardResponse
6
+
7
+
8
+ class FastestMoverEntry(BaseModel):
9
+ symbol: str
10
+ price_change_1h_percent: float
11
+ price_change_3h_percent: float | None = None
12
+ price_change_percent: float
13
+ last_price: float
14
+ turnover_24h: float
15
+
16
+
17
+ class FastestMoversSnapshot(BaseModel):
18
+ source: str
19
+ recorded_at: AwareDatetime | None
20
+ comparison_at: AwareDatetime | None
21
+ gainers: list[FastestMoverEntry]
22
+ losers: list[FastestMoverEntry]
23
+
24
+
25
+ class FastestMoversResponse(StandardResponse):
26
+ data: FastestMoversSnapshot
@@ -1,7 +1,7 @@
1
1
  """Telegram delivery shared by synchronous trading and asynchronous analytics."""
2
2
 
3
3
  import asyncio
4
- from datetime import timedelta
4
+ from datetime import datetime, timedelta
5
5
  import logging
6
6
  import os
7
7
  import re
@@ -83,35 +83,64 @@ class TelegramDelivery:
83
83
  return retry_after.total_seconds()
84
84
  return float(retry_after)
85
85
 
86
- async def send(self, message: str) -> bool:
86
+ async def send(
87
+ self,
88
+ message: str,
89
+ *,
90
+ expires_at: datetime | None = None,
91
+ retry_on_rate_limit: bool = True,
92
+ ) -> bool:
93
+ """Deliver within an optional deadline, including time spent waiting to send."""
87
94
  if not self.token or not self.chat_id or self.bot is None:
88
95
  return False
89
- async with self._send_lock:
90
- while True:
91
- await self._sleep_for_send_interval()
92
- try:
93
- await self.bot.send_message(
94
- self.chat_id,
95
- text=self._sanitize_html(message),
96
- parse_mode=ParseMode.HTML,
97
- link_preview_options=LinkPreviewOptions(is_disabled=True),
98
- )
99
- self._last_send_at = time.monotonic()
100
- return True
101
- except RetryAfter as e:
102
- retry_after = self._retry_after_seconds(e)
103
- sleep_seconds = retry_after + self._retry_after_pad_seconds
104
- logging.warning(
105
- "Telegram flood control active; retrying message in %.1f seconds",
106
- sleep_seconds,
107
- )
108
- await asyncio.sleep(sleep_seconds)
109
- except TimedOut:
110
- logging.warning("Telegram message timed out; skipping")
111
- return False
112
- except Exception as exc:
113
- logging.warning("Telegram delivery failed (%s)", type(exc).__name__)
114
- return False
96
+ remaining = expires_at.timestamp() - time.time() if expires_at else None
97
+ if remaining is not None and remaining <= 0:
98
+ return False
99
+ try:
100
+ async with asyncio.timeout(remaining):
101
+ async with self._send_lock:
102
+ while True:
103
+ await self._sleep_for_send_interval()
104
+ if (
105
+ expires_at is not None
106
+ and time.time() >= expires_at.timestamp()
107
+ ):
108
+ return False
109
+ try:
110
+ await self.bot.send_message(
111
+ self.chat_id,
112
+ text=self._sanitize_html(message),
113
+ parse_mode=ParseMode.HTML,
114
+ link_preview_options=LinkPreviewOptions(
115
+ is_disabled=True
116
+ ),
117
+ )
118
+ self._last_send_at = time.monotonic()
119
+ return True
120
+ except RetryAfter as e:
121
+ if not retry_on_rate_limit:
122
+ logging.warning(
123
+ "Telegram rate limited; skipping one-shot alert"
124
+ )
125
+ return False
126
+ retry_after = self._retry_after_seconds(e)
127
+ sleep_seconds = retry_after + self._retry_after_pad_seconds
128
+ logging.warning(
129
+ "Telegram flood control active; retrying message in %.1f seconds",
130
+ sleep_seconds,
131
+ )
132
+ await asyncio.sleep(sleep_seconds)
133
+ except TimedOut:
134
+ logging.warning("Telegram message timed out; skipping")
135
+ return False
136
+ except Exception as exc:
137
+ logging.warning(
138
+ "Telegram delivery failed (%s)", type(exc).__name__
139
+ )
140
+ return False
141
+ except TimeoutError:
142
+ logging.info("Telegram alert expired before delivery completed; skipping")
143
+ return False
115
144
 
116
145
  def send_once(self, message: str) -> bool:
117
146
  """Attempt an ephemeral synchronous delivery, without retries or cooldowns."""
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.4
2
2
  Name: pybinbot
3
- Version: 2.1.4
3
+ Version: 2.1.7
4
4
  Summary: Utility functions for the binbot project.
5
5
  Author-email: Carlos Wu <carkodw@gmail.com>
6
6
  Requires-Python: >=3.15
@@ -27,6 +27,7 @@ pybinbot/models/bot_base.py
27
27
  pybinbot/models/cvd.py
28
28
  pybinbot/models/deal.py
29
29
  pybinbot/models/derivatives.py
30
+ pybinbot/models/fastest_movers.py
30
31
  pybinbot/models/grid_ladder.py
31
32
  pybinbot/models/order.py
32
33
  pybinbot/models/routes.py
@@ -64,6 +65,7 @@ tests/test_conditional_futures_orders.py
64
65
  tests/test_cvd_api.py
65
66
  tests/test_deal_model.py
66
67
  tests/test_derivatives_api.py
68
+ tests/test_fastest_movers.py
67
69
  tests/test_futures_place_order.py
68
70
  tests/test_grid_ladder.py
69
71
  tests/test_handlers.py
@@ -22,7 +22,7 @@ build-backend = "setuptools.build_meta"
22
22
 
23
23
  [project]
24
24
  name = "pybinbot"
25
- version = "2.1.4"
25
+ version = "2.1.7"
26
26
  description = "Utility functions for the binbot project."
27
27
  authors = [
28
28
  { name="Carlos Wu", email="carkodw@gmail.com" }
@@ -0,0 +1,89 @@
1
+ from datetime import UTC, datetime
2
+ from unittest.mock import AsyncMock, patch
3
+
4
+ import pytest
5
+ from pydantic import ValidationError
6
+
7
+ from pybinbot import BinbotApi, FastestMoversSnapshot
8
+
9
+
10
+ @pytest.fixture
11
+ def api() -> BinbotApi:
12
+ with patch.object(BinbotApi, "_login_service_account"):
13
+ return BinbotApi("https://example.com", "service@example.com", "password")
14
+
15
+
16
+ @pytest.mark.asyncio
17
+ async def test_fastest_movers_client_returns_typed_rankings_and_context(api: BinbotApi):
18
+ payload = {
19
+ "source": "kucoin_futures",
20
+ "recorded_at": "2026-11-27T12:00:00Z",
21
+ "comparison_at": "2026-11-27T11:00:00Z",
22
+ "gainers": [
23
+ {
24
+ "symbol": "SOLUSDTM",
25
+ "price_change_1h_percent": 4.2,
26
+ "price_change_3h_percent": 7.8,
27
+ "price_change_percent": 12.1,
28
+ "last_price": 150,
29
+ "turnover_24h": 1000000,
30
+ }
31
+ ],
32
+ "losers": [],
33
+ }
34
+ with patch.object(
35
+ api, "fetch", new=AsyncMock(return_value={"message": "OK", "data": payload})
36
+ ) as fetch:
37
+ snapshot = await api.get_fastest_movers(limit=5)
38
+
39
+ fetch.assert_awaited_once_with(
40
+ url="https://example.com/charts/fastest_movers", params={"limit": 5}
41
+ )
42
+ assert isinstance(snapshot, FastestMoversSnapshot)
43
+ assert snapshot.recorded_at == datetime(2026, 11, 27, 12, tzinfo=UTC)
44
+ assert snapshot.gainers[0].price_change_3h_percent == 7.8
45
+ assert snapshot.gainers[0].price_change_percent == 12.1
46
+
47
+
48
+ @pytest.mark.asyncio
49
+ async def test_fastest_movers_preserves_waiting_for_history_state(api: BinbotApi):
50
+ with patch.object(
51
+ api,
52
+ "fetch",
53
+ new=AsyncMock(
54
+ return_value={
55
+ "message": "OK",
56
+ "data": {
57
+ "source": "kucoin_futures",
58
+ "recorded_at": None,
59
+ "comparison_at": None,
60
+ "gainers": [],
61
+ "losers": [],
62
+ },
63
+ }
64
+ ),
65
+ ):
66
+ snapshot = await api.get_fastest_movers()
67
+ assert snapshot.recorded_at is None
68
+ assert snapshot.comparison_at is None
69
+ assert snapshot.gainers == []
70
+
71
+
72
+ @pytest.mark.asyncio
73
+ async def test_fastest_movers_does_not_turn_api_failure_into_empty_membership(
74
+ api: BinbotApi,
75
+ ):
76
+ with patch.object(api, "fetch", new=AsyncMock(return_value={"data": None})):
77
+ with pytest.raises(ValidationError):
78
+ await api.get_fastest_movers()
79
+
80
+
81
+ def test_fastest_movers_rejects_ambiguous_snapshot_timezone():
82
+ with pytest.raises(ValidationError):
83
+ FastestMoversSnapshot(
84
+ source="kucoin_futures",
85
+ recorded_at=datetime(2026, 11, 27, 12),
86
+ comparison_at=None,
87
+ gainers=[],
88
+ losers=[],
89
+ )
@@ -1,3 +1,5 @@
1
+ import asyncio
2
+ from datetime import UTC, datetime, timedelta
1
3
  from unittest.mock import AsyncMock, patch
2
4
 
3
5
  import pytest
@@ -85,3 +87,63 @@ async def test_async_delivery_keeps_binquant_flood_control_retry():
85
87
  )
86
88
  assert await delivery.send("Signal") is True
87
89
  assert bot.return_value.send_message.await_count == 2
90
+
91
+
92
+ @pytest.mark.asyncio
93
+ async def test_one_shot_alert_does_not_retry_rate_limited_send():
94
+ with patch("pybinbot.shared.telegram_delivery.Bot") as bot:
95
+ bot.return_value.send_message = AsyncMock(side_effect=RetryAfter(60))
96
+ delivery = TelegramDelivery(token="test-token", chat_id="test-chat")
97
+ assert await delivery.send("Signal", retry_on_rate_limit=False) is False
98
+ bot.return_value.send_message.assert_awaited_once()
99
+
100
+
101
+ @pytest.mark.asyncio
102
+ async def test_expired_alert_is_never_submitted_to_telegram():
103
+ with patch("pybinbot.shared.telegram_delivery.Bot") as bot:
104
+ bot.return_value.send_message = AsyncMock()
105
+ delivery = TelegramDelivery(token="test-token", chat_id="test-chat")
106
+ assert (
107
+ await delivery.send(
108
+ "Signal", expires_at=datetime.now(UTC) - timedelta(seconds=1)
109
+ )
110
+ is False
111
+ )
112
+ bot.return_value.send_message.assert_not_awaited()
113
+
114
+
115
+ @pytest.mark.asyncio
116
+ async def test_alert_expires_while_waiting_for_another_send():
117
+ with patch("pybinbot.shared.telegram_delivery.Bot") as bot:
118
+ bot.return_value.send_message = AsyncMock()
119
+ delivery = TelegramDelivery(token="test-token", chat_id="test-chat")
120
+ async with delivery._send_lock:
121
+ assert (
122
+ await delivery.send(
123
+ "Signal", expires_at=datetime.now(UTC) + timedelta(milliseconds=20)
124
+ )
125
+ is False
126
+ )
127
+ bot.return_value.send_message.assert_not_awaited()
128
+
129
+
130
+ @pytest.mark.asyncio
131
+ async def test_delivery_deadline_cancels_a_stalled_request():
132
+ with patch("pybinbot.shared.telegram_delivery.Bot") as bot:
133
+ cancelled = asyncio.Event()
134
+
135
+ async def stalled_send(*args, **kwargs):
136
+ try:
137
+ await asyncio.Event().wait()
138
+ finally:
139
+ cancelled.set()
140
+
141
+ bot.return_value.send_message = AsyncMock(side_effect=stalled_send)
142
+ delivery = TelegramDelivery(token="test-token", chat_id="test-chat")
143
+ assert (
144
+ await delivery.send(
145
+ "Signal", expires_at=datetime.now(UTC) + timedelta(milliseconds=20)
146
+ )
147
+ is False
148
+ )
149
+ assert cancelled.is_set()
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes