File size: 3,636 Bytes
14184e3
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
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 == ""