blob: 01a7a0ea8b272224eebe41a807f4f73ed3d198e9 (
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
|
;;; test-transcription-api-key.el --- Tests for API-key retrieval -*- lexical-binding: t; -*-
;;; Commentary:
;; Characterization tests for `cj/--get-openai-api-key' and
;; `cj/--get-assemblyai-api-key'. Pin down current auth-source-search
;; behavior so the upcoming consolidation into a single parameterized
;; helper can be verified as a pure refactor.
;;; 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)
;; Captures the most recent :host passed to auth-source-search so each
;; test can assert that the right host was queried.
(defvar test-api-key-requested-host nil)
(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 either 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 — OpenAI
(ert-deftest test-api-key-openai-normal-returns-string-secret ()
"OpenAI key retrieval returns the string stored under :secret."
(test-api-key-with-auth-source "sk-abc123"
(should (equal "sk-abc123" (cj/--get-openai-api-key)))
(should (equal "api.openai.com" test-api-key-requested-host))))
(ert-deftest test-api-key-openai-normal-calls-function-secret ()
"OpenAI key retrieval invokes :secret when it's a function."
(test-api-key-with-auth-source (lambda () "sk-from-fn")
(should (equal "sk-from-fn" (cj/--get-openai-api-key)))))
;;; Normal Cases — AssemblyAI
(ert-deftest test-api-key-assemblyai-normal-returns-string-secret ()
"AssemblyAI key retrieval returns the string stored under :secret."
(test-api-key-with-auth-source "aai-xyz789"
(should (equal "aai-xyz789" (cj/--get-assemblyai-api-key)))
(should (equal "api.assemblyai.com" test-api-key-requested-host))))
(ert-deftest test-api-key-assemblyai-normal-calls-function-secret ()
"AssemblyAI key retrieval invokes :secret when it's a function."
(test-api-key-with-auth-source (lambda () "aai-from-fn")
(should (equal "aai-from-fn" (cj/--get-assemblyai-api-key)))))
;;; Boundary Cases
(ert-deftest test-api-key-openai-boundary-returns-nil-when-entry-missing ()
"OpenAI key retrieval returns nil when auth-source has no matching entry."
(test-api-key-with-auth-source nil
(should (null (cj/--get-openai-api-key)))))
(ert-deftest test-api-key-assemblyai-boundary-returns-nil-when-entry-missing ()
"AssemblyAI key retrieval returns nil when auth-source has no matching entry."
(test-api-key-with-auth-source nil
(should (null (cj/--get-assemblyai-api-key)))))
(ert-deftest test-api-key-openai-boundary-queries-correct-host ()
"OpenAI key retrieval always queries `api.openai.com', never another host."
(test-api-key-with-auth-source "sk-x"
(cj/--get-openai-api-key)
(should (equal "api.openai.com" test-api-key-requested-host))))
(ert-deftest test-api-key-assemblyai-boundary-queries-correct-host ()
"AssemblyAI key retrieval always queries `api.assemblyai.com'."
(test-api-key-with-auth-source "aai-x"
(cj/--get-assemblyai-api-key)
(should (equal "api.assemblyai.com" test-api-key-requested-host))))
(provide 'test-transcription-api-key)
;;; test-transcription-api-key.el ends here
|