|
| 1 | +from types import SimpleNamespace |
| 2 | +from unittest.mock import AsyncMock, patch |
| 3 | + |
| 4 | +import pytest |
| 5 | + |
| 6 | +from src.tgbot.handlers.treasury.constants import TrxType |
| 7 | +from src.tgbot.handlers.treasury.giveaway import handle_giveaway |
| 8 | + |
| 9 | + |
| 10 | +def _make_update(user_id: int = 90203): |
| 11 | + return SimpleNamespace(effective_user=SimpleNamespace(id=user_id)) |
| 12 | + |
| 13 | + |
| 14 | +def _make_context(): |
| 15 | + return SimpleNamespace(bot=AsyncMock()) |
| 16 | + |
| 17 | + |
| 18 | +@pytest.mark.asyncio |
| 19 | +async def test_channel_audience_giveaway_uses_tagged_ten_burger_reward(): |
| 20 | + update = _make_update() |
| 21 | + context = _make_context() |
| 22 | + deep_link = "giveaway_channel_audience_2026_05_25" |
| 23 | + |
| 24 | + with ( |
| 25 | + patch( |
| 26 | + "src.tgbot.handlers.treasury.giveaway.pay_if_not_paid", |
| 27 | + new_callable=AsyncMock, |
| 28 | + return_value=10, |
| 29 | + ) as pay, |
| 30 | + patch( |
| 31 | + "src.tgbot.handlers.treasury.giveaway.next_message", |
| 32 | + new_callable=AsyncMock, |
| 33 | + ) as next_message, |
| 34 | + ): |
| 35 | + await handle_giveaway(update, context, deep_link) |
| 36 | + |
| 37 | + pay.assert_awaited_once_with( |
| 38 | + update.effective_user.id, |
| 39 | + TrxType.CHANNEL_AUDIENCE_GIVEAWAY, |
| 40 | + deep_link, |
| 41 | + ) |
| 42 | + context.bot.send_message.assert_awaited_once() |
| 43 | + assert "+<b>10</b>" in context.bot.send_message.await_args.kwargs["text"] |
| 44 | + next_message.assert_awaited_once() |
| 45 | + |
| 46 | + |
| 47 | +@pytest.mark.asyncio |
| 48 | +async def test_legacy_giveaway_keeps_legacy_payout_type(): |
| 49 | + update = _make_update() |
| 50 | + context = _make_context() |
| 51 | + |
| 52 | + with ( |
| 53 | + patch( |
| 54 | + "src.tgbot.handlers.treasury.giveaway.pay_if_not_paid", |
| 55 | + new_callable=AsyncMock, |
| 56 | + return_value=77, |
| 57 | + ) as pay, |
| 58 | + patch("src.tgbot.handlers.treasury.giveaway.next_message", new_callable=AsyncMock), |
| 59 | + ): |
| 60 | + await handle_giveaway(update, context, "giveaway_77") |
| 61 | + |
| 62 | + pay.assert_awaited_once_with( |
| 63 | + update.effective_user.id, |
| 64 | + TrxType.GIVEAWAY, |
| 65 | + "giveaway_77", |
| 66 | + ) |
| 67 | + assert "+<b>77</b>" in context.bot.send_message.await_args.kwargs["text"] |
| 68 | + |
| 69 | + |
| 70 | +@pytest.mark.asyncio |
| 71 | +async def test_unknown_giveaway_does_not_pay(): |
| 72 | + update = _make_update() |
| 73 | + context = _make_context() |
| 74 | + |
| 75 | + with ( |
| 76 | + patch( |
| 77 | + "src.tgbot.handlers.treasury.giveaway.pay_if_not_paid", |
| 78 | + new_callable=AsyncMock, |
| 79 | + ) as pay, |
| 80 | + patch( |
| 81 | + "src.tgbot.handlers.treasury.giveaway.next_message", |
| 82 | + new_callable=AsyncMock, |
| 83 | + ) as next_message, |
| 84 | + ): |
| 85 | + await handle_giveaway(update, context, "giveaway_fake") |
| 86 | + |
| 87 | + pay.assert_not_awaited() |
| 88 | + context.bot.send_message.assert_not_awaited() |
| 89 | + next_message.assert_awaited_once() |
0 commit comments