Просмотр исходного кода

refactor(reader): remove dead code in filter and subscription finder

containsRegexPattern has had no caller since the filter started caching
compiled regexes. findSubscriptionsFromWebPage and
findSubscriptionsFromWellKnownURLs never return an error, so drop that
result and the unreachable checks in FindSubscriptions.
Frédéric Guillot 2 часов назад
Родитель
Сommit
3a6d527764

+ 0 - 8
internal/reader/filter/filter.go

@@ -286,14 +286,6 @@ func isDateMatchingPattern(pattern string, entryDate time.Time) bool {
 	return parsedPattern.matches(entryDate)
 }
 
-func containsRegexPattern(pattern string, items []string) bool {
-	re := cachedRegex(pattern)
-	if re == nil {
-		return false
-	}
-	return slices.ContainsFunc(items, re.MatchString)
-}
-
 func parseDuration(duration string) (time.Duration, error) {
 	// Handle common duration formats like "30d", "7d", "1h", "1m", etc.
 	// Go's time.ParseDuration doesn't support days, so we handle them manually

+ 0 - 56
internal/reader/filter/filter_test.go

@@ -775,62 +775,6 @@ func TestIsDateMatchingPattern(t *testing.T) {
 	}
 }
 
-// Tests for containsRegexPattern function
-func TestContainsRegexPattern(t *testing.T) {
-	tests := []struct {
-		name     string
-		pattern  string
-		items    []string
-		expected bool
-	}{
-		{
-			name:     "match found",
-			pattern:  "go.*",
-			items:    []string{"golang", "python", "javascript"},
-			expected: true,
-		},
-		{
-			name:     "no match",
-			pattern:  "rust",
-			items:    []string{"golang", "python", "javascript"},
-			expected: false,
-		},
-		{
-			name:     "empty items",
-			pattern:  "test",
-			items:    []string{},
-			expected: false,
-		},
-		{
-			name:     "invalid regex",
-			pattern:  "[",
-			items:    []string{"test"},
-			expected: false,
-		},
-		{
-			name:     "case sensitive match",
-			pattern:  "Go",
-			items:    []string{"golang", "python"},
-			expected: false,
-		},
-		{
-			name:     "exact match",
-			pattern:  "^golang$",
-			items:    []string{"golang", "go"},
-			expected: true,
-		},
-	}
-
-	for _, tt := range tests {
-		t.Run(tt.name, func(t *testing.T) {
-			result := containsRegexPattern(tt.pattern, tt.items)
-			if result != tt.expected {
-				t.Errorf("containsRegexPattern() = %v, expected %v", result, tt.expected)
-			}
-		})
-	}
-}
-
 // Tests for parseDuration function
 func TestParseDuration(t *testing.T) {
 	tests := []struct {

+ 6 - 10
internal/reader/subscription/finder.go

@@ -103,9 +103,7 @@ func (f *subscriptionFinder) FindSubscriptions(websiteURL, rssBridgeURL string,
 		slog.String("content_type", responseHandler.ContentType()),
 	)
 
-	if subscriptions, localizedError := f.findSubscriptionsFromWebPage(baseURL, doc); localizedError != nil {
-		return nil, localizedError
-	} else if len(subscriptions) > 0 {
+	if subscriptions := f.findSubscriptionsFromWebPage(baseURL, doc); len(subscriptions) > 0 {
 		slog.Debug("Subscriptions found from web page", slog.String("website_url", websiteURL), slog.Any("subscriptions", subscriptions))
 		return subscriptions, nil
 	}
@@ -123,9 +121,7 @@ func (f *subscriptionFinder) FindSubscriptions(websiteURL, rssBridgeURL string,
 
 	// Step 7) Check if the website has a known feed URL.
 	slog.Debug("Try to detect feeds from well-known URLs", slog.String("website_url", websiteURL))
-	if subscriptions, localizedError := f.findSubscriptionsFromWellKnownURLs(websiteURL); localizedError != nil {
-		return nil, localizedError
-	} else if len(subscriptions) > 0 {
+	if subscriptions := f.findSubscriptionsFromWellKnownURLs(websiteURL); len(subscriptions) > 0 {
 		slog.Debug("Subscriptions found with well-known URLs", slog.String("website_url", websiteURL), slog.Any("subscriptions", subscriptions))
 		return subscriptions, nil
 	}
@@ -133,7 +129,7 @@ func (f *subscriptionFinder) FindSubscriptions(websiteURL, rssBridgeURL string,
 	return nil, nil
 }
 
-func (f *subscriptionFinder) findSubscriptionsFromWebPage(websiteURL string, doc *goquery.Document) (Subscriptions, *locale.LocalizedErrorWrapper) {
+func (f *subscriptionFinder) findSubscriptionsFromWebPage(websiteURL string, doc *goquery.Document) Subscriptions {
 	var subscriptions Subscriptions
 	// There are 4 possible feed formats
 	subscriptionURLs := make(map[string]bool, 4)
@@ -189,10 +185,10 @@ func (f *subscriptionFinder) findSubscriptionsFromWebPage(websiteURL string, doc
 		})
 	})
 
-	return subscriptions, nil
+	return subscriptions
 }
 
-func (f *subscriptionFinder) findSubscriptionsFromWellKnownURLs(websiteURL string) (Subscriptions, *locale.LocalizedErrorWrapper) {
+func (f *subscriptionFinder) findSubscriptionsFromWellKnownURLs(websiteURL string) Subscriptions {
 	type pair struct{ path, format string }
 	knownURLs := []pair{
 		{"atom.xml", parser.FormatAtom},
@@ -260,7 +256,7 @@ func (f *subscriptionFinder) findSubscriptionsFromWellKnownURLs(websiteURL strin
 		}
 	}
 
-	return subscriptions, nil
+	return subscriptions
 }
 
 func (f *subscriptionFinder) findSubscriptionsFromRSSBridge(websiteURL, rssBridgeURL string, rssBridgeToken string) (Subscriptions, *locale.LocalizedErrorWrapper) {

+ 11 - 44
internal/reader/subscription/finder_test.go

@@ -123,10 +123,7 @@ func TestParseWebPageWithRssFeed(t *testing.T) {
 		t.Fatalf(`Unable to parse the HTML: %v`, shouldNeverHappenErr)
 	}
 
-	subscriptions, err := NewSubscriptionFinder(nil).findSubscriptionsFromWebPage("http://example.org/", doc)
-	if err != nil {
-		t.Fatalf(`Parsing a correctly formatted HTML page should not return any error: %v`, err)
-	}
+	subscriptions := NewSubscriptionFinder(nil).findSubscriptionsFromWebPage("http://example.org/", doc)
 
 	if len(subscriptions) != 1 {
 		t.Fatal(`Incorrect number of subscriptions returned`)
@@ -160,10 +157,7 @@ func TestParseWebPageWithAtomFeed(t *testing.T) {
 		t.Fatalf(`Unable to parse the HTML: %v`, shouldNeverHappenErr)
 	}
 
-	subscriptions, err := NewSubscriptionFinder(nil).findSubscriptionsFromWebPage("http://example.org/", doc)
-	if err != nil {
-		t.Fatalf(`Parsing a correctly formatted HTML page should not return any error: %v`, err)
-	}
+	subscriptions := NewSubscriptionFinder(nil).findSubscriptionsFromWebPage("http://example.org/", doc)
 
 	if len(subscriptions) != 1 {
 		t.Fatal(`Incorrect number of subscriptions returned`)
@@ -197,10 +191,7 @@ func TestParseWebPageWithJSONFeed(t *testing.T) {
 		t.Fatalf(`Unable to parse the HTML: %v`, shouldNeverHappenErr)
 	}
 
-	subscriptions, err := NewSubscriptionFinder(nil).findSubscriptionsFromWebPage("http://example.org/", doc)
-	if err != nil {
-		t.Fatalf(`Parsing a correctly formatted HTML page should not return any error: %v`, err)
-	}
+	subscriptions := NewSubscriptionFinder(nil).findSubscriptionsFromWebPage("http://example.org/", doc)
 
 	if len(subscriptions) != 1 {
 		t.Fatal(`Incorrect number of subscriptions returned`)
@@ -234,10 +225,7 @@ func TestParseWebPageWithOldJSONFeedMimeType(t *testing.T) {
 		t.Fatalf(`Unable to parse the HTML: %v`, shouldNeverHappenErr)
 	}
 
-	subscriptions, err := NewSubscriptionFinder(nil).findSubscriptionsFromWebPage("http://example.org/", doc)
-	if err != nil {
-		t.Fatalf(`Parsing a correctly formatted HTML page should not return any error: %v`, err)
-	}
+	subscriptions := NewSubscriptionFinder(nil).findSubscriptionsFromWebPage("http://example.org/", doc)
 
 	if len(subscriptions) != 1 {
 		t.Fatal(`Incorrect number of subscriptions returned`)
@@ -272,10 +260,7 @@ func TestParseWebPageWithJSONFeedWpJsonIgnored(t *testing.T) {
 		t.Fatalf(`Unable to parse the HTML: %v`, shouldNeverHappenErr)
 	}
 
-	subscriptions, err := NewSubscriptionFinder(nil).findSubscriptionsFromWebPage("http://example.org/", doc)
-	if err != nil {
-		t.Fatalf(`Parsing a correctly formatted HTML page should not return any error: %v`, err)
-	}
+	subscriptions := NewSubscriptionFinder(nil).findSubscriptionsFromWebPage("http://example.org/", doc)
 
 	if len(subscriptions) != 0 {
 		t.Fatal(`Incorrect number of subscriptions returned`)
@@ -297,10 +282,7 @@ func TestParseWebPageWithRelativeFeedURL(t *testing.T) {
 		t.Fatalf(`Unable to parse the HTML: %v`, shouldNeverHappenErr)
 	}
 
-	subscriptions, err := NewSubscriptionFinder(nil).findSubscriptionsFromWebPage("http://example.org/", doc)
-	if err != nil {
-		t.Fatalf(`Parsing a correctly formatted HTML page should not return any error: %v`, err)
-	}
+	subscriptions := NewSubscriptionFinder(nil).findSubscriptionsFromWebPage("http://example.org/", doc)
 
 	if len(subscriptions) != 1 {
 		t.Fatal(`Incorrect number of subscriptions returned`)
@@ -334,10 +316,7 @@ func TestParseWebPageWithEmptyTitle(t *testing.T) {
 		t.Fatalf(`Unable to parse the HTML: %v`, shouldNeverHappenErr)
 	}
 
-	subscriptions, err := NewSubscriptionFinder(nil).findSubscriptionsFromWebPage("http://example.org/", doc)
-	if err != nil {
-		t.Fatalf(`Parsing a correctly formatted HTML page should not return any error: %v`, err)
-	}
+	subscriptions := NewSubscriptionFinder(nil).findSubscriptionsFromWebPage("http://example.org/", doc)
 
 	if len(subscriptions) != 1 {
 		t.Fatal(`Incorrect number of subscriptions returned`)
@@ -372,10 +351,7 @@ func TestParseWebPageWithMultipleFeeds(t *testing.T) {
 		t.Fatalf(`Unable to parse the HTML: %v`, shouldNeverHappenErr)
 	}
 
-	subscriptions, err := NewSubscriptionFinder(nil).findSubscriptionsFromWebPage("http://example.org/", doc)
-	if err != nil {
-		t.Fatalf(`Parsing a correctly formatted HTML page should not return any error: %v`, err)
-	}
+	subscriptions := NewSubscriptionFinder(nil).findSubscriptionsFromWebPage("http://example.org/", doc)
 
 	if len(subscriptions) != 2 {
 		t.Fatal(`Incorrect number of subscriptions returned`)
@@ -398,10 +374,7 @@ func TestParseWebPageWithDuplicatedFeeds(t *testing.T) {
 		t.Fatalf(`Unable to parse the HTML: %v`, shouldNeverHappenErr)
 	}
 
-	subscriptions, err := NewSubscriptionFinder(nil).findSubscriptionsFromWebPage("http://example.org/", doc)
-	if err != nil {
-		t.Fatalf(`Parsing a correctly formatted HTML page should not return any error: %v`, err)
-	}
+	subscriptions := NewSubscriptionFinder(nil).findSubscriptionsFromWebPage("http://example.org/", doc)
 
 	if len(subscriptions) != 1 {
 		t.Fatal(`Incorrect number of subscriptions returned`)
@@ -435,10 +408,7 @@ func TestParseWebPageWithEmptyFeedURL(t *testing.T) {
 		t.Fatalf(`Unable to parse the HTML: %v`, shouldNeverHappenErr)
 	}
 
-	subscriptions, err := NewSubscriptionFinder(nil).findSubscriptionsFromWebPage("http://example.org/", doc)
-	if err != nil {
-		t.Fatalf(`Parsing a correctly formatted HTML page should not return any error: %v`, err)
-	}
+	subscriptions := NewSubscriptionFinder(nil).findSubscriptionsFromWebPage("http://example.org/", doc)
 
 	if len(subscriptions) != 0 {
 		t.Fatal(`Incorrect number of subscriptions returned`)
@@ -460,10 +430,7 @@ func TestParseWebPageWithNoHref(t *testing.T) {
 		t.Fatalf(`Unable to parse the HTML: %v`, shouldNeverHappenErr)
 	}
 
-	subscriptions, err := NewSubscriptionFinder(nil).findSubscriptionsFromWebPage("http://example.org/", doc)
-	if err != nil {
-		t.Fatalf(`Parsing a correctly formatted HTML page should not return any error: %v`, err)
-	}
+	subscriptions := NewSubscriptionFinder(nil).findSubscriptionsFromWebPage("http://example.org/", doc)
 
 	if len(subscriptions) != 0 {
 		t.Fatal(`Incorrect number of subscriptions returned`)