From f6e5885b47e3ab244b293f4e478af7e520180710 Mon Sep 17 00:00:00 2001 From: Craig Jennings Date: Fri, 22 May 2026 19:32:32 -0500 Subject: refactor(auth): consolidate the auth-source secret lookup into one helper The auth-source-search + funcall-the-secret block was copied four times: calendar-sync--calendar-url, cj/auth-source-secret (ai-config), cj/--auth-source-password (transcription), and cj/slack--get-credential. Each searched authinfo, pulled :secret, and called it when the netrc backend returned a function. I pulled that into cj/auth-source-secret-value in system-lib (a leaf, so calendar-sync doesn't have to depend on ai-config and drag in the gptel stack). It takes an optional user and returns the secret or nil. The four callers now delegate to it: ai-config layers its required-secret error on top, and the others keep their nil-on-miss behavior. With the direct auth-source-search calls gone, I dropped the now-unused (require 'auth-source) from transcription, slack, and calendar-sync. The helper's autoload covers it. The transcription tests that exercise the delegated path stay green, and the primitive and the error wrapper get their own tests. --- tests/test-ai-config-auth-source-secret.el | 27 +++++++++++++++++++++++++++ 1 file changed, 27 insertions(+) create mode 100644 tests/test-ai-config-auth-source-secret.el (limited to 'tests/test-ai-config-auth-source-secret.el') diff --git a/tests/test-ai-config-auth-source-secret.el b/tests/test-ai-config-auth-source-secret.el new file mode 100644 index 000000000..bab506e5f --- /dev/null +++ b/tests/test-ai-config-auth-source-secret.el @@ -0,0 +1,27 @@ +;;; test-ai-config-auth-source-secret.el --- Tests for the required-secret wrapper -*- lexical-binding: t; -*- + +;;; Commentary: +;; `cj/auth-source-secret' is the required-secret layer over the shared +;; `cj/auth-source-secret-value' primitive: it returns the secret, or errors +;; when none is found. These tests stub the primitive to exercise both paths. + +;;; Code: + +(require 'ert) +(require 'cl-lib) + +(add-to-list 'load-path (expand-file-name "modules" user-emacs-directory)) +(require 'ai-config) + +(ert-deftest test-ai-config-auth-source-secret-returns-value () + "Normal: returns the value the primitive resolves." + (cl-letf (((symbol-function 'cj/auth-source-secret-value) (lambda (&rest _) "sk-x"))) + (should (equal "sk-x" (cj/auth-source-secret "api.example.com" "apikey"))))) + +(ert-deftest test-ai-config-auth-source-secret-errors-on-miss () + "Error: signals when the primitive finds no secret." + (cl-letf (((symbol-function 'cj/auth-source-secret-value) (lambda (&rest _) nil))) + (should-error (cj/auth-source-secret "api.example.com" "apikey")))) + +(provide 'test-ai-config-auth-source-secret) +;;; test-ai-config-auth-source-secret.el ends here -- cgit v1.2.3