Fix #4: Extract send() to module-level send_telegram_message() for testability #19

Merged
shoko merged 1 commits from fix/issue-4-telegram-token-refactor into master 2026-03-25 12:17:01 +01:00
Owner

Summary

Refactor the nested send() function into a module-level send_telegram_message() to enable unit testing.

Changes

  • scripts/browse.py: Extract send()send_telegram_message(bot_token, chat_id, text, timeout=10)
  • tests/test_browse.py: 8 unit tests covering success, API errors, network errors, timeout, etc.
  • .gitignore: +.worktrees/

Test Results

  • 8 unit tests:
  • Integration tests (plain text, HTML, invalid token):

Fixes

Ref: #4

## Summary Refactor the nested `send()` function into a module-level `send_telegram_message()` to enable unit testing. ## Changes - **`scripts/browse.py`**: Extract `send()` → `send_telegram_message(bot_token, chat_id, text, timeout=10)` - **`tests/test_browse.py`**: 8 unit tests covering success, API errors, network errors, timeout, etc. - **`.gitignore`**: +`.worktrees/` ## Test Results - 8 unit tests: ✅ - Integration tests (plain text, HTML, invalid token): ✅ ## Fixes Ref: #4
shoko added 1 commit 2026-03-25 12:11:17 +01:00
Extract the nested send() function into a module-level
send_telegram_message(bot_token, chat_id, text, timeout=10)
function. This enables unit testing without hitting the real
Telegram API.

Changes:
- Add send_telegram_message() at module level in TELEGRAM section
- Replace nested send() with thin wrapper that calls
  send_telegram_message()
- Update argparse --telegram help text to use TELEGRAM_BOT_TOKEN
- Add tests/test_browse.py with 8 unit tests covering:
  - Success case (returns message_id)
  - API error (RuntimeError)
  - Invalid token (HTTPError 404)
  - Rate limit (HTTPError 429)
  - Network error (URLError)
  - Timeout (URLError)
  - Custom timeout parameter
  - HTML parse_mode in request

Ref: #4
Collaborator

lgtm

lgtm
shoko merged commit 2703b942c1 into master 2026-03-25 12:17:01 +01:00
Sign in to join this conversation.