blob: fb0fbf5a490f62380955bfd417aa5513646ecb3e (
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
|
;;; test-transcription-api-key.el --- Tests for auth-source password helper -*- lexical-binding: t; -*-
;;; Commentary:
;; Tests for `cj/--auth-source-password', the parameterized helper that
;; replaced the per-backend `cj/--get-openai-api-key' and
;; `cj/--get-assemblyai-api-key' functions.
;;; 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)
(defvar test-api-key-requested-host nil
"Captures the :host passed to the mocked `auth-source-search'.")
(defmacro test-api-key-with-auth-source (secret-value &rest body)
"Run BODY with `auth-source-search' stubbed to return SECRET-VALUE.
SECRET-VALUE is a string, a function returning a string, or nil (to
simulate no matching entry). The host passed to `auth-source-search'
is captured in `test-api-key-requested-host'."
(declare (indent 1))
`(let ((test-api-key-requested-host nil))
(cl-letf (((symbol-function 'auth-source-search)
(lambda (&rest args)
(setq test-api-key-requested-host (plist-get args :host))
(when ,secret-value
(list (list :secret ,secret-value))))))
,@body)))
;;; Normal Cases
(ert-deftest test-api-key-normal-returns-string-secret ()
"Returns the :secret value when stored as a string."
(test-api-key-with-auth-source "sk-abc123"
(should (equal "sk-abc123" (cj/--auth-source-password "api.openai.com")))))
(ert-deftest test-api-key-normal-calls-function-secret ()
"Invokes the :secret value when stored as a function."
(test-api-key-with-auth-source (lambda () "aai-from-fn")
(should (equal "aai-from-fn" (cj/--auth-source-password "api.assemblyai.com")))))
(ert-deftest test-api-key-normal-passes-host-through ()
"The caller-supplied HOST is forwarded to `auth-source-search'."
(test-api-key-with-auth-source "x"
(cj/--auth-source-password "api.openai.com")
(should (equal "api.openai.com" test-api-key-requested-host)))
(test-api-key-with-auth-source "x"
(cj/--auth-source-password "api.assemblyai.com")
(should (equal "api.assemblyai.com" test-api-key-requested-host)))
(test-api-key-with-auth-source "x"
(cj/--auth-source-password "example.com")
(should (equal "example.com" test-api-key-requested-host))))
;;; Boundary Cases
(ert-deftest test-api-key-boundary-returns-nil-when-entry-missing ()
"Returns nil when `auth-source-search' finds no matching entry."
(test-api-key-with-auth-source nil
(should (null (cj/--auth-source-password "api.openai.com")))
(should (null (cj/--auth-source-password "api.assemblyai.com")))))
(ert-deftest test-api-key-boundary-empty-string-host ()
"Empty host string is forwarded unchanged (auth-source decides)."
(test-api-key-with-auth-source "x"
(cj/--auth-source-password "")
(should (equal "" test-api-key-requested-host))))
;;; Backend Descriptor Integration
(ert-deftest test-api-key-integration-openai-backend-descriptor ()
"openai-api backend descriptor has the expected auth-host and env-var."
(let ((desc (cj/--backend-plist 'openai-api)))
(should (equal "api.openai.com" (plist-get desc :auth-host)))
(should (equal "OPENAI_API_KEY" (plist-get desc :env-var)))
(should (equal "oai-transcribe" (plist-get desc :script)))))
(ert-deftest test-api-key-integration-assemblyai-backend-descriptor ()
"assemblyai backend descriptor has the expected auth-host and env-var."
(let ((desc (cj/--backend-plist 'assemblyai)))
(should (equal "api.assemblyai.com" (plist-get desc :auth-host)))
(should (equal "ASSEMBLYAI_API_KEY" (plist-get desc :env-var)))
(should (equal "assemblyai-transcribe" (plist-get desc :script)))))
(ert-deftest test-api-key-integration-local-whisper-needs-no-key ()
"local-whisper descriptor explicitly has nil auth-host and env-var."
(let ((desc (cj/--backend-plist 'local-whisper)))
(should (null (plist-get desc :auth-host)))
(should (null (plist-get desc :env-var)))
(should (equal "local-whisper" (plist-get desc :script)))))
(ert-deftest test-api-key-integration-unknown-backend-errors ()
"Requesting an unknown backend's descriptor signals user-error."
(should-error (cj/--backend-plist 'nonexistent-backend) :type 'user-error))
(provide 'test-transcription-api-key)
;;; test-transcription-api-key.el ends here
|