Files
iishko-public/tests/test_gateway_usage.py
T
reformandClaude Opus 5.5 0ee6fb843b
checks / pytest (push) Has been cancelled
checks / moderation (push) Has been cancelled
checks / node (push) Has been cancelled
IISHKO: Telegram bot, Mini App and gateway
Initial public release.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-10-30 21:51:30 +03:00

72 lines
3.7 KiB
Python

from contextlib import closing
from datetime import datetime, timezone
from pathlib import Path
import sqlite3
import sys
import tempfile
import unittest
sys.path.insert(0, str(Path(__file__).parents[1]))
from gateway_usage import daily_snapshot, quota_headers
class DailyUsageTests(unittest.TestCase):
def setUp(self):
self.tmp = tempfile.TemporaryDirectory()
self.addCleanup(self.tmp.cleanup)
self.bot = str(Path(self.tmp.name) / "bot.db")
self.api = str(Path(self.tmp.name) / "api.db")
with closing(sqlite3.connect(self.bot)) as con, con:
con.execute("CREATE TABLE users(tg_id INTEGER PRIMARY KEY,chat_count INTEGER,quota_date TEXT,rank_cache INTEGER,vip INTEGER,admin INTEGER,unlim_until INTEGER)")
con.execute('INSERT INTO users VALUES(123,21,"2026-10-25",1,0,0,0)')
with closing(sqlite3.connect(self.api)) as con, con:
con.execute('CREATE TABLE tokens(name TEXT,key TEXT,deleted_at INTEGER)')
con.executemany('INSERT INTO tokens VALUES(?,?,NULL)', [('tg123','main'),('pm123','promo'),('sol123-k2','extra'),('tg123other','foreign')])
self.now = int(datetime(2026,10,25,20,59,59,tzinfo=timezone.utc).timestamp())
def test_shared_usage_is_read_only_and_consistent_across_key_families(self):
snapshots = [daily_snapshot(self.api,self.bot,key,now=self.now) for key in ('main','promo','extra')]
self.assertEqual(snapshots[0], snapshots[1])
self.assertEqual(snapshots[1], snapshots[2])
self.assertEqual(snapshots[0]['remaining'], 4979)
with closing(sqlite3.connect(self.bot)) as con:
self.assertEqual(con.execute('SELECT chat_count FROM users').fetchone()[0],21)
self.assertIsNone(daily_snapshot(self.api,self.bot,'foreign',now=self.now))
self.assertIsNone(daily_snapshot(self.api,self.bot,'unknown',now=self.now))
def test_reset_is_moscow_midnight_not_utc_and_stale_usage_does_not_leak(self):
before = daily_snapshot(self.api,self.bot,'main',now=self.now)
self.assertEqual(before['reset_at'], self.now+1)
after = daily_snapshot(self.api,self.bot,'main',now=self.now+1)
self.assertEqual(after['used'],0)
self.assertEqual(after['date'],'2026-10-26')
self.assertEqual(after['reset_at'],self.now+1+86400)
def test_purchased_extra_requests_extend_limit_not_rank(self):
reader=lambda db,uid,now=None: {'extra_requests':100}
data=daily_snapshot(self.api,self.bot,'main',now=self.now,entitlement_reader=reader)
self.assertEqual((data['limit'],data['remaining']),(5100,5079))
with closing(sqlite3.connect(self.bot)) as con:
self.assertEqual(con.execute('SELECT rank_cache FROM users').fetchone()[0],1)
def test_headers_are_out_of_band_and_unlimited_is_not_a_fake_zero(self):
with closing(sqlite3.connect(self.bot)) as con, con:
con.execute('UPDATE users SET vip=1')
data=daily_snapshot(self.api,self.bot,'main',now=self.now)
self.assertIsNone(data['limit'])
self.assertIsNone(data['remaining'])
headers=quota_headers(data)
self.assertEqual(headers['X-Iishko-Daily-Remaining'],'unlimited')
self.assertEqual(headers['X-Iishko-Daily-Scope'],'account')
self.assertEqual(quota_headers(None),{})
def test_broken_counter_schema_is_explicit_failure_not_zero_or_unlimited(self):
with closing(sqlite3.connect(self.bot)) as con, con:
con.execute('ALTER TABLE users RENAME COLUMN chat_count TO old_count')
with self.assertRaises(sqlite3.DatabaseError):
daily_snapshot(self.api,self.bot,'main',now=self.now)
if __name__ == '__main__':
unittest.main()