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) } }) } }