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
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
@testable import Grain
import XCTest
final class BlueskyPostTests: GrainTestCase {
private let url = "https://grain.social/profile/did:plc:abc/gallery/xyz"
/// POI with distinct name — append locality, region, country
func testBuildPostText_POILocation_IncludesFullContext() {
let text = BlueskyPost.buildPostText(
url: url,
title: "2025-10-12",
location: (
name: "Overlook Mountain Fire Tower",
address: [
"locality": AnyCodable("Town of Woodstock"),
"region": AnyCodable("New York"),
"country": AnyCodable("US"),
]
),
description: nil
)
XCTAssertEqual(text, """
2025-10-12
📍 Overlook Mountain Fire Tower, Town of Woodstock, New York, US
#GrainSocial \(url)
""")
}
/// Coffee shop POI — primary label differs from locality, so locality is kept.
func testBuildPostText_POICafe_IncludesLocality() {
let text = BlueskyPost.buildPostText(
url: url,
title: nil,
location: (
name: "Blue Bottle Coffee",
address: [
"locality": AnyCodable("Oakland"),
"region": AnyCodable("California"),
"country": AnyCodable("US"),
]
),
description: nil
)
XCTAssertTrue(text.contains("📍 Blue Bottle Coffee, Oakland, California, US"),
"Got: \(text)")
}
/// Nominatim city fallback — `name` already contains locality/region/country.
/// Must NOT duplicate by appending region + country.
func testBuildPostText_CityName_DoesNotDuplicate() {
let text = BlueskyPost.buildPostText(
url: url,
title: "2025-09-14",
location: (
name: "New York, New York, United States",
address: [
"locality": AnyCodable("New York"),
"region": AnyCodable("New York"),
"country": AnyCodable("US"),
]
),
description: nil
)
XCTAssertEqual(text, """
2025-09-14
📍 New York, US
#GrainSocial \(url)
""")
}
/// `name` already includes the state abbreviation. Must not repeat it.
func testBuildPostText_NameWithStateAbbrev_DoesNotDuplicate() {
let text = BlueskyPost.buildPostText(
url: url,
title: nil,
location: (
name: "Seattle, WA",
address: [
"locality": AnyCodable("Seattle"),
"region": AnyCodable("WA"),
"country": AnyCodable("US"),
]
),
description: nil
)
XCTAssertTrue(text.contains("📍 Seattle, WA, US"), "Got: \(text)")
XCTAssertFalse(text.contains("Seattle, WA, Seattle"))
XCTAssertFalse(text.contains("WA, WA"))
}
/// Real story from moll.blue: `name` contains county baked in by older client.
/// Must render as "Kansas City, Missouri, US" — no county, no duplication.
func testBuildPostText_NameWithEmbeddedCounty_SkipsCounty() {
let text = BlueskyPost.buildPostText(
url: url,
title: nil,
location: (
name: "Kansas City, Jackson County, Missouri, United States",
address: [
"locality": AnyCodable("Kansas City"),
"region": AnyCodable("Missouri"),
"country": AnyCodable("US"),
]
),
description: nil
)
XCTAssertTrue(text.contains("📍 Kansas City, Missouri, US"), "Got: \(text)")
XCTAssertFalse(text.contains("County"), "Got: \(text)")
}
/// POI where region differs from the primary label.
func testBuildPostText_CityWithDistinctRegion_IncludesRegion() {
let text = BlueskyPost.buildPostText(
url: url,
title: nil,
location: (
name: "Albany, New York, United States",
address: [
"locality": AnyCodable("Albany"),
"region": AnyCodable("New York"),
"country": AnyCodable("US"),
]
),
description: nil
)
XCTAssertTrue(text.contains("📍 Albany, New York, US"),
"Got: \(text)")
}
func testBuildPostText_TitleAndDescription_JoinedWithComma() {
let text = BlueskyPost.buildPostText(
url: url,
title: "Sunset at the Beach",
location: nil,
description: "A lovely evening"
)
XCTAssertTrue(text.hasPrefix("Sunset at the Beach, A lovely evening"))
}
/// Legacy community.lexicon.location.hthree records have no address.
/// Name should be preserved as-is — don't strip context after first comma.
func testBuildPostText_LegacyHthreeRecord_PreservesFullName() {
let text = BlueskyPost.buildPostText(
url: url,
title: nil,
location: (name: "Eindhoven, North Brabant, Netherlands", address: nil),
description: nil
)
XCTAssertTrue(text.contains("📍 Eindhoven, North Brabant, Netherlands"),
"Got: \(text)")
}
func testBuildPostText_NoLocationNoContent_ProducesCleanSuffix() {
let text = BlueskyPost.buildPostText(
url: url,
title: nil,
location: nil,
description: nil
)
XCTAssertEqual(text, "\n#GrainSocial \(url)")
}
func testBuildPostText_AddressOnlyCountry_StillRenders() {
let text = BlueskyPost.buildPostText(
url: url,
title: "Trip",
location: (
name: "Eiffel Tower",
address: ["country": AnyCodable("FR")]
),
description: nil
)
XCTAssertTrue(text.contains("📍 Eiffel Tower, FR"))
}
/// A long title+description used to truncate to exactly 301 graphemes —
/// one over the lexicon cap — and the PDS silently rejected the cross-post.
func testBuildPostText_LongDescription_FitsGraphemeLimit() {
let text = BlueskyPost.buildPostText(
url: url,
title: "Autumn walk",
location: (
name: "Kansas City",
address: [
"locality": AnyCodable("Kansas City"),
"region": AnyCodable("Missouri"),
"country": AnyCodable("US"),
]
),
description: String(repeating: "a", count: 600)
)
XCTAssertEqual(text.count, 300)
XCTAssertLessThanOrEqual(text.utf8.count, 3000)
XCTAssertTrue(text.hasSuffix("#GrainSocial \(url)"))
XCTAssertTrue(text.contains("…"))
}
/// Multi-byte content is capped by graphemes, not by bytes.
func testBuildPostText_LongEmojiDescription_FitsBothLimits() {
let text = BlueskyPost.buildPostText(
url: url,
title: nil,
location: nil,
description: String(repeating: "🌄", count: 400)
)
XCTAssertLessThanOrEqual(text.count, 300)
XCTAssertLessThanOrEqual(text.utf8.count, 3000)
}
/// A URL long enough to eat the whole budget drops the content rather than
/// emitting a lone ellipsis that still overflows.
func testBuildPostText_SuffixFillsBudget_DropsContent() {
let longURL = "https://grain.social/" + String(repeating: "x", count: 400)
let text = BlueskyPost.buildPostText(
url: longURL,
title: "Autumn walk",
location: nil,
description: "A lovely evening"
)
XCTAssertEqual(text, "\n#GrainSocial \(longURL)")
}
}