File size: 3,537 Bytes
a6bfba7
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
import pytest
from stackoverflow_mcp.formatter import format_response, clean_html
from stackoverflow_mcp.types import (
    SearchResult, 
    StackOverflowQuestion, 
    StackOverflowAnswer,
    StackOverflowComment,
    SearchResultComments
)

@pytest.fixture
def sample_result():
    """Create a sample search result for testing."""
    question = StackOverflowQuestion(
        question_id=12345,
        title="How to test Python code?",
        body="<p>I'm trying to test my <code>Python</code> code.</p><pre><code>def test():\n    pass</code></pre>",
        score=10,
        answer_count=2,
        is_answered=True,
        accepted_answer_id=54321,
        tags=["python", "testing"],
        link="https://stackoverflow.com/q/12345"
    )
    
    answers = [
        StackOverflowAnswer(
            answer_id=54321,
            question_id=12345,
            score=5,
            is_accepted=True,
            body="<p>You should use <code>pytest</code>.</p>",
            link="https://stackoverflow.com/a/54321"
        ),
        StackOverflowAnswer(
            answer_id=67890,
            question_id=12345,
            score=3,
            is_accepted=False,
            body="<p>Another option is <code>unittest</code>.</p>",
            link="https://stackoverflow.com/a/67890"
        )
    ]
    
    comments = SearchResultComments(
        question=[
            StackOverflowComment(
                comment_id=111,
                post_id=12345,
                score=2,
                body="Have you tried pytest?"
            )
        ],
        answers={
            54321: [
                StackOverflowComment(
                    comment_id=222,
                    post_id=54321,
                    score=1,
                    body="Great answer!"
                )
            ],
            67890: []
        }
    )
    
    return SearchResult(
        question=question,
        answers=answers,
        comments=comments
    )

def test_clean_html():
    """Test HTML cleaning."""
    html = "<p>This is <b>bold</b> and <i>italic</i>.</p><code>inline code</code><pre><code>def function():\n    return True</code></pre>"
    cleaned = clean_html(html)
    
    assert "<p>" not in cleaned
    assert "<b>" not in cleaned
    assert "<i>" not in cleaned
    assert "This is bold and italic." in cleaned
    assert "`inline code`" in cleaned
    assert "```\ndef function():\n    return True\n```" in cleaned

def test_format_response_markdown(sample_result):
    """Test formatting as Markdown."""
    markdown = format_response([sample_result], "markdown")
    
    assert "# How to test Python code?" in markdown
    assert "**Score:** 10" in markdown
    assert "## Question" in markdown
    assert "I'm trying to test my `Python` code." in markdown
    assert "```\ndef test():\n    pass\n```" in markdown
    assert "### Question Comments" in markdown
    assert "- Have you tried pytest?" in markdown
    assert "### ✓ Answer (Score: 5)" in markdown
    assert "You should use `pytest`." in markdown
    assert "### Answer (Score: 3)" in markdown
    assert "Another option is `unittest`." in markdown
    assert "[View on Stack Overflow](https://stackoverflow.com/q/12345)" in markdown

def test_format_response_json(sample_result):
    """Test formatting as JSON."""
    json_str = format_response([sample_result], "json")
    
    assert "How to test Python code?" in json_str
    assert "question_id" in json_str
    assert "answers" in json_str
    assert "comments" in json_str