Files
DeepTutor/tests/services/test_chat_hints.py
Marcello Pichini 863327af2a fix(hints): keep leading -, * and # that belong to the hint (#2852)
The composer-hint sanitizers used lstrip("-•*# ") to drop list markers,
which also ate the sign of "-3 squared...?", the star of "*args" and
the hash of "#include". Strip a marker only when whitespace follows it.

Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>
2026-12-01 00:25:50 +08:00

169 lines
5.6 KiB
Python

"""Focused tests for the home chat's dynamic composer line."""
from __future__ import annotations
import asyncio
import pytest
from deeptutor.services import chat_hints
from deeptutor.services.settings import starter_settings
@pytest.fixture(autouse=True)
def clear_hint_state() -> None:
chat_hints._cache.clear()
chat_hints._inflight.clear()
@pytest.fixture(autouse=True)
def hint_enabled(monkeypatch: pytest.MonkeyPatch) -> None:
# Keep the tests off whatever starters.json the machine running them has.
monkeypatch.setattr(starter_settings, "ask_hint_enabled", lambda: True)
def _material() -> chat_hints._Material:
return chat_hints._Material(
transcript=[
("user", "How do retries work here?"),
("assistant", "It retries up to three times with backoff."),
],
transcript_length=2,
)
def test_sanitize_allows_non_question_lines() -> None:
# Unlike the mastery/reading hints, this one must NOT require a question
# mark — a plausible next turn is often a request, not a question.
assert chat_hints._sanitize("Make the tone more casual", "en") == "Make the tone more casual"
def test_sanitize_rejects_meta_and_assistant_voice() -> None:
assert chat_hints._sanitize("You could ask about the retry limit", "en") == ""
assert chat_hints._sanitize("Sure, here's a more casual version.", "en") == ""
assert chat_hints._sanitize("好的,这是更随意一点的版本。", "zh") == ""
assert chat_hints._sanitize("你可以问问重试上限", "zh") == ""
def test_sanitize_rejects_echo_of_last_user_message() -> None:
assert (
chat_hints._sanitize("How do retries work here?", "en", "How do retries work here?") == ""
)
assert chat_hints._sanitize(
"What happens after the third retry fails?",
"en",
"How do retries work here?",
)
@pytest.mark.asyncio
async def test_empty_transcript_never_calls_the_llm(monkeypatch: pytest.MonkeyPatch) -> None:
calls = 0
async def call_llm(_material: chat_hints._Material, _language: str) -> str:
nonlocal calls
calls += 1
return "should not be reached"
monkeypatch.setattr(chat_hints, "_call_llm", call_llm)
async def collect(_session_id: str) -> chat_hints._Material:
return chat_hints._Material(transcript=[], transcript_length=0)
monkeypatch.setattr(chat_hints, "_collect", collect)
result = await chat_hints.get_ask_hint("session-1")
assert result["hint"] == ""
assert calls == 0
@pytest.mark.asyncio
async def test_cache_hit_does_not_invoke_llm_again(monkeypatch: pytest.MonkeyPatch) -> None:
material = _material()
calls = 0
async def collect(_session_id: str) -> chat_hints._Material:
return material
async def call_llm(_material: chat_hints._Material, _language: str) -> str:
nonlocal calls
calls += 1
return "What happens after the third retry fails?"
monkeypatch.setattr(chat_hints, "_collect", collect)
monkeypatch.setattr(chat_hints, "_call_llm", call_llm)
monkeypatch.setattr(chat_hints, "_response_language", lambda: "en")
first = await chat_hints.get_ask_hint("session-1")
second = await chat_hints.get_ask_hint("session-1")
assert first == second
assert first["hint"] == "What happens after the third retry fails?"
assert calls == 1
@pytest.mark.asyncio
@pytest.mark.parametrize(
"error",
[asyncio.TimeoutError(), RuntimeError("model unavailable")],
ids=["timeout", "failure"],
)
async def test_llm_failure_returns_empty_hint(
monkeypatch: pytest.MonkeyPatch, error: Exception
) -> None:
async def collect(_session_id: str) -> chat_hints._Material:
return _material()
async def fail_llm(_material: chat_hints._Material, _language: str) -> str:
raise error
monkeypatch.setattr(chat_hints, "_collect", collect)
monkeypatch.setattr(chat_hints, "_call_llm", fail_llm)
monkeypatch.setattr(chat_hints, "_response_language", lambda: "en")
result = await chat_hints.get_ask_hint("session-1")
assert result["hint"] == ""
@pytest.mark.asyncio
async def test_turning_the_hint_off_skips_the_llm_and_the_cache(
monkeypatch: pytest.MonkeyPatch,
) -> None:
"""Off means nothing is offered — not even a line cached while it was on."""
collected = 0
calls = 0
async def collect(_session_id: str) -> chat_hints._Material:
nonlocal collected
collected += 1
return _material()
async def call_llm(_material: chat_hints._Material, _language: str) -> str:
nonlocal calls
calls += 1
return "What happens after the third retry fails?"
monkeypatch.setattr(chat_hints, "_collect", collect)
monkeypatch.setattr(chat_hints, "_call_llm", call_llm)
monkeypatch.setattr(chat_hints, "_response_language", lambda: "en")
cached = await chat_hints.get_ask_hint("session-1")
assert cached["hint"]
monkeypatch.setattr(starter_settings, "ask_hint_enabled", lambda: False)
result = await chat_hints.get_ask_hint("session-1")
assert result["hint"] == ""
assert collected == 1
assert calls == 1
def test_sanitize_strips_list_markers_but_keeps_leading_text() -> None:
assert chat_hints._sanitize("- Explain recursion again", "en") == "Explain recursion again"
assert chat_hints._sanitize("## • Explain recursion again", "en") == "Explain recursion again"
assert chat_hints._sanitize("-3 squared is 9, right?", "en") == "-3 squared is 9, right?"
assert chat_hints._sanitize("*args or **kwargs here?", "en") == "*args or **kwargs here?"
assert chat_hints._sanitize("#include fails to compile", "en") == "#include fails to compile"