branch: externals/ellama
commit 9733ff5d444d2bafaccb380a6b5dfc21bbdf3504
Author: Sergey Kostyaev <[email protected]>
Commit: Sergey Kostyaev <[email protected]>
Make context add functions explicitly mark context as transient
Add tests to verify that ellama-context-add-selection and
ellama-context-add-buffer are called with an explicit `t` argument when
adding
context from ask-about, code-review, write, and code-add. Update the
corresponding commands to pass `t` to these helper functions, ensuring that
the
context is treated as temporary (ephemeral). This aligns the behavior with
the
intent of the code and improves testability.
---
ellama.el | 12 +++----
tests/test-ellama.el | 98 ++++++++++++++++++++++++++++++++++++++++++++++++++++
2 files changed, 104 insertions(+), 6 deletions(-)
diff --git a/ellama.el b/ellama.el
index 5a7243a742..103992b45b 100644
--- a/ellama.el
+++ b/ellama.el
@@ -2093,8 +2093,8 @@ ARGS contains keys for fine control.
(let ((input (read-string "Ask ellama about this text: "))
(ephemeral (plist-get args :ephemeral)))
(if (region-active-p)
- (ellama-context-add-selection)
- (ellama-context-add-buffer (buffer-name (current-buffer))))
+ (ellama-context-add-selection t)
+ (ellama-context-add-buffer (buffer-name (current-buffer)) t))
(ellama-chat input create-session :ephemeral ephemeral)))
;;;###autoload
@@ -2344,8 +2344,8 @@ ARGS contains keys for fine control.
(interactive)
(let ((ephemeral (plist-get args :ephemeral)))
(if (region-active-p)
- (ellama-context-add-selection)
- (ellama-context-add-buffer (current-buffer)))
+ (ellama-context-add-selection t)
+ (ellama-context-add-buffer (current-buffer) t))
(ellama-chat
ellama-code-review-prompt-template
create-session
@@ -2357,7 +2357,7 @@ ARGS contains keys for fine control.
"Write text based on context and INSTRUCTION at point."
(interactive "sInstruction: ")
(when (region-active-p)
- (ellama-context-add-selection))
+ (ellama-context-add-selection t))
(ellama-stream (format ellama-write-prompt-template instruction)
:point (point)
:filter (when (derived-mode-p 'org-mode)
@@ -2510,7 +2510,7 @@ If a region is active, it includes the selected text as
context for code
generation."
(interactive "sDescribe the code to be generated: ")
(when (region-active-p)
- (ellama-context-add-selection))
+ (ellama-context-add-selection t))
(ellama-stream
(format
ellama-code-add-prompt-template
diff --git a/tests/test-ellama.el b/tests/test-ellama.el
index b6527ef0a2..ab6f9fd23b 100644
--- a/tests/test-ellama.el
+++ b/tests/test-ellama.el
@@ -75,6 +75,104 @@ STYLE controls partial message shape. Default value is
`word-leading'."
(funcall response-callback response-plist)))))
(funcall fn))))
+(ert-deftest test-ellama-ask-about-add-selection-ephemeral ()
+ (let (captured-ephemeral)
+ (with-temp-buffer
+ (cl-letf (((symbol-function 'read-string)
+ (lambda (&rest _args) "question"))
+ ((symbol-function 'region-active-p)
+ (lambda () t))
+ ((symbol-function 'ellama-context-add-selection)
+ (lambda (&optional ephemeral)
+ (setq captured-ephemeral ephemeral)))
+ ((symbol-function 'ellama-context-add-buffer)
+ (lambda (&rest _args)
+ (ert-fail "Unexpected call to ellama-context-add-buffer")))
+ ((symbol-function 'ellama-chat)
+ (lambda (&rest _args) nil)))
+ (ellama-ask-about)))
+ (should (eq captured-ephemeral t))))
+
+(ert-deftest test-ellama-ask-about-add-buffer-ephemeral ()
+ (let (captured-ephemeral captured-buffer)
+ (with-temp-buffer
+ (cl-letf (((symbol-function 'read-string)
+ (lambda (&rest _args) "question"))
+ ((symbol-function 'region-active-p)
+ (lambda () nil))
+ ((symbol-function 'ellama-context-add-selection)
+ (lambda (&rest _args)
+ (ert-fail "Unexpected call to
ellama-context-add-selection")))
+ ((symbol-function 'ellama-context-add-buffer)
+ (lambda (buf &optional ephemeral)
+ (setq captured-buffer buf)
+ (setq captured-ephemeral ephemeral)))
+ ((symbol-function 'ellama-chat)
+ (lambda (&rest _args) nil)))
+ (ellama-ask-about)
+ (should (equal captured-buffer (buffer-name (current-buffer))))))
+ (should (eq captured-ephemeral t))))
+
+(ert-deftest test-ellama-code-review-add-selection-ephemeral ()
+ (let (captured-ephemeral)
+ (with-temp-buffer
+ (cl-letf (((symbol-function 'region-active-p)
+ (lambda () t))
+ ((symbol-function 'ellama-context-add-selection)
+ (lambda (&optional ephemeral)
+ (setq captured-ephemeral ephemeral)))
+ ((symbol-function 'ellama-context-add-buffer)
+ (lambda (&rest _args)
+ (ert-fail "Unexpected call to ellama-context-add-buffer")))
+ ((symbol-function 'ellama-chat)
+ (lambda (&rest _args) nil)))
+ (ellama-code-review)))
+ (should (eq captured-ephemeral t))))
+
+(ert-deftest test-ellama-code-review-add-buffer-ephemeral ()
+ (let (captured-ephemeral captured-buffer)
+ (with-temp-buffer
+ (cl-letf (((symbol-function 'region-active-p)
+ (lambda () nil))
+ ((symbol-function 'ellama-context-add-selection)
+ (lambda (&rest _args)
+ (ert-fail "Unexpected call to
ellama-context-add-selection")))
+ ((symbol-function 'ellama-context-add-buffer)
+ (lambda (buf &optional ephemeral)
+ (setq captured-buffer buf)
+ (setq captured-ephemeral ephemeral)))
+ ((symbol-function 'ellama-chat)
+ (lambda (&rest _args) nil)))
+ (ellama-code-review)
+ (should (equal captured-buffer (current-buffer)))))
+ (should (eq captured-ephemeral t))))
+
+(ert-deftest test-ellama-write-add-selection-ephemeral ()
+ (let (captured-ephemeral)
+ (with-temp-buffer
+ (cl-letf (((symbol-function 'region-active-p)
+ (lambda () t))
+ ((symbol-function 'ellama-context-add-selection)
+ (lambda (&optional ephemeral)
+ (setq captured-ephemeral ephemeral)))
+ ((symbol-function 'ellama-stream)
+ (lambda (&rest _args) nil)))
+ (ellama-write "instruction")))
+ (should (eq captured-ephemeral t))))
+
+(ert-deftest test-ellama-code-add-add-selection-ephemeral ()
+ (let (captured-ephemeral)
+ (with-temp-buffer
+ (cl-letf (((symbol-function 'region-active-p)
+ (lambda () t))
+ ((symbol-function 'ellama-context-add-selection)
+ (lambda (&optional ephemeral)
+ (setq captured-ephemeral ephemeral)))
+ ((symbol-function 'ellama-stream)
+ (lambda (&rest _args) nil)))
+ (ellama-code-add "description")))
+ (should (eq captured-ephemeral t))))
+
(ert-deftest test-ellama--code-filter ()
(should (equal "" (ellama--code-filter "")))
(should (equal "(hello)" (ellama--code-filter "(hello)")))