test(discord_tool): add regression test for per-token capability cache

Proves token A's detected capabilities do not leak to token B after the
fix in the preceding commit. Before the fix this test would have seen
both tokens return token A's cached value.
This commit is contained in:
teknium1 2026-04-30 20:32:07 -07:00 committed by Teknium
parent fa7b0b0a67
commit e21898ea98

View file

@ -696,6 +696,38 @@ class TestCapabilityDetection:
_detect_capabilities("tok", force=True)
assert mock_req.call_count == 2
@patch("tools.discord_tool._discord_request")
def test_cache_is_keyed_by_token(self, mock_req):
"""Regression: token A's capabilities must not leak to token B.
Before the fix, the cache was a single module-global dict. The first
call populated it and every subsequent call regardless of token
returned the same cached value, producing wrong schema gating for
rotated or multi-token deployments.
"""
def _per_token_flags(method, path, token, **_kwargs):
# token A: both intents; token B: neither.
if token == "tok_a":
return {"flags": (1 << 14) | (1 << 18)}
return {"flags": 0}
mock_req.side_effect = _per_token_flags
caps_a = _detect_capabilities("tok_a")
caps_b = _detect_capabilities("tok_b")
assert caps_a["has_members_intent"] is True
assert caps_a["has_message_content"] is True
assert caps_b["has_members_intent"] is False
assert caps_b["has_message_content"] is False
# Each token should hit the endpoint exactly once.
assert mock_req.call_count == 2
# Re-requesting either token serves from its own cache entry.
_detect_capabilities("tok_a")
_detect_capabilities("tok_b")
assert mock_req.call_count == 2
# ---------------------------------------------------------------------------
# Config allowlist