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="
I'm trying to test my Python
code.
def test():\n pass
",
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="You should use pytest
.
Another option is unittest
.
This is bold and italic.
inline code
def function():\n return True
"
cleaned = clean_html(html)
assert "" not in cleaned assert "" not in cleaned assert "" 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