processor_test.go 2.7 KB

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