distribusi: notifikasi Telegram mulai/selesai/gagal (telegram.py, V30)
This commit is contained in:
1 parent
397e20ec3a
commit
025f46d399
8 files changed
+170
-7
No files matched your search
@@ -17,6 +17,7 @@ from decimal import Decimal
|
||||
|
||||
import db
|
||||
import distribute as dist
|
||||
import telegram
|
||||
|
||||
|
||||
def check(tag, cond):
|
||||
@@ -68,6 +69,13 @@ def main():
|
||||
db.DB_PATH = os.path.join(tmp, 'test.db')
|
||||
_orig_query = db.query
|
||||
|
||||
# Oracle: Telegram ⊥ boleh mengirim nyata — nonaktifkan dulu, dipakai lagi §V30
|
||||
_tg_token = telegram.TELEGRAM_BOT_TOKEN
|
||||
_tg_chats = telegram.TELEGRAM_CHAT_IDS
|
||||
_tg_post = telegram.requests.post
|
||||
telegram.TELEGRAM_BOT_TOKEN = ''
|
||||
telegram.TELEGRAM_CHAT_IDS = []
|
||||
|
||||
# ————— V20: fetch_balance — parse chain get_currency_balance —————
|
||||
dist.requests.post = lambda url, json=None, timeout=30: fake_post(
|
||||
['52186.0417 VEX'])
|
||||
@@ -215,6 +223,74 @@ def main():
|
||||
check('V22 run status partial',
|
||||
calls['run_updates'][0][1] == 'partial')
|
||||
|
||||
# ————— V30: notifikasi Telegram — best-effort, ⊥ dry-run/no-op —————
|
||||
tg = {'posts': []}
|
||||
|
||||
def tg_post(url, json=None, timeout=None):
|
||||
tg['posts'].append((url, json['text']))
|
||||
return fake_post({})
|
||||
|
||||
telegram.TELEGRAM_BOT_TOKEN = '123:TEST'
|
||||
telegram.TELEGRAM_CHAT_IDS = ['-100123']
|
||||
telegram.requests.post = tg_post
|
||||
|
||||
# send_text tanpa token → no-op senyap (⊥ post)
|
||||
telegram.TELEGRAM_BOT_TOKEN = ''
|
||||
telegram.send_text('tidak boleh terkirim')
|
||||
check('V30 ⊥ token → no-op senyap', tg['posts'] == [])
|
||||
telegram.TELEGRAM_BOT_TOKEN = '123:TEST'
|
||||
|
||||
# dry-run → ⊥ notif Telegram
|
||||
make_voters_db(db.DB_PATH, [('aaa1', '1.0', 300.0, '2026-08-05T00:00:00',
|
||||
'2026-08-05T00:00:00'),
|
||||
('bbb2', '1.0', 700.0, '2026-08-05T00:00:00',
|
||||
'2026-08-05T00:00:00')])
|
||||
db.query = _orig_query
|
||||
dist.fetch_balance = lambda: Decimal('100.0000')
|
||||
dist.BP_PRIVATE_KEY = '5Ktest'
|
||||
dist.build_signed_transfer = lambda to, amount, memo: FakeSigned('txT' + to)
|
||||
dist.DISTRIBUTE_MAX_ATTEMPTS = 3
|
||||
dist.verify_txid = lambda txid: True
|
||||
calls['runs'] = []
|
||||
with redirect_stdout(io.StringIO()):
|
||||
dist.run_distribution(dry_run=True)
|
||||
check('V30 dry-run ⊥ notif', tg['posts'] == [])
|
||||
|
||||
# run nyata → notif mulai + selesai
|
||||
calls['updates'] = []
|
||||
calls['run_updates'] = []
|
||||
with redirect_stdout(io.StringIO()):
|
||||
rc = dist.run_distribution(dry_run=False)
|
||||
check('V30 run nyata exit 0', rc == 0)
|
||||
check('V30 2 notif (mulai + selesai)',
|
||||
len(tg['posts']) == 2)
|
||||
check('V30 mulai tercatat', 'DISTRIBUSI MULAI' in tg['posts'][0][1])
|
||||
check('V30 selesai + status ok', 'DISTRIBUSI SELESAI' in tg['posts'][1][1]
|
||||
and 'Status: ok' in tg['posts'][1][1])
|
||||
check('V30 ringkasan jumlah', '2/2 pemilih' in tg['posts'][1][1])
|
||||
|
||||
# no-op (pemilih kosong) → ⊥ notif baru
|
||||
db.query = lambda sql, params=(): []
|
||||
with redirect_stdout(io.StringIO()):
|
||||
dist.run_distribution(dry_run=False)
|
||||
check('V30 no-op ⊥ notif', len(tg['posts']) == 2)
|
||||
db.query = _orig_query
|
||||
|
||||
# failure (⊥ key) → notif gagal lewat main(), lalu re-raise
|
||||
dist.BP_PRIVATE_KEY = ''
|
||||
try:
|
||||
dist.main()
|
||||
raise AssertionError('V30 gagal: main harusnya re-raise')
|
||||
except RuntimeError:
|
||||
check('V30 failure → re-raise RuntimeError', True)
|
||||
check('V30 notif gagal terkirim',
|
||||
len(tg['posts']) == 3 and 'DISTRIBUSI GAGAL' in tg['posts'][2][1])
|
||||
dist.BP_PRIVATE_KEY = '5Ktest'
|
||||
|
||||
telegram.TELEGRAM_BOT_TOKEN = _tg_token
|
||||
telegram.TELEGRAM_CHAT_IDS = _tg_chats
|
||||
telegram.requests.post = _tg_post
|
||||
|
||||
print('\nSEMUA UJI PASS')
|
||||
|
||||
|
||||
|
||||
Reference in new issue
Block a user