processor_test.go 4.4 KB

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