band-aider-pub/tests/ai_client/test_cached_completion_client.py
2026-10-17 11:57:37 -07:00

69 lines
1.8 KiB
Python

# SPDX-FileCopyrightText: 2026 Art Sea LLC
#
# SPDX-License-Identifier: AGPL-3.0-only
from band_aider.ai_client.cached_completion_client import (
CompletionCache,
CompletionCacheEntry,
)
def test_completion_cache_returns_none_for_a_miss() -> None:
cache = CompletionCache()
assert cache.get("prompt") is None
def test_completion_cache_returns_stored_response() -> None:
cache = CompletionCache()
cache.put("prompt", "response")
assert cache.get("prompt") == "response"
def test_completion_cache_replaces_existing_response() -> None:
cache = CompletionCache()
cache.put("prompt", "first")
cache.put("prompt", "second")
assert cache.get("prompt") == "second"
def test_completion_cache_expires_entries() -> None:
"""An entry older than every possible live cutoff is removed on lookup."""
cache = CompletionCache(ttl_seconds=10)
expired = CompletionCacheEntry(
request="prompt",
response="response",
created_at=float("-inf"),
)
cache._entries["prompt"] = expired
cache._history.append(expired)
assert cache.get("prompt") is None
def test_completion_cache_does_not_remove_newer_replacement_when_old_entry_expires() -> None:
"""Expiring a historical entry cannot remove its later replacement."""
cache = CompletionCache(ttl_seconds=10)
expired = CompletionCacheEntry("prompt", "old", float("-inf"))
current = CompletionCacheEntry("prompt", "new", float("inf"))
cache._entries["prompt"] = current
cache._history.extend((expired, current))
assert cache.get("prompt") == "new"
def test_completion_cache_clear_removes_all_entries() -> None:
cache = CompletionCache()
cache.put("first", "response 1")
cache.put("second", "response 2")
cache.clear()
assert cache.get("first") is None
assert cache.get("second") is None