Files
DeepTutor/tests/services/test_keypool.py
Bingxi Zhao (Frank) c600a507a4 fix(tests): stop the partner suite writing to the real account store
`identity` binds `AUTH_DIR = SYSTEM_ROOT / "auth"` at import, so the
partners fixture's `setattr(paths, "SYSTEM_ROOT", tmp)` never reached it:
`save_user` wrote into the developer's own `data/system/auth/users.json`
and read it back. The suite had already deposited an `alice` account
there.

That also made `test_a_linked_sender_gets_their_own_private_history`
depend on who else happened to have an account. The first account in a
store is force-promoted to admin whatever role is asked for, and an
admin's scope *is* the shared pool — so on an empty store alice became
admin and "her private history" and "the shared pool" resolved to one
directory. It passed on a machine with existing accounts and on a full
run, and failed alone and on all four Python versions in CI (v1.5.17's
release commit is red for exactly this).

The fixture now redirects identity's file constants too, and `alice` is
saved after an admin so she is guaranteed non-admin — asserted, so the
precondition cannot rot silently. Product code is unaffected: the
behaviour under test was always correct.

Also collapses the three copies of the api_key reduction into one.
`_primary_api_key` (provider_runtime) is deleted and
`LLMConfig.get_api_key` delegates, both to `keypool.primary_api_key`,
which now treats an empty key as absent in either spelling so its list
and scalar branches agree. Equivalence checked across every configured
shape: "", None, [], [""], ["", "k"], "k", ["k"], ["k1", "k2"].

pytest 5284 passed (was 5276), pre-commit clean, ruff check + format
clean at CI's pinned 0.16.0.
2026-08-25 10:20:26 +08:00

109 lines
3.2 KiB
Python

from __future__ import annotations
import pytest
from deeptutor.services.keypool import KeyPool, primary_api_key
def test_keypool_rotates_in_round_robin_order() -> None:
pool = KeyPool(["key-a", "key-b", "key-c"])
assert [pool.next() for _ in range(5)] == [
"key-a",
"key-b",
"key-c",
"key-a",
"key-b",
]
def test_keypool_cools_key_after_two_429s_and_restores_it(
monkeypatch: pytest.MonkeyPatch,
) -> None:
from deeptutor.services import keypool as keypool_module
now = {"value": 100.0}
monkeypatch.setattr(keypool_module, "monotonic", lambda: now["value"])
pool = KeyPool(["key-a", "key-b"], cooldown_s=60)
assert pool.next() == "key-a"
pool.mark_429("key-a")
pool.mark_429("key-a")
assert [pool.next(), pool.next()] == ["key-b", "key-b"]
now["value"] = 161.0
assert pool.next() == "key-a"
def test_keypool_still_serves_a_single_cooling_key(
monkeypatch: pytest.MonkeyPatch,
) -> None:
"""A single-key setup must never be refused service.
Cooling is only useful as a hint about which key to prefer. With one key
there is nothing to prefer, so refusing to hand it out would turn a
retryable provider 429 into a hard failure for every LLM and embedding
call until the cooldown expires.
"""
from deeptutor.services import keypool as keypool_module
now = {"value": 10.0}
monkeypatch.setattr(keypool_module, "monotonic", lambda: now["value"])
pool = KeyPool(["only-key"], cooldown_s=5)
assert pool.next() == "only-key"
pool.mark_429("only-key")
assert pool.next() == "only-key"
pool.mark_429("only-key")
assert pool.next() == "only-key"
now["value"] = 16.0
assert pool.next() == "only-key"
def test_keypool_prefers_the_soonest_recovering_key_when_all_are_cooling(
monkeypatch: pytest.MonkeyPatch,
) -> None:
from deeptutor.services import keypool as keypool_module
now = {"value": 0.0}
monkeypatch.setattr(keypool_module, "monotonic", lambda: now["value"])
pool = KeyPool(["key-a", "key-b"], cooldown_s=60)
# key-a cools at t=0 (until 60), key-b at t=10 (until 70).
pool.mark_429("key-a")
pool.mark_429("key-a")
now["value"] = 10.0
pool.mark_429("key-b")
pool.mark_429("key-b")
now["value"] = 20.0
assert pool.next() == "key-a"
@pytest.mark.parametrize(
("value", "expected"),
[
("sk-a", "sk-a"),
(["sk-a"], "sk-a"),
(["sk-a", "sk-b"], "sk-a"),
# An empty key is not a key, in either spelling — so a caller can test
# the result instead of knowing which shape the config happened to use.
("", None),
(None, None),
([], None),
([""], None),
],
)
def test_primary_api_key_reduces_every_configured_shape(value, expected) -> None:
assert primary_api_key(value) == expected
def test_llm_config_get_api_key_uses_the_same_reduction() -> None:
"""``LLMConfig.get_api_key`` is a ``-> str`` front door, not a second copy."""
from deeptutor.services.llm.config import LLMConfig
for value in ("sk-a", ["sk-a", "sk-b"], "", [], [""]):
config = LLMConfig(api_key=value, model="gpt-5")
assert config.get_api_key() == (primary_api_key(value) or "")