summaryrefslogtreecommitdiff
path: root/tests/test-integration-transcription.el
blob: 96b617bcf08f315e832f64f61bbfe4cf1b50643d (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
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
;;; test-integration-transcription.el --- Integration tests for transcription -*- lexical-binding: t; -*-

;;; Commentary:
;; End-to-end integration tests for transcription workflow
;; Tests complete workflow with temporary files and mocked processes
;; Categories: Normal workflow, Error handling, Cleanup

;;; Code:

(require 'ert)
(require 'transcription-config)

;; ----------------------------- Test Helpers ----------------------------------

(defun test-transcription--make-mock-audio-file ()
  "Create a temporary mock audio file for testing.
Returns the absolute path to the file."
  (let ((file (make-temp-file "test-audio-" nil ".m4a")))
    (with-temp-file file
      (insert "Mock audio data"))
    file))

(defun test-transcription--cleanup-output-files (audio-file)
  "Delete transcript and log files associated with AUDIO-FILE."
  (let* ((outputs (cj/--transcription-output-files audio-file))
         (txt-file (car outputs))
         (log-file (cdr outputs)))
    (when (file-exists-p txt-file)
      (delete-file txt-file))
    (when (file-exists-p log-file)
      (delete-file log-file))))

;; ----------------------------- Normal Cases ----------------------------------

(ert-deftest test-integration-transcription-output-files-created ()
  "Test that .txt and .log files are created for audio file."
  (let* ((audio-file (test-transcription--make-mock-audio-file))
         (outputs (cj/--transcription-output-files audio-file))
         (txt-file (car outputs))
         (log-file (cdr outputs)))
    (unwind-protect
        (progn
          ;; Verify output file paths are correct
          (should (string-suffix-p ".txt" txt-file))
          (should (string-suffix-p ".log" log-file))
          (should (string= (file-name-sans-extension txt-file)
                          (file-name-sans-extension audio-file)))
          (should (string= (file-name-sans-extension log-file)
                          (file-name-sans-extension audio-file))))
      ;; Cleanup
      (delete-file audio-file)
      (test-transcription--cleanup-output-files audio-file))))

(ert-deftest test-integration-transcription-validates-file-exists ()
  "Test that transcription fails for non-existent file."
  (should-error
   (cj/--start-transcription-process "/nonexistent/audio.m4a")
   :type 'user-error))

(ert-deftest test-integration-transcription-validates-audio-extension ()
  "Test that transcription fails for non-audio file."
  (let ((non-audio (make-temp-file "test-" nil ".txt")))
    (unwind-protect
        (should-error
         (cj/--start-transcription-process non-audio)
         :type 'user-error)
      (delete-file non-audio))))

;; ----------------------------- Boundary Cases --------------------------------

(ert-deftest test-integration-transcription-audio-file-detection ()
  "Test various audio file extensions are accepted."
  (dolist (ext '("m4a" "mp3" "wav" "flac" "ogg" "opus"))
    (let ((audio-file (make-temp-file "test-audio-" nil (concat "." ext))))
      (unwind-protect
          (progn
            (should (cj/--audio-file-p audio-file))
            ;; Would start transcription if script existed
            )
        (delete-file audio-file)))))

(ert-deftest test-integration-transcription-filename-with-spaces ()
  "Test transcription with audio file containing spaces."
  (let ((audio-file (make-temp-file "test audio file" nil ".m4a")))
    (unwind-protect
        (let* ((outputs (cj/--transcription-output-files audio-file))
               (txt-file (car outputs))
               (log-file (cdr outputs)))
          (should (file-name-absolute-p txt-file))
          (should (file-name-absolute-p log-file)))
      (delete-file audio-file))))

(ert-deftest test-integration-transcription-filename-with-special-chars ()
  "Test transcription with special characters in filename."
  (let ((audio-file (make-temp-file "test_(final)" nil ".m4a")))
    (unwind-protect
        (let* ((outputs (cj/--transcription-output-files audio-file))
               (txt-file (car outputs)))
          ;; make-temp-file adds random suffix, so just check it ends with .txt
          ;; and contains the special chars
          (should (string-suffix-p ".txt" txt-file))
          (should (string-match-p "test_(final)" txt-file)))
      (delete-file audio-file))))

;; ----------------------------- Cleanup Tests ---------------------------------

(ert-deftest test-integration-transcription-cleanup-completed ()
  "Test that completed transcriptions are removed from tracking."
  (let ((cj/transcriptions-list
         '((proc1 "file1.m4a" nil running)
           (proc2 "file2.m4a" nil complete)
           (proc3 "file3.m4a" nil error))))
    (cj/--cleanup-completed-transcriptions)
    (should (= 1 (length cj/transcriptions-list)))
    (should (eq 'running (nth 3 (car cj/transcriptions-list))))))

(ert-deftest test-integration-transcription-cleanup-all-complete ()
  "Test cleanup when all transcriptions are complete."
  (let ((cj/transcriptions-list
         '((proc1 "file1.m4a" nil complete)
           (proc2 "file2.m4a" nil error))))
    (cj/--cleanup-completed-transcriptions)
    (should (null cj/transcriptions-list))))

(ert-deftest test-integration-transcription-cleanup-preserves-running ()
  "Test that running transcriptions are not cleaned up."
  (let ((cj/transcriptions-list
         '((proc1 "file1.m4a" nil running)
           (proc2 "file2.m4a" nil running))))
    (cj/--cleanup-completed-transcriptions)
    (should (= 2 (length cj/transcriptions-list)))))

;; ----------------------------- Backend Tests ---------------------------------

(ert-deftest test-integration-transcription-script-path-exists ()
  "Test that transcription scripts exist in expected location."
  (dolist (backend '(local-whisper openai-api))
    (let ((cj/transcribe-backend backend))
      (let ((script (cj/--transcription-script-path)))
        (should (file-name-absolute-p script))
        ;; Note: Script may not exist in test environment, just check path format
        (should (string-match-p "scripts/" script))))))

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