A message containing a YouTube link now carries a preview card. Discord and X build their own card from the URL in the text, so the work is on the bluesky side: the new preview.go asks YouTube's oEmbed endpoint for the title, channel and thumbnail and posts them as an app.bsky.embed.external. Links are found by pulling candidates out of the text and parsing them with net/url rather than by matching a URL shaped regexp, so a host like youtube.com.example.invalid is not mistaken for the real thing. Bluesky allows one embed per post, so a message with both attachments and a link keeps the attachments. A failed oEmbed lookup only costs the card, not the post. Downloading the thumbnail wants the attachment download path, so it is split into fetch and downloadImage, and both now go through a client with a timeout: the event loop is single threaded and a hung request would stall every later message. The Dockerfile built main.go by name, which stops working now that package main spans two files; it builds the package instead. Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
34 lines
1.4 KiB
Go
34 lines
1.4 KiB
Go
package main
|
|
|
|
import "testing"
|
|
|
|
func TestFindYouTubeURL(t *testing.T) {
|
|
tests := []struct {
|
|
name string
|
|
content string
|
|
want string
|
|
}{
|
|
{"watch", "みてみて https://www.youtube.com/watch?v=dQw4w9WgXcQ おもしろい", "https://www.youtube.com/watch?v=dQw4w9WgXcQ"},
|
|
{"short host", "https://youtu.be/dQw4w9WgXcQ?t=42", "https://youtu.be/dQw4w9WgXcQ?t=42"},
|
|
{"shorts", "https://www.youtube.com/shorts/abc_123", "https://www.youtube.com/shorts/abc_123"},
|
|
{"live", "https://youtube.com/live/abc-123", "https://youtube.com/live/abc-123"},
|
|
{"mobile", "https://m.youtube.com/watch?v=abc&feature=share", "https://m.youtube.com/watch?v=abc&feature=share"},
|
|
{"trailing punctuation", "これ→https://youtu.be/abc123。", "https://youtu.be/abc123"},
|
|
{"first of several", "https://youtu.be/one https://youtu.be/two", "https://youtu.be/one"},
|
|
{"skips other links", "https://example.com/watch?v=x https://youtu.be/abc", "https://youtu.be/abc"},
|
|
{"no url", "ただのつぶやき", ""},
|
|
{"other site", "https://example.com/", ""},
|
|
{"youtube without video", "https://www.youtube.com/", ""},
|
|
{"watch without v", "https://www.youtube.com/watch?list=PL123", ""},
|
|
{"lookalike host", "https://youtube.com.evil.example/watch?v=abc", ""},
|
|
}
|
|
|
|
for _, tt := range tests {
|
|
t.Run(tt.name, func(t *testing.T) {
|
|
if got := findYouTubeURL(tt.content); got != tt.want {
|
|
t.Errorf("findYouTubeURL(%q) = %q, want %q", tt.content, got, tt.want)
|
|
}
|
|
})
|
|
}
|
|
}
|