Files
wehub-resource-sync 4b6817381b
CI (OpenClaw E2E) / openclaw test (push) Has been cancelled
CI / coverage-report (push) Has been cancelled
CI / test-kubernetes (push) Has been cancelled
CI / should-run-thorough (push) Has been cancelled
CI / test-thorough (cloudwatch-demo) (push) Has been cancelled
CI / test-thorough (flink-ecs) (push) Has been cancelled
CI / test-thorough (upstream-lambda) (push) Has been cancelled
CI / test-thorough (prefect-ecs-fargate) (push) Has been cancelled
Release / build-binaries (zip, opensre.exe, onefile, windows-latest, windows-x64) (push) Has been cancelled
Benchmark image — build + push to ECR (any adapter) / build + push (push) Has been cancelled
CI / quality (ubuntu-latest) (push) Has been cancelled
CI / test (tools-runtime) (push) Has been cancelled
CI / test (e2e-general) (push) Has been cancelled
CI / test (cli-runtime) (push) Has been cancelled
CI / test (e2e-provider-and-openclaw) (push) Has been cancelled
CI / test (integrations-and-misc) (push) Has been cancelled
Release / verify (push) Has been cancelled
Release / build-python-dist (push) Has been cancelled
Release / build-binaries (tar.gz, opensre, onedir, macos-15-intel, darwin-x64) (push) Has been cancelled
Release / build-binaries (tar.gz, opensre, onedir, macos-latest, darwin-arm64) (push) Has been cancelled
Release / build-binaries (tar.gz, opensre, onedir, ubuntu-22.04, linux-x64) (push) Has been cancelled
Release / publish-release (push) Has been cancelled
Release / publish-main-release (push) Has been cancelled
Interactive Shell Live (PR + post-merge) / turn-checks (no-LLM) (push) Has been cancelled
CodeQL / Analyze (python) (push) Has been cancelled
Interactive Shell Live (PR + post-merge) / turn-live shard ${{ matrix.shard_index }} (push) Has been cancelled
Release / prepare (push) Has been cancelled
Release / build-binaries (tar.gz, opensre, onedir, ubuntu-22.04-arm, linux-arm64) (push) Has been cancelled
Synthetic Deterministic Tests / Synthetic offline (deterministic) (push) Has been cancelled
chore: import upstream snapshot with attribution
2026-07-13 13:10:45 +08:00

94 lines
2.9 KiB
Python

from __future__ import annotations
from unittest.mock import MagicMock, patch
import httpx
from gateway.polling.telegram_poller.poller import TelegramPoller, _decode_telegram_response
def test_decode_telegram_response_parses_non_200_json() -> None:
response = httpx.Response(
409,
json={
"ok": False,
"error_code": 409,
"description": "Conflict: terminated by other getUpdates request",
},
)
data = _decode_telegram_response(response)
assert data["ok"] is False
assert data["error_code"] == 409
@patch("gateway.polling.telegram_poller.poller.time.sleep")
@patch("gateway.polling.telegram_poller.poller.httpx.get")
def test_poll_once_conflict_is_debug_not_warning(
mock_get: MagicMock,
mock_sleep: MagicMock,
caplog: object,
) -> None:
import logging
caplog.set_level(logging.DEBUG, logger="gateway.polling.telegram_poller.poller")
mock_get.return_value = httpx.Response(
409,
json={
"ok": False,
"error_code": 409,
"description": "Conflict: terminated by other getUpdates request",
},
)
poller = TelegramPoller("tok")
assert poller.poll_once() == []
mock_sleep.assert_called_once_with(2.0)
assert not any(
"[telegram-gateway] getUpdates not ok" in record.message for record in caplog.records
)
@patch("gateway.polling.telegram_poller.poller.time.sleep")
@patch("gateway.polling.telegram_poller.poller.httpx.get")
def test_poll_once_success_resets_conflict_backoff(
mock_get: MagicMock, _mock_sleep: MagicMock
) -> None:
mock_get.side_effect = [
httpx.Response(
409,
json={"ok": False, "error_code": 409, "description": "conflict"},
),
httpx.Response(200, json={"ok": True, "result": []}),
]
poller = TelegramPoller("tok")
poller._conflict_backoff_seconds = 8.0
assert poller.poll_once() == []
assert poller.poll_once() == []
assert poller._conflict_backoff_seconds == 2.0
@patch("gateway.polling.telegram_poller.poller.time.sleep")
@patch("gateway.polling.telegram_poller.poller.httpx.get")
def test_poll_once_parses_inbound_message(mock_get: MagicMock, mock_sleep: MagicMock) -> None:
mock_get.return_value = httpx.Response(
200,
json={
"ok": True,
"result": [
{
"update_id": 7,
"message": {
"message_id": 11,
"from": {"id": 42},
"chat": {"id": 99, "type": "private"},
"text": "hello",
},
}
],
},
)
events = TelegramPoller("tok").poll_once()
assert len(events) == 1
assert events[0].text == "hello"
assert events[0].chat_id == "99"
mock_sleep.assert_not_called()