aboutsummaryrefslogtreecommitdiff
path: root/tests/test-quick-video-capture--url-state.el
blob: 6a0cd583e9ea617bf89ab5d2fa0128b653324013 (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
;;; test-quick-video-capture--url-state.el --- Tests for video-capture URL state -*- lexical-binding: t; -*-

;;; Commentary:
;; Unit tests for the URL handoff between the org-protocol entry point and the
;; capture handler in quick-video-capture.el.  The URL is passed via a
;; dynamically-bound variable scoped to the org-capture call, so an aborted or
;; erroring capture cannot leave a stale URL behind for the next manual capture.

;;; Code:

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

(add-to-list 'load-path (expand-file-name "modules" user-emacs-directory))
(require 'quick-video-capture)

;;; cj/video-download-capture-handler

(ert-deftest test-qvc-handler-downloads-bound-url ()
  "Normal: the handler downloads the dynamically-bound URL and saves nothing."
  (let (downloaded)
    (cl-letf (((symbol-function 'require) (lambda (&rest _) nil))
              ((symbol-function 'cj/yt-dl-it) (lambda (u) (setq downloaded u))))
      (let ((cj/--video-download-url "https://example.com/v"))
        (should (equal "" (cj/video-download-capture-handler))))
      (should (equal "https://example.com/v" downloaded)))))

(ert-deftest test-qvc-handler-prompts-when-no-bound-url ()
  "Boundary: with no bound URL the handler prompts the user."
  (let (downloaded)
    (cl-letf (((symbol-function 'require) (lambda (&rest _) nil))
              ((symbol-function 'cj/yt-dl-it) (lambda (u) (setq downloaded u)))
              ((symbol-function 'read-string) (lambda (&rest _) "https://typed/v")))
      (let ((cj/--video-download-url nil))
        (cj/video-download-capture-handler))
      (should (equal "https://typed/v" downloaded)))))

(ert-deftest test-qvc-handler-empty-url-errors ()
  "Error: an empty URL signals rather than downloading nothing."
  (cl-letf (((symbol-function 'require) (lambda (&rest _) nil))
            ((symbol-function 'read-string) (lambda (&rest _) "")))
    (let ((cj/--video-download-url ""))
      (should-error (cj/video-download-capture-handler)))))

;;; cj/org-protocol-video-download — no global leak

(ert-deftest test-qvc-protocol-binds-url-during-capture ()
  "Normal: the protocol handler makes the URL visible to the capture call."
  (let (seen-during)
    (cl-letf (((symbol-function 'cj/ensure-video-download-initialized) #'ignore)
              ((symbol-function 'org-capture)
               (lambda (&rest _) (setq seen-during cj/--video-download-url))))
      (cj/org-protocol-video-download '(:url "https://example.com/p")))
    (should (equal "https://example.com/p" seen-during))))

(ert-deftest test-qvc-protocol-leaves-no-stale-url ()
  "Boundary: after the protocol capture returns, no URL remains bound."
  (cl-letf (((symbol-function 'cj/ensure-video-download-initialized) #'ignore)
            ((symbol-function 'org-capture) #'ignore))
    (cj/org-protocol-video-download '(:url "https://example.com/p")))
  (should (null cj/--video-download-url)))

(ert-deftest test-qvc-protocol-aborted-capture-clears-url ()
  "Error: a capture that errors/aborts mid-flow still leaves no stale URL.
This is the regression the dynamic binding prevents: the old global
setq survived an interrupted capture into the next manual one."
  (cl-letf (((symbol-function 'cj/ensure-video-download-initialized) #'ignore)
            ((symbol-function 'org-capture)
             (lambda (&rest _) (error "simulated capture abort"))))
    (ignore-errors
      (cj/org-protocol-video-download '(:url "https://example.com/p"))))
  (should (null cj/--video-download-url)))

(provide 'test-quick-video-capture--url-state)
;;; test-quick-video-capture--url-state.el ends here