from knowledge_adapter.content_budget import TRUNCATION_MARKER, truncate_content def test_plain_content_is_truncated_with_explicit_marker() -> None: limit = len(TRUNCATION_MARKER) + 5 result = truncate_content("abcdefghij" * 10, max_chars=limit) assert result == f"abcde{TRUNCATION_MARKER}" assert len(result) == limit def test_truncation_never_splits_markdown_image_node() -> None: content = ( "prefix " "![image](https://assets.example.com/i/550e8400-e29b-41d4-a716-446655440000)" " trailing text" ) limit = len(TRUNCATION_MARKER) + len("prefix ![image](https://assets.") result = truncate_content(content, max_chars=limit) assert result == f"prefix {TRUNCATION_MARKER}" assert len(result) <= limit def test_truncation_never_splits_fenced_code_block() -> None: content = "intro\n```python\nprint('sensitive')\n```\n" + ("after" * 20) limit = len(TRUNCATION_MARKER) + len("intro\n```python\nprint") result = truncate_content(content, max_chars=limit) assert result == f"intro\n{TRUNCATION_MARKER}" assert result.count("```") == 0