Spaces:
Running on Zero
Running on Zero
File size: 3,741 Bytes
e12fc65 | 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 | """Unit tests for the Telegram B2C bot: catalog rendering, buy flow, purchase log.
Tests cover:
- /catalog text contains both packages and prices
- /buy with a valid package logs the purchase and returns a payment link
- /buy with an unknown package returns an error and does not log
- polling wrapper module imports cleanly and exposes handlers
"""
import importlib
import json
import sys
import tempfile
import unittest
from pathlib import Path
# Ensure scripts dir is importable
SCRIPTS = Path(__file__).resolve().parents[2] / "moldovan-qwen" / "scripts"
if str(SCRIPTS) not in sys.path:
sys.path.insert(0, str(SCRIPTS))
from telegram_bot import TelegramBeatsBot # noqa: E402
class TestTelegramBot(unittest.TestCase):
def setUp(self):
self.tmp = tempfile.TemporaryDirectory()
self.catalog_path = Path(self.tmp.name) / "b2c_catalog.json"
self.catalog_path.write_text(
json.dumps(
{
"packages": [
{
"name": "Starter",
"price_usd": 19,
"description": "Weekly beats",
"features": ["MP3"],
},
{
"name": "Pro",
"price_usd": 49,
"description": "All access",
"features": ["Stems"],
},
]
}
),
encoding="utf-8",
)
# Redirect purchase log to tmp dir
self.bot = TelegramBeatsBot(token="TEST", catalog_file=self.catalog_path)
self.bot.purchases_log = Path(self.tmp.name) / "purchases_log.jsonl"
def tearDown(self):
self.tmp.cleanup()
def test_catalog_text_lists_packages_and_prices(self):
text = self.bot.get_catalog_text()
self.assertIn("Starter", text)
self.assertIn("$19", text)
self.assertIn("Pro", text)
self.assertIn("$49", text)
def test_buy_valid_package_logs_purchase_and_returns_link(self):
response = self.bot.handle_buy("Pro")
self.assertIn("Pro", response)
self.assertIn("https://", response)
log_content = self.bot.purchases_log.read_text(encoding="utf-8")
self.assertIn('"package": "Pro"', log_content)
self.assertIn('"price_usd": 49', log_content)
def test_buy_unknown_package_does_not_log(self):
response = self.bot.handle_buy("Nonexistent")
self.assertIn("nu a fost găsit", response)
self.assertFalse(self.bot.purchases_log.exists())
class TestPollingWrapper(unittest.TestCase):
def test_polling_module_imports(self):
try:
mod = importlib.import_module("telegram_bot_polling")
except ModuleNotFoundError:
self.skipTest("python-telegram-bot not installed in test env")
return
self.assertTrue(hasattr(mod, "start_handler"))
self.assertTrue(hasattr(mod, "catalog_handler"))
self.assertTrue(hasattr(mod, "buy_handler"))
self.assertTrue(callable(mod._create_payment_url))
# Placeholder URL path (no STRIPE_SECRET_KEY set)
url = mod._create_payment_url(19)
self.assertTrue(url.startswith("https://"))
def test_placeholder_url_contains_price(self):
try:
mod = importlib.import_module("telegram_bot_polling")
except ModuleNotFoundError:
self.skipTest("python-telegram-bot not installed in test env")
return
url = mod._create_payment_url(49)
self.assertIn("49", url)
if __name__ == "__main__":
unittest.main() |