diff options
| author | Craig Jennings <c@cjennings.net> | 2026-04-19 07:06:45 -0500 |
|---|---|---|
| committer | Craig Jennings <c@cjennings.net> | 2026-04-19 07:06:45 -0500 |
| commit | 85d9127ea6a5cd624dd4567618ce87b12e491e8c (patch) | |
| tree | a1d5ae757b83ccdfaa51b3062e9febdc6c4844d5 /tests/test-transcription-api-key.el | |
| parent | cb9fe91fbb35aef0a9de578a66e5f59df964321c (diff) | |
| download | dotemacs-85d9127ea6a5cd624dd4567618ce87b12e491e8c.tar.gz dotemacs-85d9127ea6a5cd624dd4567618ce87b12e491e8c.zip | |
refactor(transcription): consolidate backends into descriptor alist
Introduce cj/--transcription-backends alist mapping each backend to
(:script :auth-host :env-var). Replace:
- two near-identical cj/--get-{openai,assemblyai}-api-key functions with
a single parameterized cj/--auth-source-password helper
- the pcase in cj/--transcription-script-path with an alist lookup
- the pcase block in cj/--start-transcription-process that assembled
the API-key env var with an alist-driven assembly
Adding a new backend is now a single line in the alist. The existing
tests plus retargeted API-key tests (now 10, covering the parameterized
helper and the descriptor data) verify no behavior change.
Diffstat (limited to 'tests/test-transcription-api-key.el')
| -rw-r--r-- | tests/test-transcription-api-key.el | 115 |
1 files changed, 64 insertions, 51 deletions
diff --git a/tests/test-transcription-api-key.el b/tests/test-transcription-api-key.el index 01a7a0ea..fb0fbf5a 100644 --- a/tests/test-transcription-api-key.el +++ b/tests/test-transcription-api-key.el @@ -1,10 +1,9 @@ -;;; test-transcription-api-key.el --- Tests for API-key retrieval -*- lexical-binding: t; -*- +;;; test-transcription-api-key.el --- Tests for auth-source password helper -*- 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. +;; 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: @@ -18,14 +17,13 @@ (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) +(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 either a string, a function returning a string, or nil -\(to simulate no matching entry). The host passed to `auth-source-search' +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)) @@ -36,55 +34,70 @@ is captured in `test-api-key-requested-host'." (list (list :secret ,secret-value)))))) ,@body))) -;;; Normal Cases — OpenAI +;;; Normal Cases -(ert-deftest test-api-key-openai-normal-returns-string-secret () - "OpenAI key retrieval returns the string stored under :secret." +(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/--get-openai-api-key))) - (should (equal "api.openai.com" test-api-key-requested-host)))) + (should (equal "sk-abc123" (cj/--auth-source-password "api.openai.com"))))) -(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." +(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/--get-assemblyai-api-key))))) + (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-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." +(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/--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)))) + (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 |
