;;; test-pearl-config.el --- Tests for pearl config helpers -*- lexical-binding: t; -*- ;; Copyright (C) 2026 Craig Jennings ;; Author: Craig Jennings ;; This program is free software: you can redistribute it and/or modify ;; it under the terms of the GNU General Public License as published by ;; the Free Software Foundation, either version 3 of the License, or ;; (at your option) any later version. ;; This program is distributed in the hope that it will be useful, ;; but WITHOUT ANY WARRANTY; without even the implied warranty of ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the ;; GNU General Public License for more details. ;; You should have received a copy of the GNU General Public License ;; along with this program. If not, see . ;;; Commentary: ;; Unit tests for the small config / state helpers: `pearl--headers', ;; `pearl-toggle-debug', and `pearl-load-api-key-from-env'. ;;; Code: (require 'test-bootstrap (expand-file-name "test-bootstrap.el")) ;;; pearl--headers (ert-deftest test-pearl-headers-no-key-errors () "Building headers without an API key signals an error." (let ((pearl-api-key nil)) (should-error (pearl--headers)))) (ert-deftest test-pearl-headers-with-key-sets-authorization () "With a key set, the Authorization header carries the raw key (no Bearer)." (let ((pearl-api-key "lin_api_abc123")) (let ((headers (pearl--headers))) (should (string-equal "lin_api_abc123" (cdr (assoc "Authorization" headers)))) (should (string-equal "application/json" (cdr (assoc "Content-Type" headers))))))) (ert-deftest test-pearl-headers-no-key-is-user-error () "A missing key signals a `user-error', not a raw error that backtraces." (let ((pearl-api-key nil) (pearl-accounts nil)) (should-error (pearl--headers) :type 'user-error))) (ert-deftest test-pearl-headers-no-key-legacy-advises-api-key () "Without accounts configured, the advice names `pearl-api-key', not customize." (let ((pearl-api-key nil) (pearl-accounts nil)) (let ((err (should-error (pearl--headers) :type 'user-error))) (should (string-match-p "pearl-api-key" (error-message-string err))) (should-not (string-match-p "customize" (error-message-string err)))))) (ert-deftest test-pearl-headers-no-key-accounts-mode-advises-accounts () "In accounts mode the missing-key advice points at the account setup." (let ((pearl-api-key nil) (pearl-accounts '(("work" :api-key-source (:literal "k") :org-file "/tmp/w.org")))) (let ((err (should-error (pearl--headers) :type 'user-error))) (should (string-match-p "account" (error-message-string err)))))) ;;; pearl-toggle-debug (ert-deftest test-pearl-toggle-debug-flips-from-nil () "Toggling from nil enables debug." (let ((pearl-debug nil)) (pearl-toggle-debug) (should (eq t pearl-debug)))) (ert-deftest test-pearl-toggle-debug-flips-from-t () "Toggling from t disables debug." (let ((pearl-debug t)) (pearl-toggle-debug) (should (null pearl-debug)))) ;;; pearl-load-api-key-from-env (ert-deftest test-pearl-load-api-key-from-env-present () "When LINEAR_API_KEY is set, the key is loaded into the variable." (let ((process-environment (cons "LINEAR_API_KEY=env-key-xyz" process-environment)) (pearl-api-key nil)) (pearl-load-api-key-from-env) (should (string-equal "env-key-xyz" pearl-api-key)))) (ert-deftest test-pearl-load-api-key-from-env-absent-leaves-key () "When LINEAR_API_KEY is unset, the key variable is left unchanged." (let ((process-environment (seq-remove (lambda (e) (string-prefix-p "LINEAR_API_KEY=" e)) process-environment)) (pearl-api-key nil)) (pearl-load-api-key-from-env) (should (null pearl-api-key)))) (provide 'test-pearl-config) ;;; test-pearl-config.el ends here