aboutsummaryrefslogtreecommitdiff
path: root/tests/test-media-utils--yt-dl-message.el
blob: 491b64cfedeaa3358e5845ec47420d4c4edc4580 (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
;;; test-media-utils--yt-dl-message.el --- Tests for the yt-dl sentinel message -*- lexical-binding: t; -*-

;;; Commentary:
;; Unit tests for cj/media--yt-dl-message, the pure helper behind
;; cj/yt-dl-it's process sentinel.
;;
;; The behavior under test is a correctness fix, not cosmetics.  cj/yt-dl-it
;; launches "tsp yt-dlp ...", and tsp enqueues the job and exits immediately.
;; The sentinel therefore fires on tsp's exit, not on yt-dlp's, so the old
;; "Finished downloading" text claimed a completed download at the moment the
;; download was merely queued -- and a yt-dlp failure minutes later was silent.
;; The helper reports queueing, which is the only thing tsp's exit actually
;; proves.
;;
;; Test organization:
;; - Normal Cases: clean tsp exit reports queued; abnormal exit reports failure
;; - Boundary Cases: unrelated events return nil; URL text passes through verbatim
;; - Error Cases: empty event string returns nil
;;
;;; Code:

(require 'ert)
(require 'media-utils)

;;; Normal Cases

(ert-deftest test-media-utils--yt-dl-message-normal-finished-says-queued ()
  "Normal: a clean tsp exit reports the job queued, never downloaded."
  (let ((msg (cj/media--yt-dl-message "finished\n" "https://example.com/v")))
    (should (string-match-p "[Qq]ueued" msg))
    (should-not (string-match-p "[Ff]inished downloading" msg))))

(ert-deftest test-media-utils--yt-dl-message-normal-abnormal-reports-failure ()
  "Normal: an abnormal tsp exit reports that queueing failed."
  (let ((msg (cj/media--yt-dl-message "exited abnormally with code 1\n"
                                      "https://example.com/v")))
    (should msg)
    (should-not (string-match-p "[Qq]ueued for" msg))))

;;; Boundary Cases

(ert-deftest test-media-utils--yt-dl-message-boundary-unrelated-event-is-nil ()
  "Boundary: an event that reports neither outcome produces no message."
  (should (null (cj/media--yt-dl-message "run\n" "https://example.com/v")))
  (should (null (cj/media--yt-dl-message "stopped\n" "https://example.com/v"))))

(ert-deftest test-media-utils--yt-dl-message-boundary-url-passes-through ()
  "Boundary: the URL text is carried into the message verbatim."
  (let ((url "https://example.com/watch?v=a&b=c%20d"))
    (should (string-match-p (regexp-quote url)
                            (cj/media--yt-dl-message "finished\n" url)))))

(ert-deftest test-media-utils--yt-dl-message-boundary-empty-url ()
  "Boundary: an empty URL still yields a message rather than signaling."
  (should (stringp (cj/media--yt-dl-message "finished\n" ""))))

;;; Error Cases

(ert-deftest test-media-utils--yt-dl-message-error-empty-event-is-nil ()
  "Error: an empty event string matches no outcome and returns nil."
  (should (null (cj/media--yt-dl-message "" "https://example.com/v"))))

(provide 'test-media-utils--yt-dl-message)
;;; test-media-utils--yt-dl-message.el ends here