Skip to content

Commit c1cdbcb

Browse files
committed
improve testing and fix a regex bug
1 parent 73aa2a9 commit c1cdbcb

File tree

3 files changed

+76
-20
lines changed

3 files changed

+76
-20
lines changed

README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ ltbsky is a lightweight library for posting to Bluesky.
77
- Create a post
88
- Embed images in a post
99
- Specify the language(s) of a post
10-
- Automatically parse web links and Bluesky mentions from a post
10+
- Automatically parse web links, hashtags, and Bluesky mentions from a post
1111
- Automatically reduce image size to fit within Bluesky's 1MB limit
1212

1313
## Examples
@@ -73,7 +73,7 @@ To specify each language used in a post, we add a call to
7373
```go
7474
// [continued from above]
7575

76-
postBuilder = ltbsky.NewPostBuilder("Hello, world! Hola, mundo!")
76+
postBuilder = ltbsky.NewPostBuilder("Hello, world! Hola, mundo! #hello")
7777
postBuilder.AddLang("en") // Add English language
7878
postBuilder.AddLang("es") // Add Spanish language
7979
uri, err = client.Post(postBuilder)

bsky.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -146,7 +146,7 @@ func (pb *PostBuilder) buildFor(server string, c *http.Client) (*postRequest, er
146146

147147
func (pb *PostBuilder) parseLinks() {
148148
// regex based on: https://docs.bsky.app/docs/advanced-guides/posts#mentions-and-links
149-
url_regex := `[$|\W](?P<url>https?:\/\/(www\.)?[-a-zA-Z0-9@:%._\+~#=]{1,256}\.[a-zA-Z0-9()]{1,6}\b([-a-zA-Z0-9()@:%_\+.~#?&//=]*[-a-zA-Z0-9@%_\+~#//=])?)`
149+
url_regex := `(?:^|\s|\W)(?P<url>https?:\/\/(www\.)?[-a-zA-Z0-9@:%._\+~#=]{1,256}\.[a-zA-Z0-9()]{1,6}\b([-a-zA-Z0-9()@:%_\+.~#?&//=]*[-a-zA-Z0-9@%_\+~#//=])?)`
150150
r, err := regexp.Compile(url_regex)
151151
if err != nil {
152152
log.Printf("Error compiling regex: %v", err)

bsky_test.go

Lines changed: 73 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -272,23 +272,79 @@ func TestAddImageFromBytes(t *testing.T) {
272272
}
273273

274274
func TestParseLinks(t *testing.T) {
275-
content := "Hello https://go.dev and https://pkg.go.dev!"
276-
pb := NewPostBuilder(content)
277-
pb.parseLinks()
278-
if len(pb.facets) != 2 {
279-
t.Errorf("wanted 2 facets, got %d", len(pb.facets))
280-
}
281-
if pb.facets[0].Index.ByteStart != 6 || pb.facets[0].Index.ByteEnd != 20 {
282-
t.Errorf("wanted first facet index [6,20], got [%d,%d]", pb.facets[0].Index.ByteStart, pb.facets[0].Index.ByteEnd)
283-
}
284-
if pb.facets[0].Features[0].Uri != "https://go.dev" {
285-
t.Errorf("wanted first facet URI 'https://go.dev', got '%s'", pb.facets[0].Features[0].Uri)
286-
}
287-
if pb.facets[1].Index.ByteStart != 25 || pb.facets[1].Index.ByteEnd != 43 {
288-
t.Errorf("wanted second facet index [25,43], got [%d,%d]", pb.facets[1].Index.ByteStart, pb.facets[1].Index.ByteEnd)
289-
}
290-
if pb.facets[1].Features[0].Uri != "https://pkg.go.dev" {
291-
t.Errorf("wanted second facet URI 'https://pkg.go.dev', got '%s'", pb.facets[1].Features[0].Uri)
275+
tests := []struct {
276+
name string
277+
content string
278+
expectedFacets []struct {
279+
ByteStart int
280+
ByteEnd int
281+
Uri string
282+
}
283+
}{
284+
{
285+
name: "Two links",
286+
content: "Hello https://go.dev and https://pkg.go.dev!",
287+
expectedFacets: []struct {
288+
ByteStart int
289+
ByteEnd int
290+
Uri string
291+
}{
292+
{ByteStart: 6, ByteEnd: 20, Uri: "https://go.dev"},
293+
{ByteStart: 25, ByteEnd: 43, Uri: "https://pkg.go.dev"},
294+
},
295+
},
296+
{
297+
name: "No links",
298+
content: "Hello world!",
299+
expectedFacets: []struct {
300+
ByteStart int
301+
ByteEnd int
302+
Uri string
303+
}{},
304+
},
305+
{
306+
name: "One link at start",
307+
content: "https://go.dev Hello world!",
308+
expectedFacets: []struct {
309+
ByteStart int
310+
ByteEnd int
311+
Uri string
312+
}{
313+
{ByteStart: 0, ByteEnd: 14, Uri: "https://go.dev"},
314+
},
315+
},
316+
{
317+
name: "One link at end",
318+
content: "Hello world! https://go.dev",
319+
expectedFacets: []struct {
320+
ByteStart int
321+
ByteEnd int
322+
Uri string
323+
}{
324+
{ByteStart: 13, ByteEnd: 27, Uri: "https://go.dev"},
325+
},
326+
},
327+
}
328+
329+
for _, tt := range tests {
330+
t.Run(tt.name, func(t *testing.T) {
331+
pb := NewPostBuilder(tt.content)
332+
pb.parseLinks()
333+
334+
if len(pb.facets) != len(tt.expectedFacets) {
335+
t.Errorf("wanted %d facets, got %d", len(tt.expectedFacets), len(pb.facets))
336+
return
337+
}
338+
339+
for i, expected := range tt.expectedFacets {
340+
if pb.facets[i].Index.ByteStart != expected.ByteStart || pb.facets[i].Index.ByteEnd != expected.ByteEnd {
341+
t.Errorf("facet %d: wanted index [%d,%d], got [%d,%d]", i, expected.ByteStart, expected.ByteEnd, pb.facets[i].Index.ByteStart, pb.facets[i].Index.ByteEnd)
342+
}
343+
if pb.facets[i].Features[0].Uri != expected.Uri {
344+
t.Errorf("facet %d: wanted URI '%s', got '%s'", i, expected.Uri, pb.facets[i].Features[0].Uri)
345+
}
346+
}
347+
})
292348
}
293349
}
294350

0 commit comments

Comments
 (0)