aboutsummaryrefslogtreecommitdiff
path: root/tests/test-music-config-playlist-commands.el
blob: 3d6dfd8b9513ddfc9f87b86917b2f7db0de7846c (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
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
;;; test-music-config-playlist-commands.el --- Tests for the music-config playlist commands -*- coding: utf-8; lexical-binding: t; -*-
;;
;; Author: Craig Jennings <c@cjennings.net>
;;
;;; Commentary:
;; Sibling tests cover the pure helpers and a first batch of small
;; commands.  This file covers the remaining interactive playlist
;; commands plus the random-aware navigation pair:
;;
;;   cj/music-playlist-load
;;   cj/music-playlist-reload
;;   cj/music-playlist-edit
;;   cj/music-next
;;   cj/music-previous
;;   cj/music--consume-track
;;
;; EMMS primitives (`emms-playlist-clear', `emms-play-playlist',
;; `emms-stop', `emms-random', `emms-next', `emms-previous',
;; `emms-start', `emms-playlist-select') are stubbed throughout.
;;
;;; Code:

(require 'ert)
(require 'cl-lib)
(require 'testutil-general)

(defvar-keymap cj/custom-keymap :doc "Stub keymap for testing")

(let ((emms-dir (car (file-expand-wildcards
                      (expand-file-name "elpa/emms-*" user-emacs-directory)))))
  (when emms-dir
    (add-to-list 'load-path emms-dir)))

(require 'emms)
(require 'emms-playlist-mode)
(require 'music-config)

(defun test-mc-pc--setup ()
  "Per-test setup."
  (cj/create-test-base-dir)
  (let ((buf (get-buffer-create cj/music-playlist-buffer-name)))
    (with-current-buffer buf
      (emms-playlist-mode)
      (setq emms-playlist-buffer-p t)
      (setq cj/music-playlist-file nil))
    (setq emms-playlist-buffer buf)
    buf))

(defun test-mc-pc--teardown ()
  "Per-test cleanup."
  (when-let ((buf (get-buffer cj/music-playlist-buffer-name)))
    (with-current-buffer buf
      (setq cj/music-playlist-file nil))
    (kill-buffer buf))
  (cj/delete-test-base-dir))

;;; cj/music-playlist-load

(ert-deftest test-mc-playlist-load-normal-loads-selected-file ()
  "Normal: user picks an existing playlist; load wires it via emms-play-playlist."
  (test-mc-pc--setup)
  (unwind-protect
      (let ((tmp (make-temp-file "test-mc-pl-" nil ".m3u"))
            (played nil))
        (with-temp-file tmp (insert "track.mp3\n"))
        (cl-letf (((symbol-function 'cj/music--get-m3u-files)
                   (lambda () (list (cons "test.m3u" tmp))))
                  ((symbol-function 'completing-read)
                   (lambda (&rest _) "test.m3u"))
                  ((symbol-function 'emms-playlist-clear) #'ignore)
                  ((symbol-function 'emms-play-playlist)
                   (lambda (f) (setq played f)))
                  ((symbol-function 'message) #'ignore))
          (cj/music-playlist-load))
        (should (equal played tmp))
        (delete-file tmp))
    (test-mc-pc--teardown)))

(ert-deftest test-mc-playlist-load-error-on-missing-file ()
  "Error: user picks a name whose file isn't on disk -> user-error."
  (test-mc-pc--setup)
  (unwind-protect
      (cl-letf (((symbol-function 'cj/music--get-m3u-files)
                 (lambda () '(("ghost.m3u" . "/nope/ghost.m3u"))))
                ((symbol-function 'completing-read)
                 (lambda (&rest _) "ghost.m3u"))
                ((symbol-function 'emms-playlist-clear) #'ignore)
                ((symbol-function 'emms-play-playlist) #'ignore))
        (should-error (cj/music-playlist-load) :type 'user-error))
    (test-mc-pc--teardown)))

;;; cj/music-playlist-reload

(ert-deftest test-mc-playlist-reload-replays-current-file ()
  "Normal: with a valid playlist file the function plays it again."
  (test-mc-pc--setup)
  (unwind-protect
      (let ((tmp (make-temp-file "test-mc-reload-" nil ".m3u"))
            (played nil))
        (with-temp-file tmp (insert "track.mp3\n"))
        (with-current-buffer cj/music-playlist-buffer-name
          (setq cj/music-playlist-file tmp))
        (cl-letf (((symbol-function 'emms-playlist-clear) #'ignore)
                  ((symbol-function 'emms-play-playlist)
                   (lambda (f) (setq played f)))
                  ((symbol-function 'message) #'ignore))
          (cj/music-playlist-reload))
        (should (equal played tmp))
        (delete-file tmp))
    (test-mc-pc--teardown)))

(ert-deftest test-mc-playlist-reload-error-no-associated-file ()
  "Error: no associated playlist file -> assert step errors with user-error."
  (test-mc-pc--setup)
  (unwind-protect
      (with-current-buffer cj/music-playlist-buffer-name
        (setq cj/music-playlist-file nil)
        (should-error (cj/music-playlist-reload) :type 'user-error))
    (test-mc-pc--teardown)))

;;; cj/music-playlist-edit

(ert-deftest test-mc-playlist-edit-opens-other-window-when-clean ()
  "Normal: with a clean playlist, edit opens the file in another window."
  (test-mc-pc--setup)
  (unwind-protect
      (let ((tmp (make-temp-file "test-mc-edit-" nil ".m3u"))
            (opened nil))
        (with-temp-file tmp (insert "track.mp3\n"))
        (with-current-buffer cj/music-playlist-buffer-name
          (setq cj/music-playlist-file tmp))
        (cl-letf (((symbol-function 'cj/music--playlist-modified-p)
                   (lambda () nil))
                  ((symbol-function 'find-file-other-window)
                   (lambda (p) (setq opened p))))
          (cj/music-playlist-edit))
        (should (equal opened tmp))
        (delete-file tmp))
    (test-mc-pc--teardown)))

;;; cj/music-next / cj/music-previous

(ert-deftest test-mc-next-uses-emms-next-when-not-random ()
  "Normal: with random off, next delegates to emms-next."
  (let ((emms-random-playlist nil)
        (called nil))
    (cl-letf (((symbol-function 'emms-next)
               (lambda () (setq called 'next)))
              ((symbol-function 'emms-random)
               (lambda () (setq called 'random))))
      (cj/music-next))
    (should (eq called 'next))))

(ert-deftest test-mc-next-uses-emms-random-when-random-on ()
  "Normal: with random on, next picks a random track."
  (let ((emms-random-playlist t)
        (called nil))
    (cl-letf (((symbol-function 'emms-next)
               (lambda () (setq called 'next)))
              ((symbol-function 'emms-random)
               (lambda () (setq called 'random))))
      (cj/music-next))
    (should (eq called 'random))))

(ert-deftest test-mc-previous-uses-emms-previous-when-not-random ()
  "Normal: with random off, previous delegates to emms-previous."
  (let ((emms-random-playlist nil)
        (called nil))
    (cl-letf (((symbol-function 'emms-previous)
               (lambda () (setq called 'prev))))
      (cj/music-previous))
    (should (eq called 'prev))))

(ert-deftest test-mc-previous-empty-random-history-messages-and-delegates ()
  "Boundary: random on but no history -> message + delegate to emms-previous."
  (let ((emms-random-playlist t)
        (cj/music--random-history nil)
        (called nil)
        (msg nil))
    (cl-letf (((symbol-function 'emms-previous)
               (lambda () (setq called 'prev)))
              ((symbol-function 'message)
               (lambda (fmt &rest args) (setq msg (apply #'format fmt args)))))
      (cj/music-previous))
    (should (eq called 'prev))
    (should (string-match-p "No random history" msg))))

(ert-deftest test-mc-previous-random-history-jumps-to-popped-track ()
  "Normal: random with history -> pop name, find pos, emms-playlist-select + start."
  (test-mc-pc--setup)
  (unwind-protect
      (let ((emms-random-playlist t)
            (cj/music--random-history '("/a/track-2.mp3" "/a/track-1.mp3"))
            (selected nil)
            (started nil))
        (cl-letf (((symbol-function 'cj/music--find-track-in-playlist)
                   (lambda (name) (when (equal name "/a/track-2.mp3") 42)))
                  ((symbol-function 'emms-playlist-select)
                   (lambda (pos) (setq selected pos)))
                  ((symbol-function 'emms-start)
                   (lambda () (setq started t))))
          (cj/music-previous))
        (should (= selected 42))
        (should started)
        ;; Top of history was consumed.
        (should (equal cj/music--random-history '("/a/track-1.mp3"))))
    (test-mc-pc--teardown)))

(ert-deftest test-mc-previous-random-popped-track-missing-messages ()
  "Boundary: history pop returns a track no longer in the playlist -> message."
  (test-mc-pc--setup)
  (unwind-protect
      (let ((emms-random-playlist t)
            (cj/music--random-history '("/a/gone.mp3"))
            (started nil)
            (msg nil))
        (cl-letf (((symbol-function 'cj/music--find-track-in-playlist)
                   (lambda (_) nil))
                  ((symbol-function 'emms-start)
                   (lambda () (setq started t)))
                  ((symbol-function 'message)
                   (lambda (fmt &rest args)
                     (setq msg (apply #'format fmt args)))))
          (cj/music-previous))
        (should-not started)
        (should (string-match-p "no longer in playlist" msg)))
    (test-mc-pc--teardown)))

;;; cj/music--consume-track

(ert-deftest test-mc-consume-track-no-op-when-mode-off ()
  "Boundary: with consume-mode off, the function doesn't touch the playlist."
  (test-mc-pc--setup)
  (unwind-protect
      (let ((cj/music-consume-mode nil)
            (killed nil))
        (cl-letf (((symbol-function 'emms-playlist-mode-kill-track)
                   (lambda () (setq killed t))))
          (cj/music--consume-track))
        (should-not killed))
    (test-mc-pc--teardown)))

(ert-deftest test-mc-consume-track-kills-selected-when-mode-on ()
  "Normal: with consume on + a live selected marker, kill-track is called."
  (test-mc-pc--setup)
  (unwind-protect
      (let ((cj/music-consume-mode t)
            (killed nil))
        (with-current-buffer cj/music-playlist-buffer-name
          (let ((inhibit-read-only t))
            (erase-buffer)
            (insert "track\n"))
          (setq emms-playlist-selected-marker (point-min-marker)))
        (cl-letf (((symbol-function 'emms-playlist-mode-kill-track)
                   (lambda () (setq killed t))))
          (cj/music--consume-track))
        (should killed))
    (test-mc-pc--teardown)))

(provide 'test-music-config-playlist-commands)
;;; test-music-config-playlist-commands.el ends here