processor_test.go 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  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/model"
  8. )
  9. func TestBlockingEntries(t *testing.T) {
  10. var scenarios = []struct {
  11. feed *model.Feed
  12. entry *model.Entry
  13. expected bool
  14. }{
  15. {&model.Feed{ID: 1, BlocklistRules: "(?i)example"}, &model.Entry{URL: "https://example.com"}, true},
  16. {&model.Feed{ID: 1, BlocklistRules: "(?i)example"}, &model.Entry{URL: "https://different.com"}, false},
  17. {&model.Feed{ID: 1, BlocklistRules: "(?i)example"}, &model.Entry{Title: "Some Example"}, true},
  18. {&model.Feed{ID: 1, BlocklistRules: "(?i)example"}, &model.Entry{Title: "Something different"}, false},
  19. {&model.Feed{ID: 1, BlocklistRules: "(?i)example"}, &model.Entry{Title: "Something different", Tags: []string{"example", "something else"}}, true},
  20. {&model.Feed{ID: 1, BlocklistRules: "(?i)example"}, &model.Entry{Title: "Example", Tags: []string{"example", "something else"}}, true},
  21. {&model.Feed{ID: 1, BlocklistRules: "(?i)example"}, &model.Entry{Title: "Example", Tags: []string{"something different", "something else"}}, true},
  22. {&model.Feed{ID: 1, BlocklistRules: "(?i)example"}, &model.Entry{Title: "Something different", Tags: []string{"something different", "something else"}}, false},
  23. {&model.Feed{ID: 1}, &model.Entry{Title: "No rule defined"}, false},
  24. }
  25. for _, tc := range scenarios {
  26. result := isBlockedEntry(tc.feed, tc.entry)
  27. if tc.expected != result {
  28. t.Errorf(`Unexpected result, got %v for entry %q`, result, tc.entry.Title)
  29. }
  30. }
  31. }
  32. func TestAllowEntries(t *testing.T) {
  33. var scenarios = []struct {
  34. feed *model.Feed
  35. entry *model.Entry
  36. expected bool
  37. }{
  38. {&model.Feed{ID: 1, KeeplistRules: "(?i)example"}, &model.Entry{Title: "https://example.com"}, true},
  39. {&model.Feed{ID: 1, KeeplistRules: "(?i)example"}, &model.Entry{Title: "https://different.com"}, false},
  40. {&model.Feed{ID: 1, KeeplistRules: "(?i)example"}, &model.Entry{Title: "Some Example"}, true},
  41. {&model.Feed{ID: 1, KeeplistRules: "(?i)example"}, &model.Entry{Title: "Something different"}, false},
  42. {&model.Feed{ID: 1}, &model.Entry{Title: "No rule defined"}, true},
  43. {&model.Feed{ID: 1, KeeplistRules: "(?i)example"}, &model.Entry{Title: "Something different", Tags: []string{"example", "something else"}}, true},
  44. {&model.Feed{ID: 1, KeeplistRules: "(?i)example"}, &model.Entry{Title: "Example", Tags: []string{"example", "something else"}}, true},
  45. {&model.Feed{ID: 1, KeeplistRules: "(?i)example"}, &model.Entry{Title: "Example", Tags: []string{"something different", "something else"}}, true},
  46. {&model.Feed{ID: 1, KeeplistRules: "(?i)example"}, &model.Entry{Title: "Something more", Tags: []string{"something different", "something else"}}, false},
  47. }
  48. for _, tc := range scenarios {
  49. result := isAllowedEntry(tc.feed, tc.entry)
  50. if tc.expected != result {
  51. t.Errorf(`Unexpected result, got %v for entry %q`, result, tc.entry.Title)
  52. }
  53. }
  54. }
  55. func TestParseISO8601(t *testing.T) {
  56. var scenarios = []struct {
  57. duration string
  58. expected time.Duration
  59. }{
  60. // Live streams and radio.
  61. {"PT0M0S", 0},
  62. // https://www.youtube.com/watch?v=HLrqNhgdiC0
  63. {"PT6M20S", (6 * time.Minute) + (20 * time.Second)},
  64. // https://www.youtube.com/watch?v=LZa5KKfqHtA
  65. {"PT5M41S", (5 * time.Minute) + (41 * time.Second)},
  66. // https://www.youtube.com/watch?v=yIxEEgEuhT4
  67. {"PT51M52S", (51 * time.Minute) + (52 * time.Second)},
  68. // https://www.youtube.com/watch?v=bpHf1XcoiFs
  69. {"PT80M42S", (1 * time.Hour) + (20 * time.Minute) + (42 * time.Second)},
  70. }
  71. for _, tc := range scenarios {
  72. result, err := parseISO8601(tc.duration)
  73. if err != nil {
  74. t.Errorf("Got an error when parsing %q: %v", tc.duration, err)
  75. }
  76. if tc.expected != result {
  77. t.Errorf(`Unexpected result, got %v for duration %q`, result, tc.duration)
  78. }
  79. }
  80. }