aboutsummaryrefslogtreecommitdiff
path: root/tests/test-transcription-process-environment.el
blob: 95f26e13fc839f4d4a859d3d1fc91de24cd745b4 (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
;;; test-transcription-process-environment.el --- Tests for env-var assembly -*- lexical-binding: t; -*-

;;; Commentary:
;; Tests for `cj/--build-process-environment', which returns a
;; `process-environment'-compatible list augmented with the API-key env
;; var for the active backend (or unchanged for local backends).

;;; 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-env-with-api-key (key-value &rest body)
  "Run BODY with `cj/--auth-source-password' returning KEY-VALUE."
  (declare (indent 1))
  `(cl-letf (((symbol-function 'cj/--auth-source-password)
              (lambda (&rest _) ,key-value)))
     ,@body))

;;; Normal Cases

(ert-deftest test-build-env-normal-openai-prepends-key-var ()
  "openai-api backend prepends OPENAI_API_KEY=<key>."
  (test-env-with-api-key "sk-abc"
    (let ((process-environment '("PATH=/usr/bin")))
      (let ((result (cj/--build-process-environment 'openai-api)))
        (should (equal "OPENAI_API_KEY=sk-abc" (car result)))
        (should (member "PATH=/usr/bin" result))))))

(ert-deftest test-build-env-normal-assemblyai-prepends-key-var ()
  "assemblyai backend prepends ASSEMBLYAI_API_KEY=<key>."
  (test-env-with-api-key "aai-xyz"
    (let ((process-environment '("PATH=/usr/bin")))
      (let ((result (cj/--build-process-environment 'assemblyai)))
        (should (equal "ASSEMBLYAI_API_KEY=aai-xyz" (car result)))
        (should (member "PATH=/usr/bin" result))))))

(ert-deftest test-build-env-normal-local-whisper-unchanged ()
  "local-whisper has no API key; environment is returned unchanged."
  (let ((process-environment '("PATH=/usr/bin" "HOME=/tmp")))
    (let ((result (cj/--build-process-environment 'local-whisper)))
      (should (equal '("PATH=/usr/bin" "HOME=/tmp") result)))))

(ert-deftest test-build-env-normal-preserves-parent-environment ()
  "Every entry in the parent `process-environment' remains present."
  (test-env-with-api-key "k"
    (let ((process-environment '("A=1" "B=2" "C=3")))
      (let ((result (cj/--build-process-environment 'openai-api)))
        (should (member "A=1" result))
        (should (member "B=2" result))
        (should (member "C=3" result))))))

;;; Boundary Cases

(ert-deftest test-build-env-boundary-empty-parent-env ()
  "Empty parent `process-environment' yields a list with just the API-key var."
  (test-env-with-api-key "k"
    (let ((process-environment '()))
      (let ((result (cj/--build-process-environment 'openai-api)))
        (should (equal '("OPENAI_API_KEY=k") result))))))

(ert-deftest test-build-env-boundary-does-not-mutate-input ()
  "Parent `process-environment' is not destructively modified."
  (test-env-with-api-key "k"
    (let* ((parent '("PATH=/usr/bin"))
           (process-environment parent))
      (cj/--build-process-environment 'openai-api)
      (should (equal '("PATH=/usr/bin") parent)))))

;;; Error Cases

(ert-deftest test-build-env-error-openai-missing-key ()
  "openai-api with no key configured signals user-error."
  (test-env-with-api-key nil
    (should-error (cj/--build-process-environment 'openai-api) :type 'user-error)))

(ert-deftest test-build-env-error-assemblyai-missing-key ()
  "assemblyai with no key configured signals user-error."
  (test-env-with-api-key nil
    (should-error (cj/--build-process-environment 'assemblyai) :type 'user-error)))

(ert-deftest test-build-env-error-unknown-backend ()
  "Unknown backend bubbles up user-error from cj/--backend-plist."
  (should-error (cj/--build-process-environment 'nonexistent) :type 'user-error))

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