403Webshell
Server IP : 3.147.158.171  /  Your IP : 216.73.216.88
Web Server : Apache/2.4.67 (Amazon Linux) OpenSSL/3.5.5
System : Linux ip-172-31-2-178.us-east-2.compute.internal 6.1.172-216.329.amzn2023.x86_64 #1 SMP PREEMPT_DYNAMIC Wed May 20 06:31:34 UTC 2026 x86_64
User : ec2-user ( 1000)
PHP Version : 8.4.21
Disable Function : NONE
MySQL : OFF  |  cURL : ON  |  WGET : ON  |  Perl : ON  |  Python : OFF  |  Sudo : ON  |  Pkexec : OFF
Directory :  /tsai/repo/api/tests/unit/

Upload File :
current_dir [ Writeable ] document_root [ Writeable ]

 

Command :


[ Back ]     

Current File : /tsai/repo/api/tests/unit/test_comments_search_results.py
"""Tests for _format_search_results in app.services.comments_service."""

import json

from app.services.comments_service import (
    SEARCH_CONTENT_TRUNCATE,
    SEARCH_USAGE_NOTE,
    _format_search_results,
)


def _article_with_meta(meta_value):
    return {
        'id': 1,
        'title': {'rendered': 'Test'},
        'content': {'rendered': '<p>body</p>'},
        'meta': {'_tsai_search_results': meta_value},
    }


def test_returns_empty_when_meta_absent():
    assert _format_search_results({}) == ''
    assert _format_search_results({'meta': {}}) == ''
    assert _format_search_results({'meta': None}) == ''


def test_returns_empty_when_meta_value_empty():
    assert _format_search_results(_article_with_meta('')) == ''
    assert _format_search_results(_article_with_meta(None)) == ''


def test_returns_empty_when_meta_unparseable():
    assert _format_search_results(_article_with_meta('not-json')) == ''


def test_returns_empty_for_empty_list():
    assert _format_search_results(_article_with_meta(json.dumps([]))) == ''


def test_returns_empty_when_meta_field_is_not_a_dict():
    # WP sometimes returns meta as [] when no registered meta is set
    article = {'meta': [], 'id': 1}
    assert _format_search_results(article) == ''


def test_includes_title_url_content_and_usage_note():
    results = [
        {'title': 'Source One', 'url': 'https://a.example/x', 'content': 'Short content.'},
    ]
    out = _format_search_results(_article_with_meta(json.dumps(results)))
    assert 'Research sources used to write this article:' in out
    assert 'Source One' in out
    assert 'https://a.example/x' in out
    assert 'Short content.' in out
    assert SEARCH_USAGE_NOTE in out


def test_handles_missing_fields_gracefully():
    results = [{'url': 'https://a.example/x'}]  # no title, no content
    out = _format_search_results(_article_with_meta(json.dumps(results)))
    assert 'Untitled' in out
    assert 'https://a.example/x' in out


def test_truncates_long_content_with_ellipsis():
    long_content = 'x' * (SEARCH_CONTENT_TRUNCATE + 200)
    results = [{'title': 'T', 'url': 'https://a/x', 'content': long_content}]
    out = _format_search_results(_article_with_meta(json.dumps(results)))
    # original full-length content not present
    assert long_content not in out
    # ellipsis marker present
    assert '…' in out
    # truncated body length around the limit (allow for the ellipsis and trim)
    assert 'x' * (SEARCH_CONTENT_TRUNCATE + 1) not in out


def test_numbers_each_source_sequentially():
    results = [
        {'title': 'A', 'url': 'https://a', 'content': 'a'},
        {'title': 'B', 'url': 'https://b', 'content': 'b'},
        {'title': 'C', 'url': 'https://c', 'content': 'c'},
    ]
    out = _format_search_results(_article_with_meta(json.dumps(results)))
    assert '1. A' in out
    assert '2. B' in out
    assert '3. C' in out


def test_trailing_blank_line_for_clean_concat():
    results = [{'title': 'A', 'url': 'https://a', 'content': 'a'}]
    out = _format_search_results(_article_with_meta(json.dumps(results)))
    assert out.endswith('\n\n')

Youez - 2016 - github.com/yon3zu
LinuXploit