File size: 3,344 Bytes
cd40eba
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
4ec29a9
cd40eba
4ec29a9
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
cd40eba
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
79
80
81
82
83
84
85
86
87
88
89
90
"""Turning the web plugin's 【domain】 markers into real links.

The plugin emits citations as a bare domain in fullwidth brackets, which
renders as text that looks clickable but isn't. The matching URLs come back
in message.annotations; these pin how the two get paired up.
"""
import pytest

from app.llm import _citation_urls, link_citations

PM = "https://www.pmindia.gov.in/en/pms-profile/"
WIKI = "https://en.wikipedia.org/wiki/Narendra_Modi"


def test_marker_becomes_a_link_to_the_deep_url():
    out = link_citations(
        "Narendra Modi is the Prime Minister of India【pmindia.gov.in】",
        [PM])
    # note the inserted space: the model glues the marker onto the last word
    assert out == (
        f"Narendra Modi is the Prime Minister of India [pmindia.gov.in]({PM})")


def test_space_inserted_only_when_needed():
    # "Forcesen.wikipedia.org" was the reported symptom
    glued = link_citations("the Self-Defence Forces【wikipedia.org】", [WIKI])
    assert "Forces [wikipedia.org]" in glued
    # already spaced -> no double space
    spaced = link_citations("the Self-Defence Forces 【wikipedia.org】", [WIKI])
    assert "Forces  [" not in spaced
    assert "Forces [wikipedia.org]" in spaced


def test_title_rides_along_for_the_hover_card():
    out = link_citations("x【pmindia.gov.in】",
                         [{"url": PM, "title": "Know the PM"}])
    assert out == f'x [pmindia.gov.in]({PM} "Know the PM")'


def test_quotes_in_title_are_neutralised():
    # a raw double quote would terminate the markdown title early
    out = link_citations("x【pmindia.gov.in】",
                         [{"url": PM, "title": 'The "PM" profile'}])
    assert '"The \'PM\' profile"' in out


def test_multiple_markers_each_match_their_own_host():
    out = link_citations("Modi【pmindia.gov.in】【wikipedia.org】", [PM, WIKI])
    assert f"[pmindia.gov.in]({PM})" in out
    assert f"[wikipedia.org]({WIKI})" in out


def test_subdomain_marker_matches_parent_annotation():
    # annotation host is en.wikipedia.org, marker says wikipedia.org
    out = link_citations("x【wikipedia.org】", [WIKI])
    assert f"[wikipedia.org]({WIKI})" in out


def test_unmatched_marker_is_left_alone():
    # Linking an unmatched domain would be a guess; a wrong link is worse
    # than a plain one, so the marker survives untouched.
    out = link_citations("x【example.com】", [PM])
    assert out == "x【example.com】"


def test_no_annotations_is_a_no_op():
    assert link_citations("x【pmindia.gov.in】", []) == "x【pmindia.gov.in】"


def test_text_without_markers_is_unchanged():
    assert link_citations("plain answer", [PM]) == "plain answer"


def test_existing_markdown_links_are_untouched():
    src = "see [the profile](https://a.example) for more"
    assert link_citations(src, [PM]) == src


@pytest.mark.parametrize("annotations,expected", [
    (None, []),
    ([], []),
    ([{"type": "url_citation", "url_citation": {"url": PM}}], [PM]),
    # duplicates collapse
    ([{"url_citation": {"url": PM}}, {"url_citation": {"url": PM}}], [PM]),
    # junk shapes are skipped rather than raising
    (["nonsense", {"url_citation": {}}, {"url_citation": {"url": "ftp://x"}}], []),
])
def test_citation_urls_extraction(annotations, expected):
    assert _citation_urls(annotations) == expected