blob: f6c82881e4b060886ffd13757ae3f0ce6c81aa9c (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
|
;;; test-elfeed-config-youtube-feed-format.el --- Tests for YouTube feed conversion -*- lexical-binding: t; -*-
;;; Commentary:
;; cj/youtube-to-elfeed-feed-format fetches a YouTube page synchronously to
;; derive the channel/playlist feed URL and title. These tests mock the
;; network boundary (url-retrieve-synchronously) and verify it passes a
;; timeout and always kills the temporary URL buffer — including when the
;; page doesn't contain the expected markers, the path that previously leaked
;; the buffer.
;;; Code:
(require 'ert)
(require 'cl-lib)
(require 'url)
(add-to-list 'load-path (expand-file-name "modules" user-emacs-directory))
(require 'elfeed-config)
(defun test-elfeed--url-buffer (html)
"Return a fresh buffer containing HTML, as url-retrieve-synchronously would."
(let ((b (generate-new-buffer " *test-url-retrieve*")))
(with-current-buffer b (insert html))
b))
;;; Normal Cases
(ert-deftest test-elfeed-youtube-channel-parses-and-cleans-up ()
"Normal: a channel page yields the feed line and the temp buffer is killed."
(let ((url-buf nil))
(cl-letf (((symbol-function 'url-retrieve-synchronously)
(lambda (&rest _)
(setq url-buf
(test-elfeed--url-buffer
(concat "<link href=\"https://www.youtube.com/feeds/videos.xml"
"?channel_id=UCabc123\">"
"<meta property=\"og:title\" content=\"Test Channel\">"))))))
(let ((result (cj/youtube-to-elfeed-feed-format "https://youtube.com/@test" 'channel)))
(should (string-match-p "channel_id=UCabc123" result))
(should (string-match-p "Test Channel" result)))
(should-not (buffer-live-p url-buf)))))
;;; Boundary Cases
(ert-deftest test-elfeed-youtube-passes-a-timeout ()
"Boundary: the synchronous fetch is given a timeout, not left unbounded."
(let ((captured nil))
(cl-letf (((symbol-function 'url-retrieve-synchronously)
(lambda (&rest args)
(setq captured args)
(test-elfeed--url-buffer ""))))
(ignore-errors (cj/youtube-to-elfeed-feed-format "https://youtube.com/@t" 'channel))
;; (url &optional silent inhibit-cookies timeout) — a 4th arg is present.
(should (>= (length captured) 4))
(should (numberp (nth 3 captured))))))
;;; Error Cases
(ert-deftest test-elfeed-youtube-cleans-up-buffer-on-parse-failure ()
"Error: a page without the markers errors, and the temp buffer is not leaked."
(let ((url-buf nil))
(cl-letf (((symbol-function 'url-retrieve-synchronously)
(lambda (&rest _)
(setq url-buf (test-elfeed--url-buffer "<html>nothing useful</html>")))))
(should-error (cj/youtube-to-elfeed-feed-format "https://youtube.com/@t" 'channel))
(should-not (buffer-live-p url-buf)))))
;;; Playlist branch
(ert-deftest test-elfeed-youtube-playlist-parses-id-and-title ()
"Normal: a playlist URL yields the playlist feed line and the og:title."
(cl-letf (((symbol-function 'url-retrieve-synchronously)
(lambda (&rest _)
(test-elfeed--url-buffer
"<meta property=\"og:title\" content=\"My Playlist\">"))))
(let ((result (cj/youtube-to-elfeed-feed-format
"https://www.youtube.com/playlist?list=PLabc123" 'playlist)))
(should (string-match-p "playlist_id=PLabc123" result))
(should (string-match-p "My Playlist" result)))))
(ert-deftest test-elfeed-youtube-playlist-id-stops-at-ampersand ()
"Boundary: extra query params after list= are not captured into the id."
(cl-letf (((symbol-function 'url-retrieve-synchronously)
(lambda (&rest _)
(test-elfeed--url-buffer
"<meta property=\"og:title\" content=\"X\">"))))
(let ((result (cj/youtube-to-elfeed-feed-format
"https://www.youtube.com/playlist?list=PLxyz&index=2" 'playlist)))
(should (string-match-p "playlist_id=PLxyz" result))
(should-not (string-match-p "index=2" result)))))
(ert-deftest test-elfeed-youtube-playlist-no-list-param-errors ()
"Error: a playlist URL with no list= parameter signals an extraction error."
(cl-letf (((symbol-function 'url-retrieve-synchronously)
(lambda (&rest _) (test-elfeed--url-buffer ""))))
(should-error (cj/youtube-to-elfeed-feed-format
"https://www.youtube.com/watch?v=abc" 'playlist))))
(ert-deftest test-elfeed-youtube-playlist-decodes-html-entities-in-title ()
"Normal: HTML entities in the og:title are decoded in the feed comment."
(cl-letf (((symbol-function 'url-retrieve-synchronously)
(lambda (&rest _)
(test-elfeed--url-buffer
(concat "<meta property=\"og:title\" content=\""
"Rock & Roll 'n' <Test> "X""
"\">")))))
(let ((result (cj/youtube-to-elfeed-feed-format
"https://www.youtube.com/playlist?list=PLe" 'playlist)))
(should (string-match-p (regexp-quote "Rock & Roll 'n' <Test> \"X\"")
result)))))
(provide 'test-elfeed-config-youtube-feed-format)
;;; test-elfeed-config-youtube-feed-format.el ends here
|