from __future__ import annotations import unittest from app.session_cache import SessionCache, hash_user_context class SessionCacheToolingTests(unittest.IsolatedAsyncioTestCase): def test_hash_user_context_ignores_assistant_and_tool(self) -> None: base = [ {"role": "system", "content": "S"}, {"role": "user", "content": "U"}, ] with_extra = base + [ {"role": "assistant", "content": "A1"}, {"role": "tool", "content": "T1"}, ] self.assertEqual(hash_user_context(base), hash_user_context(with_extra)) def test_build_key_changes_with_tool_config(self) -> None: cache = SessionCache(max_entries=8, ttl_sec=60) msgs = [{"role": "user", "content": "hi"}] key1 = cache.build_key("k", msgs, tool_config={"a": 1, "b": 2}) key2 = cache.build_key("k", msgs, tool_config={"b": 2, "a": 1}) key3 = cache.build_key("k", msgs, tool_config={"a": 1}) self.assertEqual(key1, key2) self.assertNotEqual(key1, key3) async def test_lru_evicts_oldest(self) -> None: cache = SessionCache(max_entries=2, ttl_sec=600) await cache.put("k1", "s1") await cache.put("k2", "s2") await cache.put("k3", "s3") self.assertIsNone(await cache.get("k1")) self.assertEqual(cache.evict_total, 1) async def test_ttl_expiry_increments_expire_counter(self) -> None: cache = SessionCache(max_entries=4, ttl_sec=0.001) await cache.put("k1", "s1") await __import__("asyncio").sleep(0.01) self.assertIsNone(await cache.get("k1")) self.assertEqual(cache.expire_total, 1) if __name__ == "__main__": unittest.main()