| 1 |
# BSD 2-Clause License |
| 2 |
# |
| 3 |
# Apprise - Push Notification Library. |
| 4 |
# Copyright (c) 2026, Chris Caron <[email protected]> |
| 5 |
# |
| 6 |
# Redistribution and use in source and binary forms, with or without |
| 7 |
# modification, are permitted provided that the following conditions are met: |
| 8 |
# |
| 9 |
# 1. Redistributions of source code must retain the above copyright notice, |
| 10 |
# this list of conditions and the following disclaimer. |
| 11 |
# |
| 12 |
# 2. Redistributions in binary form must reproduce the above copyright notice, |
| 13 |
# this list of conditions and the following disclaimer in the documentation |
| 14 |
# and/or other materials provided with the distribution. |
| 15 |
# |
| 16 |
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" |
| 17 |
# AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
| 18 |
# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE |
| 19 |
# ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE |
| 20 |
# LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR |
| 21 |
# CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF |
| 22 |
# SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS |
| 23 |
# INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN |
| 24 |
# CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) |
| 25 |
# ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE |
| 26 |
# POSSIBILITY OF SUCH DAMAGE. |
| 27 |
|
| 28 |
import random |
| 29 |
|
| 30 |
from apprise.asset import AppriseAsset |
| 31 |
from apprise.common import NotifyFormat |
| 32 |
from apprise.plugins.matrix import NotifyMatrix, sizing |
| 33 |
|
| 34 |
|
| 35 |
def test_plugin_matrix_utf8_char_len(): |
| 36 |
"""Calculate UTF-8 widths without encoding each character.""" |
| 37 |
assert sizing.utf8_char_len("a") == 1 |
| 38 |
assert sizing.utf8_char_len("é") == 2 |
| 39 |
assert sizing.utf8_char_len("中") == 3 |
| 40 |
assert sizing.utf8_char_len("\U0001f600") == 4 |
| 41 |
|
| 42 |
|
| 43 |
def test_plugin_matrix_json_char_bytes(): |
| 44 |
"""Include JSON escaping in each character's byte cost.""" |
| 45 |
assert sizing.json_char_bytes("a") == 1 |
| 46 |
assert sizing.json_char_bytes('"') == 2 |
| 47 |
assert sizing.json_char_bytes("\\") == 2 |
| 48 |
|
| 49 |
# Short control escapes use two bytes. |
| 50 |
for ch in ("\b", "\f", "\n", "\r", "\t"): |
| 51 |
assert sizing.json_char_bytes(ch) == 2 |
| 52 |
|
| 53 |
# Other control characters use the six-byte Unicode form. |
| 54 |
assert sizing.json_char_bytes("\x00") == 6 |
| 55 |
assert sizing.json_char_bytes("\x1f") == 6 |
| 56 |
assert sizing.json_char_bytes("é") == 2 |
| 57 |
assert sizing.json_char_bytes("中") == 3 |
| 58 |
assert sizing.json_char_bytes("\U0001f600") == 4 |
| 59 |
|
| 60 |
|
| 61 |
def test_plugin_matrix_max_byte_budget(): |
| 62 |
"""Find the largest input accepted by the event cost model.""" |
| 63 |
assert sizing.max_byte_budget(lambda value: value + 10, 110, 1000) == 100 |
| 64 |
assert sizing.max_byte_budget(lambda value: value + 1000, 10, 1000) == 0 |
| 65 |
|
| 66 |
|
| 67 |
def test_plugin_matrix_sanitize_text(): |
| 68 |
"""Replace lone surrogates while preserving regular text.""" |
| 69 |
assert sizing.sanitize_text(None) is None |
| 70 |
assert sizing.sanitize_text("") == "" |
| 71 |
assert sizing.sanitize_text("hello") == "hello" |
| 72 |
|
| 73 |
sanitized = sizing.sanitize_text("a" + chr(0xD800) + "b") |
| 74 |
assert sanitized == "a?b" |
| 75 |
sanitized.encode("utf-8") |
| 76 |
|
| 77 |
|
| 78 |
def test_plugin_matrix_trim_bounds(): |
| 79 |
"""Find normal strip boundaries without copying the input.""" |
| 80 |
assert sizing.rstrip_end("hello") == len("hello") |
| 81 |
assert sizing.rstrip_end("hello ") == len("hello") |
| 82 |
assert sizing.rstrip_end(" ") == 0 |
| 83 |
assert sizing.rstrip_end("") == 0 |
| 84 |
|
| 85 |
assert sizing.strip_bounds("hello") == (0, len("hello")) |
| 86 |
text = " hello " |
| 87 |
start, end = sizing.strip_bounds(text) |
| 88 |
assert text[start:end] == "hello" |
| 89 |
|
| 90 |
start, end = sizing.strip_bounds(" ") |
| 91 |
assert start == end |
| 92 |
assert sizing.strip_bounds("") == (0, 0) |
| 93 |
|
| 94 |
|
| 95 |
def test_plugin_matrix_payload_budget(): |
| 96 |
"""Match every branch of the asset's payload-cap allocation.""" |
| 97 |
assert sizing.payload_budget(AppriseAsset(), 500, 500) == (500, 500) |
| 98 |
|
| 99 |
asset = AppriseAsset(payload_max_size=100) |
| 100 |
assert sizing.payload_budget(asset, 10, 10) == (10, 10) |
| 101 |
assert sizing.payload_budget(asset, 0, 5000) == (0, 100) |
| 102 |
assert sizing.payload_budget(asset, 5000, 0) == (100, 0) |
| 103 |
assert sizing.payload_budget(asset, 5, 5000) == (5, 95) |
| 104 |
assert sizing.payload_budget(asset, 5000, 5) == (95, 5) |
| 105 |
assert sizing.payload_budget(asset, 5000, 5000) == (50, 50) |
| 106 |
|
| 107 |
# A local generator avoids changing randomness for unrelated tests. |
| 108 |
generator = random.Random(2026) |
| 109 |
for _ in range(500): |
| 110 |
cap = generator.choice([0, 1, 5, 25, 50, 100, 300]) |
| 111 |
threshold = generator.choice([1, 5, 10, 20]) |
| 112 |
min_buffer = generator.choice([1, 5, 25, 50]) |
| 113 |
title_len = generator.choice([0, 1, 5, 10, 50, 100, 500]) |
| 114 |
body_len = generator.choice([0, 1, 5, 10, 50, 100, 500, 5000]) |
| 115 |
|
| 116 |
asset = AppriseAsset( |
| 117 |
payload_max_size=cap, |
| 118 |
payload_buffer_threshold=threshold, |
| 119 |
payload_min_buffer=min_buffer, |
| 120 |
) |
| 121 |
real_title, real_body = asset.enforce_payload_max_size( |
| 122 |
"T" * title_len, "B" * body_len |
| 123 |
) |
| 124 |
assert sizing.payload_budget(asset, title_len, body_len) == ( |
| 125 |
len(real_title), |
| 126 |
len(real_body), |
| 127 |
) |
| 128 |
|
| 129 |
|
| 130 |
def test_plugin_matrix_title_overhead_bytes(): |
| 131 |
"""Measure title wrappers exactly as Matrix embeds them.""" |
| 132 |
|
| 133 |
def json_bytes(value): |
| 134 |
return sum(sizing.json_char_bytes(ch) for ch in value) |
| 135 |
|
| 136 |
escape_html = NotifyMatrix.escape_html |
| 137 |
assert sizing.title_overhead_bytes("", NotifyFormat.TEXT, escape_html) == 0 |
| 138 |
|
| 139 |
plain = sizing.title_overhead_bytes("hi", NotifyFormat.TEXT, escape_html) |
| 140 |
assert plain == json_bytes("# hi\r\n") |
| 141 |
|
| 142 |
html = sizing.title_overhead_bytes("hi", NotifyFormat.HTML, escape_html) |
| 143 |
assert html == json_bytes("# hi\r\n") + json_bytes("<h1>hi</h1>") |
| 144 |
|
| 145 |
markdown = sizing.title_overhead_bytes( |
| 146 |
"<b>", NotifyFormat.MARKDOWN, escape_html |
| 147 |
) |
| 148 |
escaped = escape_html("<b>", whitespace=False) |
| 149 |
assert markdown == json_bytes("# <b>\r\n") + json_bytes( |
| 150 |
f"<h1>{escaped}</h1>" |
| 151 |
) |
| 152 |
|
| 153 |
heavy = sizing.title_overhead_bytes( |
| 154 |
"\x01" * 10, NotifyFormat.TEXT, escape_html |
| 155 |
) |
| 156 |
assert heavy == json_bytes("# \r\n") + 10 * 6 |
| 157 |
|
| 158 |
|
| 159 |
def test_plugin_matrix_device_id_overhead_bytes(): |
| 160 |
"""Measure known device IDs and use a fallback for unknown IDs.""" |
| 161 |
assert ( |
| 162 |
sizing.device_id_overhead_bytes(None) |
| 163 |
== sizing.MATRIX_DEVICE_ID_FALLBACK_BYTES |
| 164 |
) |
| 165 |
assert sizing.device_id_overhead_bytes("ABCDEFGHIJ") == 10 |
| 166 |
assert sizing.device_id_overhead_bytes("D" * 500) == 500 |
| 167 |
|
| 168 |
|
| 169 |
def test_plugin_matrix_title_sizing_stays_separate(): |
| 170 |
"""Keep Matrix title sizing independent from body chunk lengths.""" |
| 171 |
assert NotifyMatrix.title_maxlen == 250 |
| 172 |
assert NotifyMatrix.overflow_amalgamate_title is False |
| 173 |
|
| 174 |
|
| 175 |
def test_plugin_matrix_fit_limit_performance(monkeypatch): |
| 176 |
"""Keep the sizing scan linear for large messages.""" |
| 177 |
|
| 178 |
def cost_fn(window_bytes): |
| 179 |
return window_bytes + 200 |
| 180 |
|
| 181 |
length = 1_000_000 |
| 182 |
body = "a" * length |
| 183 |
calls = {"count": 0} |
| 184 |
real_json_char_bytes = sizing.json_char_bytes |
| 185 |
|
| 186 |
def counting_json_char_bytes(ch): |
| 187 |
calls["count"] += 1 |
| 188 |
return real_json_char_bytes(ch) |
| 189 |
|
| 190 |
monkeypatch.setattr(sizing, "json_char_bytes", counting_json_char_bytes) |
| 191 |
result = sizing.fit_char_limit(body, 61536, cost_fn) |
| 192 |
|
| 193 |
assert result > 0 |
| 194 |
assert calls["count"] <= length * 3 |