From e6a87bffca9d44ec180f7a17f6891de8f4bfccec Mon Sep 17 00:00:00 2001 From: Craig Jennings Date: Sun, 19 Apr 2026 07:15:29 -0500 Subject: refactor(transcription): extract running-transcriptions and format-entry Two cleanups round out the transcription-config refactor: - cj/--running-transcriptions: the 'status = running' filter used by cleanup and count helpers is now one function. Existing counter tests cover both callers. - cj/--format-transcription-entry: the 13-line dolist body inside cj/transcriptions-buffer becomes a testable pure function. 6 tests cover status-face mapping, basename-only rendering, duration format, trailing newline. --- modules/transcription-config.el | 45 ++++++++++++---------- tests/test-transcription-format-entry.el | 66 ++++++++++++++++++++++++++++++++ 2 files changed, 91 insertions(+), 20 deletions(-) create mode 100644 tests/test-transcription-format-entry.el diff --git a/modules/transcription-config.el b/modules/transcription-config.el index 1c864b95..91d33a8e 100644 --- a/modules/transcription-config.el +++ b/modules/transcription-config.el @@ -246,19 +246,19 @@ associated output files." (run-at-time 600 nil #'cj/--cleanup-completed-transcriptions) (force-mode-line-update t))) +(defun cj/--running-transcriptions () + "Return the subset of `cj/transcriptions-list' whose status is `running'." + (seq-filter (lambda (entry) (eq (nth 3 entry) 'running)) + cj/transcriptions-list)) + (defun cj/--cleanup-completed-transcriptions () "Remove completed/errored transcriptions from tracking list." - (setq cj/transcriptions-list - (seq-filter (lambda (entry) - (eq (nth 3 entry) 'running)) - cj/transcriptions-list)) + (setq cj/transcriptions-list (cj/--running-transcriptions)) (force-mode-line-update t)) (defun cj/--count-active-transcriptions () "Return count of running transcriptions." - (length (seq-filter (lambda (entry) - (eq (nth 3 entry) 'running)) - cj/transcriptions-list))) + (length (cj/--running-transcriptions))) ;; ----------------------------- Modeline Integration -------------------------- @@ -304,6 +304,23 @@ Uses backend specified by `cj/transcribe-backend'." (user-error "No file at point")) (cj/transcribe-audio file))) +(defun cj/--format-transcription-entry (entry) + "Return a display string for a transcription ENTRY. +ENTRY is (PROCESS AUDIO-FILE START-TIME STATUS). Status drives the face; +duration is computed from START-TIME." + (let* ((audio-file (nth 1 entry)) + (start-time (nth 2 entry)) + (status (nth 3 entry)) + (duration (cj/--transcription-duration start-time)) + (status-face (pcase status + ('running 'warning) + ('complete 'success) + ('error 'error)))) + (concat (propertize (format "%-10s" status) 'face status-face) + " " + (file-name-nondirectory audio-file) + (format " (%s)\n" duration)))) + ;;;###autoload (defun cj/transcriptions-buffer () "Show buffer with active transcriptions." @@ -318,19 +335,7 @@ Uses backend specified by `cj/transcribe-backend'." (if (null cj/transcriptions-list) (insert "No active transcriptions.\n") (dolist (entry cj/transcriptions-list) - (let* ((process (nth 0 entry)) - (audio-file (nth 1 entry)) - (start-time (nth 2 entry)) - (status (nth 3 entry)) - (duration (cj/--transcription-duration start-time)) - (status-face (pcase status - ('running 'warning) - ('complete 'success) - ('error 'error)))) - (insert (propertize (format "%-10s" status) 'face status-face) - " " - (file-name-nondirectory audio-file) - (format " (%s)\n" duration)))))) + (insert (cj/--format-transcription-entry entry))))) (goto-char (point-min)) (special-mode)) (display-buffer buffer))) diff --git a/tests/test-transcription-format-entry.el b/tests/test-transcription-format-entry.el new file mode 100644 index 00000000..a91b531b --- /dev/null +++ b/tests/test-transcription-format-entry.el @@ -0,0 +1,66 @@ +;;; test-transcription-format-entry.el --- Tests for format-transcription-entry -*- lexical-binding: t; -*- + +;;; Commentary: +;; Tests for `cj/--format-transcription-entry', which renders a single +;; transcription entry for display in the *Transcriptions* buffer. + +;;; Code: + +(require 'ert) + +(defvar cj/custom-keymap (make-sparse-keymap)) + +(unless (fboundp 'notifications-notify) + (defun notifications-notify (&rest _args) nil)) + +(require 'transcription-config) + +(defun test-format-entry-face-at (string needle) + "Return the `face' property at the start of NEEDLE within STRING." + (let ((pos (string-match-p (regexp-quote needle) string))) + (and pos (get-text-property pos 'face string)))) + +;;; Normal Cases + +(ert-deftest test-format-entry-normal-running-uses-warning-face () + "Running status renders with the `warning' face." + (let* ((entry '(proc "/tmp/a.m4a" (0 0 0 0) running)) + (s (cj/--format-transcription-entry entry))) + (should (string-match-p "running" s)) + (should (eq 'warning (test-format-entry-face-at s "running"))))) + +(ert-deftest test-format-entry-normal-complete-uses-success-face () + "Complete status renders with the `success' face." + (let* ((entry '(proc "/tmp/a.m4a" (0 0 0 0) complete)) + (s (cj/--format-transcription-entry entry))) + (should (string-match-p "complete" s)) + (should (eq 'success (test-format-entry-face-at s "complete"))))) + +(ert-deftest test-format-entry-normal-error-uses-error-face () + "Error status renders with the `error' face." + (let* ((entry '(proc "/tmp/a.m4a" (0 0 0 0) error)) + (s (cj/--format-transcription-entry entry))) + (should (string-match-p "error" s)) + (should (eq 'error (test-format-entry-face-at s "error"))))) + +(ert-deftest test-format-entry-normal-shows-basename-only () + "Only the file's basename appears; the directory path is stripped." + (let* ((entry '(proc "/deep/nested/path/track.m4a" (0 0 0 0) running)) + (s (cj/--format-transcription-entry entry))) + (should (string-match-p "track\\.m4a" s)) + (should-not (string-match-p "/deep/nested" s)))) + +(ert-deftest test-format-entry-normal-includes-duration-in-parens () + "Rendering includes a parenthesised duration." + (let* ((entry '(proc "/a.m4a" (0 0 0 0) running)) + (s (cj/--format-transcription-entry entry))) + (should (string-match-p "([0-9]+:[0-9][0-9])" s)))) + +(ert-deftest test-format-entry-normal-ends-with-newline () + "Each rendered entry terminates with a newline for buffer concatenation." + (let* ((entry '(proc "/a.m4a" (0 0 0 0) running)) + (s (cj/--format-transcription-entry entry))) + (should (string-suffix-p "\n" s)))) + +(provide 'test-transcription-format-entry) +;;; test-transcription-format-entry.el ends here -- cgit v1.2.3