aboutsummaryrefslogtreecommitdiff
path: root/tests/test-org-refile-build-targets.el
blob: dd18a91105a4596a618186f6d4b33583db2399bb (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
;;; test-org-refile-build-targets.el --- Tests for cj/build-org-refile-targets -*- lexical-binding: t; -*-

;;; Commentary:
;; Tests for `cj/build-org-refile-targets' and the underlying cache
;; shape.  The wrapper delegates to `cj-cache.el', so these tests
;; exercise the integration -- they don't reach into the cache plist
;; directly (the cache primitives have their own tests in
;; test-cj-cache.el).  Stubs `cj/--org-refile-scan-targets' to avoid
;; touching the filesystem.

;;; Code:

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

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

;; Stub dependencies before loading the module
(defvar inbox-file "/tmp/test-inbox.org")
(defvar reference-file "/tmp/test-reference.org")
(defvar schedule-file "/tmp/test-schedule.org")
(defvar code-dir "/tmp/test-code/")
(defvar projects-dir "/tmp/test-projects/")

(require 'org-refile-config)

(defun test-org-refile--reset ()
  "Reset cache and `org-refile-targets' between tests."
  (cj/cache-invalidate cj/--org-refile-targets-cache)
  (setq org-refile-targets nil))

;;; Normal Cases

(ert-deftest test-org-refile-build-targets-first-call-builds-and-populates ()
  "Normal: first call calls the scan helper and assigns to `org-refile-targets'."
  (test-org-refile--reset)
  (unwind-protect
      (let ((scan-calls 0))
        (cl-letf (((symbol-function 'cj/--org-refile-scan-targets)
                   (lambda ()
                     (cl-incf scan-calls)
                     '(("/a.org" :maxlevel . 1) ("/b.org" :maxlevel . 1)))))
          (cj/build-org-refile-targets)
          (should (= 1 scan-calls))
          (should (equal '(("/a.org" :maxlevel . 1) ("/b.org" :maxlevel . 1))
                         org-refile-targets))))
    (test-org-refile--reset)))

(ert-deftest test-org-refile-build-targets-second-call-uses-cache ()
  "Normal: a valid cache means the second call doesn't call the scan helper."
  (test-org-refile--reset)
  (unwind-protect
      (let ((scan-calls 0))
        (cl-letf (((symbol-function 'cj/--org-refile-scan-targets)
                   (lambda ()
                     (cl-incf scan-calls)
                     '(("/a.org" :maxlevel . 1)))))
          (cj/build-org-refile-targets)
          (cj/build-org-refile-targets)
          (should (= 1 scan-calls))))
    (test-org-refile--reset)))

(ert-deftest test-org-refile-build-targets-force-rebuild-bypasses-cache ()
  "Normal: FORCE-REBUILD calls the scan helper even when the cache is valid."
  (test-org-refile--reset)
  (unwind-protect
      (let ((scan-calls 0))
        (cl-letf (((symbol-function 'cj/--org-refile-scan-targets)
                   (lambda ()
                     (cl-incf scan-calls)
                     '(("/a.org" :maxlevel . 1)))))
          (cj/build-org-refile-targets)
          (cj/build-org-refile-targets 'force)
          (should (= 2 scan-calls))))
    (test-org-refile--reset)))

;;; Boundary Cases

(ert-deftest test-org-refile-build-targets-cache-expires-after-ttl ()
  "Boundary: an expired cache rebuilds (cache time backdated past TTL)."
  (test-org-refile--reset)
  (unwind-protect
      (let ((scan-calls 0))
        (cl-letf (((symbol-function 'cj/--org-refile-scan-targets)
                   (lambda ()
                     (cl-incf scan-calls)
                     '(("/a.org" :maxlevel . 1)))))
          (cj/build-org-refile-targets)
          (let ((ttl (plist-get cj/--org-refile-targets-cache :ttl)))
            (plist-put cj/--org-refile-targets-cache :time
                       (- (float-time) (1+ ttl))))
          (cj/build-org-refile-targets)
          (should (= 2 scan-calls))))
    (test-org-refile--reset)))

(ert-deftest test-org-refile-build-targets-empty-scan-still-assigns ()
  "Boundary: scan returning nil assigns nil and does not cache (per the
documented \"nil reads as invalid\" contract)."
  (test-org-refile--reset)
  (unwind-protect
      (let ((scan-calls 0))
        (cl-letf (((symbol-function 'cj/--org-refile-scan-targets)
                   (lambda () (cl-incf scan-calls) nil)))
          (cj/build-org-refile-targets)
          (should (= 1 scan-calls))
          (should (null org-refile-targets))
          (cj/build-org-refile-targets)
          (should (= 2 scan-calls))))
    (test-org-refile--reset)))

(ert-deftest test-org-refile-build-targets-building-flag-clears-on-success ()
  "Boundary: the cache's :building flag is cleared after a successful build."
  (test-org-refile--reset)
  (unwind-protect
      (cl-letf (((symbol-function 'cj/--org-refile-scan-targets)
                 (lambda () '(("/a.org" :maxlevel . 1)))))
        (cj/build-org-refile-targets)
        (should-not (cj/cache-building-p cj/--org-refile-targets-cache)))
    (test-org-refile--reset)))

;;; Error Cases

(ert-deftest test-org-refile-build-targets-scan-error-propagates ()
  "Error: a failure inside the scan helper propagates to the caller."
  (test-org-refile--reset)
  (unwind-protect
      (cl-letf (((symbol-function 'cj/--org-refile-scan-targets)
                 (lambda () (error "Permission denied"))))
        (should-error (cj/build-org-refile-targets)))
    (test-org-refile--reset)))

(ert-deftest test-org-refile-build-targets-building-flag-clears-on-error ()
  "Error: the building flag is cleared even when the scan helper signals."
  (test-org-refile--reset)
  (unwind-protect
      (cl-letf (((symbol-function 'cj/--org-refile-scan-targets)
                 (lambda () (error "Simulated failure"))))
        (ignore-errors (cj/build-org-refile-targets))
        (should-not (cj/cache-building-p cj/--org-refile-targets-cache)))
    (test-org-refile--reset)))

(provide 'test-org-refile-build-targets)
;;; test-org-refile-build-targets.el ends here