aboutsummaryrefslogtreecommitdiff
path: root/tests/test-org-drill-leitner-rebox-missing-box.el
blob: 68c49c961142ee372d762a38812c2d53d9485655 (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
;;; test-org-drill-leitner-rebox-missing-box.el --- Regression for missing DRILL_LEITNER_BOX  -*- lexical-binding: t; -*-

;;; Commentary:
;; `org-drill-leitner-rebox' read DRILL_LEITNER_BOX via `org-entry-get'
;; and passed the result straight into `string-to-number'.  When the
;; property is absent, org-entry-get returns nil and string-to-number
;; errors with "Wrong type argument: char-or-string-p, nil".  Reachable
;; if a user removes the property mid-session, or if a Leitner-tagged
;; entry is rebox'd before its DRILL_LEITNER_BOX is set.
;;
;; Fix wraps the value with `(or ... "0")' so a missing property is
;; treated as box 0 (which makes the rating semantics still sensible —
;; a "downgrade by one" stays at 0, and a promotion goes to 1).

;;; Code:

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

;;;; Regression

(ert-deftest test-leitner-rebox-survives-missing-leitner-box-property ()
  "On a leitner entry without DRILL_LEITNER_BOX, rebox should not crash."
  (with-temp-buffer
    (let ((org-startup-folded nil))
      (insert "* Question :leitner:\nbody\n")    ; no DRILL_LEITNER_BOX
      (org-mode)
      (goto-char (point-min))
      (let ((session (org-drill-session)))
        (cl-letf (((symbol-function 'read-key-sequence)
                   (lambda (_prompt) "5"))
                  ((symbol-function 'sit-for) #'ignore))
          ;; Should not error.
          (org-drill-leitner-rebox session)
          ;; After a quality-5 promotion from missing (treated as box 0):
          ;; new box = 0 + 1 = 1.  Either way, the property should now exist.
          (should (org-entry-get (point) "DRILL_LEITNER_BOX")))))))

(provide 'test-org-drill-leitner-rebox-missing-box)

;;; test-org-drill-leitner-rebox-missing-box.el ends here