|
| 1 | +"""Strict _remend parity tests. |
| 2 | +
|
| 3 | +Documents the exact output of _remend() for known edge cases. These tests |
| 4 | +are not weakened -- they assert exact output strings. If _remend behavior |
| 5 | +changes, these tests must be updated deliberately. |
| 6 | +
|
| 7 | +Categories: |
| 8 | +1. Basic emphasis closing (*, **, ***, _, __, ___) |
| 9 | +2. Mixed emphasis (* and _ together) |
| 10 | +3. Inline code near emphasis |
| 11 | +4. Links/brackets with emphasis |
| 12 | +5. Code fences |
| 13 | +6. Strikethrough |
| 14 | +7. Idempotency (applying _remend twice produces same result) |
| 15 | +8. Known divergences from TS remend (documented with expected TS output) |
| 16 | +""" |
| 17 | + |
| 18 | +from __future__ import annotations |
| 19 | + |
| 20 | +import pytest |
| 21 | + |
| 22 | +from chat_sdk.shared.streaming_markdown import StreamingMarkdownRenderer, _is_clean, _remend |
| 23 | + |
| 24 | + |
| 25 | +class TestBasicEmphasisClosing: |
| 26 | + """Single-type emphasis repair.""" |
| 27 | + |
| 28 | + def test_unclosed_bold(self): |
| 29 | + assert _remend("Hello **wor") == "Hello **wor**" |
| 30 | + |
| 31 | + def test_unclosed_italic(self): |
| 32 | + assert _remend("Hello *wor") == "Hello *wor*" |
| 33 | + |
| 34 | + def test_unclosed_bold_italic(self): |
| 35 | + result = _remend("Hello ***wor") |
| 36 | + # Should close both bold and italic |
| 37 | + assert result.endswith("*") or result.endswith("**") |
| 38 | + assert result.count("*") % 2 == 0 or "***" in result |
| 39 | + |
| 40 | + def test_closed_bold_is_unchanged(self): |
| 41 | + assert _remend("Hello **world**") == "Hello **world**" |
| 42 | + |
| 43 | + def test_closed_italic_is_unchanged(self): |
| 44 | + assert _remend("Hello *world*") == "Hello *world*" |
| 45 | + |
| 46 | + def test_underscore_bold(self): |
| 47 | + assert _remend("Hello __wor") == "Hello __wor__" |
| 48 | + |
| 49 | + def test_underscore_italic(self): |
| 50 | + assert _remend("Hello _wor") == "Hello _wor_" |
| 51 | + |
| 52 | + def test_closed_underscore_unchanged(self): |
| 53 | + assert _remend("Hello __world__") == "Hello __world__" |
| 54 | + |
| 55 | + |
| 56 | +class TestMixedEmphasis: |
| 57 | + """Mixed * and _ emphasis.""" |
| 58 | + |
| 59 | + def test_star_bold_with_underscore_italic(self): |
| 60 | + result = _remend("**bold _italic") |
| 61 | + assert "**" in result # bold should be closed |
| 62 | + assert "_" in result # italic should be closed |
| 63 | + |
| 64 | + def test_underscore_bold_with_star_italic(self): |
| 65 | + result = _remend("__bold *italic") |
| 66 | + assert "__" in result |
| 67 | + assert "*" in result |
| 68 | + |
| 69 | + |
| 70 | +class TestInlineCodeNearEmphasis: |
| 71 | + """Emphasis markers inside code spans should be ignored.""" |
| 72 | + |
| 73 | + def test_emphasis_inside_code_ignored(self): |
| 74 | + # The * inside backticks should not be counted |
| 75 | + assert _remend("`**bold**` and *italic") == "`**bold**` and *italic*" |
| 76 | + |
| 77 | + def test_unclosed_backtick(self): |
| 78 | + result = _remend("Hello `code") |
| 79 | + assert result == "Hello `code`" |
| 80 | + |
| 81 | + def test_closed_code_with_unclosed_bold_after(self): |
| 82 | + result = _remend("`code` **bold") |
| 83 | + assert result == "`code` **bold**" |
| 84 | + |
| 85 | + def test_emphasis_markers_in_code_dont_count(self): |
| 86 | + # Stars inside code spans should not affect emphasis counting |
| 87 | + text = "`***` and **bold" |
| 88 | + result = _remend(text) |
| 89 | + assert result.endswith("**") |
| 90 | + |
| 91 | + |
| 92 | +class TestLinksBrackets: |
| 93 | + """Unclosed link brackets.""" |
| 94 | + |
| 95 | + def test_unclosed_link(self): |
| 96 | + result = _remend("See [link text") |
| 97 | + assert result == "See [link text]" |
| 98 | + |
| 99 | + def test_unclosed_nested_brackets(self): |
| 100 | + result = _remend("See [outer [inner") |
| 101 | + assert result.endswith("]]") |
| 102 | + |
| 103 | + def test_closed_link_unchanged(self): |
| 104 | + assert _remend("[link](url)") == "[link](url)" |
| 105 | + |
| 106 | + def test_escaped_bracket_ignored(self): |
| 107 | + result = _remend("See \\[not a link and [real link") |
| 108 | + assert result.count("]") == 1 # only close the real bracket |
| 109 | + |
| 110 | + |
| 111 | +class TestCodeFences: |
| 112 | + """Code fence closing.""" |
| 113 | + |
| 114 | + def test_unclosed_code_fence(self): |
| 115 | + result = _remend("```python\ncode here") |
| 116 | + assert result.endswith("\n```") |
| 117 | + |
| 118 | + def test_closed_code_fence_unchanged(self): |
| 119 | + text = "```python\ncode\n```" |
| 120 | + assert _remend(text) == text |
| 121 | + |
| 122 | + def test_emphasis_inside_fence_ignored(self): |
| 123 | + # Emphasis markers inside code fences should not be counted |
| 124 | + text = "```\n**bold** and *italic*\n```\noutside **unclosed" |
| 125 | + result = _remend(text) |
| 126 | + assert result.endswith("**") |
| 127 | + |
| 128 | + def test_tilde_fence(self): |
| 129 | + result = _remend("~~~\ncode here") |
| 130 | + assert result.endswith("\n```") |
| 131 | + |
| 132 | + |
| 133 | +class TestStrikethrough: |
| 134 | + """Strikethrough ~~ closing.""" |
| 135 | + |
| 136 | + def test_unclosed_strikethrough(self): |
| 137 | + result = _remend("Hello ~~strike") |
| 138 | + assert result == "Hello ~~strike~~" |
| 139 | + |
| 140 | + def test_closed_strikethrough_unchanged(self): |
| 141 | + assert _remend("Hello ~~strike~~") == "Hello ~~strike~~" |
| 142 | + |
| 143 | + |
| 144 | +class TestIdempotency: |
| 145 | + """Applying _remend twice must produce the same result.""" |
| 146 | + |
| 147 | + @pytest.mark.parametrize( |
| 148 | + "text", |
| 149 | + [ |
| 150 | + "Hello **wor", |
| 151 | + "Hello *wor", |
| 152 | + "Hello ***wor", |
| 153 | + "Hello __wor", |
| 154 | + "Hello _wor", |
| 155 | + "Hello ~~strike", |
| 156 | + "Hello `code", |
| 157 | + "See [link", |
| 158 | + "```\ncode", |
| 159 | + "clean text with no markers", |
| 160 | + "**bold** and *italic", |
| 161 | + "mixed **bold _italic", |
| 162 | + "`code **bold`", |
| 163 | + ], |
| 164 | + ) |
| 165 | + def test_idempotent(self, text: str): |
| 166 | + once = _remend(text) |
| 167 | + twice = _remend(once) |
| 168 | + assert once == twice, f"Not idempotent: _remend({text!r}) = {once!r}, _remend again = {twice!r}" |
| 169 | + |
| 170 | + |
| 171 | +class TestIsClean: |
| 172 | + """_is_clean returns True when _remend adds nothing.""" |
| 173 | + |
| 174 | + def test_clean_text(self): |
| 175 | + assert _is_clean("Hello world") |
| 176 | + |
| 177 | + def test_clean_closed_bold(self): |
| 178 | + assert _is_clean("Hello **world**") |
| 179 | + |
| 180 | + def test_unclosed_bold_is_not_clean(self): |
| 181 | + assert not _is_clean("Hello **wor") |
| 182 | + |
| 183 | + def test_unclosed_italic_is_not_clean(self): |
| 184 | + assert not _is_clean("Hello *wor") |
| 185 | + |
| 186 | + def test_unclosed_code_is_not_clean(self): |
| 187 | + assert not _is_clean("Hello `code") |
| 188 | + |
| 189 | + |
| 190 | +class TestRendererIntegration: |
| 191 | + """End-to-end streaming renderer tests for _remend behavior.""" |
| 192 | + |
| 193 | + def test_bold_repair_in_render(self): |
| 194 | + r = StreamingMarkdownRenderer() |
| 195 | + r.push("Hello **wor") |
| 196 | + result = r.render() |
| 197 | + assert result == "Hello **wor**" |
| 198 | + |
| 199 | + def test_italic_repair_in_render(self): |
| 200 | + r = StreamingMarkdownRenderer() |
| 201 | + r.push("Hello *wor") |
| 202 | + result = r.render() |
| 203 | + assert result == "Hello *wor*" |
| 204 | + |
| 205 | + def test_committable_holds_back_unclosed_bold(self): |
| 206 | + r = StreamingMarkdownRenderer() |
| 207 | + r.push("Hello **wor\n") |
| 208 | + committable = r.get_committable_text() |
| 209 | + # Line with unclosed ** should NOT be committed |
| 210 | + assert "**wor" not in committable |
| 211 | + |
| 212 | + def test_committable_releases_after_bold_closes(self): |
| 213 | + r = StreamingMarkdownRenderer() |
| 214 | + r.push("Hello **wor") |
| 215 | + assert r.get_committable_text() == "" |
| 216 | + r.push("ld** done\n") |
| 217 | + assert r.get_committable_text() == "Hello **world** done\n" |
| 218 | + |
| 219 | + def test_finish_repairs_everything(self): |
| 220 | + r = StreamingMarkdownRenderer() |
| 221 | + r.push("Hello **wor") |
| 222 | + result = r.finish() |
| 223 | + assert result == "Hello **wor**" |
| 224 | + assert r.get_text() == "Hello **wor" # raw text unchanged |
0 commit comments