aboutsummaryrefslogtreecommitdiff
path: root/tests/test-transcription-start-helpers.el
blob: f52d2066e320a0bb846c887d16996e365ff24b06 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
;;; test-transcription-start-helpers.el --- Tests for process-start helpers -*- lexical-binding: t; -*-

;;; Commentary:
;; Tests for the two helpers extracted from `cj/--start-transcription-process':
;; - `cj/--init-log-file' (writes the initial log header)
;; - `cj/--track-transcription' (pushes an entry onto the active list)

;;; Code:

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

(defvar cj/custom-keymap (make-sparse-keymap))

(unless (fboundp 'notifications-notify)
  (defun notifications-notify (&rest _args) nil))

(require 'transcription-config)

(defmacro test-start-helpers-with-temp-log (var &rest body)
  "Bind VAR to a fresh temp log-file path and run BODY, cleaning up after."
  (declare (indent 1))
  `(let ((,var (make-temp-file "transcription-log-" nil ".log")))
     (unwind-protect
         (progn ,@body)
       (when (file-exists-p ,var) (delete-file ,var)))))

(defun test-start-helpers-file-contents (path)
  "Return contents of PATH as a string."
  (with-temp-buffer
    (insert-file-contents path)
    (buffer-string)))

;;; cj/--init-log-file — Normal Cases

(ert-deftest test-init-log-file-normal-creates-file ()
  "The log file exists after `cj/--init-log-file' runs."
  (test-start-helpers-with-temp-log log-file
    (delete-file log-file)  ; start from a clean slate
    (cj/--init-log-file log-file "/tmp/foo.m4a" "/tmp/script")
    (should (file-exists-p log-file))))

(ert-deftest test-init-log-file-normal-records-audio-path ()
  "Log header includes the full AUDIO-FILE path."
  (test-start-helpers-with-temp-log log-file
    (cj/--init-log-file log-file "/tmp/foo.m4a" "/tmp/script")
    (should (string-match-p "Audio file: /tmp/foo\\.m4a"
                            (test-start-helpers-file-contents log-file)))))

(ert-deftest test-init-log-file-normal-records-script-path ()
  "Log header includes the SCRIPT path."
  (test-start-helpers-with-temp-log log-file
    (cj/--init-log-file log-file "/tmp/foo.m4a" "/usr/local/bin/whisper")
    (should (string-match-p "Script: /usr/local/bin/whisper"
                            (test-start-helpers-file-contents log-file)))))

(ert-deftest test-init-log-file-normal-records-backend ()
  "Log header includes the current `cj/transcribe-backend' value."
  (test-start-helpers-with-temp-log log-file
    (let ((cj/transcribe-backend 'local-whisper))
      (cj/--init-log-file log-file "/tmp/foo.m4a" "/tmp/script"))
    (should (string-match-p "Backend: local-whisper"
                            (test-start-helpers-file-contents log-file)))))

(ert-deftest test-init-log-file-normal-records-start-timestamp ()
  "Log header begins with a `Transcription started:' line."
  (test-start-helpers-with-temp-log log-file
    (cj/--init-log-file log-file "/tmp/foo.m4a" "/tmp/script")
    (should (string-match-p "Transcription started:"
                            (test-start-helpers-file-contents log-file)))))

;;; cj/--init-log-file — Boundary Cases

(ert-deftest test-init-log-file-boundary-overwrites-existing ()
  "An existing log file is overwritten (not appended to)."
  (test-start-helpers-with-temp-log log-file
    (with-temp-file log-file (insert "STALE CONTENT\n"))
    (cj/--init-log-file log-file "/tmp/foo.m4a" "/tmp/script")
    (should-not (string-match-p "STALE CONTENT"
                                (test-start-helpers-file-contents log-file)))))

;;; cj/--track-transcription — Normal Cases

(ert-deftest test-track-transcription-normal-adds-entry-to-list ()
  "A call to `cj/--track-transcription' pushes one entry."
  (let ((cj/transcriptions-list '()))
    (cj/--track-transcription 'proc1 "/tmp/foo.m4a")
    (should (= 1 (length cj/transcriptions-list)))))

(ert-deftest test-track-transcription-normal-entry-shape ()
  "Pushed entry is (PROC AUDIO-FILE TIME running)."
  (let ((cj/transcriptions-list '()))
    (cj/--track-transcription 'proc1 "/tmp/foo.m4a")
    (let ((entry (car cj/transcriptions-list)))
      (should (eq 'proc1 (nth 0 entry)))
      (should (equal "/tmp/foo.m4a" (nth 1 entry)))
      (should (nth 2 entry))          ; start-time is non-nil
      (should (eq 'running (nth 3 entry))))))

(ert-deftest test-track-transcription-normal-multiple-entries-accumulate ()
  "Multiple calls accumulate onto the list."
  (let ((cj/transcriptions-list '()))
    (cj/--track-transcription 'p1 "/a.m4a")
    (cj/--track-transcription 'p2 "/b.m4a")
    (cj/--track-transcription 'p3 "/c.m4a")
    (should (= 3 (length cj/transcriptions-list)))))

(ert-deftest test-track-transcription-normal-preserves-existing-entries ()
  "Pre-existing entries in the list are not disturbed."
  (let ((cj/transcriptions-list '((old-proc "/old.m4a" nil complete))))
    (cj/--track-transcription 'new-proc "/new.m4a")
    (should (= 2 (length cj/transcriptions-list)))
    (should (member '(old-proc "/old.m4a" nil complete) cj/transcriptions-list))))

(provide 'test-transcription-start-helpers)
;;; test-transcription-start-helpers.el ends here