processor_test.go 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  1. // SPDX-FileCopyrightText: Copyright The Miniflux Authors. All rights reserved.
  2. // SPDX-License-Identifier: Apache-2.0
  3. package processor // import "miniflux.app/v2/internal/reader/processor"
  4. import (
  5. "testing"
  6. "time"
  7. "miniflux.app/v2/internal/config"
  8. "miniflux.app/v2/internal/model"
  9. )
  10. func TestBlockingEntries(t *testing.T) {
  11. var scenarios = []struct {
  12. feed *model.Feed
  13. entry *model.Entry
  14. expected bool
  15. }{
  16. {&model.Feed{ID: 1, BlocklistRules: "(?i)example"}, &model.Entry{URL: "https://example.com"}, true},
  17. {&model.Feed{ID: 1, BlocklistRules: "(?i)example"}, &model.Entry{URL: "https://different.com"}, false},
  18. {&model.Feed{ID: 1, BlocklistRules: "(?i)example"}, &model.Entry{Title: "Some Example"}, true},
  19. {&model.Feed{ID: 1, BlocklistRules: "(?i)example"}, &model.Entry{Title: "Something different"}, false},
  20. {&model.Feed{ID: 1, BlocklistRules: "(?i)example"}, &model.Entry{Title: "Something different", Tags: []string{"example", "something else"}}, true},
  21. {&model.Feed{ID: 1, BlocklistRules: "(?i)example"}, &model.Entry{Title: "Example", Tags: []string{"example", "something else"}}, true},
  22. {&model.Feed{ID: 1, BlocklistRules: "(?i)example"}, &model.Entry{Title: "Example", Tags: []string{"something different", "something else"}}, true},
  23. {&model.Feed{ID: 1, BlocklistRules: "(?i)example"}, &model.Entry{Title: "Something different", Tags: []string{"something different", "something else"}}, false},
  24. {&model.Feed{ID: 1, BlocklistRules: "(?i)example"}, &model.Entry{Title: "Something different", Author: "Example"}, true},
  25. {&model.Feed{ID: 1, BlocklistRules: "(?i)example"}, &model.Entry{Title: "Something different", Author: "Something different"}, false},
  26. {&model.Feed{ID: 1}, &model.Entry{Title: "No rule defined"}, false},
  27. }
  28. for _, tc := range scenarios {
  29. result := isBlockedEntry(tc.feed, tc.entry)
  30. if tc.expected != result {
  31. t.Errorf(`Unexpected result, got %v for entry %q`, result, tc.entry.Title)
  32. }
  33. }
  34. }
  35. func TestAllowEntries(t *testing.T) {
  36. var scenarios = []struct {
  37. feed *model.Feed
  38. entry *model.Entry
  39. expected bool
  40. }{
  41. {&model.Feed{ID: 1, KeeplistRules: "(?i)example"}, &model.Entry{Title: "https://example.com"}, true},
  42. {&model.Feed{ID: 1, KeeplistRules: "(?i)example"}, &model.Entry{Title: "https://different.com"}, false},
  43. {&model.Feed{ID: 1, KeeplistRules: "(?i)example"}, &model.Entry{Title: "Some Example"}, true},
  44. {&model.Feed{ID: 1, KeeplistRules: "(?i)example"}, &model.Entry{Title: "Something different"}, false},
  45. {&model.Feed{ID: 1}, &model.Entry{Title: "No rule defined"}, true},
  46. {&model.Feed{ID: 1, KeeplistRules: "(?i)example"}, &model.Entry{Title: "Something different", Tags: []string{"example", "something else"}}, true},
  47. {&model.Feed{ID: 1, KeeplistRules: "(?i)example"}, &model.Entry{Title: "Example", Tags: []string{"example", "something else"}}, true},
  48. {&model.Feed{ID: 1, KeeplistRules: "(?i)example"}, &model.Entry{Title: "Example", Tags: []string{"something different", "something else"}}, true},
  49. {&model.Feed{ID: 1, KeeplistRules: "(?i)example"}, &model.Entry{Title: "Something more", Tags: []string{"something different", "something else"}}, false},
  50. {&model.Feed{ID: 1, KeeplistRules: "(?i)example"}, &model.Entry{Title: "Something different", Author: "Example"}, true},
  51. {&model.Feed{ID: 1, KeeplistRules: "(?i)example"}, &model.Entry{Title: "Something different", Author: "Something different"}, false},
  52. }
  53. for _, tc := range scenarios {
  54. result := isAllowedEntry(tc.feed, tc.entry)
  55. if tc.expected != result {
  56. t.Errorf(`Unexpected result, got %v for entry %q`, result, tc.entry.Title)
  57. }
  58. }
  59. }
  60. func TestParseISO8601(t *testing.T) {
  61. var scenarios = []struct {
  62. duration string
  63. expected time.Duration
  64. }{
  65. // Live streams and radio.
  66. {"PT0M0S", 0},
  67. // https://www.youtube.com/watch?v=HLrqNhgdiC0
  68. {"PT6M20S", (6 * time.Minute) + (20 * time.Second)},
  69. // https://www.youtube.com/watch?v=LZa5KKfqHtA
  70. {"PT5M41S", (5 * time.Minute) + (41 * time.Second)},
  71. // https://www.youtube.com/watch?v=yIxEEgEuhT4
  72. {"PT51M52S", (51 * time.Minute) + (52 * time.Second)},
  73. // https://www.youtube.com/watch?v=bpHf1XcoiFs
  74. {"PT80M42S", (1 * time.Hour) + (20 * time.Minute) + (42 * time.Second)},
  75. }
  76. for _, tc := range scenarios {
  77. result, err := parseISO8601(tc.duration)
  78. if err != nil {
  79. t.Errorf("Got an error when parsing %q: %v", tc.duration, err)
  80. }
  81. if tc.expected != result {
  82. t.Errorf(`Unexpected result, got %v for duration %q`, result, tc.duration)
  83. }
  84. }
  85. }
  86. func TestIsRecentEntry(t *testing.T) {
  87. parser := config.NewParser()
  88. var err error
  89. config.Opts, err = parser.ParseEnvironmentVariables()
  90. if err != nil {
  91. t.Fatalf(`Parsing failure: %v`, err)
  92. }
  93. var scenarios = []struct {
  94. entry *model.Entry
  95. expected bool
  96. }{
  97. {&model.Entry{Title: "Example1", Date: time.Date(2005, 5, 1, 05, 05, 05, 05, time.UTC)}, true},
  98. {&model.Entry{Title: "Example2", Date: time.Date(2010, 5, 1, 05, 05, 05, 05, time.UTC)}, true},
  99. {&model.Entry{Title: "Example3", Date: time.Date(2020, 5, 1, 05, 05, 05, 05, time.UTC)}, true},
  100. {&model.Entry{Title: "Example4", Date: time.Date(2024, 3, 15, 05, 05, 05, 05, time.UTC)}, true},
  101. }
  102. for _, tc := range scenarios {
  103. result := isRecentEntry(tc.entry)
  104. if tc.expected != result {
  105. t.Errorf(`Unexpected result, got %v for entry %q`, result, tc.entry.Title)
  106. }
  107. }
  108. }