51 lines
2.2 KiB
Python
51 lines
2.2 KiB
Python
"""Request integration uses real temporary summary storage, no remote models."""
|
|
|
|
import json
|
|
from types import SimpleNamespace
|
|
from unittest.mock import AsyncMock
|
|
|
|
from langchain_core.messages import AIMessage, HumanMessage
|
|
from quart import Quart
|
|
|
|
from blueprints.conversation import _prepare_conversation_summary
|
|
from blueprints.conversation.summary import SummaryStore
|
|
|
|
|
|
async def test_prepares_summary_from_checkpoint_and_preserves_payload(tmp_path):
|
|
app = Quart(__name__)
|
|
app.conversation_summary_store = SummaryStore(tmp_path / "summary.db")
|
|
history = [
|
|
HumanMessage(content="Never include transfers", id="q1"),
|
|
AIMessage(content="Agreed: only outflows", id="a1"),
|
|
]
|
|
agent = SimpleNamespace(
|
|
aget_state=AsyncMock(return_value=SimpleNamespace(values={"messages": history}))
|
|
)
|
|
payload = {"messages": [{"role": "user", "content": "Current question"}]}
|
|
config = {"configurable": {"user_id": "u", "thread_id": "c"}}
|
|
async with app.app_context():
|
|
await _prepare_conversation_summary(agent, payload, config)
|
|
summary = config["configurable"]["conversation_summary"]
|
|
assert isinstance(summary, AIMessage)
|
|
assert "Never include transfers" in summary.content
|
|
assert "Current question" not in summary.content
|
|
assert payload == {"messages": [{"role": "user", "content": "Current question"}]}
|
|
assert len(history) == 2
|
|
assert json.loads(summary.content)["state"]["cursor"] == 2
|
|
reopened = SummaryStore(tmp_path / "summary.db")
|
|
assert (await reopened.load("u", "c")).cursor == 2
|
|
assert (await reopened.load("other", "c")).cursor == 0
|
|
|
|
|
|
async def test_optional_summary_failure_does_not_break_request(tmp_path, caplog):
|
|
app = Quart(__name__)
|
|
app.conversation_summary_store = SummaryStore(tmp_path / "summary.db")
|
|
agent = SimpleNamespace(
|
|
aget_state=AsyncMock(side_effect=RuntimeError("checkpoint offline"))
|
|
)
|
|
config = {"configurable": {"user_id": "u", "thread_id": "c"}}
|
|
async with app.app_context():
|
|
await _prepare_conversation_summary(agent, {"messages": []}, config)
|
|
assert "conversation_summary" not in config["configurable"]
|
|
assert "Failed to prepare conversation summary" in caplog.text
|