File size: 4,181 Bytes
1cecfb1 | 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 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 | from io import StringIO
from rich.console import Console
from rich.markdown import Markdown
from cli import _render_final_assistant_content
def _render_to_text(renderable) -> str:
buf = StringIO()
Console(file=buf, width=80, force_terminal=False, color_system=None).print(renderable)
return buf.getvalue()
def test_final_assistant_content_uses_markdown_renderable():
renderable = _render_final_assistant_content("# Title\n\n- one\n- two")
assert isinstance(renderable, Markdown)
output = _render_to_text(renderable)
assert "Title" in output
assert "one" in output
assert "two" in output
def test_final_assistant_content_strips_ansi_before_markdown_rendering():
renderable = _render_final_assistant_content("\x1b[31m# Title\x1b[0m")
output = _render_to_text(renderable)
assert "Title" in output
assert "\x1b" not in output
def test_final_assistant_content_can_strip_markdown_syntax():
renderable = _render_final_assistant_content(
"***Bold italic***\n~~Strike~~\n- item\n# Title\n`code`",
mode="strip",
)
output = _render_to_text(renderable)
assert "Bold italic" in output
assert "Strike" in output
assert "item" in output
assert "Title" in output
assert "code" in output
assert "***" not in output
assert "~~" not in output
assert "`" not in output
def test_strip_mode_preserves_lists():
renderable = _render_final_assistant_content(
"**Formatting**\n- Ran prettier\n- Files changed\n- Verified clean",
mode="strip",
)
output = _render_to_text(renderable)
assert "- Ran prettier" in output
assert "- Files changed" in output
assert "- Verified clean" in output
assert "**" not in output
def test_strip_mode_preserves_ordered_lists():
renderable = _render_final_assistant_content(
"1. First item\n2. Second item\n3. Third item",
mode="strip",
)
output = _render_to_text(renderable)
assert "1. First" in output
assert "2. Second" in output
assert "3. Third" in output
def test_strip_mode_preserves_blockquotes():
renderable = _render_final_assistant_content(
"> This is quoted text\n> Another quoted line",
mode="strip",
)
output = _render_to_text(renderable)
assert "> This is quoted" in output
assert "> Another quoted" in output
def test_strip_mode_preserves_checkboxes():
renderable = _render_final_assistant_content(
"- [ ] Todo item\n- [x] Done item",
mode="strip",
)
output = _render_to_text(renderable)
assert "- [ ] Todo" in output
assert "- [x] Done" in output
def test_strip_mode_preserves_table_structure_while_cleaning_cell_markdown():
renderable = _render_final_assistant_content(
"| Syntax | Example |\n|---|---|\n| Bold | `**bold**` |\n| Strike | `~~strike~~` |",
mode="strip",
)
output = _render_to_text(renderable)
assert "| Syntax | Example |" in output
assert "|---|---|" in output
assert "| Bold | bold |" in output
assert "| Strike | strike |" in output
assert "**" not in output
assert "~~" not in output
assert "`" not in output
def test_final_assistant_content_can_leave_markdown_raw():
renderable = _render_final_assistant_content("***Bold italic***", mode="raw")
output = _render_to_text(renderable)
assert "***Bold italic***" in output
def test_strip_mode_preserves_intraword_underscores_in_snake_case_identifiers():
renderable = _render_final_assistant_content(
"Let me look at test_case_with_underscores and SOME_CONST "
"then /tmp/snake_case_dir/file_with_name.py",
mode="strip",
)
output = _render_to_text(renderable)
assert "test_case_with_underscores" in output
assert "SOME_CONST" in output
assert "snake_case_dir" in output
assert "file_with_name" in output
def test_strip_mode_still_strips_boundary_underscore_emphasis():
renderable = _render_final_assistant_content(
"say _hi_ and __bold__ now",
mode="strip",
)
output = _render_to_text(renderable)
assert "say hi and bold now" in output
|