aboutsummaryrefslogtreecommitdiff
path: root/tests
diff options
context:
space:
mode:
Diffstat (limited to 'tests')
-rw-r--r--tests/test-org-drill-statistics-attention-data.el263
-rw-r--r--tests/test-org-drill-statistics-distribution.el84
-rw-r--r--tests/test-org-drill-statistics-forecast.el136
-rw-r--r--tests/test-org-drill-statistics-overview-counts.el181
-rw-r--r--tests/test-org-drill-statistics-pass-rate-by-day.el200
-rw-r--r--tests/test-org-drill-statistics-primitives.el139
-rw-r--r--tests/test-org-drill-statistics-quality-histogram.el100
-rw-r--r--tests/test-org-drill-statistics-render-attention.el141
-rw-r--r--tests/test-org-drill-statistics-render-forecast.el131
-rw-r--r--tests/test-org-drill-statistics-render-overview.el106
-rw-r--r--tests/test-org-drill-statistics-render-trends.el129
-rw-r--r--tests/test-org-drill-statistics-reviews-by-day.el111
-rw-r--r--tests/test-org-drill-statistics-shell.el153
-rw-r--r--tests/test-org-drill-statistics-sparkline.el84
-rw-r--r--tests/test-org-drill-statistics-weekly-aggregates.el184
15 files changed, 2142 insertions, 0 deletions
diff --git a/tests/test-org-drill-statistics-attention-data.el b/tests/test-org-drill-statistics-attention-data.el
new file mode 100644
index 0000000..7c3d8c7
--- /dev/null
+++ b/tests/test-org-drill-statistics-attention-data.el
@@ -0,0 +1,263 @@
+;;; test-org-drill-statistics-attention-data.el --- Tests for attention-data statistics -*- lexical-binding: t; -*-
+
+;;; Commentary:
+;; ERT tests for the org-drill statistics dashboard attention-data block.
+
+;;; Code:
+
+(require 'ert)
+(require 'org-drill)
+(require 'cl-lib)
+(require 'org)
+
+;;; ERT tests for the needs-attention selectors.
+;;
+;; The org-traversal collector and the public selectors are exercised
+;; through a `with-temp-buffer' fixture with deterministic data. Day
+;; offsets are computed relative to (current-time) so the fixture never
+;; hardcodes a calendar date. The pure predicates and the row cap are
+;; tested directly on structs without any buffer.
+
+
+(defun test-org-drill-statistics--inactive-stamp (days-ago)
+ "Return an inactive org timestamp string DAYS-AGO before today.
+Derived from (current-time) so the fixture stays date-independent."
+ (org-drill-time-to-inactive-org-timestamp
+ (time-subtract (current-time) (days-to-time days-ago))))
+
+(defun test-org-drill-statistics--mkdata (&rest kw)
+ "Build an entry-attention-data struct from keyword args KW.
+Defaults: failure 0, avg nil, review nil, added nil, repeats 0, pos 1."
+ (org-drill-statistics--make-entry-attention-data
+ :heading (or (plist-get kw :heading) "card")
+ :pos (or (plist-get kw :pos) 1)
+ :failure-count (or (plist-get kw :failure-count) 0)
+ :avg-quality (plist-get kw :avg-quality)
+ :days-since-review (plist-get kw :days-since-review)
+ :days-since-added (plist-get kw :days-since-added)
+ :total-repeats (or (plist-get kw :total-repeats) 0)))
+
+;;; ---- Normal cases: predicates ----
+
+(ert-deftest test-org-drill-statistics-leech-predicate-flags-low-quality-failer ()
+ "A card over the failure threshold with low avg quality is a leech."
+ (let ((org-drill-leech-failure-threshold 3)
+ (org-drill-statistics-leech-quality-threshold 2.5))
+ (should (org-drill-statistics--leech-candidate-p
+ (test-org-drill-statistics--mkdata
+ :failure-count 4 :avg-quality 1.8)))))
+
+(ert-deftest test-org-drill-statistics-long-overdue-predicate-flags-stale-review ()
+ "A review older than the lapse threshold is long overdue."
+ (let ((org-drill-lapse-threshold-days 30))
+ (should (org-drill-statistics--long-overdue-p
+ (test-org-drill-statistics--mkdata :days-since-review 45)))))
+
+(ert-deftest test-org-drill-statistics-forgotten-new-predicate-flags-unrepeated-old ()
+ "A card added 20 days ago with zero repeats is forgotten-new."
+ (should (org-drill-statistics--forgotten-new-p
+ (test-org-drill-statistics--mkdata
+ :days-since-added 20 :total-repeats 0))))
+
+;;; ---- Boundary cases: predicates ----
+
+(ert-deftest test-org-drill-statistics-leech-predicate-quality-at-threshold-excluded ()
+ "Average quality exactly at the ceiling is not a leech (strict <)."
+ (let ((org-drill-leech-failure-threshold 3)
+ (org-drill-statistics-leech-quality-threshold 2.5))
+ (should-not (org-drill-statistics--leech-candidate-p
+ (test-org-drill-statistics--mkdata
+ :failure-count 5 :avg-quality 2.5)))))
+
+(ert-deftest test-org-drill-statistics-leech-predicate-failures-at-threshold-included ()
+ "Failure count equal to the threshold satisfies the >= test."
+ (let ((org-drill-leech-failure-threshold 3)
+ (org-drill-statistics-leech-quality-threshold 2.5))
+ (should (org-drill-statistics--leech-candidate-p
+ (test-org-drill-statistics--mkdata
+ :failure-count 3 :avg-quality 2.0)))))
+
+(ert-deftest test-org-drill-statistics-long-overdue-predicate-equal-threshold-excluded ()
+ "Exactly the lapse threshold is not yet over it (strict >)."
+ (let ((org-drill-lapse-threshold-days 30))
+ (should-not (org-drill-statistics--long-overdue-p
+ (test-org-drill-statistics--mkdata :days-since-review 30)))))
+
+(ert-deftest test-org-drill-statistics-forgotten-new-predicate-exactly-14-days-included ()
+ "Added exactly 14 days ago meets the >= 14 day floor."
+ (should (org-drill-statistics--forgotten-new-p
+ (test-org-drill-statistics--mkdata
+ :days-since-added 14 :total-repeats 0))))
+
+(ert-deftest test-org-drill-statistics-forgotten-new-predicate-13-days-excluded ()
+ "Added 13 days ago is below the 14-day floor."
+ (should-not (org-drill-statistics--forgotten-new-p
+ (test-org-drill-statistics--mkdata
+ :days-since-added 13 :total-repeats 0))))
+
+;;; ---- Error / absent-data cases: predicates ----
+
+(ert-deftest test-org-drill-statistics-leech-predicate-missing-quality-excluded ()
+ "A card with no recorded average quality is not a leech."
+ (let ((org-drill-leech-failure-threshold 3)
+ (org-drill-statistics-leech-quality-threshold 2.5))
+ (should-not (org-drill-statistics--leech-candidate-p
+ (test-org-drill-statistics--mkdata
+ :failure-count 9 :avg-quality nil)))))
+
+(ert-deftest test-org-drill-statistics-long-overdue-predicate-never-reviewed-excluded ()
+ "A never-reviewed card (nil days) is not long overdue."
+ (let ((org-drill-lapse-threshold-days 30))
+ (should-not (org-drill-statistics--long-overdue-p
+ (test-org-drill-statistics--mkdata :days-since-review nil)))))
+
+(ert-deftest test-org-drill-statistics-forgotten-new-predicate-missing-add-date-excluded ()
+ "A card with no add date is not forgotten-new."
+ (should-not (org-drill-statistics--forgotten-new-p
+ (test-org-drill-statistics--mkdata
+ :days-since-added nil :total-repeats 0))))
+
+(ert-deftest test-org-drill-statistics-forgotten-new-predicate-repeated-excluded ()
+ "An old card that has been repeated is not forgotten-new."
+ (should-not (org-drill-statistics--forgotten-new-p
+ (test-org-drill-statistics--mkdata
+ :days-since-added 30 :total-repeats 2))))
+
+;;; ---- Row cap ----
+
+(ert-deftest test-org-drill-statistics-cap-rows-under-limit-unchanged ()
+ "A list shorter than the limit is returned unchanged."
+ (let ((org-drill-statistics-attention-row-limit 10))
+ (should (equal '(a b c) (org-drill-statistics--cap-rows '(a b c))))))
+
+(ert-deftest test-org-drill-statistics-cap-rows-over-limit-truncated ()
+ "A list longer than the limit is truncated to the limit length."
+ (let ((org-drill-statistics-attention-row-limit 3))
+ (should (equal '(a b c)
+ (org-drill-statistics--cap-rows '(a b c d e))))))
+
+(ert-deftest test-org-drill-statistics-cap-rows-empty-stays-empty ()
+ "An empty list caps to empty."
+ (let ((org-drill-statistics-attention-row-limit 5))
+ (should (null (org-drill-statistics--cap-rows '())))))
+
+;;; ---- timestamp helper ----
+
+(ert-deftest test-org-drill-statistics-days-since-timestamp-nil-returns-nil ()
+ "A nil timestamp yields nil days."
+ (should (null (org-drill-statistics--days-since-org-timestamp nil 1000))))
+
+(ert-deftest test-org-drill-statistics-days-since-timestamp-malformed-returns-nil ()
+ "A malformed timestamp is caught and yields nil rather than erroring."
+ (should (null (org-drill-statistics--days-since-org-timestamp
+ "not-a-date" 1000))))
+
+;;; ---- Integration via with-temp-buffer fixture ----
+
+(defmacro test-org-drill-statistics--with-cards (&rest body)
+ "Run BODY in a temp org buffer holding drill cards.
+The buffer holds one card per needs-attention category plus a clean
+card. Standard thresholds are bound so the predicates have stable
+inputs. Dates are relative to today."
+ `(let ((org-drill-leech-failure-threshold 3)
+ (org-drill-statistics-leech-quality-threshold 2.5)
+ (org-drill-lapse-threshold-days 30)
+ (org-drill-statistics-attention-row-limit 10)
+ (org-drill-question-tag "drill")
+ (org-drill-scope 'file)
+ (org-drill-match nil))
+ (with-temp-buffer
+ (org-mode)
+ (insert
+ "* Leech card :drill:\n"
+ ":PROPERTIES:\n"
+ ":DRILL_FAILURE_COUNT: 5\n"
+ ":DRILL_AVERAGE_QUALITY: 1.2\n"
+ ":DRILL_LAST_REVIEWED: " (test-org-drill-statistics--inactive-stamp 2) "\n"
+ ":DRILL_TOTAL_REPEATS: 7\n"
+ ":END:\n"
+ "* Overdue card :drill:\n"
+ ":PROPERTIES:\n"
+ ":DRILL_LAST_REVIEWED: " (test-org-drill-statistics--inactive-stamp 60) "\n"
+ ":DRILL_TOTAL_REPEATS: 3\n"
+ ":END:\n"
+ "* Forgotten new card :drill:\n"
+ ":PROPERTIES:\n"
+ ":DATE_ADDED: " (test-org-drill-statistics--inactive-stamp 20) "\n"
+ ":END:\n"
+ "* Healthy card :drill:\n"
+ ":PROPERTIES:\n"
+ ":DRILL_FAILURE_COUNT: 0\n"
+ ":DRILL_AVERAGE_QUALITY: 4.8\n"
+ ":DRILL_LAST_REVIEWED: " (test-org-drill-statistics--inactive-stamp 1) "\n"
+ ":DATE_ADDED: " (test-org-drill-statistics--inactive-stamp 1) "\n"
+ ":DRILL_TOTAL_REPEATS: 12\n"
+ ":END:\n")
+ ,@body)))
+
+(ert-deftest test-org-drill-statistics-leech-candidates-selects-leech-only ()
+ "Only the leech card is returned by the leech selector."
+ (test-org-drill-statistics--with-cards
+ (let ((result (org-drill-statistics--leech-candidates)))
+ (should (equal '("Leech card") (mapcar #'car result)))
+ (should (integerp (cdr (car result)))))))
+
+(ert-deftest test-org-drill-statistics-long-overdue-selects-overdue-only ()
+ "Only the overdue card is returned by the overdue selector."
+ (test-org-drill-statistics--with-cards
+ (should (equal '("Overdue card")
+ (mapcar #'car (org-drill-statistics--long-overdue))))))
+
+(ert-deftest test-org-drill-statistics-forgotten-new-selects-forgotten-only ()
+ "Only the forgotten-new card is returned by that selector."
+ (test-org-drill-statistics--with-cards
+ (should (equal '("Forgotten new card")
+ (mapcar #'car (org-drill-statistics--forgotten-new))))))
+
+(ert-deftest test-org-drill-statistics-long-overdue-sorted-most-overdue-first ()
+ "The overdue list is ordered by descending staleness."
+ (let ((org-drill-lapse-threshold-days 10)
+ (org-drill-question-tag "drill")
+ (org-drill-scope 'file)
+ (org-drill-statistics-attention-row-limit 10)
+ (org-drill-match nil))
+ (with-temp-buffer
+ (org-mode)
+ (insert
+ "* Mild :drill:\n:PROPERTIES:\n:DRILL_LAST_REVIEWED: "
+ (test-org-drill-statistics--inactive-stamp 15) "\n:END:\n"
+ "* Severe :drill:\n:PROPERTIES:\n:DRILL_LAST_REVIEWED: "
+ (test-org-drill-statistics--inactive-stamp 90) "\n:END:\n")
+ (should (equal '("Severe" "Mild")
+ (mapcar #'car (org-drill-statistics--long-overdue)))))))
+
+(ert-deftest test-org-drill-statistics-leech-candidates-empty-buffer-returns-nil ()
+ "A buffer with no drill entries yields no leech candidates."
+ (let ((org-drill-question-tag "drill")
+ (org-drill-scope 'file)
+ (org-drill-statistics-attention-row-limit 10)
+ (org-drill-match nil))
+ (with-temp-buffer
+ (org-mode)
+ (insert "* Just a heading\nNo drill tag here.\n")
+ (should (null (org-drill-statistics--leech-candidates))))))
+
+(ert-deftest test-org-drill-statistics-leech-candidates-respects-row-limit ()
+ "More leeches than the limit are truncated to the limit count."
+ (let ((org-drill-leech-failure-threshold 3)
+ (org-drill-statistics-leech-quality-threshold 2.5)
+ (org-drill-statistics-attention-row-limit 2)
+ (org-drill-question-tag "drill")
+ (org-drill-scope 'file)
+ (org-drill-match nil))
+ (with-temp-buffer
+ (org-mode)
+ (dotimes (i 4)
+ (insert
+ (format "* Leech %d :drill:\n:PROPERTIES:\n:DRILL_FAILURE_COUNT: 4\n:DRILL_AVERAGE_QUALITY: %s\n:END:\n"
+ i (+ 1.0 (* i 0.1)))))
+ (should (= 2 (length (org-drill-statistics--leech-candidates)))))))
+
+(provide 'test-org-drill-statistics-attention-data)
+
+;;; test-org-drill-statistics-attention-data.el ends here
diff --git a/tests/test-org-drill-statistics-distribution.el b/tests/test-org-drill-statistics-distribution.el
new file mode 100644
index 0000000..f98d9aa
--- /dev/null
+++ b/tests/test-org-drill-statistics-distribution.el
@@ -0,0 +1,84 @@
+;;; test-org-drill-statistics-distribution.el --- Tests for distribution statistics -*- lexical-binding: t; -*-
+
+;;; Commentary:
+;; ERT tests for the org-drill statistics dashboard distribution block.
+
+;;; Code:
+
+(require 'ert)
+(require 'org-drill)
+(require 'cl-lib)
+(require 'org)
+
+(ert-deftest test-org-drill-statistics-distribution-subheading ()
+ "The rendered section opens with the Quality Distribution subheading."
+ (let ((out (org-drill-statistics--render-distribution [1 0 2 3 5 4])))
+ (should (string-prefix-p "** Quality Distribution" out))))
+
+(ert-deftest test-org-drill-statistics-distribution-all-six-rows ()
+ "Every quality 0..5 gets a row even when its count is zero."
+ (let* ((out (org-drill-statistics--render-distribution [0 0 0 0 0 0]))
+ (lines (split-string out "\n" t)))
+ ;; subheading + note line + six quality rows.
+ (dotimes (q 6)
+ (should (string-match-p (format "^%d " q) out)))))
+
+(ert-deftest test-org-drill-statistics-distribution-counts-and-percent ()
+ "Each row shows the absolute count and the percent of total."
+ ;; Total 10: quality 5 has 5 (50%), quality 0 has 1 (10%).
+ (let ((out (org-drill-statistics--render-distribution [1 1 1 1 1 5])))
+ (should (string-match-p "^5 .* 5 50%$" out))
+ (should (string-match-p "^0 .* 1 10%$" out))))
+
+(ert-deftest test-org-drill-statistics-distribution-total-line ()
+ "A non-empty histogram reports the total rating count."
+ (let ((out (org-drill-statistics--render-distribution [2 0 0 0 0 3])))
+ (should (string-match-p "Total ratings: 5" out))))
+
+(ert-deftest test-org-drill-statistics-distribution-empty-note ()
+ "An all-zero histogram emits the empty note and 0%% percentages."
+ (let ((out (org-drill-statistics--render-distribution [0 0 0 0 0 0])))
+ (should (string-match-p "No quality ratings recorded yet\\." out))
+ (should (string-match-p "^3 .* 0 0%$" out))))
+
+(ert-deftest test-org-drill-statistics-distribution-bar-scales-to-max ()
+ "The largest count fills the full bar width; smaller counts scale down."
+ (let* ((org-drill-statistics-distribution-bar-width 10)
+ (out (org-drill-statistics--render-distribution [10 0 0 0 0 5]))
+ (block (char-to-string ?\x2588)))
+ ;; Quality 0 (count 10, the max) fills all 10 blocks.
+ (should (string-match-p
+ (concat "^0 " (regexp-quote (make-string 10 ?\x2588)))
+ out))
+ ;; Quality 5 (count 5, half the max) fills 5 blocks.
+ (should (string-match-p
+ (concat "^5 " (regexp-quote (make-string 5 ?\x2588)) " ")
+ out))))
+
+(ert-deftest test-org-drill-statistics-distribution-pure-no-buffer ()
+ "Rendering returns a string and does not switch or create buffers."
+ (let ((before (buffer-list)))
+ (should (stringp (org-drill-statistics--render-distribution [1 2 3 4 5 6])))
+ (should (equal before (buffer-list)))))
+
+(ert-deftest test-org-drill-statistics-distribution-from-histogram-helper ()
+ "Renderer composes with the quality-histogram aggregator over a fixture log.
+Components integrated:
+- org-drill-statistics--quality-histogram (real)
+- org-drill-statistics--render-distribution (real, entry point)
+Validates the count for a known fixture flows through to the rendered row."
+ (let* ((rec (make-org-drill-session-record
+ :start-time (float-time)
+ :end-time (float-time)
+ :qualities [5 5 5 0]))
+ (hist (org-drill-statistics--quality-histogram (list rec)))
+ (out (org-drill-statistics--render-distribution hist)))
+ (should (string-match-p "Total ratings: 4" out))
+ ;; Quality 5 appears three times (75%).
+ (should (string-match-p "^5 .* 3 75%$" out))
+ ;; Quality 0 appears once (25%).
+ (should (string-match-p "^0 .* 1 25%$" out))))
+
+(provide 'test-org-drill-statistics-distribution)
+
+;;; test-org-drill-statistics-distribution.el ends here
diff --git a/tests/test-org-drill-statistics-forecast.el b/tests/test-org-drill-statistics-forecast.el
new file mode 100644
index 0000000..cf6efae
--- /dev/null
+++ b/tests/test-org-drill-statistics-forecast.el
@@ -0,0 +1,136 @@
+;;; test-org-drill-statistics-forecast.el --- Tests for forecast statistics -*- lexical-binding: t; -*-
+
+;;; Commentary:
+;; ERT tests for the org-drill statistics dashboard forecast block.
+
+;;; Code:
+
+(require 'ert)
+(require 'org-drill)
+(require 'cl-lib)
+(require 'org)
+
+;;; Tests for org-drill-statistics--forecast and its pure bucketer.
+;;
+;; The bucketing math is exercised directly against
+;; `org-drill-statistics--bucket-forecast-days' with synthetic day
+;; numbers (no org buffer needed). The org-reading path is exercised
+;; through a with-temp-buffer fixture whose SCHEDULED dates are derived
+;; relative to (current-time), never hardcoded.
+
+
+(defun test-org-drill-statistics--scheduled-offset-string (days-from-today)
+ "Return an active org SCHEDULED stamp DAYS-FROM-TODAY days out.
+Derived from `current-time' so the fixture never hardcodes a date."
+ (format-time-string
+ "<%Y-%m-%d %a>"
+ (time-add (current-time) (days-to-time days-from-today))))
+
+(defun test-org-drill-statistics--forecast-fixture (offsets)
+ "Insert one scheduled drill card per integer in OFFSETS.
+Each card is tagged `drill' and scheduled that many days from today.
+Returns nothing; call inside a `with-temp-buffer' after `org-mode'."
+ (let ((n 0))
+ (dolist (off offsets)
+ (setq n (1+ n))
+ (insert (format "* Card %d :drill:\nSCHEDULED: %s\n"
+ n
+ (test-org-drill-statistics--scheduled-offset-string off))))))
+
+;; Normal cases --------------------------------------------------------
+
+(ert-deftest test-org-drill-statistics-forecast-bucket-counts-by-offset ()
+ "Bucketer tallies each day-offset into the matching index."
+ (let ((today 1000))
+ ;; Two due today, one due tomorrow, one due in 3 days.
+ (should (equal (org-drill-statistics--bucket-forecast-days
+ (list 1000 1000 1001 1003) today 7)
+ '(2 1 0 1 0 0 0)))))
+
+(ert-deftest test-org-drill-statistics-forecast-bucket-empty-input ()
+ "Bucketer returns an all-zero vector of the requested length."
+ (should (equal (org-drill-statistics--bucket-forecast-days nil 500 5)
+ '(0 0 0 0 0))))
+
+(ert-deftest test-org-drill-statistics-forecast-reads-scheduled-entries ()
+ "Forecast over a fixture counts cards by their SCHEDULED day."
+ (with-temp-buffer
+ (org-mode)
+ ;; today, today, tomorrow, day-after, plus a far-future card.
+ (test-org-drill-statistics--forecast-fixture '(0 0 1 2 30))
+ (let ((org-drill-scope 'file)
+ (org-drill-question-tag "drill")
+ (org-drill-match nil))
+ ;; Default window of 7: the day-30 card falls outside it.
+ ;; Offsets 0,0,1,2 bucket to today=2, +1=1, +2=1; +30 dropped.
+ (should (equal (org-drill-statistics--forecast 'file 7)
+ '(2 1 1 0 0 0 0))))))
+
+;; Boundary cases ------------------------------------------------------
+
+(ert-deftest test-org-drill-statistics-forecast-bucket-window-edges ()
+ "Cards on the last in-window day count; the next day does not."
+ (let ((today 0))
+ ;; offset 6 is the last bucket of a 7-day window; offset 7 is out.
+ (should (equal (org-drill-statistics--bucket-forecast-days
+ (list 6 7) today 7)
+ '(0 0 0 0 0 0 1)))))
+
+(ert-deftest test-org-drill-statistics-forecast-bucket-past-and-future-dropped ()
+ "Days before today and beyond the window are not counted."
+ (let ((today 100))
+ (should (equal (org-drill-statistics--bucket-forecast-days
+ (list 99 100 110) today 3)
+ '(1 0 0)))))
+
+(ert-deftest test-org-drill-statistics-forecast-bucket-single-day-window ()
+ "A one-day window yields a single bucket counting today's cards."
+ ;; Two cards due today, one due tomorrow; a window of 1 keeps only the
+ ;; today bucket, so the tomorrow card is dropped.
+ (should (equal (org-drill-statistics--bucket-forecast-days
+ (list 50 50 51) 50 1)
+ '(2))))
+
+(ert-deftest test-org-drill-statistics-forecast-skips-unscheduled-entries ()
+ "Entries without a SCHEDULED time are ignored by the reader."
+ (with-temp-buffer
+ (org-mode)
+ (insert "* Scheduled card :drill:\nSCHEDULED: "
+ (test-org-drill-statistics--scheduled-offset-string 0)
+ "\n* No-schedule card :drill:\n")
+ (let ((org-drill-scope 'file)
+ (org-drill-question-tag "drill")
+ (org-drill-match nil))
+ (should (equal (org-drill-statistics--scheduled-days 'file)
+ (list (org-drill-statistics--today-day))))
+ (should (equal (org-drill-statistics--forecast 'file 3)
+ '(1 0 0))))))
+
+;; Error / degenerate cases -------------------------------------------
+
+(ert-deftest test-org-drill-statistics-forecast-bucket-zero-days ()
+ "A zero-length window yields the empty list."
+ (should (equal (org-drill-statistics--bucket-forecast-days
+ (list 10 11) 10 0)
+ nil)))
+
+(ert-deftest test-org-drill-statistics-forecast-bucket-negative-days ()
+ "A negative window length is treated as empty, not an error."
+ (should (equal (org-drill-statistics--bucket-forecast-days
+ (list 10 11) 10 -3)
+ nil)))
+
+(ert-deftest test-org-drill-statistics-forecast-empty-scope ()
+ "A scope with no drill entries forecasts all zeros."
+ (with-temp-buffer
+ (org-mode)
+ (insert "* Not a card\nSome prose.\n")
+ (let ((org-drill-scope 'file)
+ (org-drill-question-tag "drill")
+ (org-drill-match nil))
+ (should (equal (org-drill-statistics--forecast 'file 4)
+ '(0 0 0 0))))))
+
+(provide 'test-org-drill-statistics-forecast)
+
+;;; test-org-drill-statistics-forecast.el ends here
diff --git a/tests/test-org-drill-statistics-overview-counts.el b/tests/test-org-drill-statistics-overview-counts.el
new file mode 100644
index 0000000..8153531
--- /dev/null
+++ b/tests/test-org-drill-statistics-overview-counts.el
@@ -0,0 +1,181 @@
+;;; test-org-drill-statistics-overview-counts.el --- Tests for overview-counts statistics -*- lexical-binding: t; -*-
+
+;;; Commentary:
+;; ERT tests for the org-drill statistics dashboard overview-counts block.
+
+;;; Code:
+
+(require 'ert)
+(require 'org-drill)
+(require 'cl-lib)
+(require 'org)
+
+;;; test-org-drill-statistics-overview-counts.el --- overview-counts tests -*- lexical-binding: t; -*-
+
+;;; Commentary:
+;; Tests for `org-drill-statistics--overview-counts': walk the drill
+;; entries in a scope and bucket them into a (:total :new :mature
+;; :lapsed) plist via `org-drill-entry-status'.
+
+;;; Code:
+
+
+(defmacro with-fixed-now (&rest body)
+ "Run BODY with `current-time' pinned to 2026-05-05 12:00."
+ `(cl-letf (((symbol-function 'current-time)
+ (lambda () (encode-time 0 0 12 5 5 2026))))
+ ,@body))
+
+(defmacro with-overview-buffer (content &rest body)
+ "Insert CONTENT into a temp org buffer, then run BODY at point-min."
+ (declare (indent 1))
+ `(with-temp-buffer
+ (let ((org-startup-folded nil))
+ (insert ,content)
+ (org-mode)
+ (goto-char (point-min))
+ ,@body)))
+
+;;;; Normal cases
+
+(ert-deftest test-org-drill-statistics-overview-counts-mixed-population ()
+ "A buffer with one new, one mature (young), and one lapsed card buckets
+each correctly and totals three."
+ (with-overview-buffer
+ (concat
+ "* New card :drill:\nbody of a brand new card\n"
+ "* Young card :drill:\n"
+ "SCHEDULED: <2026-05-04 Mon>\n"
+ ":PROPERTIES:\n"
+ ":DRILL_LAST_QUALITY: 5\n:DRILL_LAST_INTERVAL: 3\n"
+ ":DRILL_TOTAL_REPEATS: 2\n:END:\nbody\n"
+ "* Lapsed card :drill:\n"
+ "SCHEDULED: <2026-04-30 Thu>\n"
+ ":PROPERTIES:\n"
+ ":DRILL_LAST_QUALITY: 1\n:DRILL_LAST_INTERVAL: 5\n"
+ ":DRILL_TOTAL_REPEATS: 3\n:END:\nbody\n")
+ (with-fixed-now
+ (let ((counts (org-drill-statistics--overview-counts 'file)))
+ (should (= 3 (plist-get counts :total)))
+ (should (= 1 (plist-get counts :new)))
+ (should (= 1 (plist-get counts :mature)))
+ (should (= 1 (plist-get counts :lapsed)))))))
+
+(ert-deftest test-org-drill-statistics-overview-counts-overdue-counts-as-mature ()
+ "An :overdue card lands in the mature bucket, not its own."
+ (with-overview-buffer
+ (concat
+ "* Very overdue :drill:\n"
+ "SCHEDULED: <2026-04-15 Wed>\n"
+ ":PROPERTIES:\n"
+ ":DRILL_LAST_QUALITY: 5\n:DRILL_LAST_INTERVAL: 5\n"
+ ":DRILL_TOTAL_REPEATS: 3\n:END:\nbody\n")
+ (with-fixed-now
+ (let ((counts (org-drill-statistics--overview-counts 'file)))
+ (should (= 1 (plist-get counts :total)))
+ (should (= 0 (plist-get counts :new)))
+ (should (= 1 (plist-get counts :mature)))
+ (should (= 0 (plist-get counts :lapsed)))))))
+
+(ert-deftest test-org-drill-statistics-overview-counts-future-counts-in-total-only ()
+ "A future-scheduled card counts toward :total but not new/mature/lapsed."
+ (with-overview-buffer
+ (concat
+ "* Future card :drill:\n"
+ "SCHEDULED: <2026-05-10 Sun>\n"
+ "body\n")
+ (with-fixed-now
+ (let ((counts (org-drill-statistics--overview-counts 'file)))
+ (should (= 1 (plist-get counts :total)))
+ (should (= 0 (plist-get counts :new)))
+ (should (= 0 (plist-get counts :mature)))
+ (should (= 0 (plist-get counts :lapsed)))))))
+
+;;;; Boundary cases
+
+(ert-deftest test-org-drill-statistics-overview-counts-empty-buffer-all-zero ()
+ "No headings at all yields zero across every bucket."
+ (with-overview-buffer "Just some prose, no headings.\n"
+ (with-fixed-now
+ (let ((counts (org-drill-statistics--overview-counts 'file)))
+ (should (= 0 (plist-get counts :total)))
+ (should (= 0 (plist-get counts :new)))
+ (should (= 0 (plist-get counts :mature)))
+ (should (= 0 (plist-get counts :lapsed)))))))
+
+(ert-deftest test-org-drill-statistics-overview-counts-non-drill-headings-ignored ()
+ "Plain headings without the drill tag never reach :total."
+ (with-overview-buffer
+ (concat
+ "* Plain heading one\nbody\n"
+ "* The only drill :drill:\nbody of a new card\n"
+ "* Plain heading two\nbody\n")
+ (with-fixed-now
+ (let ((counts (org-drill-statistics--overview-counts 'file)))
+ (should (= 1 (plist-get counts :total)))
+ (should (= 1 (plist-get counts :new)))))))
+
+(ert-deftest test-org-drill-statistics-overview-counts-single-new-card ()
+ "A single new card: total and new are one, the rest zero."
+ (with-overview-buffer "* Lonely :drill:\nbody of the only card\n"
+ (with-fixed-now
+ (let ((counts (org-drill-statistics--overview-counts 'file)))
+ (should (= 1 (plist-get counts :total)))
+ (should (= 1 (plist-get counts :new)))
+ (should (= 0 (plist-get counts :mature)))
+ (should (= 0 (plist-get counts :lapsed)))))))
+
+(ert-deftest test-org-drill-statistics-overview-counts-empty-drill-card-not-counted ()
+ "A drill-tagged heading with an empty body and a default card type has
+status nil and is excluded from :total."
+ (with-overview-buffer
+ "* Empty drill :drill:\n:PROPERTIES:\n:ID: x\n:END:\n"
+ (with-fixed-now
+ (let ((counts (org-drill-statistics--overview-counts 'file)))
+ (should (= 0 (plist-get counts :total)))))))
+
+;;;; Error / robustness cases
+
+(ert-deftest test-org-drill-statistics-overview-counts-returns-plist-shape ()
+ "The return value always carries the four documented keys, even when
+the buffer holds no cards."
+ (with-overview-buffer "no headings here\n"
+ (with-fixed-now
+ (let ((counts (org-drill-statistics--overview-counts 'file)))
+ (should (plist-member counts :total))
+ (should (plist-member counts :new))
+ (should (plist-member counts :mature))
+ (should (plist-member counts :lapsed))
+ (should (cl-every #'integerp
+ (list (plist-get counts :total)
+ (plist-get counts :new)
+ (plist-get counts :mature)
+ (plist-get counts :lapsed))))))))
+
+(ert-deftest test-org-drill-statistics-overview-counts-totals-are-additive ()
+ "New + mature + lapsed never exceeds :total, and the dormant remainder
+(:total minus the three actionable buckets) is non-negative."
+ (with-overview-buffer
+ (concat
+ "* New :drill:\nbody one\n"
+ "* Young :drill:\n"
+ "SCHEDULED: <2026-05-04 Mon>\n"
+ ":PROPERTIES:\n"
+ ":DRILL_LAST_QUALITY: 5\n:DRILL_LAST_INTERVAL: 3\n"
+ ":DRILL_TOTAL_REPEATS: 2\n:END:\nbody\n"
+ "* Future :drill:\n"
+ "SCHEDULED: <2026-05-10 Sun>\n"
+ "body\n")
+ (with-fixed-now
+ (let* ((counts (org-drill-statistics--overview-counts 'file))
+ (actionable (+ (plist-get counts :new)
+ (plist-get counts :mature)
+ (plist-get counts :lapsed))))
+ (should (>= (plist-get counts :total) actionable))
+ ;; one new + one young + one future dormant = total 3, actionable 2
+ (should (= 3 (plist-get counts :total)))
+ (should (= 2 actionable))))))
+
+(provide 'test-org-drill-statistics-overview-counts)
+
+;;; test-org-drill-statistics-overview-counts.el ends here
diff --git a/tests/test-org-drill-statistics-pass-rate-by-day.el b/tests/test-org-drill-statistics-pass-rate-by-day.el
new file mode 100644
index 0000000..38f7ae3
--- /dev/null
+++ b/tests/test-org-drill-statistics-pass-rate-by-day.el
@@ -0,0 +1,200 @@
+;;; test-org-drill-statistics-pass-rate-by-day.el --- Tests for pass-rate-by-day statistics -*- lexical-binding: t; -*-
+
+;;; Commentary:
+;; ERT tests for the org-drill statistics dashboard pass-rate-by-day block.
+
+;;; Code:
+
+(require 'ert)
+(require 'org-drill)
+(require 'cl-lib)
+(require 'org)
+
+(defun test-org-drill-statistics--fixed-today (day)
+ "Install a stub for `org-drill-statistics--today-day' returning DAY.
+Returns nothing useful, used for its side effect inside a fixture."
+ (advice-add 'org-drill-statistics--today-day :override
+ (lambda () day) '((name . test-fixed-today))))
+
+(defun test-org-drill-statistics--clear-today ()
+ "Remove the fixed-today stub installed by the helper above."
+ (advice-remove 'org-drill-statistics--today-day 'test-fixed-today))
+
+(defun test-org-drill-statistics--record-on-day (day qualities)
+ "Build a session record started on absolute DAY with QUALITIES vector.
+DAY is an absolute day number as from `time-to-days'. QUALITIES is a
+vector of integer qualities. The start-time is the float-time at noon
+of that day, so day bucketing is unambiguous."
+ (let ((start (+ (float-time
+ (encode-time 0 0 0 1 1 2000))
+ (* (- day (time-to-days
+ (encode-time 0 0 0 1 1 2000)))
+ 86400)
+ (* 12 3600))))
+ (make-org-drill-session-record
+ :start-time start
+ :end-time start
+ :scope 'directory
+ :algorithm 'sm5
+ :qualities qualities
+ :pass-percent 0
+ :new-count 0
+ :mature-count 0
+ :failed-count 0
+ :cram-mode nil)))
+
+;; Normal: a multi-day log produces per-day pass rates in chronological
+;; order, with the failure-quality threshold deciding pass vs fail.
+(ert-deftest test-org-drill-statistics-pass-rate-by-day-basic ()
+ (let ((org-drill-failure-quality 2)
+ (today 700000))
+ (unwind-protect
+ (progn
+ (test-org-drill-statistics--fixed-today today)
+ ;; today: qualities 5 5 1 0 -> 2 pass of 4 -> 50
+ ;; yesterday: qualities 4 3 -> 2 pass of 2 -> 100
+ (let* ((log (list
+ (test-org-drill-statistics--record-on-day
+ today [5 5 1 0])
+ (test-org-drill-statistics--record-on-day
+ (1- today) [4 3])))
+ (v (org-drill-statistics--pass-rate-by-day log 3)))
+ (should (= (length v) 3))
+ (should (null (aref v 0))) ; two days ago, no data
+ (should (= (aref v 1) 100)) ; yesterday
+ (should (= (aref v 2) 50)))) ; today
+ (test-org-drill-statistics--clear-today))))
+
+;; Normal: multiple records on the same day aggregate together.
+(ert-deftest test-org-drill-statistics-pass-rate-by-day-same-day-merge ()
+ (let ((org-drill-failure-quality 2)
+ (today 700000))
+ (unwind-protect
+ (progn
+ (test-org-drill-statistics--fixed-today today)
+ ;; today across two records: [5 1] and [4 0 2]
+ ;; passes: 5,4 -> 2 ; total 5 -> 40
+ (let* ((log (list
+ (test-org-drill-statistics--record-on-day
+ today [5 1])
+ (test-org-drill-statistics--record-on-day
+ today [4 0 2])))
+ (v (org-drill-statistics--pass-rate-by-day log 1)))
+ (should (= (length v) 1))
+ (should (= (aref v 0) 40))))
+ (test-org-drill-statistics--clear-today))))
+
+;; Boundary: empty log yields an all-nil vector of the requested length.
+(ert-deftest test-org-drill-statistics-pass-rate-by-day-empty-log ()
+ (let ((today 700000))
+ (unwind-protect
+ (progn
+ (test-org-drill-statistics--fixed-today today)
+ (let ((v (org-drill-statistics--pass-rate-by-day nil 5)))
+ (should (= (length v) 5))
+ (should (cl-every #'null (append v nil)))))
+ (test-org-drill-statistics--clear-today))))
+
+;; Boundary: a record outside the window is ignored; one at the oldest
+;; edge of the window is counted at index 0.
+(ert-deftest test-org-drill-statistics-pass-rate-by-day-window-edges ()
+ (let ((org-drill-failure-quality 2)
+ (today 700000))
+ (unwind-protect
+ (progn
+ (test-org-drill-statistics--fixed-today today)
+ (let* ((days 3)
+ (oldest (- today (1- days))) ; today-2
+ (log (list
+ ;; just outside the window (too old): ignored
+ (test-org-drill-statistics--record-on-day
+ (1- oldest) [5 5])
+ ;; oldest day in the window: index 0
+ (test-org-drill-statistics--record-on-day
+ oldest [5 0])))
+ (v (org-drill-statistics--pass-rate-by-day log days)))
+ (should (= (length v) days))
+ (should (= (aref v 0) 50)) ; oldest in-window day
+ (should (null (aref v 1)))
+ (should (null (aref v 2)))))
+ (test-org-drill-statistics--clear-today))))
+
+;; Boundary: a record dated in the future relative to today is ignored.
+(ert-deftest test-org-drill-statistics-pass-rate-by-day-future-ignored ()
+ (let ((org-drill-failure-quality 2)
+ (today 700000))
+ (unwind-protect
+ (progn
+ (test-org-drill-statistics--fixed-today today)
+ (let* ((log (list
+ (test-org-drill-statistics--record-on-day
+ (1+ today) [5 5 5])))
+ (v (org-drill-statistics--pass-rate-by-day log 3)))
+ (should (cl-every #'null (append v nil)))))
+ (test-org-drill-statistics--clear-today))))
+
+;; Boundary: threshold edge. A quality equal to the threshold is a fail;
+;; one above it is a pass.
+(ert-deftest test-org-drill-statistics-pass-rate-by-day-threshold-edge ()
+ (let ((org-drill-failure-quality 2)
+ (today 700000))
+ (unwind-protect
+ (progn
+ (test-org-drill-statistics--fixed-today today)
+ ;; qualities 2 (fail) and 3 (pass) -> 1 of 2 -> 50
+ (let* ((log (list
+ (test-org-drill-statistics--record-on-day
+ today [2 3])))
+ (v (org-drill-statistics--pass-rate-by-day log 1)))
+ (should (= (aref v 0) 50))))
+ (test-org-drill-statistics--clear-today))))
+
+;; Boundary: a record whose qualities vector is empty contributes no
+;; total, leaving that day as no-data rather than a division by zero.
+(ert-deftest test-org-drill-statistics-pass-rate-by-day-empty-qualities ()
+ (let ((org-drill-failure-quality 2)
+ (today 700000))
+ (unwind-protect
+ (progn
+ (test-org-drill-statistics--fixed-today today)
+ (let* ((log (list
+ (test-org-drill-statistics--record-on-day
+ today [])))
+ (v (org-drill-statistics--pass-rate-by-day log 1)))
+ (should (null (aref v 0)))))
+ (test-org-drill-statistics--clear-today))))
+
+;; Boundary: DAYS defaults to `org-drill-statistics-trend-days' when
+;; omitted, and a non-positive DAYS is clamped to a length-1 vector.
+(ert-deftest test-org-drill-statistics-pass-rate-by-day-days-arg ()
+ (let ((org-drill-statistics-trend-days 12)
+ (today 700000))
+ (unwind-protect
+ (progn
+ (test-org-drill-statistics--fixed-today today)
+ (should (= (length
+ (org-drill-statistics--pass-rate-by-day nil))
+ 12))
+ (should (= (length
+ (org-drill-statistics--pass-rate-by-day nil 0))
+ 1)))
+ (test-org-drill-statistics--clear-today))))
+
+;; Error: a nil qualities slot is tolerated as no-data, not a crash.
+(ert-deftest test-org-drill-statistics-pass-rate-by-day-nil-qualities ()
+ (let ((org-drill-failure-quality 2)
+ (today 700000))
+ (unwind-protect
+ (progn
+ (test-org-drill-statistics--fixed-today today)
+ (let* ((rec (test-org-drill-statistics--record-on-day
+ today [5 5]))
+ (_ (setf (org-drill-session-record-qualities rec) nil))
+ (v (org-drill-statistics--pass-rate-by-day
+ (list rec) 1)))
+ (should (null (aref v 0)))))
+ (test-org-drill-statistics--clear-today))))
+
+(provide 'test-org-drill-statistics-pass-rate-by-day)
+
+;;; test-org-drill-statistics-pass-rate-by-day.el ends here
diff --git a/tests/test-org-drill-statistics-primitives.el b/tests/test-org-drill-statistics-primitives.el
new file mode 100644
index 0000000..2022e0d
--- /dev/null
+++ b/tests/test-org-drill-statistics-primitives.el
@@ -0,0 +1,139 @@
+;;; test-org-drill-statistics-primitives.el --- Tests for primitives statistics -*- lexical-binding: t; -*-
+
+;;; Commentary:
+;; ERT tests for the org-drill statistics dashboard primitives block.
+
+;;; Code:
+
+(require 'ert)
+(require 'org-drill)
+(require 'cl-lib)
+(require 'org)
+
+;;; Tests for statistics primitives. Require 'org-drill and 'cl-lib.
+;;; Fixtures are deterministic: start-time values are fixed floats, and
+;;; day numbers are derived, never hardcoded against today's date.
+
+
+(defun test-org-drill-statistics--make-record (start-time algorithm)
+ "Build a minimal `org-drill-session-record' for tests.
+START-TIME is a float; ALGORITHM is a symbol. Other slots get inert
+placeholder values sufficient for the primitive under test."
+ (make-org-drill-session-record
+ :start-time start-time
+ :end-time (+ start-time 60.0)
+ :scope 'directory
+ :algorithm algorithm
+ :qualities (vector 5 4 3)
+ :pass-percent 67
+ :new-count 1
+ :mature-count 1
+ :failed-count 1
+ :cram-mode nil))
+
+;;; org-drill-statistics--today-day
+
+(ert-deftest test-org-drill-statistics-today-day-matches-time-to-days ()
+ "`--today-day' equals `time-to-days' of the current time."
+ (should (= (org-drill-statistics--today-day)
+ (time-to-days (current-time)))))
+
+(ert-deftest test-org-drill-statistics-today-day-redefinable ()
+ "`--today-day' can be redefined to a fixed day for deterministic tests."
+ (cl-letf (((symbol-function 'org-drill-statistics--today-day)
+ (lambda () 700000)))
+ (should (= (org-drill-statistics--today-day) 700000))))
+
+;;; org-drill-statistics--record-day
+
+(ert-deftest test-org-drill-statistics-record-day-derives-from-start-time ()
+ "`--record-day' returns the day number of the record's start time."
+ (let* ((now (float-time))
+ (record (test-org-drill-statistics--make-record now 'sm5)))
+ (should (= (org-drill-statistics--record-day record)
+ (time-to-days (seconds-to-time now))))))
+
+(ert-deftest test-org-drill-statistics-record-day-earlier-is-smaller ()
+ "A record started a day earlier has a day number one less."
+ (let* ((now (float-time))
+ (today (test-org-drill-statistics--make-record now 'sm5))
+ (yesterday (test-org-drill-statistics--make-record
+ (- now 86400.0) 'sm5)))
+ (should (= (- (org-drill-statistics--record-day today)
+ (org-drill-statistics--record-day yesterday))
+ 1))))
+
+;;; org-drill-statistics--filter-log
+
+(ert-deftest test-org-drill-statistics-filter-log-nil-returns-all ()
+ "Filtering with nil algorithm returns the log unchanged."
+ (let ((log (list (test-org-drill-statistics--make-record 100.0 'sm5)
+ (test-org-drill-statistics--make-record 200.0 'simple8))))
+ (should (equal (org-drill-statistics--filter-log log nil) log))))
+
+(ert-deftest test-org-drill-statistics-filter-log-keeps-matching ()
+ "Filtering keeps only records whose algorithm matches."
+ (let* ((a (test-org-drill-statistics--make-record 100.0 'sm5))
+ (b (test-org-drill-statistics--make-record 200.0 'simple8))
+ (c (test-org-drill-statistics--make-record 300.0 'sm5))
+ (log (list a b c))
+ (result (org-drill-statistics--filter-log log 'sm5)))
+ (should (equal result (list a c)))))
+
+(ert-deftest test-org-drill-statistics-filter-log-no-match-returns-empty ()
+ "Filtering on an absent algorithm returns an empty list."
+ (let ((log (list (test-org-drill-statistics--make-record 100.0 'sm5))))
+ (should (null (org-drill-statistics--filter-log log 'simple8)))))
+
+(ert-deftest test-org-drill-statistics-filter-log-empty-log ()
+ "Filtering an empty log returns an empty list for any algorithm."
+ (should (null (org-drill-statistics--filter-log nil 'sm5)))
+ (should (null (org-drill-statistics--filter-log nil nil))))
+
+(ert-deftest test-org-drill-statistics-filter-log-does-not-mutate ()
+ "Filtering leaves the input list intact."
+ (let* ((log (list (test-org-drill-statistics--make-record 100.0 'sm5)
+ (test-org-drill-statistics--make-record 200.0 'simple8)))
+ (copy (copy-sequence log)))
+ (org-drill-statistics--filter-log log 'sm5)
+ (should (equal log copy))))
+
+;;; org-drill-statistics--log-since
+
+(ert-deftest test-org-drill-statistics-log-since-keeps-at-or-after-cutoff ()
+ "Records at or after the cutoff are kept; earlier ones dropped."
+ (let* ((before (test-org-drill-statistics--make-record 100.0 'sm5))
+ (at (test-org-drill-statistics--make-record 200.0 'sm5))
+ (after (test-org-drill-statistics--make-record 300.0 'sm5))
+ (log (list before at after))
+ (result (org-drill-statistics--log-since log 200.0)))
+ (should (equal result (list at after)))))
+
+(ert-deftest test-org-drill-statistics-log-since-all-before-cutoff ()
+ "When every record predates the cutoff, the result is empty."
+ (let ((log (list (test-org-drill-statistics--make-record 100.0 'sm5)
+ (test-org-drill-statistics--make-record 150.0 'sm5))))
+ (should (null (org-drill-statistics--log-since log 200.0)))))
+
+(ert-deftest test-org-drill-statistics-log-since-all-after-cutoff ()
+ "When every record is at or after the cutoff, all are kept."
+ (let* ((log (list (test-org-drill-statistics--make-record 300.0 'sm5)
+ (test-org-drill-statistics--make-record 400.0 'sm5)))
+ (result (org-drill-statistics--log-since log 200.0)))
+ (should (equal result log))))
+
+(ert-deftest test-org-drill-statistics-log-since-empty-log ()
+ "Filtering an empty log by cutoff returns an empty list."
+ (should (null (org-drill-statistics--log-since nil 200.0))))
+
+(ert-deftest test-org-drill-statistics-log-since-does-not-mutate ()
+ "Cutoff filtering leaves the input list intact."
+ (let* ((log (list (test-org-drill-statistics--make-record 100.0 'sm5)
+ (test-org-drill-statistics--make-record 300.0 'sm5)))
+ (copy (copy-sequence log)))
+ (org-drill-statistics--log-since log 200.0)
+ (should (equal log copy))))
+
+(provide 'test-org-drill-statistics-primitives)
+
+;;; test-org-drill-statistics-primitives.el ends here
diff --git a/tests/test-org-drill-statistics-quality-histogram.el b/tests/test-org-drill-statistics-quality-histogram.el
new file mode 100644
index 0000000..7a66314
--- /dev/null
+++ b/tests/test-org-drill-statistics-quality-histogram.el
@@ -0,0 +1,100 @@
+;;; test-org-drill-statistics-quality-histogram.el --- Tests for quality-histogram statistics -*- lexical-binding: t; -*-
+
+;;; Commentary:
+;; ERT tests for the org-drill statistics dashboard quality-histogram block.
+
+;;; Code:
+
+(require 'ert)
+(require 'org-drill)
+(require 'cl-lib)
+(require 'org)
+
+(defun test-org-drill-statistics--make-record (qualities)
+ "Build a minimal `org-drill-session-record' carrying QUALITIES.
+QUALITIES is a vector of ints. Other slots are filled with inert
+defaults so the histogram tests stay focused on the qualities slot."
+ (make-org-drill-session-record
+ :start-time 0.0
+ :end-time 0.0
+ :scope nil
+ :algorithm 'sm5
+ :qualities qualities
+ :pass-percent 0
+ :new-count 0
+ :mature-count 0
+ :failed-count 0
+ :cram-mode nil))
+
+;; Normal cases.
+
+(ert-deftest test-org-drill-statistics-quality-histogram-single-record ()
+ "A single record's qualities are tallied into the right buckets."
+ (let* ((record (test-org-drill-statistics--make-record [0 3 3 5 3]))
+ (result (org-drill-statistics--quality-histogram (list record))))
+ (should (equal result [1 0 0 3 0 1]))))
+
+(ert-deftest test-org-drill-statistics-quality-histogram-multiple-records ()
+ "Counts sum across every record in the log."
+ (let* ((r1 (test-org-drill-statistics--make-record [0 1 2]))
+ (r2 (test-org-drill-statistics--make-record [3 4 5]))
+ (r3 (test-org-drill-statistics--make-record [0 5 5]))
+ (result (org-drill-statistics--quality-histogram (list r1 r2 r3))))
+ (should (equal result [2 1 1 1 1 3]))))
+
+(ert-deftest test-org-drill-statistics-quality-histogram-all-same-quality ()
+ "A record with every entry the same quality concentrates in one bucket."
+ (let* ((record (test-org-drill-statistics--make-record [4 4 4 4]))
+ (result (org-drill-statistics--quality-histogram (list record))))
+ (should (equal result [0 0 0 0 4 0]))))
+
+;; Boundary cases.
+
+(ert-deftest test-org-drill-statistics-quality-histogram-empty-log ()
+ "An empty log yields an all-zero histogram, never nil."
+ (let ((result (org-drill-statistics--quality-histogram '())))
+ (should (equal result [0 0 0 0 0 0]))))
+
+(ert-deftest test-org-drill-statistics-quality-histogram-empty-qualities ()
+ "A record with an empty qualities vector contributes nothing."
+ (let* ((record (test-org-drill-statistics--make-record []))
+ (result (org-drill-statistics--quality-histogram (list record))))
+ (should (equal result [0 0 0 0 0 0]))))
+
+(ert-deftest test-org-drill-statistics-quality-histogram-nil-qualities ()
+ "A record whose qualities slot is nil is skipped without error."
+ (let* ((r1 (test-org-drill-statistics--make-record nil))
+ (r2 (test-org-drill-statistics--make-record [2 2]))
+ (result (org-drill-statistics--quality-histogram (list r1 r2))))
+ (should (equal result [0 0 2 0 0 0]))))
+
+(ert-deftest test-org-drill-statistics-quality-histogram-extreme-buckets ()
+ "Quality 0 and quality 5, the range endpoints, both land correctly."
+ (let* ((record (test-org-drill-statistics--make-record [0 0 5 5 5]))
+ (result (org-drill-statistics--quality-histogram (list record))))
+ (should (equal result [2 0 0 0 0 3]))))
+
+;; Error cases.
+
+(ert-deftest test-org-drill-statistics-quality-histogram-out-of-range-ignored ()
+ "Qualities outside 0..5 are dropped, valid ones still counted."
+ (let* ((record (test-org-drill-statistics--make-record [-1 6 3 99 2]))
+ (result (org-drill-statistics--quality-histogram (list record))))
+ (should (equal result [0 0 1 1 0 0]))))
+
+(ert-deftest test-org-drill-statistics-quality-histogram-non-integer-ignored ()
+ "Non-integer quality entries are ignored rather than signalling."
+ (let* ((record (test-org-drill-statistics--make-record [2 nil 2.5 3]))
+ (result (org-drill-statistics--quality-histogram (list record))))
+ (should (equal result [0 0 1 1 0 0]))))
+
+(ert-deftest test-org-drill-statistics-quality-histogram-does-not-mutate-input ()
+ "The qualities vectors are read, never written."
+ (let* ((qualities (vector 1 2 3))
+ (record (test-org-drill-statistics--make-record qualities)))
+ (org-drill-statistics--quality-histogram (list record))
+ (should (equal qualities [1 2 3]))))
+
+(provide 'test-org-drill-statistics-quality-histogram)
+
+;;; test-org-drill-statistics-quality-histogram.el ends here
diff --git a/tests/test-org-drill-statistics-render-attention.el b/tests/test-org-drill-statistics-render-attention.el
new file mode 100644
index 0000000..b3f2375
--- /dev/null
+++ b/tests/test-org-drill-statistics-render-attention.el
@@ -0,0 +1,141 @@
+;;; test-org-drill-statistics-render-attention.el --- Tests for render-attention statistics -*- lexical-binding: t; -*-
+
+;;; Commentary:
+;; ERT tests for the org-drill statistics dashboard render-attention block.
+
+;;; Code:
+
+(require 'ert)
+(require 'org-drill)
+(require 'cl-lib)
+(require 'org)
+
+;;; tests/test-org-drill-statistics-render-attention.el -*- lexical-binding: t; -*-
+
+(defun test-org-drill-stats--attn-fixture ()
+ "Insert a fixture buffer of drill cards and return today's day number.
+Twelve leech candidates so the cap and footer are exercised, plus one
+long-overdue card and one forgotten-new card. Dates derive from the
+current time so the fixture never hardcodes today."
+ (let* ((today (org-drill-statistics--today-day))
+ (old-review (org-drill-time-to-inactive-org-timestamp
+ (org-time-string-to-time
+ (format-time-string
+ "%Y-%m-%d"
+ (time-subtract (current-time)
+ (days-to-time 400))))))
+ (old-added (format-time-string
+ "%Y-%m-%d"
+ (time-subtract (current-time) (days-to-time 30)))))
+ (insert "* Drill cards\n")
+ (dotimes (i 12)
+ (insert (format "** Leech %02d :drill:\n" i))
+ (insert ":PROPERTIES:\n")
+ (insert (format ":DRILL_FAILURE_COUNT: %d\n"
+ (+ org-drill-leech-failure-threshold 2)))
+ (insert (format ":DRILL_AVERAGE_QUALITY: %s\n"
+ (number-to-string (+ 1.0 (* 0.1 i)))))
+ (insert ":END:\n"))
+ (insert "** Overdue card :drill:\n")
+ (insert ":PROPERTIES:\n")
+ (insert (format ":DRILL_LAST_REVIEWED: %s\n" old-review))
+ (insert ":DRILL_FAILURE_COUNT: 0\n")
+ (insert ":END:\n")
+ (insert "** Forgotten card :drill:\n")
+ (insert ":PROPERTIES:\n")
+ (insert (format ":DATE_ADDED: %s\n" old-added))
+ (insert ":DRILL_TOTAL_REPEATS: 0\n")
+ (insert ":END:\n")
+ (org-mode)
+ today))
+
+(ert-deftest test-org-drill-statistics-attention-section-heading ()
+ "The rendered section opens with the Needs attention heading."
+ (with-temp-buffer
+ (test-org-drill-stats--attn-fixture)
+ (let ((out (org-drill-statistics--render-attention 'file)))
+ (should (string-match-p "^\\*\\* Needs attention$" out))
+ (should (string-match-p "^\\*\\*\\* Leech candidates$" out))
+ (should (string-match-p "^\\*\\*\\* Long overdue$" out))
+ (should (string-match-p "^\\*\\*\\* Forgotten new$" out)))))
+
+(ert-deftest test-org-drill-statistics-attention-leech-rows-as-links ()
+ "Leech candidates render as org links carrying card headings."
+ (with-temp-buffer
+ (test-org-drill-stats--attn-fixture)
+ (let ((out (org-drill-statistics--render-attention 'file)))
+ (should (string-match-p "| Card |" out))
+ (should (string-match-p "\\[\\[org-drill-card:[0-9]+\\]\\[Leech 00\\]\\]"
+ out)))))
+
+(ert-deftest test-org-drill-statistics-attention-cap-and-footer ()
+ "Twelve leeches over a 10 cap show 10 rows and a +2 more footer."
+ (with-temp-buffer
+ (test-org-drill-stats--attn-fixture)
+ (let* ((org-drill-statistics-attention-row-limit 10)
+ (out (org-drill-statistics--render-attention 'file))
+ (link-count
+ (cl-count ?\n
+ (mapconcat #'identity
+ (seq-filter
+ (lambda (l) (string-match-p "org-drill-card:" l))
+ (split-string out "\n"))
+ "\n"))))
+ (should (string-match-p "+2 more" out))
+ ;; 10 leech (capped) + 1 overdue + 1 forgotten = 12 link rows.
+ (should (= 12 (1+ link-count))))))
+
+(ert-deftest test-org-drill-statistics-attention-leech-sort-worst-first ()
+ "Leech rows are ordered by ascending average quality, worst first."
+ (with-temp-buffer
+ (test-org-drill-stats--attn-fixture)
+ (let* ((out (org-drill-statistics--render-attention 'file))
+ (leech-00 (string-match "Leech 00" out))
+ (leech-01 (string-match "Leech 01" out)))
+ (should leech-00)
+ (should leech-01)
+ ;; Leech 00 has avg 1.0, Leech 01 has 1.1, so 00 sorts first.
+ (should (< leech-00 leech-01)))))
+
+(ert-deftest test-org-drill-statistics-attention-empty-category-note ()
+ "A category with no matches renders a note rather than a table."
+ (with-temp-buffer
+ (insert "* Cards\n** Healthy :drill:\n:PROPERTIES:\n")
+ (insert ":DRILL_FAILURE_COUNT: 0\n:DRILL_TOTAL_REPEATS: 5\n:END:\n")
+ (org-mode)
+ (let ((out (org-drill-statistics--render-attention 'file)))
+ (should (string-match-p "No leech candidates\\." out))
+ (should (string-match-p "No long-overdue cards\\." out))
+ (should (string-match-p "No forgotten-new cards\\." out)))))
+
+(ert-deftest test-org-drill-statistics-attention-no-footer-under-cap ()
+ "With matches at or under the cap, no +N more footer appears."
+ (with-temp-buffer
+ (insert "* Cards\n")
+ (dotimes (i 3)
+ (insert (format "** Leech %d :drill:\n" i))
+ (insert ":PROPERTIES:\n")
+ (insert (format ":DRILL_FAILURE_COUNT: %d\n"
+ (+ org-drill-leech-failure-threshold 1)))
+ (insert ":DRILL_AVERAGE_QUALITY: 1.0\n:END:\n"))
+ (org-mode)
+ (let* ((org-drill-statistics-attention-row-limit 10)
+ (out (org-drill-statistics--render-attention 'file)))
+ (should-not (string-match-p "more" out)))))
+
+(ert-deftest test-org-drill-statistics-card-link-sanitizes-brackets ()
+ "Closing brackets in a heading cannot terminate the link early."
+ (let ((link (org-drill-statistics--card-link "a]] b" 42)))
+ (should (string-prefix-p "[[org-drill-card:42][" link))
+ (should (string-suffix-p "]]" link))
+ (should-not (string-match-p "a]] b" link))))
+
+(ert-deftest test-org-drill-statistics-card-link-empty-heading-fallback ()
+ "An empty heading falls back to a position-based description."
+ (let ((link (org-drill-statistics--card-link "" 99)))
+ (should (string-match-p "\\[\\[org-drill-card:99\\]\\[card at 99\\]\\]"
+ link))))
+
+(provide 'test-org-drill-statistics-render-attention)
+
+;;; test-org-drill-statistics-render-attention.el ends here
diff --git a/tests/test-org-drill-statistics-render-forecast.el b/tests/test-org-drill-statistics-render-forecast.el
new file mode 100644
index 0000000..8001d68
--- /dev/null
+++ b/tests/test-org-drill-statistics-render-forecast.el
@@ -0,0 +1,131 @@
+;;; test-org-drill-statistics-render-forecast.el --- Tests for render-forecast statistics -*- lexical-binding: t; -*-
+
+;;; Commentary:
+;; ERT tests for the org-drill statistics dashboard render-forecast block.
+
+;;; Code:
+
+(require 'ert)
+(require 'org-drill)
+(require 'cl-lib)
+(require 'org)
+
+(defun test-org-drill-statistics--make-record
+ (start-day-offset &optional qualities)
+ "Build an `org-drill-session-record' START-DAY-OFFSET days from now.
+QUALITIES defaults to a single passing quality. Helper for forecast
+render tests, though the forecast itself reads org entries, not the log;
+kept minimal and self-contained."
+ (let ((start (float-time
+ (time-add (current-time)
+ (days-to-time start-day-offset)))))
+ (make-org-drill-session-record
+ :start-time start
+ :end-time (+ start 60.0)
+ :scope 'file
+ :algorithm 'sm5
+ :qualities (or qualities (vector 5))
+ :pass-percent 100
+ :new-count 0
+ :mature-count 0
+ :failed-count 0
+ :cram-mode nil)))
+
+(ert-deftest test-org-drill-statistics-render-forecast-has-subheading ()
+ "The rendered section starts with the Forecast subheading."
+ (cl-letf (((symbol-function 'org-drill-statistics--forecast)
+ (lambda (&rest _) '(0 0 0 0 0 0 0))))
+ (let ((out (org-drill-statistics--render-forecast)))
+ (should (string-prefix-p "** Forecast\n" out)))))
+
+(ert-deftest test-org-drill-statistics-render-forecast-header-labels ()
+ "The header row labels Today and the +1..+6 offsets in order."
+ (cl-letf (((symbol-function 'org-drill-statistics--forecast)
+ (lambda (&rest _) '(0 0 0 0 0 0 0))))
+ (let ((out (org-drill-statistics--render-forecast)))
+ (should (string-match-p
+ "| Today | \\+1 | \\+2 | \\+3 | \\+4 | \\+5 | \\+6 |"
+ out)))))
+
+(ert-deftest test-org-drill-statistics-render-forecast-counts-row ()
+ "The counts row reflects the forecast helper's per-day values."
+ (cl-letf (((symbol-function 'org-drill-statistics--forecast)
+ (lambda (&rest _) '(3 1 0 5 0 2 4))))
+ (let ((out (org-drill-statistics--render-forecast)))
+ (should (string-match-p "| 3 | 1 | 0 | 5 | 0 | 2 | 4 |" out)))))
+
+(ert-deftest test-org-drill-statistics-render-forecast-trailing-newline ()
+ "The section ends with a newline so sections concatenate cleanly."
+ (cl-letf (((symbol-function 'org-drill-statistics--forecast)
+ (lambda (&rest _) '(0 0 0 0 0 0 0))))
+ (let ((out (org-drill-statistics--render-forecast)))
+ (should (string-suffix-p "\n" out)))))
+
+(ert-deftest test-org-drill-statistics-render-forecast-honors-days ()
+ "A custom DAYS yields a header and row sized to the forecast length."
+ (cl-letf (((symbol-function 'org-drill-statistics--forecast)
+ (lambda (&optional _scope _days) '(2 7 1))))
+ (let ((out (org-drill-statistics--render-forecast nil 3)))
+ (should (string-match-p "| Today | \\+1 | \\+2 |" out))
+ (should (string-match-p "| 2 | 7 | 1 |" out))
+ ;; No fourth column should appear.
+ (should-not (string-match-p "\\+3" out)))))
+
+(ert-deftest test-org-drill-statistics-render-forecast-empty-window ()
+ "A zero-length forecast renders a note instead of a table."
+ (cl-letf (((symbol-function 'org-drill-statistics--forecast)
+ (lambda (&rest _) '())))
+ (let ((out (org-drill-statistics--render-forecast nil 0)))
+ (should (string-prefix-p "** Forecast\n" out))
+ (should (string-match-p "No forecast window configured." out))
+ (should-not (string-match-p "|" out)))))
+
+(ert-deftest test-org-drill-statistics-render-forecast-single-day ()
+ "A one-day forecast renders just the Today column."
+ (cl-letf (((symbol-function 'org-drill-statistics--forecast)
+ (lambda (&rest _) '(9)))
+ (org-drill-statistics-forecast-days 1))
+ (let ((out (org-drill-statistics--render-forecast)))
+ (should (string-match-p "| Today |" out))
+ (should (string-match-p "| 9 |" out))
+ (should-not (string-match-p "\\+1" out)))))
+
+(ert-deftest test-org-drill-statistics-render-forecast-default-days ()
+ "With no DAYS argument the section spans the configured default."
+ (let ((org-drill-statistics-forecast-days 7))
+ (cl-letf (((symbol-function 'org-drill-statistics--forecast)
+ (lambda (&optional _scope days)
+ ;; Echo the resolved day count as a flat list so the
+ ;; renderer's column count is observable.
+ (make-list (or days org-drill-statistics-forecast-days)
+ 0))))
+ (let ((out (org-drill-statistics--render-forecast)))
+ (should (string-match-p "\\+6 |" out))
+ (should-not (string-match-p "\\+7" out))))))
+
+(ert-deftest test-org-drill-statistics-render-forecast-with-scope-buffer ()
+ "End to end through the real forecast helper against a temp org buffer.
+Two cards are scheduled today and one is scheduled three days out; the
+rendered counts row must reflect that bucketing."
+ (let ((today (format-time-string "%Y-%m-%d" (current-time)))
+ (plus3 (format-time-string
+ "%Y-%m-%d" (time-add (current-time) (days-to-time 3)))))
+ (with-temp-buffer
+ ;; Cards carry the drill tag and put SCHEDULED right after the
+ ;; heading so the traversal sees them; scope is 'file (a buffer
+ ;; list would be read as a list of file paths and fail).
+ (insert (format "* Drill cards\n"))
+ (insert (format "** Card one :drill:\nSCHEDULED: <%s>\n:PROPERTIES:\n:DRILL_CARD_TYPE: simple\n:END:\nfront\n" today))
+ (insert (format "** Card two :drill:\nSCHEDULED: <%s>\n:PROPERTIES:\n:DRILL_CARD_TYPE: simple\n:END:\nfront\n" today))
+ (insert (format "** Card three :drill:\nSCHEDULED: <%s>\n:PROPERTIES:\n:DRILL_CARD_TYPE: simple\n:END:\nfront\n" plus3))
+ (org-mode)
+ (let* ((org-drill-scope 'file)
+ (org-drill-question-tag "drill")
+ (org-drill-match nil)
+ (out (org-drill-statistics--render-forecast 'file 7)))
+ ;; Today column = 2, +3 column = 1, others 0.
+ (should (string-match-p "| 2 | 0 | 0 | 1 | 0 | 0 | 0 |" out))))))
+
+(provide 'test-org-drill-statistics-render-forecast)
+
+;;; test-org-drill-statistics-render-forecast.el ends here
diff --git a/tests/test-org-drill-statistics-render-overview.el b/tests/test-org-drill-statistics-render-overview.el
new file mode 100644
index 0000000..05cb166
--- /dev/null
+++ b/tests/test-org-drill-statistics-render-overview.el
@@ -0,0 +1,106 @@
+;;; test-org-drill-statistics-render-overview.el --- Tests for render-overview statistics -*- lexical-binding: t; -*-
+
+;;; Commentary:
+;; ERT tests for the org-drill statistics dashboard render-overview block.
+
+;;; Code:
+
+(require 'ert)
+(require 'org-drill)
+(require 'cl-lib)
+(require 'org)
+
+(defun test-org-drill-statistics--overview-record (start end qualities pass)
+ "Build a session record fixture for overview renderer tests.
+START and END are floats; QUALITIES a vector of ints; PASS an int."
+ (make-org-drill-session-record
+ :start-time start
+ :end-time end
+ :scope 'file
+ :algorithm 'sm5
+ :qualities qualities
+ :pass-percent pass
+ :new-count 0
+ :mature-count 0
+ :failed-count 0
+ :cram-mode nil))
+
+(ert-deftest test-org-drill-statistics-overview-table-row ()
+ "Overview renders the header and a data row from the scope counts."
+ (cl-letf (((symbol-function 'org-drill-statistics--overview-counts)
+ (lambda (&optional _scope)
+ (list :total 42 :new 7 :mature 30 :lapsed 5))))
+ (let ((out (org-drill-statistics--render-overview nil nil)))
+ (should (string-match-p "\\*\\* Overview" out))
+ (should (string-match-p
+ "| Total cards | New | Mature | Lapsed |" out))
+ (should (string-match-p "| 42 | 7 | 30 | 5 |" out)))))
+
+(ert-deftest test-org-drill-statistics-overview-last-session-recap ()
+ "The recap line reports date, duration, card count, and pass percent."
+ (cl-letf (((symbol-function 'org-drill-statistics--overview-counts)
+ (lambda (&optional _scope)
+ (list :total 1 :new 1 :mature 0 :lapsed 0))))
+ ;; 2026-05-15 12:00:00 local, 15 minutes long, 3 cards, 67% pass.
+ (let* ((start (float-time (encode-time 0 0 12 15 5 2026)))
+ (end (+ start (* 15 60)))
+ (record (test-org-drill-statistics--overview-record
+ start end (vector 4 4 1) 67))
+ (out (org-drill-statistics--render-overview nil (list record))))
+ (should (string-match-p "Last session: 2026-05-15" out))
+ (should (string-match-p "15 min" out))
+ (should (string-match-p "3 cards reviewed" out))
+ (should (string-match-p "67% pass" out)))))
+
+(ert-deftest test-org-drill-statistics-overview-singular-card ()
+ "A one-card session uses the singular \"card\" in the recap."
+ (cl-letf (((symbol-function 'org-drill-statistics--overview-counts)
+ (lambda (&optional _scope)
+ (list :total 1 :new 0 :mature 1 :lapsed 0))))
+ (let* ((start (float-time (encode-time 0 0 9 1 1 2026)))
+ (end (+ start 60.0))
+ (record (test-org-drill-statistics--overview-record
+ start end (vector 5) 100))
+ (out (org-drill-statistics--render-overview nil (list record))))
+ (should (string-match-p "1 card reviewed" out))
+ (should-not (string-match-p "1 cards reviewed" out)))))
+
+(ert-deftest test-org-drill-statistics-overview-empty-log ()
+ "With no logged sessions the recap states none recorded."
+ (cl-letf (((symbol-function 'org-drill-statistics--overview-counts)
+ (lambda (&optional _scope)
+ (list :total 0 :new 0 :mature 0 :lapsed 0))))
+ ;; Bind the persistent log to empty so a nil LOG arg resolves to an
+ ;; empty log rather than falling back to whatever sessions the
+ ;; running Emacs has persisted.
+ (let* ((org-drill-session-log nil)
+ (out (org-drill-statistics--render-overview nil nil)))
+ (should (string-match-p "Last session: none recorded yet" out))
+ (should (string-match-p "| 0 | 0 | 0 | 0 |" out)))))
+
+(ert-deftest test-org-drill-statistics-overview-scope-traversal ()
+ "Counts come from the org buffer in scope via the real aggregator.
+Components integrated:
+- org-drill-statistics--render-overview (entry point, real)
+- org-drill-statistics--overview-counts (real, traverses the buffer)
+- org-drill-entry-status / org-drill-session (real)
+Validates the renderer threads SCOPE through to a live org traversal
+rather than relying on a stub."
+ (with-temp-buffer
+ ;; The card must carry the drill question tag, otherwise
+ ;; `org-drill-map-entries' skips it and the population is zero.
+ (insert "* Cards\n"
+ "** Card one :drill:\n"
+ ":PROPERTIES:\n:DRILL_CARD_TYPE: simple\n:END:\n"
+ "Front\n")
+ (org-mode)
+ (let* ((org-drill-scope 'file)
+ (org-drill-question-tag "drill")
+ (org-drill-match nil)
+ (out (org-drill-statistics--render-overview 'file nil)))
+ ;; One genuine drill card, never reviewed, so total and new are 1.
+ (should (string-match-p "| 1 | 1 | 0 | 0 |" out)))))
+
+(provide 'test-org-drill-statistics-render-overview)
+
+;;; test-org-drill-statistics-render-overview.el ends here
diff --git a/tests/test-org-drill-statistics-render-trends.el b/tests/test-org-drill-statistics-render-trends.el
new file mode 100644
index 0000000..bf50b83
--- /dev/null
+++ b/tests/test-org-drill-statistics-render-trends.el
@@ -0,0 +1,129 @@
+;;; test-org-drill-statistics-render-trends.el --- Tests for render-trends statistics -*- lexical-binding: t; -*-
+
+;;; Commentary:
+;; ERT tests for the org-drill statistics dashboard render-trends block.
+
+;;; Code:
+
+(require 'ert)
+(require 'org-drill)
+(require 'cl-lib)
+(require 'org)
+
+;;; Tests for the Trends render helper (render 2/5).
+
+
+(defun org-drill-statistics-test--record (start-day-offset qualities
+ &optional duration-min)
+ "Build an `org-drill-session-record' for trends tests.
+START-DAY-OFFSET is an integer day offset from today (0 is today, -7 is
+a week ago). QUALITIES is a list of int qualities, stored as a vector.
+DURATION-MIN defaults to 10 minutes. The record's start-time and
+end-time floats land on the requested day; only the day component is
+load bearing for these tests."
+ (let* ((duration (or duration-min 10))
+ (today-secs (float-time (current-time)))
+ (start (+ today-secs (* start-day-offset 86400.0)))
+ (end (+ start (* duration 60.0))))
+ (make-org-drill-session-record
+ :start-time start
+ :end-time end
+ :scope 'file
+ :algorithm 'sm5
+ :qualities (vconcat qualities)
+ :pass-percent (org-drill--compute-pass-percent (vconcat qualities))
+ :new-count 0
+ :mature-count (length qualities)
+ :failed-count 0
+ :cram-mode nil)))
+
+(ert-deftest test-org-drill-statistics-render-trends-has-subheading ()
+ "The rendered section opens with the \"* Trends\" org subheading."
+ (let ((out (org-drill-statistics--render-trends nil)))
+ (should (string-prefix-p "* Trends\n" out))))
+
+(ert-deftest test-org-drill-statistics-render-trends-empty-log ()
+ "An empty log still renders both sparkline lines and a table header.
+The sparklines are all-space (no data) and the table has only its header
+and separator rows."
+ (let ((out (org-drill-statistics--render-trends nil)))
+ (should (string-match-p "Reviews/day (last 90):" out))
+ (should (string-match-p "Pass rate/day (last 90):" out))
+ (should (string-match-p "| Week | Reviews | Pass % | Avg min |" out))
+ (should (string-match-p "|------" out))))
+
+(ert-deftest test-org-drill-statistics-render-trends-sparkline-glyph ()
+ "A record today puts a non-space block glyph in the reviews sparkline.
+The final sparkline column (today) must be a quadrant block, not the
+space rendered for empty days."
+ (let* ((log (list (org-drill-statistics-test--record 0 '(5 4 3))))
+ (out (org-drill-statistics--render-trends log))
+ (line (car (seq-filter
+ (lambda (l) (string-prefix-p "Reviews/day" l))
+ (split-string out "\n")))))
+ ;; The sparkline glyph for the busiest day is the full block, since
+ ;; today is the only day with data so it scales to the ceiling.
+ (should (string-match-p "█" line))))
+
+(ert-deftest test-org-drill-statistics-render-trends-weekly-row ()
+ "A this-week session produces a body row with its counts.
+The row carries the Monday date of this week, the review count, the
+pass percentage, and a one-decimal average duration."
+ (let* ((today (time-to-days (current-time)))
+ (week-start (org-drill-statistics--week-start-day today))
+ (expected-date (org-drill-statistics--format-week-start week-start))
+ ;; Three qualities, two passes (> failure-quality default 2).
+ (log (list (org-drill-statistics-test--record 0 '(5 4 1) 20)))
+ (out (org-drill-statistics--render-trends log)))
+ ;; Pass percent: 2 of 3 -> 67. Avg duration: 20.0 minutes.
+ (should (string-match-p
+ (regexp-quote (format "| %s | 3 | 67 | 20.0 |" expected-date))
+ out))))
+
+(ert-deftest test-org-drill-statistics-render-trends-twelve-week-rows ()
+ "The table body has exactly 12 week rows, one per week in the window."
+ (let* ((out (org-drill-statistics--render-trends nil))
+ (lines (split-string out "\n" t))
+ (body (seq-filter
+ (lambda (l)
+ (and (string-prefix-p "| " l)
+ (not (string-match-p "Week" l))))
+ lines)))
+ (should (= (length body) 12))))
+
+(ert-deftest test-org-drill-statistics-render-trends-algorithm-filter ()
+ "Passing an algorithm filters records out of the aggregates.
+A record under `sm5' is excluded when the section filters for `sm2',
+leaving an empty (all-zero) this-week row."
+ (let* ((today (time-to-days (current-time)))
+ (week-start (org-drill-statistics--week-start-day today))
+ (date (org-drill-statistics--format-week-start week-start))
+ (log (list (org-drill-statistics-test--record 0 '(5 4 3) 15)))
+ (out (org-drill-statistics--render-trends log 'sm2)))
+ ;; sm5 record is filtered out, so this week's row is zeroed.
+ (should (string-match-p
+ (regexp-quote (format "| %s | 0 | 0 | 0.0 |" date))
+ out))
+ ;; And the unfiltered render keeps it.
+ (let ((unfiltered (org-drill-statistics--render-trends log)))
+ (should (string-match-p
+ (regexp-quote (format "| %s | 3 |" date))
+ unfiltered)))))
+
+(ert-deftest test-org-drill-statistics-render-trends-pass-rate-absolute-scale ()
+ "The pass-rate sparkline scales against 100, not the window peak.
+A day with a 50 percent pass rate must render a mid-height glyph, not
+the full block it would reach if scaled to its own maximum."
+ (let* ((log (list (org-drill-statistics-test--record 0 '(5 1))))
+ (out (org-drill-statistics--render-trends log))
+ (line (car (seq-filter
+ (lambda (l) (string-prefix-p "Pass rate/day" l))
+ (split-string out "\n")))))
+ ;; 1 pass of 2 -> 50 percent. Scaled to 100 over an 8-glyph charset,
+ ;; round(50/100 * 7) = 4 -> the 5th glyph, not the full block.
+ (should-not (string-match-p "█" line))
+ (should (string-match-p "▅" line))))
+
+(provide 'test-org-drill-statistics-render-trends)
+
+;;; test-org-drill-statistics-render-trends.el ends here
diff --git a/tests/test-org-drill-statistics-reviews-by-day.el b/tests/test-org-drill-statistics-reviews-by-day.el
new file mode 100644
index 0000000..94b5aa2
--- /dev/null
+++ b/tests/test-org-drill-statistics-reviews-by-day.el
@@ -0,0 +1,111 @@
+;;; test-org-drill-statistics-reviews-by-day.el --- Tests for reviews-by-day statistics -*- lexical-binding: t; -*-
+
+;;; Commentary:
+;; ERT tests for the org-drill statistics dashboard reviews-by-day block.
+
+;;; Code:
+
+(require 'ert)
+(require 'org-drill)
+(require 'cl-lib)
+(require 'org)
+
+;; Shared fixture: build a session record whose start day is a fixed
+;; offset from today, so `org-drill-statistics--record-day' maps it
+;; back to the intended absolute day without depending on a literal
+;; date. The start-time is noon local time on that calendar day, which
+;; keeps `time-to-days' off any DST or midnight boundary.
+
+(defun org-drill-statistics-test--record-at (day-offset n-qualities)
+ "Build a session record starting DAY-OFFSET days before today.
+The start-time is noon local time on that calendar day, so
+`org-drill-statistics--record-day' maps it back to the intended day.
+N-QUALITIES sets the length of the qualities vector, the review count
+the record contributes; 0 yields an empty vector."
+ (let* ((target-day (- (org-drill-statistics--today-day) day-offset))
+ (greg (calendar-gregorian-from-absolute target-day))
+ (ts (encode-time 0 0 12 (nth 1 greg) (nth 0 greg) (nth 2 greg))))
+ (make-org-drill-session-record
+ :start-time (float-time ts)
+ :qualities (make-vector n-qualities 3))))
+
+;; Normal cases
+
+(ert-deftest test-org-drill-statistics-reviews-by-day-normal-counts-per-day ()
+ "Reviews land in the correct oldest-to-newest slots by start day."
+ (let ((v (org-drill-statistics--reviews-by-day
+ (list (org-drill-statistics-test--record-at 0 2)
+ (org-drill-statistics-test--record-at 1 3)
+ (org-drill-statistics-test--record-at 6 1))
+ 7)))
+ (should (= (length v) 7))
+ (should (= (aref v 6) 2))
+ (should (= (aref v 5) 3))
+ (should (= (aref v 0) 1))
+ (should (= (apply #'+ (append v nil)) 6))))
+
+(ert-deftest test-org-drill-statistics-reviews-by-day-normal-same-day-accumulates ()
+ "Multiple records on the same day sum their quality counts."
+ (let ((v (org-drill-statistics--reviews-by-day
+ (list (org-drill-statistics-test--record-at 0 2)
+ (org-drill-statistics-test--record-at 0 3))
+ 3)))
+ (should (= (aref v 2) 5))))
+
+(ert-deftest test-org-drill-statistics-reviews-by-day-normal-default-days ()
+ "Omitting DAYS uses `org-drill-statistics-trend-days' for the length."
+ (let ((org-drill-statistics-trend-days 30))
+ (should (= (length (org-drill-statistics--reviews-by-day nil)) 30))))
+
+;; Boundary cases
+
+(ert-deftest test-org-drill-statistics-reviews-by-day-boundary-empty-log ()
+ "An empty log yields an all-zero vector of the requested length."
+ (let ((v (org-drill-statistics--reviews-by-day nil 5)))
+ (should (= (length v) 5))
+ (should (= (apply #'+ (append v nil)) 0))))
+
+(ert-deftest test-org-drill-statistics-reviews-by-day-boundary-single-day ()
+ "DAYS of 1 yields a one-slot vector holding today's count."
+ (let ((v (org-drill-statistics--reviews-by-day
+ (list (org-drill-statistics-test--record-at 0 4)) 1)))
+ (should (= (length v) 1))
+ (should (= (aref v 0) 4))))
+
+(ert-deftest test-org-drill-statistics-reviews-by-day-boundary-edges-of-window ()
+ "The oldest in-window day fills slot 0; one day older is dropped."
+ (let ((v (org-drill-statistics--reviews-by-day
+ (list (org-drill-statistics-test--record-at 6 2)
+ (org-drill-statistics-test--record-at 7 9))
+ 7)))
+ (should (= (aref v 0) 2))
+ (should (= (apply #'+ (append v nil)) 2))))
+
+(ert-deftest test-org-drill-statistics-reviews-by-day-boundary-empty-qualities ()
+ "A record with an empty qualities vector contributes zero."
+ (let ((v (org-drill-statistics--reviews-by-day
+ (list (org-drill-statistics-test--record-at 0 0)) 3)))
+ (should (= (apply #'+ (append v nil)) 0))))
+
+;; Error cases
+
+(ert-deftest test-org-drill-statistics-reviews-by-day-error-future-record-ignored ()
+ "A record dated in the future falls outside the window and is ignored."
+ (let ((v (org-drill-statistics--reviews-by-day
+ (list (org-drill-statistics-test--record-at -3 5)) 7)))
+ (should (= (apply #'+ (append v nil)) 0))))
+
+(ert-deftest test-org-drill-statistics-reviews-by-day-error-nonpositive-days-clamped ()
+ "DAYS of 0 or negative is clamped to a single today slot."
+ (let ((v0 (org-drill-statistics--reviews-by-day
+ (list (org-drill-statistics-test--record-at 0 2)) 0))
+ (vn (org-drill-statistics--reviews-by-day
+ (list (org-drill-statistics-test--record-at 0 3)) -5)))
+ (should (= (length v0) 1))
+ (should (= (aref v0 0) 2))
+ (should (= (length vn) 1))
+ (should (= (aref vn 0) 3))))
+
+(provide 'test-org-drill-statistics-reviews-by-day)
+
+;;; test-org-drill-statistics-reviews-by-day.el ends here
diff --git a/tests/test-org-drill-statistics-shell.el b/tests/test-org-drill-statistics-shell.el
new file mode 100644
index 0000000..a4492d5
--- /dev/null
+++ b/tests/test-org-drill-statistics-shell.el
@@ -0,0 +1,153 @@
+;;; test-org-drill-statistics-shell.el --- Tests for shell statistics -*- lexical-binding: t; -*-
+
+;;; Commentary:
+;; ERT tests for the org-drill statistics dashboard shell block.
+
+;;; Code:
+
+(require 'ert)
+(require 'org-drill)
+(require 'cl-lib)
+(require 'org)
+
+;;; Tests for the statistics dashboard shell (step 2).
+
+
+(defun org-drill-statistics-test--record (start-offset-days algorithm qualities)
+ "Build a session record START-OFFSET-DAYS before now.
+ALGORITHM is the algorithm symbol. QUALITIES is a vector of int. The
+session lasts ten minutes. Offsets are relative to `current-time' so the
+fixture never hardcodes a date."
+ (let* ((start (- (float-time) (* start-offset-days 86400.0)))
+ (end (+ start 600.0)))
+ (make-org-drill-session-record
+ :start-time start
+ :end-time end
+ :scope 'file
+ :algorithm algorithm
+ :qualities qualities
+ :pass-percent 50
+ :new-count 1
+ :mature-count 2
+ :failed-count 0
+ :cram-mode nil)))
+
+(ert-deftest test-org-drill-statistics-shell-range-cutoff-known-label ()
+ "A range preset with a day count yields a cutoff that many days back."
+ (let ((now (float-time)))
+ (let ((cutoff (org-drill-statistics--range-cutoff-float "last 7d")))
+ (should cutoff)
+ ;; Cutoff is roughly seven days before now, within a generous slop.
+ (should (< (abs (- cutoff (- now (* 7 86400.0)))) 5.0)))))
+
+(ert-deftest test-org-drill-statistics-shell-range-cutoff-all-time-nil ()
+ "The all-time preset (nil days) yields no cutoff."
+ (should (null (org-drill-statistics--range-cutoff-float "all time")))
+ (should (null (org-drill-statistics--range-cutoff-float "no such label"))))
+
+(ert-deftest test-org-drill-statistics-shell-filtered-log-by-algorithm ()
+ "Filtering the log by algorithm keeps only matching records."
+ (let ((org-drill-session-log
+ (list (org-drill-statistics-test--record 1 'simple8 [4 5])
+ (org-drill-statistics-test--record 2 'sm5 [3 2]))))
+ (let ((only-sm5 (org-drill-statistics--filtered-log "all time" 'sm5)))
+ (should (= 1 (length only-sm5)))
+ (should (eq 'sm5 (org-drill-session-record-algorithm
+ (car only-sm5)))))
+ (should (= 2 (length (org-drill-statistics--filtered-log
+ "all time" nil))))))
+
+(ert-deftest test-org-drill-statistics-shell-filtered-log-by-range ()
+ "An old record falls outside a short range window."
+ (let ((org-drill-session-log
+ (list (org-drill-statistics-test--record 1 'simple8 [4])
+ (org-drill-statistics-test--record 40 'simple8 [3]))))
+ (should (= 1 (length (org-drill-statistics--filtered-log
+ "last 7d" nil))))
+ (should (= 2 (length (org-drill-statistics--filtered-log
+ "last 90d" nil))))))
+
+(ert-deftest test-org-drill-statistics-shell-header-line-format ()
+ "The header line names all three active filters."
+ (let ((line (org-drill-statistics--header-line 'file "last 90d" 'simple8)))
+ (should (string-match-p "Scope: file" line))
+ (should (string-match-p "Range: last 90d" line))
+ (should (string-match-p "Algorithm: simple8" line)))
+ ;; nil scope falls back to org-drill-scope, nil algorithm reads "all".
+ (let* ((org-drill-scope 'directory)
+ (line (org-drill-statistics--header-line nil "last 7d" nil)))
+ (should (string-match-p "Scope: directory" line))
+ (should (string-match-p "Algorithm: all" line))))
+
+(ert-deftest test-org-drill-statistics-shell-cycle-range-wraps ()
+ "Cycling range advances through presets and wraps to the first."
+ (with-temp-buffer
+ (let ((org-drill-statistics-range-presets
+ '(("last 90d" . 90) ("last 30d" . 30) ("all time" . nil)))
+ ;; Stub the in-place re-render so the cycle command stays pure
+ ;; with respect to buffer contents and the renderers.
+ (org-drill-session-log nil))
+ (cl-letf (((symbol-function 'org-drill-statistics-refresh)
+ (lambda () nil)))
+ (setq org-drill-statistics--range "last 90d")
+ (org-drill-statistics-cycle-range)
+ (should (equal "last 30d" org-drill-statistics--range))
+ (org-drill-statistics-cycle-range)
+ (should (equal "all time" org-drill-statistics--range))
+ (org-drill-statistics-cycle-range)
+ (should (equal "last 90d" org-drill-statistics--range))))))
+
+(ert-deftest test-org-drill-statistics-shell-cycle-algorithm-from-log ()
+ "Cycling algorithm walks nil then each algorithm seen in the log."
+ (with-temp-buffer
+ (let ((org-drill-session-log
+ (list (org-drill-statistics-test--record 1 'simple8 [4])
+ (org-drill-statistics-test--record 2 'sm5 [3]))))
+ (cl-letf (((symbol-function 'org-drill-statistics-refresh)
+ (lambda () nil)))
+ (setq org-drill-statistics--algorithm nil)
+ (org-drill-statistics-cycle-algorithm)
+ (should (memq org-drill-statistics--algorithm '(simple8 sm5)))
+ (org-drill-statistics-cycle-algorithm)
+ (should (memq org-drill-statistics--algorithm '(simple8 sm5)))
+ ;; Third cycle wraps back to all-algorithms (nil).
+ (org-drill-statistics-cycle-algorithm)
+ (should (null org-drill-statistics--algorithm))))))
+
+(ert-deftest test-org-drill-statistics-shell-integration-assembles-sections ()
+ "The assembled dashboard body contains every section's output.
+Components integrated:
+- org-drill-statistics--render-all (entry point, real)
+- the five org-drill-statistics--render-* helpers (real)
+- org-drill-session-log fixture (real, let-bound)
+The card-scanning helpers are exercised against an empty current buffer,
+so the card population is zero, but every section header must still
+appear in the assembled string."
+ (with-temp-buffer
+ (org-mode)
+ (let ((org-drill-session-log
+ (list (org-drill-statistics-test--record 1 'simple8 [4 5 2])
+ (org-drill-statistics-test--record 3 'simple8 [3 4])
+ (org-drill-statistics-test--record 8 'sm5 [5 5 1]))))
+ ;; Use 'file scope: the buffer has no headline, and 'tree errors
+ ;; when point is before the first headline. 'file scans the
+ ;; whole (empty) buffer and yields a zero card population.
+ (let ((body (org-drill-statistics--render-all
+ 'file (caar org-drill-statistics-range-presets) nil)))
+ (should (stringp body))
+ ;; The header line is always present.
+ (should (string-match-p "Scope:" body))
+ (should (string-match-p "Range:" body))
+ (should (string-match-p "Algorithm:" body))
+ ;; Each render section contributes recognizable text. The exact
+ ;; header wording lives in the render helpers; assert on the
+ ;; section keywords the spec fixes rather than full prose.
+ (should (string-match-p "[Oo]verview" body))
+ (should (string-match-p "[Tt]rend" body))
+ (should (string-match-p "[Dd]istribution" body))
+ (should (string-match-p "[Aa]ttention" body))
+ (should (string-match-p "[Ff]orecast" body))))))
+
+(provide 'test-org-drill-statistics-shell)
+
+;;; test-org-drill-statistics-shell.el ends here
diff --git a/tests/test-org-drill-statistics-sparkline.el b/tests/test-org-drill-statistics-sparkline.el
new file mode 100644
index 0000000..ae1f5c0
--- /dev/null
+++ b/tests/test-org-drill-statistics-sparkline.el
@@ -0,0 +1,84 @@
+;;; test-org-drill-statistics-sparkline.el --- Tests for sparkline statistics -*- lexical-binding: t; -*-
+
+;;; Commentary:
+;; ERT tests for the org-drill statistics dashboard sparkline block.
+
+;;; Code:
+
+(require 'ert)
+(require 'org-drill)
+(require 'cl-lib)
+(require 'org)
+
+(ert-deftest test-org-drill-statistics-sparkline-empty ()
+ "An empty sequence renders as the empty string."
+ (should (equal (org-drill-statistics--sparkline '()) ""))
+ (should (equal (org-drill-statistics--sparkline []) "")))
+
+(ert-deftest test-org-drill-statistics-sparkline-single-value ()
+ "A single positive value renders as the full block.
+With a one-element sequence and no explicit MAX, the value equals the
+derived max and maps to the tallest glyph."
+ (should (equal (org-drill-statistics--sparkline '(5)) "█")))
+
+(ert-deftest test-org-drill-statistics-sparkline-single-zero ()
+ "A single zero value renders as the lowest block, not an error.
+The derived max is zero, so the all-zero branch applies."
+ (should (equal (org-drill-statistics--sparkline '(0)) "▁")))
+
+(ert-deftest test-org-drill-statistics-sparkline-all-equal ()
+ "All-equal positive values all render as the full block.
+Each value equals the derived max, so every glyph is the tallest."
+ (should (equal (org-drill-statistics--sparkline '(3 3 3 3)) "████")))
+
+(ert-deftest test-org-drill-statistics-sparkline-all-zero ()
+ "All-zero values render as the lowest block, one per entry.
+Max is zero, so the division-by-zero guard returns the lowest glyph for
+every value instead of erroring."
+ (should (equal (org-drill-statistics--sparkline '(0 0 0)) "▁▁▁")))
+
+(ert-deftest test-org-drill-statistics-sparkline-known-ramp ()
+ "A 0..7 ramp against MAX 7 maps to each glyph in order.
+With value I and MAX 7, the index is (round (* (/ I 7.0) 7)) = I, so the
+ramp walks the charset from lowest to highest exactly once."
+ (should (equal (org-drill-statistics--sparkline '(0 1 2 3 4 5 6 7) 7)
+ "▁▂▃▄▅▆▇█")))
+
+(ert-deftest test-org-drill-statistics-sparkline-nil-entries ()
+ "Nil entries render as spaces and do not affect the derived max.
+The values 1 and 2 against derived max 2 scale to indices 4 (1/2*7
+rounds to 4) and 7."
+ (should (equal (org-drill-statistics--sparkline '(nil 1 nil 2 nil))
+ " ▅ █ ")))
+
+(ert-deftest test-org-drill-statistics-sparkline-all-nil ()
+ "An all-nil sequence renders as one space per entry."
+ (should (equal (org-drill-statistics--sparkline '(nil nil nil)) " ")))
+
+(ert-deftest test-org-drill-statistics-sparkline-explicit-max ()
+ "An explicit MAX scales values against it, not the sequence max.
+With MAX 10, value 10 is the full block, 0 is the lowest, and 5 scales
+to 5/10*7 = 3.5 which rounds to index 4."
+ (should (equal (org-drill-statistics--sparkline '(0 5 10) 10) "▁▅█")))
+
+(ert-deftest test-org-drill-statistics-sparkline-explicit-zero-max ()
+ "An explicit MAX of zero renders every value as the lowest block.
+The guard treats a zero ceiling like the all-zero case rather than
+dividing by zero."
+ (should (equal (org-drill-statistics--sparkline '(0 1 2) 0) "▁▁▁")))
+
+(ert-deftest test-org-drill-statistics-sparkline-value-above-max ()
+ "A value exceeding MAX clamps to the full block instead of overflowing.
+Without the clamp the computed index would exceed the charset length.
+The 5 scales to 5/10*7 = 3.5 which rounds to index 4."
+ (should (equal (org-drill-statistics--sparkline '(20) 10) "█"))
+ (should (equal (org-drill-statistics--sparkline '(5 20) 10) "▅█")))
+
+(ert-deftest test-org-drill-statistics-sparkline-vector-input ()
+ "A vector argument is accepted and rendered like a list.
+The helper coerces any sequence, so callers may pass either."
+ (should (equal (org-drill-statistics--sparkline [0 7] 7) "▁█")))
+
+(provide 'test-org-drill-statistics-sparkline)
+
+;;; test-org-drill-statistics-sparkline.el ends here
diff --git a/tests/test-org-drill-statistics-weekly-aggregates.el b/tests/test-org-drill-statistics-weekly-aggregates.el
new file mode 100644
index 0000000..446ba54
--- /dev/null
+++ b/tests/test-org-drill-statistics-weekly-aggregates.el
@@ -0,0 +1,184 @@
+;;; test-org-drill-statistics-weekly-aggregates.el --- Tests for weekly-aggregates statistics -*- lexical-binding: t; -*-
+
+;;; Commentary:
+;; ERT tests for the org-drill statistics dashboard weekly-aggregates block.
+
+;;; Code:
+
+(require 'ert)
+(require 'org-drill)
+(require 'cl-lib)
+(require 'org)
+
+;;; Tests for org-drill-statistics--weekly-aggregates.
+;;
+;; All tests redefine `org-drill-statistics--today-day' to a fixed
+;; absolute day so the window is deterministic and never anchored to the
+;; real clock. The chosen day, 739767, is Sunday 2026-05-31; its
+;; Monday-based week start is 739761. Fixtures build records at noon of
+;; a chosen absolute day, which round-trips cleanly through the
+;; `time-to-days' path that `org-drill-statistics--record-day' uses.
+
+(defun test-org-drill-statistics-weekly--abs-to-float (abs hour)
+ "Return a float-time for HOUR (local) on absolute day ABS."
+ (let ((g (calendar-gregorian-from-absolute abs)))
+ (float-time
+ (encode-time (list 0 0 hour
+ (calendar-extract-day g)
+ (calendar-extract-month g)
+ (calendar-extract-year g)
+ nil -1 nil)))))
+
+(defun test-org-drill-statistics-weekly--rec (abs qualities &optional dur-min)
+ "Build a record starting at noon on ABS, lasting DUR-MIN minutes.
+QUALITIES is a sequence of integers; DUR-MIN defaults to 10."
+ (let ((start (test-org-drill-statistics-weekly--abs-to-float abs 12)))
+ (make-org-drill-session-record
+ :start-time start
+ :end-time (+ start (* 60 (or dur-min 10)))
+ :qualities (vconcat qualities)
+ :algorithm 'sm5)))
+
+(defmacro test-org-drill-statistics-weekly--with-today (abs &rest body)
+ "Run BODY with `org-drill-statistics--today-day' fixed to ABS."
+ (declare (indent 1))
+ `(cl-letf (((symbol-function 'org-drill-statistics--today-day)
+ (lambda () ,abs)))
+ ,@body))
+
+(defconst test-org-drill-statistics-weekly--today 739767
+ "Fixed today for tests: Sunday 2026-05-31, absolute day number.")
+(defconst test-org-drill-statistics-weekly--this-mon 739761
+ "Monday starting the week of `test-org-drill-statistics-weekly--today'.")
+
+;;; ---- Normal cases ----
+
+(ert-deftest test-org-drill-statistics-weekly-default-span ()
+ "Default WEEKS is 12, oldest-first, with each week 7 days apart."
+ (test-org-drill-statistics-weekly--with-today
+ test-org-drill-statistics-weekly--today
+ (let ((agg (org-drill-statistics--weekly-aggregates nil)))
+ (should (= 12 (length agg)))
+ (should (= (- test-org-drill-statistics-weekly--this-mon (* 7 11))
+ (plist-get (car agg) :week-start)))
+ (should (= test-org-drill-statistics-weekly--this-mon
+ (plist-get (car (last agg)) :week-start)))
+ (cl-loop for (a b) on agg while b
+ do (should (= 7 (- (plist-get b :week-start)
+ (plist-get a :week-start))))))))
+
+(ert-deftest test-org-drill-statistics-weekly-pooled-pass-percent ()
+ "Reviews sum pooled qualities; pass-percent is pooled, not averaged."
+ (test-org-drill-statistics-weekly--with-today
+ test-org-drill-statistics-weekly--today
+ ;; Two sessions this week: pooled (5 4 1 2) -> 2 pass of 4 -> 50.
+ (let* ((log (list (test-org-drill-statistics-weekly--rec
+ test-org-drill-statistics-weekly--today '(5 4) 10)
+ (test-org-drill-statistics-weekly--rec
+ (1+ test-org-drill-statistics-weekly--this-mon)
+ '(1 2) 30)))
+ (agg (org-drill-statistics--weekly-aggregates log))
+ (this (car (last agg))))
+ (should (= test-org-drill-statistics-weekly--this-mon
+ (plist-get this :week-start)))
+ (should (= 4 (plist-get this :reviews)))
+ (should (= 50 (plist-get this :pass-percent)))
+ (should (= 20.0 (plist-get this :avg-duration-min))))))
+
+(ert-deftest test-org-drill-statistics-weekly-records-spread-weeks ()
+ "Records land in their own Monday-based week buckets."
+ (test-org-drill-statistics-weekly--with-today
+ test-org-drill-statistics-weekly--today
+ (let* ((mon test-org-drill-statistics-weekly--this-mon)
+ (log (list (test-org-drill-statistics-weekly--rec
+ test-org-drill-statistics-weekly--today '(5))
+ (test-org-drill-statistics-weekly--rec
+ (- mon 7) '(0 0))
+ (test-org-drill-statistics-weekly--rec
+ (- mon 14) '(4))))
+ (agg (org-drill-statistics--weekly-aggregates log 12))
+ (by-start (mapcar (lambda (p) (cons (plist-get p :week-start)
+ (plist-get p :reviews)))
+ agg)))
+ (should (= 1 (cdr (assoc mon by-start))))
+ (should (= 2 (cdr (assoc (- mon 7) by-start))))
+ (should (= 1 (cdr (assoc (- mon 14) by-start)))))))
+
+;;; ---- Boundary cases ----
+
+(ert-deftest test-org-drill-statistics-weekly-empty-log ()
+ "An empty log yields WEEKS all-zero plists."
+ (test-org-drill-statistics-weekly--with-today
+ test-org-drill-statistics-weekly--today
+ (let ((agg (org-drill-statistics--weekly-aggregates nil 3)))
+ (should (= 3 (length agg)))
+ (dolist (p agg)
+ (should (= 0 (plist-get p :reviews)))
+ (should (= 0 (plist-get p :pass-percent)))
+ (should (= 0.0 (plist-get p :avg-duration-min)))))))
+
+(ert-deftest test-org-drill-statistics-weekly-single-week ()
+ "WEEKS = 1 keeps only the current week's records."
+ (test-org-drill-statistics-weekly--with-today
+ test-org-drill-statistics-weekly--today
+ (let* ((log (list (test-org-drill-statistics-weekly--rec
+ test-org-drill-statistics-weekly--today '(5 5))
+ (test-org-drill-statistics-weekly--rec
+ (- test-org-drill-statistics-weekly--this-mon 7) '(0))))
+ (agg (org-drill-statistics--weekly-aggregates log 1)))
+ (should (= 1 (length agg)))
+ (should (= test-org-drill-statistics-weekly--this-mon
+ (plist-get (car agg) :week-start)))
+ (should (= 2 (plist-get (car agg) :reviews)))
+ (should (= 100 (plist-get (car agg) :pass-percent))))))
+
+(ert-deftest test-org-drill-statistics-weekly-out-of-window-dropped ()
+ "Records older than the window are not bucketed."
+ (test-org-drill-statistics-weekly--with-today
+ test-org-drill-statistics-weekly--today
+ (let* ((log (list (test-org-drill-statistics-weekly--rec
+ (- test-org-drill-statistics-weekly--this-mon (* 7 5))
+ '(5))))
+ (agg (org-drill-statistics--weekly-aggregates log 3)))
+ (should (cl-every (lambda (p) (= 0 (plist-get p :reviews))) agg)))))
+
+(ert-deftest test-org-drill-statistics-weekly-week-boundary-monday ()
+ "A Monday session counts in its week; the Sunday before is the prior week."
+ (test-org-drill-statistics-weekly--with-today
+ test-org-drill-statistics-weekly--today
+ (let* ((mon test-org-drill-statistics-weekly--this-mon)
+ (log (list (test-org-drill-statistics-weekly--rec mon '(5))
+ (test-org-drill-statistics-weekly--rec (1- mon) '(4))))
+ (agg (org-drill-statistics--weekly-aggregates log 2))
+ (prior (car agg))
+ (this (cadr agg)))
+ (should (= (- mon 7) (plist-get prior :week-start)))
+ (should (= 1 (plist-get prior :reviews)))
+ (should (= mon (plist-get this :week-start)))
+ (should (= 1 (plist-get this :reviews))))))
+
+;;; ---- Error cases ----
+
+(ert-deftest test-org-drill-statistics-weekly-non-positive-weeks-errors ()
+ "WEEKS below 1 signals an error."
+ (test-org-drill-statistics-weekly--with-today
+ test-org-drill-statistics-weekly--today
+ (should-error (org-drill-statistics--weekly-aggregates nil 0))
+ (should-error (org-drill-statistics--weekly-aggregates nil -3))))
+
+(ert-deftest test-org-drill-statistics-weekly-empty-qualities-record ()
+ "A record with no qualities adds 0 reviews but still counts toward the
+week's average duration."
+ (test-org-drill-statistics-weekly--with-today
+ test-org-drill-statistics-weekly--today
+ (let* ((log (list (test-org-drill-statistics-weekly--rec
+ test-org-drill-statistics-weekly--today [] 10)))
+ (agg (org-drill-statistics--weekly-aggregates log 1))
+ (this (car agg)))
+ (should (= 0 (plist-get this :reviews)))
+ (should (= 0 (plist-get this :pass-percent)))
+ (should (= 10.0 (plist-get this :avg-duration-min))))))
+
+(provide 'test-org-drill-statistics-weekly-aggregates)
+
+;;; test-org-drill-statistics-weekly-aggregates.el ends here