a digital entity named phi that roams bsky
phi.zzstoatzz.io
1"""Regression tests for resolve_facet_links — ensures phi sees full URLs from facets."""
2
3from types import SimpleNamespace
4
5from bot.utils.thread import resolve_facet_links
6
7
8def _make_record(text, facets=None):
9 return SimpleNamespace(text=text, facets=facets)
10
11
12def _make_facet(byte_start, byte_end, uri):
13 return SimpleNamespace(
14 index=SimpleNamespace(byte_start=byte_start, byte_end=byte_end),
15 features=[SimpleNamespace(py_type="app.bsky.richtext.facet#link", uri=uri)],
16 )
17
18
19def test_no_facets():
20 record = _make_record("hello world")
21 assert resolve_facet_links(record) == "hello world"
22
23
24def test_none_facets():
25 record = _make_record("hello world", facets=None)
26 assert resolve_facet_links(record) == "hello world"
27
28
29def test_truncated_url_replaced():
30 """The exact bug from trace 019d5004 — bluesky truncated the URL in display text."""
31 text = (
32 "cool. onto something else\n\n"
33 "can you read this: www.letta.com/blog/context...\n\n"
34 "and tell me whether there's anything interesting as far as your M.O.?"
35 )
36 # byte offsets for "www.letta.com/blog/context..." in the text
37 encoded = text.encode("utf-8")
38 start = encoded.index(b"www.letta.com/blog/context...")
39 end = start + len(b"www.letta.com/blog/context...")
40
41 record = _make_record(
42 text,
43 facets=[
44 _make_facet(start, end, "https://www.letta.com/blog/context-constitution")
45 ],
46 )
47
48 result = resolve_facet_links(record)
49 assert "https://www.letta.com/blog/context-constitution" in result
50 assert "context..." not in result
51
52
53def test_multiple_links():
54 text = "check out link1... and link2..."
55 encoded = text.encode("utf-8")
56 s1 = encoded.index(b"link1...")
57 e1 = s1 + len(b"link1...")
58 s2 = encoded.index(b"link2...")
59 e2 = s2 + len(b"link2...")
60
61 record = _make_record(
62 text,
63 facets=[
64 _make_facet(s1, e1, "https://example.com/link1-full"),
65 _make_facet(s2, e2, "https://example.com/link2-full"),
66 ],
67 )
68
69 result = resolve_facet_links(record)
70 assert "https://example.com/link1-full" in result
71 assert "https://example.com/link2-full" in result
72 assert "link1..." not in result
73 assert "link2..." not in result
74
75
76def test_mention_facet_ignored():
77 """Mention facets should not affect the text."""
78 text = "hey @someone check this"
79 record = _make_record(
80 text,
81 facets=[
82 SimpleNamespace(
83 index=SimpleNamespace(byte_start=4, byte_end=12),
84 features=[
85 SimpleNamespace(
86 py_type="app.bsky.richtext.facet#mention",
87 did="did:plc:abc",
88 )
89 ],
90 )
91 ],
92 )
93 assert resolve_facet_links(record) == text
94
95
96def test_unicode_text_byte_offsets():
97 """Facet byte offsets are in UTF-8 bytes, not characters."""
98 # emoji is 4 bytes in UTF-8
99 text = "\U0001f600 see link..."
100 encoded = text.encode("utf-8")
101 start = encoded.index(b"link...")
102 end = start + len(b"link...")
103
104 record = _make_record(
105 text,
106 facets=[_make_facet(start, end, "https://example.com/full-link")],
107 )
108
109 result = resolve_facet_links(record)
110 assert "https://example.com/full-link" in result
111 assert "link..." not in result
112 assert result.startswith("\U0001f600")