aboutsummaryrefslogtreecommitdiff
path: root/tests
diff options
context:
space:
mode:
authorCraig Jennings <c@cjennings.net>2026-05-27 01:11:35 -0500
committerCraig Jennings <c@cjennings.net>2026-05-27 01:11:35 -0500
commit1f362535a109939081a9a65a4601add87afc052d (patch)
treec7f8b70acda4b97b165ecbc7a28fbb01c4b45c12 /tests
parent7eece407772d7c5cfba93ba914439094f0d9fbf2 (diff)
downloadorg-drill-1f362535a109939081a9a65a4601add87afc052d.tar.gz
org-drill-1f362535a109939081a9a65a4601add87afc052d.zip
feat: undo last rating, customizable keys, and configurable text limit
A batch of self-contained user-facing improvements, squashed from the feat/org-drill-solo-features branch. I added an undo for the last rating (issue #2 follow-up). The rating prompt now takes an undo key (org-drill--undo-key, default u): it restores the previous card's scheduling snapshot, drops the recorded quality, and re-queues that card, then returns to the current prompt. Each rating snapshots the scheduling properties and SCHEDULED line onto a per-session stack capped at org-drill-undo-limit (default 3). org-drill-reschedule loops on the rating read so undo doesn't rate the current card. I made the five session-control keys (quit, edit, help, skip, tags) defcustoms so they can be rebound from customize-group (issue #35), keeping their defaults. The 0-5 rating keys stay as-is, since they're tied to the quality scale rather than being variables. I lifted the hardcoded 100-line entry-text limit in org-drill-get-entry-text into the org-drill-entry-text-max-lines defcustom, defaulting to 100. I also deleted a commented-out old org-entry-empty-p that the real definition had already replaced. Existing tests stay green and each change added its own, including snapshot/restore and prompt-loop tests for undo.
Diffstat (limited to 'tests')
-rw-r--r--tests/test-org-drill-entry-text-limit.el34
-rw-r--r--tests/test-org-drill-session-keys.el33
-rw-r--r--tests/test-org-drill-undo.el114
3 files changed, 181 insertions, 0 deletions
diff --git a/tests/test-org-drill-entry-text-limit.el b/tests/test-org-drill-entry-text-limit.el
new file mode 100644
index 0000000..c9466cc
--- /dev/null
+++ b/tests/test-org-drill-entry-text-limit.el
@@ -0,0 +1,34 @@
+;;; test-org-drill-entry-text-limit.el --- Tests for the entry-text line limit -*- lexical-binding: t; -*-
+
+;;; Commentary:
+;; org-drill-get-entry-text asks org-agenda for at most N lines of an entry's
+;; text. That limit used to be a hardcoded 100; it is now the defcustom
+;; org-drill-entry-text-max-lines so a user with very long cards can raise it.
+
+;;; Code:
+
+(require 'ert)
+(require 'cl-lib)
+(require 'org-drill)
+
+(ert-deftest test-org-drill-entry-text-max-lines-defaults-to-100 ()
+ "The limit keeps its historical default of 100 lines."
+ (should (eq 100 (default-value 'org-drill-entry-text-max-lines))))
+
+(ert-deftest test-org-drill-get-entry-text-uses-the-custom-limit ()
+ "get-entry-text passes the defcustom value as the line limit, not a
+hardcoded number."
+ (let ((org-drill-entry-text-max-lines 7)
+ (captured nil))
+ (cl-letf (((symbol-function 'org-agenda-get-some-entry-text)
+ (lambda (_marker n &rest _) (setq captured n) "text")))
+ (with-temp-buffer
+ (insert "* Card :drill:\nbody\n")
+ (org-mode)
+ (goto-char (point-min))
+ (org-drill-get-entry-text))
+ (should (eq 7 captured)))))
+
+(provide 'test-org-drill-entry-text-limit)
+
+;;; test-org-drill-entry-text-limit.el ends here
diff --git a/tests/test-org-drill-session-keys.el b/tests/test-org-drill-session-keys.el
new file mode 100644
index 0000000..ff1b4b1
--- /dev/null
+++ b/tests/test-org-drill-session-keys.el
@@ -0,0 +1,33 @@
+;;; test-org-drill-session-keys.el --- Tests for customizable session keys -*- lexical-binding: t; -*-
+
+;;; Commentary:
+;; The session-control keys (quit/edit/help/skip/tags) are defcustoms so they
+;; can be rebound through customize-group (upstream issue #35), while keeping
+;; their historical default characters.
+
+;;; Code:
+
+(require 'ert)
+(require 'org-drill)
+
+(defconst test-org-drill-session-key-defaults
+ '((org-drill--quit-key . ?q)
+ (org-drill--edit-key . ?e)
+ (org-drill--help-key . ??)
+ (org-drill--skip-key . ?s)
+ (org-drill--tags-key . ?t))
+ "Each session-control key variable and its historical default character.")
+
+(ert-deftest test-org-drill-session-keys-are-customizable ()
+ "Each session-control key is a defcustom, so customize-group can set it."
+ (dolist (cell test-org-drill-session-key-defaults)
+ (should (custom-variable-p (car cell)))))
+
+(ert-deftest test-org-drill-session-keys-keep-their-defaults ()
+ "Promoting to defcustom does not change the default bindings."
+ (dolist (cell test-org-drill-session-key-defaults)
+ (should (eq (cdr cell) (default-value (car cell))))))
+
+(provide 'test-org-drill-session-keys)
+
+;;; test-org-drill-session-keys.el ends here
diff --git a/tests/test-org-drill-undo.el b/tests/test-org-drill-undo.el
new file mode 100644
index 0000000..a5727e4
--- /dev/null
+++ b/tests/test-org-drill-undo.el
@@ -0,0 +1,114 @@
+;;; test-org-drill-undo.el --- Tests for undo/retry of the last rating -*- lexical-binding: t; -*-
+
+;;; Commentary:
+;; Undo lets the user take back a misclicked rating. Before a rating is
+;; applied, the entry's scheduling state is snapshotted onto the session's
+;; undo-stack (capped at org-drill-undo-limit). org-drill-undo-last-rating
+;; restores that state, drops the recorded quality, and re-queues the card so
+;; it comes around again.
+
+;;; Code:
+
+(require 'ert)
+(require 'org-drill)
+
+;;;; defcustoms
+
+(ert-deftest test-org-drill-undo-limit-defaults-to-3 ()
+ (should (eq 3 (default-value 'org-drill-undo-limit))))
+
+(ert-deftest test-org-drill-undo-key-is-customizable-and-defaults-to-u ()
+ (should (custom-variable-p 'org-drill--undo-key))
+ (should (eq ?u (default-value 'org-drill--undo-key))))
+
+;;;; snapshot / restore round-trip
+
+(ert-deftest test-org-drill-snapshot-restore-round-trips-scheduling-data ()
+ "Restoring a snapshot puts changed properties back to their captured values."
+ (with-temp-buffer
+ (insert "* Card :drill:\n")
+ (org-mode)
+ (goto-char (point-min))
+ (org-set-property "DRILL_LAST_QUALITY" "4")
+ (org-set-property "DRILL_EASE" "2.5")
+ (let ((snap (org-drill--snapshot-entry-data)))
+ (org-set-property "DRILL_LAST_QUALITY" "1")
+ (org-set-property "DRILL_EASE" "1.8")
+ (org-drill--restore-entry-data snap)
+ (should (equal "4" (org-entry-get (point) "DRILL_LAST_QUALITY")))
+ (should (equal "2.5" (org-entry-get (point) "DRILL_EASE"))))))
+
+(ert-deftest test-org-drill-restore-deletes-properties-absent-at-snapshot ()
+ "A property that did not exist when snapshotted is removed on restore."
+ (with-temp-buffer
+ (insert "* Card :drill:\n")
+ (org-mode)
+ (goto-char (point-min))
+ (let ((snap (org-drill--snapshot-entry-data)))
+ (org-set-property "DRILL_LAST_QUALITY" "3")
+ (org-drill--restore-entry-data snap)
+ (should (null (org-entry-get (point) "DRILL_LAST_QUALITY"))))))
+
+;;;; org-drill-undo-last-rating
+
+(ert-deftest test-org-drill-undo-last-rating-restores-pops-and-requeues ()
+ "Undo restores the entry data, drops the recorded quality, and re-queues
+the card onto the again list."
+ (with-temp-buffer
+ (insert "* Card :drill:\n")
+ (org-mode)
+ (goto-char (point-min))
+ (org-set-property "DRILL_LAST_QUALITY" "4")
+ (let ((session (org-drill-session)))
+ (push (org-drill--snapshot-entry-data) (oref session undo-stack))
+ (org-set-property "DRILL_LAST_QUALITY" "1")
+ (push 1 (oref session qualities))
+ (org-drill-undo-last-rating session)
+ (should (equal "4" (org-entry-get (point) "DRILL_LAST_QUALITY")))
+ (should (null (oref session qualities)))
+ (should (= 1 (length (oref session again-entries)))))))
+
+(ert-deftest test-org-drill-undo-last-rating-empty-stack-is-noop ()
+ "Undo with nothing recorded does not error and changes no session state."
+ (let ((session (org-drill-session)))
+ (org-drill-undo-last-rating session)
+ (should (null (oref session qualities)))
+ (should (null (oref session again-entries)))))
+
+;;;; undo-key routing through the rating prompt
+
+(ert-deftest test-org-drill-reschedule-undo-key-takes-back-previous-rating ()
+ "Pressing the undo key at the prompt restores the previous card and
+re-queues it, then the prompt loop continues (here, a quit ends it).
+
+Components integrated:
+- org-drill-reschedule (entry point, real)
+- org-drill--read-rating-key (MOCKED — returns undo key then quit key)
+- org-drill-undo-last-rating + snapshot/restore (real)
+
+Validates the undo key is handled in the prompt loop rather than treated
+as a rating, and that the loop keeps prompting afterward."
+ (with-temp-buffer
+ (insert "* Prev :drill:\n* Current :drill:\n")
+ (org-mode)
+ (let ((session (org-drill-session)))
+ (goto-char (point-min))
+ (org-set-property "DRILL_LAST_QUALITY" "5")
+ (push (org-drill--snapshot-entry-data) (oref session undo-stack))
+ (org-set-property "DRILL_LAST_QUALITY" "0") ; the misrating to take back
+ (push 0 (oref session qualities))
+ (goto-char (point-min))
+ (re-search-forward "Current")
+ (let ((keys (list org-drill--undo-key org-drill--quit-key)))
+ (cl-letf (((symbol-function 'org-drill--read-rating-key)
+ (lambda (&rest _) (pop keys)))
+ ((symbol-function 'sit-for) #'ignore))
+ (should (null (org-drill-reschedule session)))))
+ (goto-char (point-min))
+ (should (equal "5" (org-entry-get (point) "DRILL_LAST_QUALITY")))
+ (should (null (oref session qualities)))
+ (should (= 1 (length (oref session again-entries)))))))
+
+(provide 'test-org-drill-undo)
+
+;;; test-org-drill-undo.el ends here