From 1c5a2ebab7c721d795ed9331afdb305fd683e172 Mon Sep 17 00:00:00 2001 From: Craig Jennings Date: Sat, 16 May 2026 02:35:38 -0500 Subject: refactor(foundation): hygiene pass across early-init, user-constants, system-defaults, chrono-tools Six small fixes the 2026-05-15 module-by-module re-review surfaced: - Consolidate `user-home-dir` -- canonical defconst stays in early-init.el (package-archive bootstrap needs it before normal modules load); user-constants.el switches to a `defvar` with the identical `(getenv "HOME")` expression so the module still loads / byte-compiles standalone, but at runtime early-init's defconst wins. - Drop the redundant `(autoload 'env-bsd-p ...)` line in system-defaults.el. The `(eval-when-compile (require 'host-environment))` already exposes the symbol to the byte compiler, and at runtime host-environment is loaded earlier in init.el. Added a comment documenting the boundary. - Convert `cj/debug-modules` and `cj/use-online-repos` from `defvar` to `defcustom`, with `:type`, `:group 'cj`, and a top-level `(defgroup cj ...)` so both show up in M-x customize. - Name the package-archive priorities in early-init.el. Nine new defconsts replace the magic numbers (200 / 125 / 120 / 115 / 100 / 25 / 20 / 15 / 5) with one constant each, plus a header comment explaining the local-first ordering and the gnu > nongnu > melpa > melpa-stable trust ranking within each tier. - Delete the 19-line commented-out `use-package time` world-clock block in chrono-tools.el. `time-zones` immediately above is the active replacement; git history preserves the old config if anyone needs it. - Add coverage for `cj/tmr-select-sound-file`. Collapsed the prefix-arg branch into a delegation to `cj/tmr-reset-sound-to-default` (single reset source) and extracted `cj/tmr--available-sound-files` as a pure helper that tests directly. 9 ERT tests across Normal / Boundary / Error cover the available-sounds helper, the reset path, the prefix-arg delegation (no prompt), the normal selection path, and the empty-dir / missing-dir / cancel boundaries. --- tests/test-chrono-tools-tmr-sound.el | 130 +++++++++++++++++++++++++++++++++++ 1 file changed, 130 insertions(+) create mode 100644 tests/test-chrono-tools-tmr-sound.el (limited to 'tests') diff --git a/tests/test-chrono-tools-tmr-sound.el b/tests/test-chrono-tools-tmr-sound.el new file mode 100644 index 00000000..2f7dbbab --- /dev/null +++ b/tests/test-chrono-tools-tmr-sound.el @@ -0,0 +1,130 @@ +;;; test-chrono-tools-tmr-sound.el --- Tests for tmr sound selection -*- lexical-binding: t; -*- + +;;; Commentary: +;; Tests for `cj/tmr-select-sound-file' and its helpers in chrono-tools.el. +;; The prefix-arg branch delegates to `cj/tmr-reset-sound-to-default' so +;; there's no duplicated reset logic to test twice. + +;;; Code: + +(require 'ert) +(require 'cl-lib) + +(add-to-list 'load-path (expand-file-name "modules" user-emacs-directory)) + +;; Stub vars that chrono-tools.el assumes are bound at runtime. +(defvar sounds-dir nil) +(defvar tmr-sound-file nil) +(defvar notification-sound "/tmp/notify.mp3") + +(require 'chrono-tools) + +;; -------------------------------- helpers + +(defun test-chrono-tools--with-sounds-dir (files fn) + "Create a temp `sounds-dir' containing FILES, call FN, clean up." + (let* ((dir (make-temp-file "test-chrono-tools-tmr-" t)) + (sounds-dir dir)) + (unwind-protect + (progn + (dolist (f files) + (with-temp-file (expand-file-name f dir) (insert "x"))) + (funcall fn dir)) + (when (file-exists-p dir) (delete-directory dir t))))) + +;; -------------------------------- available-sound-files + +(ert-deftest test-chrono-tools-available-sounds-normal () + "Normal: returns audio files in `sounds-dir' matching the extension list." + (test-chrono-tools--with-sounds-dir + '("a.mp3" "b.opus" "readme.txt") + (lambda (_dir) + (let ((files (cj/tmr--available-sound-files))) + (should (member "a.mp3" files)) + (should (member "b.opus" files)) + (should-not (member "readme.txt" files)))))) + +(ert-deftest test-chrono-tools-available-sounds-empty-dir () + "Boundary: empty `sounds-dir' returns nil." + (test-chrono-tools--with-sounds-dir + '() + (lambda (_dir) + (should-not (cj/tmr--available-sound-files))))) + +(ert-deftest test-chrono-tools-available-sounds-no-dir () + "Boundary: missing `sounds-dir' returns nil." + (let ((sounds-dir (expand-file-name (format "missing-%s" (random)) "/tmp"))) + (should-not (cj/tmr--available-sound-files)))) + +;; -------------------------------- reset-sound-to-default + +(ert-deftest test-chrono-tools-reset-sets-default () + "Reset assigns `notification-sound' to `tmr-sound-file'." + (let ((tmr-sound-file "/tmp/other.mp3") + (notification-sound "/tmp/the-default.mp3")) + (cj/tmr-reset-sound-to-default) + (should (equal tmr-sound-file "/tmp/the-default.mp3")))) + +;; -------------------------------- select-sound-file + +(ert-deftest test-chrono-tools-select-with-prefix-arg-resets () + "Prefix arg delegates to reset, no completing-read prompt." + (let ((current-prefix-arg '(4)) + (tmr-sound-file "/tmp/other.mp3") + (notification-sound "/tmp/the-default.mp3") + (prompted nil)) + (cl-letf (((symbol-function 'completing-read) + (lambda (&rest _) (setq prompted t) ""))) + (cj/tmr-select-sound-file)) + (should-not prompted) + (should (equal tmr-sound-file "/tmp/the-default.mp3")))) + +(ert-deftest test-chrono-tools-select-normal-sets-selected-file () + "Normal: pick a file, `tmr-sound-file' updates to its full path." + (test-chrono-tools--with-sounds-dir + '("alpha.mp3" "beta.opus") + (lambda (dir) + (let ((current-prefix-arg nil) + (tmr-sound-file nil) + (notification-sound "/tmp/the-default.mp3")) + (cl-letf (((symbol-function 'completing-read) + (lambda (&rest _) "alpha.mp3"))) + (cj/tmr-select-sound-file)) + (should (equal tmr-sound-file + (expand-file-name "alpha.mp3" dir))))))) + +(ert-deftest test-chrono-tools-select-boundary-empty-dir () + "Boundary: empty sounds dir leaves `tmr-sound-file' unchanged." + (test-chrono-tools--with-sounds-dir + '() + (lambda (_dir) + (let ((current-prefix-arg nil) + (tmr-sound-file "/tmp/keep-me.mp3") + (notification-sound "/tmp/the-default.mp3")) + (cj/tmr-select-sound-file) + (should (equal tmr-sound-file "/tmp/keep-me.mp3")))))) + +(ert-deftest test-chrono-tools-select-boundary-missing-dir () + "Boundary: missing sounds dir leaves `tmr-sound-file' unchanged." + (let ((current-prefix-arg nil) + (sounds-dir (expand-file-name (format "missing-%s" (random)) "/tmp")) + (tmr-sound-file "/tmp/keep-me.mp3") + (notification-sound "/tmp/the-default.mp3")) + (cj/tmr-select-sound-file) + (should (equal tmr-sound-file "/tmp/keep-me.mp3")))) + +(ert-deftest test-chrono-tools-select-boundary-cancel-returns-no-change () + "Boundary: empty completion result leaves `tmr-sound-file' unchanged." + (test-chrono-tools--with-sounds-dir + '("alpha.mp3") + (lambda (_dir) + (let ((current-prefix-arg nil) + (tmr-sound-file "/tmp/keep-me.mp3") + (notification-sound "/tmp/the-default.mp3")) + (cl-letf (((symbol-function 'completing-read) + (lambda (&rest _) ""))) + (cj/tmr-select-sound-file)) + (should (equal tmr-sound-file "/tmp/keep-me.mp3")))))) + +(provide 'test-chrono-tools-tmr-sound) +;;; test-chrono-tools-tmr-sound.el ends here -- cgit v1.2.3