Skip to content

Commit 5e30385

Browse files
authored
Merge pull request #4955 from yuedai-pbc/yuedai/public-safety-text-budget
test(public-safety): pin what a compacted value returns over its budget
2 parents 050483d + 522d517 commit 5e30385

1 file changed

Lines changed: 81 additions & 0 deletions

File tree

Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
1+
"""Budget rules for the shared public-safety text surface.
2+
3+
``compact_text`` is the bound every projection spends before it hands a value to
4+
a host, a dashboard or another tool, and 202 production call sites pass it an
5+
explicit ``limit``. These cases pin what a caller reads back when a value does
6+
not fit: how much survives, where the ellipsis goes, and that the refusal rules
7+
still fire inside the kept prefix.
8+
"""
9+
10+
import pytest
11+
12+
from loopx.control_plane.runtime.public_safety import (
13+
compact_text,
14+
public_safe_compact_text,
15+
)
16+
17+
# A synthetic credential shape, never a real one: enough of the pattern to be
18+
# refused and nothing else.
19+
SYNTHETIC_CREDENTIAL = "token=" + "abcdefghijklmn"
20+
21+
22+
@pytest.mark.parametrize("limit", [2, 3, 5, 12, 40])
23+
def test_a_value_over_budget_keeps_the_head_and_exactly_one_ellipsis(
24+
limit: int,
25+
) -> None:
26+
text = "y" * 80
27+
28+
compacted = compact_text(text, limit=limit)
29+
30+
assert compacted == text[: limit - 1] + "…"
31+
assert len(compacted) == limit
32+
assert compacted.endswith("…")
33+
34+
35+
def test_whitespace_is_collapsed_before_the_budget_is_spent() -> None:
36+
raw = " padded value "
37+
38+
assert len(raw) > 14
39+
assert compact_text(raw, limit=14) == "padded value"
40+
assert "…" not in compact_text(raw, limit=14)
41+
42+
43+
def test_a_value_that_exactly_fits_is_returned_without_an_ellipsis() -> None:
44+
text = "abcdefghij"
45+
46+
assert compact_text(text, limit=len(text)) == text
47+
assert len(compact_text(text, limit=len(text) - 1)) == len(text) - 1
48+
49+
50+
def test_the_ellipsis_replaces_a_character_rather_than_adding_one() -> None:
51+
compacted = compact_text("alpha beta\tgamma " + "x" * 40, limit=12)
52+
53+
assert compacted == "alpha beta…"
54+
assert len(compacted) <= 12
55+
56+
57+
def test_values_are_coerced_before_the_budget_and_the_empty_stays_empty() -> None:
58+
assert compact_text(123456, limit=3) == "12…"
59+
assert compact_text(None, limit=5) == ""
60+
assert public_safe_compact_text(None, limit=20) is None
61+
62+
63+
def test_default_budget_is_two_hundred_twenty_characters() -> None:
64+
compacted = public_safe_compact_text("w" * 400)
65+
66+
assert compacted is not None
67+
assert len(compacted) == 220
68+
assert compacted.endswith("…")
69+
70+
71+
def test_a_credential_inside_the_kept_prefix_is_still_refused() -> None:
72+
assert (
73+
public_safe_compact_text(
74+
f"note {SYNTHETIC_CREDENTIAL}",
75+
limit=len(SYNTHETIC_CREDENTIAL) + 8,
76+
)
77+
is None
78+
)
79+
assert public_safe_compact_text("note ordinary prose", limit=40) == (
80+
"note ordinary prose"
81+
)

0 commit comments

Comments
 (0)