aboutsummaryrefslogtreecommitdiff
path: root/tests/test-music-config-create-radio-station.el
blob: 212419657917f5fb0dbd0f7086c766a450608186 (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
;;; test-music-config-create-radio-station.el --- Tests for manual radio-station entry -*- coding: utf-8; lexical-binding: t; -*-
;;
;; Author: Craig Jennings <c@cjennings.net>
;;
;;; Commentary:
;; Unit tests for cj/music-create-radio-station under the queue-first model:
;; a hand-entered name + URL becomes a url track in the playlist queue (with
;; the name as its title property) and playback starts.  Nothing is written
;; to disk — saving is the normal playlist-save flow.
;;
;; Test organization:
;; - Normal Cases: track queued with title, playback started, no file written
;; - Boundary Cases: unicode name preserved verbatim
;; - Error Cases: empty name, empty URL

;;; Code:

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

;; Stub missing dependencies before loading music-config
(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 'music-config)

(defmacro test-music-create-radio--with-env (&rest body)
  "Run BODY with a fresh playlist buffer, playback mocked, messages captured."
  `(let* ((cj/music-playlist-buffer-name
           (generate-new-buffer-name "*test-create-radio*"))
          (emms-player-playing-p nil)
          (started 0) (msg nil))
     (ignore started msg)
     (unwind-protect
         (cl-letf (((symbol-function 'emms-start)
                    (lambda () (setq started (1+ started))))
                   ((symbol-function 'emms-stop) #'ignore)
                   ((symbol-function 'message)
                    (lambda (fmt &rest args)
                      (when fmt (setq msg (apply #'format fmt args))))))
           ,@body)
       (when (get-buffer cj/music-playlist-buffer-name)
         (kill-buffer cj/music-playlist-buffer-name)))))

(defun test-music-create-radio--queued-tracks ()
  "Track objects currently in the test playlist buffer."
  (let ((tracks '()))
    (with-current-buffer cj/music-playlist-buffer-name
      (save-excursion
        (goto-char (point-min))
        (while (not (eobp))
          (when-let ((tr (emms-playlist-track-at (point))))
            (push tr tracks))
          (forward-line 1))))
    (nreverse tracks)))

;;; Normal Cases

(ert-deftest test-music-config-create-radio-station-normal-queues-track ()
  "Normal: name+url queues a url track carrying the name, and playback starts."
  (test-music-create-radio--with-env
   (cj/music-create-radio-station "Jazz FM" "http://stream.jazzfm.com/radio")
   (let ((tracks (test-music-create-radio--queued-tracks)))
     (should (= (length tracks) 1))
     (should (eq (emms-track-type (car tracks)) 'url))
     (should (equal (emms-track-name (car tracks)) "http://stream.jazzfm.com/radio"))
     (should (equal (emms-track-get (car tracks) 'info-title) "Jazz FM")))
   (should (= started 1))
   (should (string-match-p "Jazz FM" msg))))

(ert-deftest test-music-config-create-radio-station-normal-writes-no-file ()
  "Normal: nothing lands on disk — saving is the playlist-save flow's job."
  (let ((tmp (file-name-as-directory (make-temp-file "cj-radio-nofile-" t))))
    (unwind-protect
        (test-music-create-radio--with-env
         (let ((cj/music-m3u-root tmp)
               (cj/music-radio-save-dir tmp))
           (cj/music-create-radio-station "NPR" "https://example.test/stream")
           (should-not (directory-files tmp nil "\\.m3u\\'"))))
      (delete-directory tmp t))))

;;; Boundary Cases

(ert-deftest test-music-config-create-radio-station-boundary-unicode-name ()
  "Boundary: a unicode name is kept verbatim on the track (no filename munging)."
  (test-music-create-radio--with-env
   (cj/music-create-radio-station "Café Del Mar ☕" "https://cafe.example/stream")
   (should (equal (emms-track-get (car (test-music-create-radio--queued-tracks))
                                  'info-title)
                  "Café Del Mar ☕"))))

;;; Error Cases

(ert-deftest test-music-config-create-radio-station-error-empty-name-signals-user-error ()
  "Error: empty name signals user-error."
  (should-error (cj/music-create-radio-station "" "https://x") :type 'user-error))

(ert-deftest test-music-config-create-radio-station-error-empty-url-signals-user-error ()
  "Error: empty URL signals user-error."
  (should-error (cj/music-create-radio-station "NPR" "") :type 'user-error))

(provide 'test-music-config-create-radio-station)
;;; test-music-config-create-radio-station.el ends here