Files
ewoooc/tests/test_telegram_update_guard.py
OoO 1a886d962b
All checks were successful
CD Pipeline / deploy (push) Successful in 8m50s
fix(telegram): dedupe webhook+polling updates via shared DB guard
Webhook (Flask) and polling (momo-telegram-bot) consumed the same
Telegram update_id, causing /menu callbacks to fire twice. Add a
shared dedup module backed by telegram_update_dedup table (300s TTL,
60s cleanup) with in-memory fallback, wired into both paths.

Polling launcher now skips startup when webhook is configured to
prevent dual-consumption at the source.

38 tests across webhook, menu keyboards, telegram_api, dedup guard,
and trend bot service.

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
2026-05-02 12:01:04 +08:00

27 lines
880 B
Python
Raw Permalink Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
from time import time
def test_update_guard_detects_duplicate_key():
from services import telegram_update_guard as guard
# 使用可變動 key避免被歷史資料干擾
unique = f"unit-test-{int(time() * 1000)}"
# 清掉本機快取,避免測試順序影響
guard._seen_update_ids.clear()
guard._seen_update_id_set.clear()
assert guard.is_duplicate_update(unique, namespace="pytest") is False
assert guard.is_duplicate_update(unique, namespace="pytest") is True
def test_update_guard_separates_namespace():
from services import telegram_update_guard as guard
guard._seen_update_ids.clear()
guard._seen_update_id_set.clear()
event_id = f"namespace-check-{int(time() * 1000)}"
assert guard.is_duplicate_update(event_id, namespace="a") is False
assert guard.is_duplicate_update(event_id, namespace="b") is False