| 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 |
from apprise import NotifyFormat |
| 28 |
from apprise.utils.format import html_adjust, markdown_adjust, smart_split |
| 29 |
|
| 30 |
|
| 31 |
def test_smart_split_prefers_newlines_over_spaces_and_punctuation(): |
| 32 |
""" |
| 33 |
Newlines should win even if there are spaces and punctuation before the |
| 34 |
limit. |
| 35 |
""" |
| 36 |
text = "line1\nline2 line3. line4" |
| 37 |
|
| 38 |
limit = 12 |
| 39 |
|
| 40 |
chunks = smart_split(text, limit, body_format=NotifyFormat.TEXT) |
| 41 |
|
| 42 |
|
| 43 |
assert chunks[0] == "line1\n" |
| 44 |
|
| 45 |
assert "".join(chunks) == text |
| 46 |
|
| 47 |
|
| 48 |
def test_smart_split_prefers_spaces_over_hard_split(): |
| 49 |
""" |
| 50 |
When there are no newlines, split on the last space/tab before falling back |
| 51 |
to a hard character-limit split. |
| 52 |
""" |
| 53 |
text = "word1 word2 word3" |
| 54 |
|
| 55 |
limit = 12 |
| 56 |
|
| 57 |
chunks = smart_split(text, limit, body_format=NotifyFormat.TEXT) |
| 58 |
|
| 59 |
assert chunks == ["word1 word2 ", "word3"] |
| 60 |
assert "".join(chunks) == text |
| 61 |
|
| 62 |
|
| 63 |
def test_smart_split_can_split_after_punctuation_plus_whitespace(): |
| 64 |
""" |
| 65 |
Exercise the punctuation+whitespace pattern. In practice this collapses |
| 66 |
to the same split point as the last space, but we verify the behaviour. |
| 67 |
""" |
| 68 |
text = "Hello world. Again" |
| 69 |
|
| 70 |
|
| 71 |
|
| 72 |
limit = 13 |
| 73 |
|
| 74 |
chunks = smart_split(text, limit, body_format=NotifyFormat.TEXT) |
| 75 |
|
| 76 |
|
| 77 |
assert chunks[0] == "Hello world. " |
| 78 |
assert chunks[1] == "Again" |
| 79 |
assert "".join(chunks) == text |
| 80 |
|
| 81 |
|
| 82 |
def test_smart_split_avoids_splitting_inside_html_entity() -> None: |
| 83 |
""" |
| 84 |
In HTML mode we must not end a chunk in the middle of '&...;'. |
| 85 |
|
| 86 |
We do NOT assert exact chunk values. Instead we assert: |
| 87 |
- TEXT mode can split inside the entity. |
| 88 |
- HTML mode never has a chunk that contains '&' without a matching ';' |
| 89 |
after it in the same chunk. |
| 90 |
""" |
| 91 |
text = "1234 5678" |
| 92 |
limit = 8 |
| 93 |
|
| 94 |
|
| 95 |
chunks_text = smart_split(text, limit, body_format=NotifyFormat.TEXT) |
| 96 |
assert "".join(chunks_text) == text |
| 97 |
|
| 98 |
|
| 99 |
assert any( |
| 100 |
"&" in chunk and ";" not in chunk[chunk.find("&") :] |
| 101 |
for chunk in chunks_text |
| 102 |
) |
| 103 |
|
| 104 |
|
| 105 |
chunks_html = smart_split(text, limit, body_format=NotifyFormat.HTML) |
| 106 |
assert "".join(chunks_html) == text |
| 107 |
|
| 108 |
|
| 109 |
|
| 110 |
for chunk in chunks_html: |
| 111 |
idx = chunk.find("&") |
| 112 |
if idx == -1: |
| 113 |
continue |
| 114 |
semi = chunk.find(";", idx + 1) |
| 115 |
assert semi != -1, f"Chunk ends inside HTML entity: {chunk!r}" |
| 116 |
|
| 117 |
|
| 118 |
def test_smart_split_avoids_splitting_inside_markdown_link() -> None: |
| 119 |
""" |
| 120 |
In MARKDOWN mode, do not split inside [text](url). |
| 121 |
|
| 122 |
We only require that the full [link](...) lies in a single chunk and that |
| 123 |
any '[' appearing in a chunk has a matching ')' in that same chunk. |
| 124 |
""" |
| 125 |
link = "[link](https://example.com)" |
| 126 |
text = "AAAA" + link |
| 127 |
limit = len(link) |
| 128 |
|
| 129 |
chunks = smart_split(text, limit, body_format=NotifyFormat.MARKDOWN) |
| 130 |
assert "".join(chunks) == text |
| 131 |
|
| 132 |
|
| 133 |
assert any(link in chunk for chunk in chunks) |
| 134 |
|
| 135 |
|
| 136 |
for chunk in chunks: |
| 137 |
idx = chunk.find("[") |
| 138 |
if idx == -1: |
| 139 |
continue |
| 140 |
semi = chunk.find(")", idx + 1) |
| 141 |
assert semi != -1, f"Markdown link was split inside chunk: {chunk!r}" |
| 142 |
|
| 143 |
|
| 144 |
def test_smart_split_avoids_splitting_inside_markdown_image() -> None: |
| 145 |
""" |
| 146 |
In MARKDOWN mode, do not split inside the [alt](url) of an image. |
| 147 |
|
| 148 |
The implementation currently splits "AAAA" as: |
| 149 |
- "AAAA!" |
| 150 |
- "[alt](...)" |
| 151 |
which is acceptable, as the [alt](url) part is kept intact. |
| 152 |
""" |
| 153 |
image = "" |
| 154 |
text = "AAAA" + image |
| 155 |
limit = len(image) |
| 156 |
|
| 157 |
chunks = smart_split(text, limit, body_format=NotifyFormat.MARKDOWN) |
| 158 |
assert "".join(chunks) == text |
| 159 |
|
| 160 |
inner = "[alt](https://example.com/image.png)" |
| 161 |
|
| 162 |
|
| 163 |
assert any(inner in chunk for chunk in chunks) |
| 164 |
|
| 165 |
|
| 166 |
|
| 167 |
for chunk in chunks: |
| 168 |
idx = chunk.find("[") |
| 169 |
if idx == -1: |
| 170 |
continue |
| 171 |
semi = chunk.find(")", idx + 1) |
| 172 |
assert semi != -1, f"Markdown image was split inside chunk: {chunk!r}" |
| 173 |
|
| 174 |
|
| 175 |
def test_smart_split_empty_and_none_input() -> None: |
| 176 |
""" |
| 177 |
Empty / None input should be returned as a single-element list unchanged. |
| 178 |
""" |
| 179 |
assert smart_split("", 10, body_format=NotifyFormat.TEXT) == [""] |
| 180 |
assert smart_split("", 0, body_format=NotifyFormat.TEXT) == [""] |
| 181 |
assert smart_split("content", 0, body_format=NotifyFormat.TEXT) == [""] |
| 182 |
|
| 183 |
assert smart_split(None, 10, body_format=NotifyFormat.TEXT) == [""] |
| 184 |
|
| 185 |
|
| 186 |
def test_smart_split_html_entity_exact_boundary() -> None: |
| 187 |
""" |
| 188 |
Splitting exactly at an HTML entity boundary should not shift the split |
| 189 |
point (no need to "fix up" a perfectly aligned boundary). |
| 190 |
""" |
| 191 |
text = "AAAA BBBB" |
| 192 |
limit = len("AAAA ") |
| 193 |
|
| 194 |
chunks = smart_split(text, limit, body_format=NotifyFormat.HTML) |
| 195 |
|
| 196 |
|
| 197 |
assert chunks == ["AAAA ", "BBBB"] |
| 198 |
assert "".join(chunks) == text |
| 199 |
|
| 200 |
|
| 201 |
def test_smart_split_markdown_link_exact_boundary() -> None: |
| 202 |
""" |
| 203 |
Splitting exactly after a Markdown link should not cause any adjustment. |
| 204 |
""" |
| 205 |
link = "[link](https://example.com)" |
| 206 |
tail = " TAIL" |
| 207 |
text = link + tail |
| 208 |
limit = len(link) |
| 209 |
|
| 210 |
chunks = smart_split(text, limit, body_format=NotifyFormat.MARKDOWN) |
| 211 |
|
| 212 |
|
| 213 |
assert chunks[0] == link |
| 214 |
assert "".join(chunks) == text |
| 215 |
|
| 216 |
|
| 217 |
assert any(link in chunk for chunk in chunks) |
| 218 |
|
| 219 |
|
| 220 |
def test_smart_split_whitespace_priority_with_tabs_and_newlines() -> None: |
| 221 |
""" |
| 222 |
Exercise newline vs space/tab priority with a mix of whitespace. |
| 223 |
""" |
| 224 |
text = "word1\tword2\nword3" |
| 225 |
|
| 226 |
|
| 227 |
limit_without_newline = text.index("\n") |
| 228 |
chunks_no_nl = smart_split( |
| 229 |
text, limit_without_newline, body_format=NotifyFormat.TEXT |
| 230 |
) |
| 231 |
|
| 232 |
assert chunks_no_nl[0] == "word1\t" |
| 233 |
assert "".join(chunks_no_nl) == text |
| 234 |
|
| 235 |
|
| 236 |
limit_with_newline = text.index("\n") + 1 |
| 237 |
chunks_with_nl = smart_split( |
| 238 |
text, limit_with_newline, body_format=NotifyFormat.TEXT |
| 239 |
) |
| 240 |
|
| 241 |
assert chunks_with_nl[0] == "word1\tword2\n" |
| 242 |
assert "".join(chunks_with_nl) == text |
| 243 |
|
| 244 |
|
| 245 |
def test_smart_split_very_short_limit() -> None: |
| 246 |
""" |
| 247 |
Very small limits should still split deterministically without loss. |
| 248 |
""" |
| 249 |
text = "ABC" |
| 250 |
chunks = smart_split(text, 1, body_format=NotifyFormat.TEXT) |
| 251 |
|
| 252 |
|
| 253 |
assert chunks == ["A", "B", "C"] |
| 254 |
assert "".join(chunks) == text |
| 255 |
|
| 256 |
|
| 257 |
def test_smart_split_very_long_limit() -> None: |
| 258 |
""" |
| 259 |
Very large limits (>= len(text)) should return a single chunk. |
| 260 |
""" |
| 261 |
text = "A short message for testing" |
| 262 |
chunks = smart_split(text, 10_000, body_format=NotifyFormat.TEXT) |
| 263 |
|
| 264 |
assert chunks == [text] |
| 265 |
assert "".join(chunks) == text |
| 266 |
|
| 267 |
|
| 268 |
def test_html_adjust_guard_paths_and_no_entity() -> None: |
| 269 |
""" |
| 270 |
Cover the early-return guard in html_adjust and the path where there is |
| 271 |
no '&' at all in the search window. |
| 272 |
""" |
| 273 |
text = "abcdef" |
| 274 |
|
| 275 |
|
| 276 |
assert html_adjust(text, window_start=2, split_at=2) == 2 |
| 277 |
|
| 278 |
|
| 279 |
assert ( |
| 280 |
html_adjust(text, window_start=0, split_at=len(text) + 5) |
| 281 |
== len(text) + 5 |
| 282 |
) |
| 283 |
|
| 284 |
|
| 285 |
assert html_adjust(text, window_start=0, split_at=3) == 3 |
| 286 |
|
| 287 |
|
| 288 |
def test_html_adjust_inside_and_at_boundary_of_entity() -> None: |
| 289 |
""" |
| 290 |
Exercise the path where html_adjust moves the split back to '&' when the |
| 291 |
split falls inside an entity, and the path where the split is exactly at |
| 292 |
the entity boundary and should not move. |
| 293 |
""" |
| 294 |
text = "1234 5678" |
| 295 |
|
| 296 |
|
| 297 |
|
| 298 |
assert html_adjust(text, window_start=0, split_at=8) == 4 |
| 299 |
|
| 300 |
|
| 301 |
assert html_adjust(text, window_start=0, split_at=10) == 10 |
| 302 |
|
| 303 |
|
| 304 |
def test_markdown_adjust_guard_and_no_construct() -> None: |
| 305 |
""" |
| 306 |
Cover the guard in markdown_adjust and the case where there is no |
| 307 |
'[' or '!' in the window. |
| 308 |
""" |
| 309 |
text = "plain text" |
| 310 |
|
| 311 |
|
| 312 |
assert markdown_adjust(text, window_start=4, split_at=4) == 4 |
| 313 |
|
| 314 |
|
| 315 |
assert ( |
| 316 |
markdown_adjust(text, window_start=0, split_at=len(text) + 3) |
| 317 |
== len(text) + 3 |
| 318 |
) |
| 319 |
|
| 320 |
|
| 321 |
assert markdown_adjust(text, window_start=0, split_at=5) == 5 |
| 322 |
|
| 323 |
|
| 324 |
def test_markdown_adjust_inside_construct_moves_to_start() -> None: |
| 325 |
""" |
| 326 |
Exercise the positive path in markdown_adjust where the split lands |
| 327 |
inside a [text](url) construct and the function moves the split |
| 328 |
back to the start of the construct. |
| 329 |
|
| 330 |
The forward scan is capped at split_at + (split_at - window_start) + 1 |
| 331 |
to prevent O(n^2) work on adversarial input (see |
| 332 |
test_markdown_adjust_dos_cap). This means split_at must be at least half |
| 333 |
the link length so the cap reaches the closing ')'. Realistic splits from |
| 334 |
smart_split() always satisfy this because the following applies for any |
| 335 |
link that fits in a single chunk: |
| 336 |
split_at - window_start >= limit >= link_length // 2 |
| 337 |
""" |
| 338 |
link = "[link](https://example.com)" |
| 339 |
|
| 340 |
|
| 341 |
split_at = len(link) // 2 |
| 342 |
adjusted = markdown_adjust(link, window_start=0, split_at=split_at) |
| 343 |
|
| 344 |
|
| 345 |
assert adjusted == 0 |
| 346 |
|
| 347 |
|
| 348 |
def test_markdown_adjust_inside_angle_bracket_construct() -> None: |
| 349 |
"""Exercise the <URL|label> angle-bracket path in markdown_adjust. |
| 350 |
|
| 351 |
When a split point lands ANYWHERE inside a '<url|label>' construct (as |
| 352 |
used by Slack mrkdwn and Google Chat), the function must move it back to |
| 353 |
the opening '<'. This covers three sub-cases: |
| 354 |
|
| 355 |
(a) Split in the label portion (after the pipe). |
| 356 |
(b) Split exactly on the pipe character. |
| 357 |
(c) Split in the URL portion (before the pipe) -- previously the pipe |
| 358 |
search stopped at split_at so no pipe was found; the fix extends |
| 359 |
the search to forward_end so this case is now also protected. |
| 360 |
""" |
| 361 |
|
| 362 |
text = "<https://example.com|click here>" |
| 363 |
pipe_pos = text.index("|") |
| 364 |
|
| 365 |
|
| 366 |
split_at = pipe_pos + 3 |
| 367 |
adjusted = markdown_adjust(text, window_start=0, split_at=split_at) |
| 368 |
|
| 369 |
assert adjusted == 0 |
| 370 |
|
| 371 |
|
| 372 |
split_at2 = text.index(">") |
| 373 |
adjusted2 = markdown_adjust(text, window_start=0, split_at=split_at2) |
| 374 |
|
| 375 |
assert adjusted2 == 0 |
| 376 |
|
| 377 |
|
| 378 |
split_on_pipe = pipe_pos |
| 379 |
assert markdown_adjust(text, 0, split_on_pipe) == 0 |
| 380 |
|
| 381 |
|
| 382 |
|
| 383 |
|
| 384 |
|
| 385 |
|
| 386 |
split_in_url = pipe_pos - 3 |
| 387 |
assert markdown_adjust(text, 0, split_in_url) == 0 |
| 388 |
|
| 389 |
|
| 390 |
|
| 391 |
|
| 392 |
|
| 393 |
prefixed = "pre " + text |
| 394 |
split_in_url2 = 20 |
| 395 |
|
| 396 |
assert markdown_adjust(prefixed, 0, split_in_url2) == 4 |
| 397 |
|
| 398 |
|
| 399 |
|
| 400 |
|
| 401 |
|
| 402 |
|
| 403 |
split_too_small = 5 |
| 404 |
assert markdown_adjust(text, 0, split_too_small) == split_too_small |
| 405 |
|
| 406 |
|
| 407 |
|
| 408 |
|
| 409 |
split_past_end = len(text) |
| 410 |
assert markdown_adjust(text, 0, split_past_end) == split_past_end |
| 411 |
|
| 412 |
|
| 413 |
def test_markdown_adjust_dos_cap() -> None: |
| 414 |
"""markdown_adjust forward scan is capped to O(window_size). |
| 415 |
|
| 416 |
An adversarial body of repeated unterminated '[' openers must not |
| 417 |
cause O(n^2) work. We verify the cap by checking that a large body |
| 418 |
produces the same split position as a small one (the cap does not |
| 419 |
change the output for well-formed constructs within the window). |
| 420 |
""" |
| 421 |
from timeit import default_timer |
| 422 |
|
| 423 |
|
| 424 |
body = "[broken " * 128_000 |
| 425 |
limit = 80 |
| 426 |
start = default_timer() |
| 427 |
chunks = smart_split(body, limit, body_format=NotifyFormat.MARKDOWN) |
| 428 |
elapsed = default_timer() - start |
| 429 |
|
| 430 |
|
| 431 |
assert elapsed < 1.0 |
| 432 |
|
| 433 |
assert all(chunks) |
| 434 |
assert "".join(chunks) == body |
| 435 |
|
| 436 |
|
| 437 |
def test_smart_split_markdown_guard_split_at_start_is_reset() -> None: |
| 438 |
"""Cover smart_split guard 'if split_at <= start: split_at = orig_split'. |
| 439 |
|
| 440 |
We force markdown_adjust to move the split back to the window start |
| 441 |
(link_start_idx == start == 0), then verify smart_split resets to the |
| 442 |
original split so progress is still made and chunks join back to the |
| 443 |
original text. |
| 444 |
|
| 445 |
The link "[link](url)" is 11 chars. With limit=7 the first hard split |
| 446 |
lands at index 7 (inside the URL). The forward-scan cap is |
| 447 |
7 + (7 - 0) + 1 = 15, which is > 10 (position of ")"), so markdown_adjust |
| 448 |
finds the closer and moves split to link_start_idx=0. Since 0 == start, |
| 449 |
smart_split resets to orig_split=7 and emits the next chunk from there. |
| 450 |
""" |
| 451 |
text = "[link](url)" |
| 452 |
limit = 7 |
| 453 |
|
| 454 |
chunks = smart_split(text, limit, body_format=NotifyFormat.MARKDOWN) |
| 455 |
|
| 456 |
|
| 457 |
assert len(chunks) >= 2 |
| 458 |
assert all(chunks) |
| 459 |
|
| 460 |
|
| 461 |
assert "".join(chunks) == text |
| 462 |
|
| 463 |
|
| 464 |
def test_markdown_adjust_image_bang_opener_path() -> None: |
| 465 |
"""Cover the '!' opener path in markdown_adjust.""" |
| 466 |
|
| 467 |
text = "AAAAAA" |
| 468 |
assert markdown_adjust(text, window_start=0, split_at=7) == 6 |
| 469 |
|
| 470 |
|
| 471 |
|
| 472 |
text2 = "hello!world" |
| 473 |
assert markdown_adjust(text2, window_start=0, split_at=6) == 6 |
| 474 |
|
| 475 |
|
| 476 |
def test_smart_split_uses_punctuation_branch_on_rare_whitespace() -> None: |
| 477 |
""" |
| 478 |
When punctuation is followed by rare whitespace (vertical tab / form feed) |
| 479 |
and there are no spaces/tabs/newlines, we should use the punctuation |
| 480 |
+ whitespace split branch. |
| 481 |
""" |
| 482 |
vt = "\x0b" |
| 483 |
text = f"Hello.{vt}World" |
| 484 |
|
| 485 |
limit = len("Hello.") + 1 |
| 486 |
|
| 487 |
chunks = smart_split(text, limit, body_format=NotifyFormat.TEXT) |
| 488 |
|
| 489 |
assert "".join(chunks) == text |
| 490 |
|
| 491 |
assert chunks[0] == f"Hello.{vt}" |
| 492 |
assert chunks[1] == "World" |