1
0
Fork 0
mirror of https://github.com/miniflux/v2.git synced 2025-10-15 19:42:07 +00:00

refactor(subscription): combine findSubscriptionsFromYouTubeChannelPage and findSubscriptionsFromYouTubePlaylistPage functions

This commit is contained in:
Julien Voisin 2025-09-13 02:59:41 +02:00 committed by GitHub
parent 93a8629910
commit 0b93d8abcc
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
2 changed files with 31 additions and 138 deletions

View file

@ -8,96 +8,7 @@ import (
"testing"
)
func TestFindYoutubePlaylistFeed(t *testing.T) {
type testResult struct {
websiteURL string
feedURL string
discoveryError bool
}
scenarios := []testResult{
// Video URL
{
websiteURL: "https://www.youtube.com/watch?v=dQw4w9WgXcQ",
feedURL: "",
},
// Video URL with position argument
{
websiteURL: "https://www.youtube.com/watch?v=dQw4w9WgXcQ&t=1",
feedURL: "",
},
// Video URL with position argument
{
websiteURL: "https://www.youtube.com/watch?t=1&v=dQw4w9WgXcQ",
feedURL: "",
},
// Channel URL
{
websiteURL: "https://www.youtube.com/channel/UC-Qj80avWItNRjkZ41rzHyw",
feedURL: "",
},
// Channel URL with name
{
websiteURL: "https://www.youtube.com/@ABCDEFG",
feedURL: "",
},
// Playlist URL
{
websiteURL: "https://www.youtube.com/playlist?list=PLOOwEPgFWm_NHcQd9aCi5JXWASHO_n5uR",
feedURL: "https://www.youtube.com/feeds/videos.xml?playlist_id=PLOOwEPgFWm_NHcQd9aCi5JXWASHO_n5uR",
},
// Playlist URL with video ID
{
websiteURL: "https://www.youtube.com/watch?v=dQw4w9WgXcQ&list=PLOOwEPgFWm_N42HlCLhqyJ0ZBWr5K1QDM",
feedURL: "https://www.youtube.com/feeds/videos.xml?playlist_id=PLOOwEPgFWm_N42HlCLhqyJ0ZBWr5K1QDM",
},
// Playlist URL with video ID and index argument
{
websiteURL: "https://www.youtube.com/watch?v=6IutBmRJNLk&list=PLOOwEPgFWm_NHcQd9aCi5JXWASHO_n5uR&index=4",
feedURL: "https://www.youtube.com/feeds/videos.xml?playlist_id=PLOOwEPgFWm_NHcQd9aCi5JXWASHO_n5uR",
},
// Non-Youtube URL
{
websiteURL: "https://www.example.com/channel/UC-Qj80avWItNRjkZ41rzHyw",
feedURL: "",
},
// Invalid URL
{
websiteURL: "https://example|org/",
feedURL: "",
discoveryError: true,
},
}
for _, scenario := range scenarios {
subscriptions, localizedError := NewSubscriptionFinder(nil).findSubscriptionsFromYouTubePlaylistPage(scenario.websiteURL)
if scenario.discoveryError {
if localizedError == nil {
t.Fatalf(`Parsing an invalid URL should return an error`)
}
}
if scenario.feedURL == "" {
if len(subscriptions) > 0 {
t.Fatalf(`Parsing a non-playlist URL should not return any subscription: %q`, scenario.websiteURL)
}
} else {
if localizedError != nil {
t.Fatalf(`Parsing a correctly formatted YouTube playlist page should not return any error: %v`, localizedError)
}
if len(subscriptions) != 1 {
t.Fatalf(`Incorrect number of subscriptions returned`)
}
if subscriptions[0].URL != scenario.feedURL {
t.Errorf(`Unexpected Feed, got %s, instead of %s`, subscriptions[0].URL, scenario.feedURL)
}
}
}
}
func TestFindYoutubeChannelFeed(t *testing.T) {
func TestFindYoutubeFeed(t *testing.T) {
type testResult struct {
websiteURL string
feedURL string
@ -133,16 +44,21 @@ func TestFindYoutubeChannelFeed(t *testing.T) {
// Playlist URL
{
websiteURL: "https://www.youtube.com/playlist?list=PLOOwEPgFWm_NHcQd9aCi5JXWASHO_n5uR",
feedURL: "",
feedURL: "https://www.youtube.com/feeds/videos.xml?playlist_id=PLOOwEPgFWm_NHcQd9aCi5JXWASHO_n5uR",
},
// Playlist URL with video ID
{
websiteURL: "https://www.youtube.com/watch?v=dQw4w9WgXcQ&list=PLOOwEPgFWm_N42HlCLhqyJ0ZBWr5K1QDM",
feedURL: "",
feedURL: "https://www.youtube.com/feeds/videos.xml?playlist_id=PLOOwEPgFWm_N42HlCLhqyJ0ZBWr5K1QDM",
},
// Playlist URL with video ID and index argument
{
websiteURL: "https://www.youtube.com/watch?v=6IutBmRJNLk&list=PLOOwEPgFWm_NHcQd9aCi5JXWASHO_n5uR&index=4",
feedURL: "https://www.youtube.com/feeds/videos.xml?playlist_id=PLOOwEPgFWm_NHcQd9aCi5JXWASHO_n5uR",
},
// Empty playlist ID parameter
{
websiteURL: "https://www.youtube.com/playlist?list=",
feedURL: "",
},
// Non-Youtube URL
@ -159,7 +75,7 @@ func TestFindYoutubeChannelFeed(t *testing.T) {
}
for _, scenario := range scenarios {
subscriptions, localizedError := NewSubscriptionFinder(nil).findSubscriptionsFromYouTubeChannelPage(scenario.websiteURL)
subscriptions, localizedError := NewSubscriptionFinder(nil).findSubscriptionsFromYouTube(scenario.websiteURL)
if scenario.discoveryError {
if localizedError == nil {
t.Fatalf(`Parsing an invalid URL should return an error`)
@ -168,11 +84,11 @@ func TestFindYoutubeChannelFeed(t *testing.T) {
if scenario.feedURL == "" {
if len(subscriptions) > 0 {
t.Fatalf(`Parsing a non-channel URL should not return any subscription: %q`, scenario.websiteURL)
t.Fatalf(`Parsing an invalid URL should not return any subscription: %q -> %v`, scenario.websiteURL, subscriptions)
}
} else {
if localizedError != nil {
t.Fatalf(`Parsing a correctly formatted YouTube channel page should not return any error: %v`, localizedError)
t.Fatalf(`Parsing a correctly formatted YouTube playlist or channel page should not return any error: %v`, localizedError)
}
if len(subscriptions) != 1 {
@ -180,7 +96,7 @@ func TestFindYoutubeChannelFeed(t *testing.T) {
}
if subscriptions[0].URL != scenario.feedURL {
t.Errorf(`Unexpected Feed, got %s, instead of %s`, subscriptions[0].URL, scenario.feedURL)
t.Errorf(`Unexpected feed, got %s, instead of %s`, subscriptions[0].URL, scenario.feedURL)
}
}
}