File size: 1,348 Bytes
732b14f
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
"""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