Spaces:
Runtime error
Runtime error
| """Coverage anchor for OpenAI prompt caching helpers (Phase 2).""" | |
| from __future__ import annotations | |
| from app.llm.prompt_cache import ( | |
| build_chat_messages, | |
| count_prompt_tokens, | |
| ensure_cacheable_system_prefix, | |
| normalize_messages_for_caching, | |
| prompt_cache_key, | |
| prompt_caching_active, | |
| ) | |
| def test_prompt_cache_key_stable() -> None: | |
| a = prompt_cache_key(phase="generate_section", model="gpt-4o-mini", survey_level=3) | |
| b = prompt_cache_key(phase="generate_section", model="gpt-4o-mini", survey_level=3) | |
| assert a == b | |
| assert len(a) == 32 | |
| def test_prompt_cache_key_tenant_isolation() -> None: | |
| a = prompt_cache_key( | |
| phase="generate_section", model="gpt-4o-mini", tenant_id="tenant-a" | |
| ) | |
| b = prompt_cache_key( | |
| phase="generate_section", model="gpt-4o-mini", tenant_id="tenant-b" | |
| ) | |
| assert a != b | |
| def test_build_chat_messages_order() -> None: | |
| msgs = build_chat_messages(system="sys", user="usr") | |
| assert msgs[0]["role"] == "system" | |
| assert msgs[1]["role"] == "user" | |
| def test_ensure_cacheable_prefix_grows() -> None: | |
| short = "You are a RICS surveyor." | |
| out = ensure_cacheable_system_prefix(short, min_tokens=1100) | |
| assert count_prompt_tokens(out) >= 1100 | |
| def test_prompt_caching_inactive_by_default() -> None: | |
| assert prompt_caching_active() is False | |