Files
devplacepy/tests/unit/services/bot/bot.py
T
retoor a3dd747d07 fix: clear rate-limit hot-settings cache and add retry loop for maintenance-mode tests
- Invalidate `_hot_settings_value` and `_hot_settings_at` in `_patch_limits` so stale
  cached limits from prior tests do not persist after monkey-patching `get_int_setting`.
- Add a polling retry loop (up to 5 seconds) in `test_maintenance_mode_blocks_guests`
  to handle eventual consistency of the maintenance-mode flag propagation.
- Replace brittle hardcoded `@bob_test` assertion in mention test with dynamic
  `data-username` attribute extraction from the active dropdown item.
- Replace `asyncio.run` calls in bot unit tests with shared `run_async` helper from
  `tests.conftest` to avoid event-loop conflicts in the test suite.
- Change `insert` to `upsert` with `["uid"]` conflict target in Gitea test fixture
  to prevent duplicate-key errors on repeated test runs.
- Replace `asyncio.new_event_loop().run_until_complete` in Telegram worker tests
  with the same shared `run_async` helper for consistency.
2026-06-19 23:21:53 +00:00

56 lines
1.8 KiB
Python

# retoor <retoor@molodetz.nl>
import types
from devplacepy.services.bot import bot as bot_module
from devplacepy.services.bot.bot import DevPlaceBot
from tests.conftest import run_async
STALE_KEY = "019ec5b5-410c-7660-a129-c80589859080"
OWN_KEY = "019ed655-b699-7fa0-bf25-dd77910fb6d4"
def _fake_bot(cached_key, fetched_key):
fake = types.SimpleNamespace()
fake.state = types.SimpleNamespace(account_api_key=cached_key)
fake.llm = types.SimpleNamespace(api_key=cached_key)
fake.saves = []
fake.logs = []
async def _fetch():
return fetched_key
fake._fetch_account_api_key = _fetch
fake._save = lambda: fake.saves.append(fake.state.account_api_key)
fake._log = lambda *args, **kwargs: fake.logs.append(args[0] if args else "")
return fake
def test_adopt_replaces_stale_cached_key_from_recycled_slot():
fake = _fake_bot(cached_key=STALE_KEY, fetched_key=OWN_KEY)
result = run_async(DevPlaceBot._adopt_account_api_key(fake))
assert result is True
assert fake.llm.api_key == OWN_KEY
assert fake.state.account_api_key == OWN_KEY
assert fake.saves == [OWN_KEY]
def test_adopt_keeps_matching_key_without_resaving():
fake = _fake_bot(cached_key=OWN_KEY, fetched_key=OWN_KEY)
result = run_async(DevPlaceBot._adopt_account_api_key(fake))
assert result is True
assert fake.llm.api_key == OWN_KEY
assert fake.saves == []
def test_adopt_fails_when_own_key_unavailable(monkeypatch):
async def _instant_sleep(_seconds):
return None
monkeypatch.setattr(bot_module.asyncio, "sleep", _instant_sleep)
fake = _fake_bot(cached_key=STALE_KEY, fetched_key="")
result = run_async(DevPlaceBot._adopt_account_api_key(fake))
assert result is False
assert fake.llm.api_key == STALE_KEY
assert fake.saves == []