summaryrefslogtreecommitdiff
path: root/tests/test-music-config-random-navigation.el
blob: efeea05c90017ac238ac63ef0ee76ac50eeb68e1 (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
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
;;; test-music-config-random-navigation.el --- Tests for random-aware next/previous -*- coding: utf-8; lexical-binding: t; -*-
;;
;; Author: Craig Jennings <c@cjennings.net>
;;
;;; Commentary:
;; Unit tests for cj/music-next, cj/music-previous, and the random
;; history tracking mechanism.
;;
;; When emms-random-playlist is active, next should pick a random track
;; (not sequential), and previous should navigate back through the
;; history of recently played tracks.
;;
;; Test organization:
;; - Normal Cases: next/previous in sequential mode, next/previous in random mode
;; - Boundary Cases: empty history, single history entry, history at max capacity
;; - Error Cases: previous with no history, no tracks in playlist
;;
;;; Code:

(require 'ert)

;; Stub missing dependencies before loading music-config
(defvar-keymap cj/custom-keymap
  :doc "Stub keymap for testing")

;; Add EMMS elpa directory to load path for batch 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)))

;; Load EMMS for playlist buffer setup
(require 'emms)
(require 'emms-playlist-mode)

;; Load production code
(require 'music-config)

;;; Test helpers

(defun test-randnav--setup-playlist-buffer (track-names)
  "Create an EMMS playlist buffer with TRACK-NAMES and return it.
Each entry in TRACK-NAMES becomes a file track in the playlist."
  (let ((buf (get-buffer-create "*EMMS-Test-Playlist*")))
    (with-current-buffer buf
      (emms-playlist-mode)
      (setq emms-playlist-buffer-p t)
      (let ((inhibit-read-only t))
        (erase-buffer)
        (dolist (name track-names)
          (emms-playlist-insert-track (emms-track 'file name)))))
    buf))

(defun test-randnav--teardown ()
  "Clean up random navigation state and test buffers."
  (setq cj/music--random-history nil)
  (setq emms-random-playlist nil)
  (when-let ((buf (get-buffer "*EMMS-Test-Playlist*")))
    (kill-buffer buf)))

(defun test-randnav--capture-message (fn &rest args)
  "Call FN with ARGS and return the message it produces."
  (let ((captured nil))
    (cl-letf (((symbol-function 'message)
               (lambda (fmt &rest msg-args)
                 (setq captured (apply #'format fmt msg-args)))))
      (apply fn args))
    captured))

;;; Normal Cases - cj/music-next

(ert-deftest test-music-config-next-normal-sequential-calls-emms-next ()
  "Validate cj/music-next calls emms-next when random mode is off."
  (unwind-protect
      (let ((emms-random-playlist nil)
            (called nil))
        (cl-letf (((symbol-function 'emms-next)
                   (lambda () (setq called t))))
          (cj/music-next)
          (should called)))
    (test-randnav--teardown)))

(ert-deftest test-music-config-next-normal-random-calls-emms-random ()
  "Validate cj/music-next calls emms-random when random mode is on."
  (unwind-protect
      (let ((emms-random-playlist t)
            (called nil))
        (cl-letf (((symbol-function 'emms-random)
                   (lambda () (setq called t))))
          (cj/music-next)
          (should called)))
    (test-randnav--teardown)))

(ert-deftest test-music-config-next-normal-sequential-does-not-call-random ()
  "Validate cj/music-next does not call emms-random when random mode is off."
  (unwind-protect
      (let ((emms-random-playlist nil)
            (random-called nil))
        (cl-letf (((symbol-function 'emms-next)
                   (lambda () nil))
                  ((symbol-function 'emms-random)
                   (lambda () (setq random-called t))))
          (cj/music-next)
          (should-not random-called)))
    (test-randnav--teardown)))

;;; Normal Cases - cj/music-previous

(ert-deftest test-music-config-previous-normal-sequential-calls-emms-previous ()
  "Validate cj/music-previous calls emms-previous when random mode is off."
  (unwind-protect
      (let ((emms-random-playlist nil)
            (called nil))
        (cl-letf (((symbol-function 'emms-previous)
                   (lambda () (setq called t))))
          (cj/music-previous)
          (should called)))
    (test-randnav--teardown)))

(ert-deftest test-music-config-previous-normal-random-selects-history-track ()
  "Validate cj/music-previous selects the history track within the existing playlist."
  (unwind-protect
      (let* ((cj/music-playlist-buffer-name "*EMMS-Test-Playlist*")
             (buf (test-randnav--setup-playlist-buffer
                   '("/music/track1.mp3" "/music/track2.mp3" "/music/track3.mp3")))
             (emms-random-playlist t)
             (cj/music--random-history (list "/music/track2.mp3" "/music/track1.mp3"))
             (selected-pos nil))
        (cl-letf (((symbol-function 'emms-playlist-select)
                   (lambda (pos) (setq selected-pos pos)))
                  ((symbol-function 'emms-start)
                   (lambda () nil)))
          (cj/music-previous)
          ;; Should have selected a position in the buffer
          (should selected-pos)
          ;; The track at that position should be track2
          (with-current-buffer buf
            (goto-char selected-pos)
            (let ((track (emms-playlist-track-at (point))))
              (should (equal (emms-track-name track) "/music/track2.mp3"))))))
    (test-randnav--teardown)))

(ert-deftest test-music-config-previous-normal-random-pops-history ()
  "Validate cj/music-previous removes the played track from history."
  (unwind-protect
      (let* ((cj/music-playlist-buffer-name "*EMMS-Test-Playlist*")
             (_buf (test-randnav--setup-playlist-buffer
                    '("/music/track1.mp3" "/music/track2.mp3")))
             (emms-random-playlist t)
             (cj/music--random-history (list "/music/track2.mp3" "/music/track1.mp3")))
        (cl-letf (((symbol-function 'emms-playlist-select)
                   (lambda (_pos) nil))
                  ((symbol-function 'emms-start)
                   (lambda () nil)))
          (cj/music-previous)
          (should (equal cj/music--random-history '("/music/track1.mp3")))))
    (test-randnav--teardown)))

(ert-deftest test-music-config-previous-normal-sequential-does-not-touch-history ()
  "Validate cj/music-previous in sequential mode ignores history."
  (unwind-protect
      (let ((emms-random-playlist nil)
            (cj/music--random-history (list "/music/track2.mp3" "/music/track1.mp3")))
        (cl-letf (((symbol-function 'emms-previous)
                   (lambda () nil)))
          (cj/music-previous)
          (should (equal cj/music--random-history
                         '("/music/track2.mp3" "/music/track1.mp3")))))
    (test-randnav--teardown)))

;;; Normal Cases - cj/music--record-random-history

(ert-deftest test-music-config--record-random-history-normal-pushes-track ()
  "Validate recording history pushes current track name onto the list."
  (unwind-protect
      (let ((emms-random-playlist t)
            (cj/music--random-history nil))
        (cl-letf (((symbol-function 'emms-playlist-current-selected-track)
                   (lambda () (emms-track 'file "/music/track1.mp3"))))
          (cj/music--record-random-history)
          (should (equal cj/music--random-history '("/music/track1.mp3")))))
    (test-randnav--teardown)))

(ert-deftest test-music-config--record-random-history-normal-preserves-order ()
  "Validate history maintains most-recent-first order."
  (unwind-protect
      (let ((emms-random-playlist t)
            (cj/music--random-history '("/music/track1.mp3")))
        (cl-letf (((symbol-function 'emms-playlist-current-selected-track)
                   (lambda () (emms-track 'file "/music/track2.mp3"))))
          (cj/music--record-random-history)
          (should (equal (car cj/music--random-history) "/music/track2.mp3"))
          (should (equal (cadr cj/music--random-history) "/music/track1.mp3"))))
    (test-randnav--teardown)))

(ert-deftest test-music-config--record-random-history-normal-skips-when-sequential ()
  "Validate recording is skipped when random mode is off."
  (unwind-protect
      (let ((emms-random-playlist nil)
            (cj/music--random-history nil))
        (cl-letf (((symbol-function 'emms-playlist-current-selected-track)
                   (lambda () (emms-track 'file "/music/track1.mp3"))))
          (cj/music--record-random-history)
          (should (null cj/music--random-history))))
    (test-randnav--teardown)))

(ert-deftest test-music-config--record-random-history-normal-skips-duplicate ()
  "Validate recording skips if current track is already at head of history."
  (unwind-protect
      (let ((emms-random-playlist t)
            (cj/music--random-history '("/music/track1.mp3")))
        (cl-letf (((symbol-function 'emms-playlist-current-selected-track)
                   (lambda () (emms-track 'file "/music/track1.mp3"))))
          (cj/music--record-random-history)
          (should (= 1 (length cj/music--random-history)))))
    (test-randnav--teardown)))

;;; Boundary Cases

(ert-deftest test-music-config-previous-boundary-empty-history-falls-back ()
  "Validate cj/music-previous falls back to emms-previous when history is empty."
  (unwind-protect
      (let ((emms-random-playlist t)
            (cj/music--random-history nil)
            (fallback-called nil))
        (cl-letf (((symbol-function 'emms-previous)
                   (lambda () (setq fallback-called t))))
          (cj/music-previous)
          (should fallback-called)))
    (test-randnav--teardown)))

(ert-deftest test-music-config-previous-boundary-empty-history-messages ()
  "Validate cj/music-previous shows message when history is empty in random mode."
  (unwind-protect
      (let ((emms-random-playlist t)
            (cj/music--random-history nil))
        (cl-letf (((symbol-function 'emms-previous)
                   (lambda () nil)))
          (let ((msg (test-randnav--capture-message #'cj/music-previous)))
            (should (stringp msg))
            (should (string-match-p "history" msg)))))
    (test-randnav--teardown)))

(ert-deftest test-music-config-previous-boundary-single-entry-empties-history ()
  "Validate popping the last history entry leaves history empty."
  (unwind-protect
      (let* ((cj/music-playlist-buffer-name "*EMMS-Test-Playlist*")
             (_buf (test-randnav--setup-playlist-buffer
                    '("/music/track1.mp3" "/music/track2.mp3")))
             (emms-random-playlist t)
             (cj/music--random-history (list "/music/track1.mp3")))
        (cl-letf (((symbol-function 'emms-playlist-select)
                   (lambda (_pos) nil))
                  ((symbol-function 'emms-start)
                   (lambda () nil)))
          (cj/music-previous)
          (should (null cj/music--random-history))))
    (test-randnav--teardown)))

(ert-deftest test-music-config--record-random-history-boundary-caps-at-max ()
  "Validate history never exceeds cj/music--random-history-max entries."
  (unwind-protect
      (let* ((emms-random-playlist t)
             (cj/music--random-history-max 5)
             (cj/music--random-history (number-sequence 1 5)))
        (cl-letf (((symbol-function 'emms-playlist-current-selected-track)
                   (lambda () (emms-track 'file "/music/new.mp3"))))
          (cj/music--record-random-history)
          (should (= (length cj/music--random-history) 5))
          (should (equal (car cj/music--random-history) "/music/new.mp3"))))
    (test-randnav--teardown)))

(ert-deftest test-music-config--record-random-history-boundary-drops-oldest ()
  "Validate that when history is full, the oldest entry is dropped."
  (unwind-protect
      (let* ((emms-random-playlist t)
             (cj/music--random-history-max 3)
             (cj/music--random-history '("/music/c.mp3" "/music/b.mp3" "/music/a.mp3")))
        (cl-letf (((symbol-function 'emms-playlist-current-selected-track)
                   (lambda () (emms-track 'file "/music/d.mp3"))))
          (cj/music--record-random-history)
          (should (= (length cj/music--random-history) 3))
          (should (equal (car cj/music--random-history) "/music/d.mp3"))
          (should-not (member "/music/a.mp3" cj/music--random-history))))
    (test-randnav--teardown)))

(ert-deftest test-music-config--record-random-history-boundary-no-track-no-crash ()
  "Validate recording handles nil current track without error."
  (unwind-protect
      (let ((emms-random-playlist t)
            (cj/music--random-history nil))
        (cl-letf (((symbol-function 'emms-playlist-current-selected-track)
                   (lambda () nil)))
          (cj/music--record-random-history)
          (should (null cj/music--random-history))))
    (test-randnav--teardown)))

;;; Error Cases

(ert-deftest test-music-config-previous-error-random-no-history-no-crash ()
  "Validate cj/music-previous does not crash when random is on but history is nil."
  (unwind-protect
      (let ((emms-random-playlist t)
            (cj/music--random-history nil))
        (cl-letf (((symbol-function 'emms-previous)
                   (lambda () nil)))
          (should-not (condition-case err
                          (progn (cj/music-previous) nil)
                        (error err)))))
    (test-randnav--teardown)))

(ert-deftest test-music-config-previous-error-track-not-in-playlist-messages ()
  "Validate cj/music-previous shows message when history track is no longer in playlist."
  (unwind-protect
      (let* ((cj/music-playlist-buffer-name "*EMMS-Test-Playlist*")
             (_buf (test-randnav--setup-playlist-buffer
                    '("/music/track1.mp3" "/music/track3.mp3")))
             (emms-random-playlist t)
             (cj/music--random-history (list "/music/gone.mp3")))
        (let ((msg (test-randnav--capture-message #'cj/music-previous)))
          (should (stringp msg))
          (should (string-match-p "no longer" msg))))
    (test-randnav--teardown)))

;;; Normal Cases - cj/music--find-track-in-playlist

(ert-deftest test-music-config--find-track-in-playlist-normal-returns-position ()
  "Validate finding a track returns its buffer position."
  (unwind-protect
      (let* ((cj/music-playlist-buffer-name "*EMMS-Test-Playlist*")
             (buf (test-randnav--setup-playlist-buffer
                   '("/music/track1.mp3" "/music/track2.mp3" "/music/track3.mp3"))))
        (let ((pos (cj/music--find-track-in-playlist "/music/track2.mp3")))
          (should pos)
          (with-current-buffer buf
            (goto-char pos)
            (should (equal (emms-track-name (emms-playlist-track-at (point)))
                           "/music/track2.mp3")))))
    (test-randnav--teardown)))

(ert-deftest test-music-config--find-track-in-playlist-normal-finds-first-track ()
  "Validate finding the first track in the playlist."
  (unwind-protect
      (let* ((cj/music-playlist-buffer-name "*EMMS-Test-Playlist*")
             (_buf (test-randnav--setup-playlist-buffer
                    '("/music/track1.mp3" "/music/track2.mp3"))))
        (let ((pos (cj/music--find-track-in-playlist "/music/track1.mp3")))
          (should pos)))
    (test-randnav--teardown)))

(ert-deftest test-music-config--find-track-in-playlist-normal-finds-last-track ()
  "Validate finding the last track in the playlist."
  (unwind-protect
      (let* ((cj/music-playlist-buffer-name "*EMMS-Test-Playlist*")
             (_buf (test-randnav--setup-playlist-buffer
                    '("/music/track1.mp3" "/music/track2.mp3" "/music/track3.mp3"))))
        (let ((pos (cj/music--find-track-in-playlist "/music/track3.mp3")))
          (should pos)))
    (test-randnav--teardown)))

;;; Boundary Cases - cj/music--find-track-in-playlist

(ert-deftest test-music-config--find-track-in-playlist-boundary-not-found-returns-nil ()
  "Validate returning nil when track is not in playlist."
  (unwind-protect
      (let* ((cj/music-playlist-buffer-name "*EMMS-Test-Playlist*")
             (_buf (test-randnav--setup-playlist-buffer
                    '("/music/track1.mp3" "/music/track2.mp3"))))
        (should-not (cj/music--find-track-in-playlist "/music/gone.mp3")))
    (test-randnav--teardown)))

(ert-deftest test-music-config--find-track-in-playlist-boundary-empty-playlist ()
  "Validate returning nil for an empty playlist."
  (unwind-protect
      (let* ((cj/music-playlist-buffer-name "*EMMS-Test-Playlist*")
             (_buf (test-randnav--setup-playlist-buffer '())))
        (should-not (cj/music--find-track-in-playlist "/music/track1.mp3")))
    (test-randnav--teardown)))

(provide 'test-music-config-random-navigation)
;;; test-music-config-random-navigation.el ends here