youtube_test.go 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  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. )
  8. func TestParseISO8601(t *testing.T) {
  9. var scenarios = []struct {
  10. duration string
  11. expected time.Duration
  12. }{
  13. // Live streams and radio.
  14. {"PT0M0S", 0},
  15. // https://www.youtube.com/watch?v=HLrqNhgdiC0
  16. {"PT6M20S", (6 * time.Minute) + (20 * time.Second)},
  17. // https://www.youtube.com/watch?v=LZa5KKfqHtA
  18. {"PT5M41S", (5 * time.Minute) + (41 * time.Second)},
  19. // https://www.youtube.com/watch?v=yIxEEgEuhT4
  20. {"PT51M52S", (51 * time.Minute) + (52 * time.Second)},
  21. // https://www.youtube.com/watch?v=bpHf1XcoiFs
  22. {"PT80M42S", (1 * time.Hour) + (20 * time.Minute) + (42 * time.Second)},
  23. }
  24. for _, tc := range scenarios {
  25. result, err := parseISO8601(tc.duration)
  26. if err != nil {
  27. t.Errorf("Got an error when parsing %q: %v", tc.duration, err)
  28. }
  29. if tc.expected != result {
  30. t.Errorf(`Unexpected result, got %v for duration %q`, result, tc.duration)
  31. }
  32. }
  33. }
  34. func TestGetYouTubeVideoIDFromURL(t *testing.T) {
  35. scenarios := []struct {
  36. url string
  37. expected string
  38. }{
  39. {"https://www.youtube.com/watch?v=HLrqNhgdiC0", "HLrqNhgdiC0"},
  40. {"https://www.youtube.com/watch?v=HLrqNhgdiC0&feature=youtu.be", "HLrqNhgdiC0"},
  41. {"https://example.org/test", ""},
  42. }
  43. for _, tc := range scenarios {
  44. result := getVideoIDFromYouTubeURL(tc.url)
  45. if tc.expected != result {
  46. t.Errorf(`Unexpected result, got %q for url %q`, result, tc.url)
  47. }
  48. }
  49. }
  50. func TestIsYouTubeVideoURL(t *testing.T) {
  51. scenarios := []struct {
  52. url string
  53. expected bool
  54. }{
  55. {"https://www.youtube.com/watch?v=HLrqNhgdiC0", true},
  56. {"https://www.youtube.com/watch?v=HLrqNhgdiC0&feature=youtu.be", true},
  57. {"https://example.org/test", false},
  58. }
  59. for _, tc := range scenarios {
  60. result := isYouTubeVideoURL(tc.url)
  61. if tc.expected != result {
  62. t.Errorf(`Unexpected result, got %v for url %q`, result, tc.url)
  63. }
  64. }
  65. }