study-buddy / tests /test_text_ref_engine.py
GitHub Actions
deploy b3d187d69e5df10bb2ec396e89f539f853465a06
14184e3
Raw
History Blame Contribute Delete
3.64 kB
from app.agents.text_ref_engine import TextRefEngine, TextRefGrounding
class _FakeClient:
def __init__(self, obj):
self._obj = obj
def structured_complete(self, messages, schema, **kw):
return self._obj
def test_generate_happy_path_with_web_results_populates_source():
chunks = [{"source": "paper.pdf", "text": "The transformer architecture relies entirely on attention mechanisms, dispensing with recurrence."}]
grounding = TextRefGrounding(
renderable=True,
anchor="relies entirely on attention mechanisms, dispensing with recurrence",
body_markdown="The transformer relies on attention instead of recurrence, as covered in the source material.",
)
engine = TextRefEngine(client=_FakeClient(grounding))
web_results = [{"title": "Attention Is All You Need", "url": "https://example.com/paper"}]
payload = engine.generate("transformer architecture", chunks, "high_school", web_results=web_results)
assert payload.animation_type == "2d_text"
assert payload.html_code == ""
assert payload.text_ref is not None
assert payload.text_ref.body_markdown == grounding.body_markdown
assert payload.text_ref.source_label == "Attention Is All You Need"
assert payload.text_ref.source_url == "https://example.com/paper"
def test_generate_happy_path_without_web_results_leaves_source_blank():
chunks = [{"source": "paper.pdf", "text": "The transformer architecture relies entirely on attention mechanisms, dispensing with recurrence."}]
grounding = TextRefGrounding(
renderable=True,
anchor="relies entirely on attention mechanisms, dispensing with recurrence",
body_markdown="The transformer relies on attention instead of recurrence.",
)
engine = TextRefEngine(client=_FakeClient(grounding))
payload = engine.generate("transformer architecture", chunks, "high_school", web_results=None)
assert payload.text_ref is not None
assert payload.text_ref.source_label == ""
assert payload.text_ref.source_url == ""
payload_empty_list = engine.generate("transformer architecture", chunks, "high_school", web_results=[])
assert payload_empty_list.text_ref.source_label == ""
assert payload_empty_list.text_ref.source_url == ""
def test_generate_decline_path_returns_2d_text_card_with_no_text_ref():
chunks = [{"source": "paper.pdf", "text": "This section is purely a list of numeric results with no narrative worth summarizing."}]
grounding = TextRefGrounding(
renderable=False,
decline_reason="'results table' doesn't have a narrative worth a dedicated explainer — better explored in chat.",
)
engine = TextRefEngine(client=_FakeClient(grounding))
payload = engine.generate("results table", chunks, "high_school")
assert payload.animation_type == "2d_text"
assert payload.html_code != ""
assert payload.text_ref is None
assert "results table" in payload.html_code
def test_generate_discards_hallucinated_anchor():
chunks = [{"source": "paper.pdf", "text": "Attention mechanisms compute a weighted sum over value vectors."}]
grounding = TextRefGrounding(
renderable=True,
anchor="this text was never in the source at all",
body_markdown="Attention computes a weighted sum over values.",
)
engine = TextRefEngine(client=_FakeClient(grounding))
payload = engine.generate("attention mechanism", chunks, "high_school")
assert payload.animation_type == "2d_text"
assert "this text was never in the source at all" not in payload.explanation
assert payload.explanation == ""