package main import ( "net/url" "testing" ) func TestFindLink(t *testing.T) { tests := []struct { name string content string want string }{ {"plain", "みてみて https://example.com/article おもしろい", "https://example.com/article"}, {"query", "https://example.com/watch?v=abc&t=1", "https://example.com/watch?v=abc&t=1"}, {"trailing punctuation", "これ→https://example.com/記事。", "https://example.com/記事"}, {"first of several", "https://one.example https://two.example", "https://one.example"}, {"no link", "ただのつぶやき", ""}, {"scheme only", "http:// と書いただけ", ""}, } for _, tt := range tests { t.Run(tt.name, func(t *testing.T) { if got := findLink(tt.content); got != tt.want { t.Errorf("findLink(%q) = %q, want %q", tt.content, got, tt.want) } }) } } func TestIsYouTube(t *testing.T) { tests := []struct { raw string want bool }{ {"https://www.youtube.com/watch?v=dQw4w9WgXcQ", true}, {"https://youtu.be/dQw4w9WgXcQ?t=42", true}, {"https://www.youtube.com/shorts/abc_123", true}, {"https://youtube.com/live/abc-123", true}, {"https://m.youtube.com/watch?v=abc&feature=share", true}, {"https://www.youtube.com/", false}, {"https://www.youtube.com/watch?list=PL123", false}, {"https://youtube.com.evil.example/watch?v=abc", false}, {"https://example.com/", false}, } for _, tt := range tests { t.Run(tt.raw, func(t *testing.T) { u, err := url.Parse(tt.raw) if err != nil { t.Fatal(err) } if got := isYouTube(u); got != tt.want { t.Errorf("isYouTube(%q) = %v, want %v", tt.raw, got, tt.want) } }) } } func TestParseMetaTags(t *testing.T) { page := &fetched{ contentType: "text/html; charset=utf-8", body: []byte(`