[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
[elpa] externals/llm cff5db8ad5 16/34: Add unit tests and fix all broken
From: |
Andrew Hyatt |
Subject: |
[elpa] externals/llm cff5db8ad5 16/34: Add unit tests and fix all brokenness detected by them |
Date: |
Sat, 16 Sep 2023 01:32:48 -0400 (EDT) |
branch: externals/llm
commit cff5db8ad5185ac623759f737fc2554948b62c6a
Author: Andrew Hyatt <ahyatt@gmail.com>
Commit: Andrew Hyatt <ahyatt@gmail.com>
Add unit tests and fix all brokenness detected by them
---
llm-fake.el | 32 +++++++++++++++++---------------
llm-test.el | 57 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++
llm.el | 2 +-
3 files changed, 75 insertions(+), 16 deletions(-)
diff --git a/llm-fake.el b/llm-fake.el
index f74f868c1f..8a72ccebd1 100644
--- a/llm-fake.el
+++ b/llm-fake.el
@@ -51,25 +51,27 @@ message cons. If nil, the response will be a simple vector."
(with-current-buffer (get-buffer-create (llm-fake-output-to-buffer
provider))
(goto-char (point-max))
(insert "\nCall to llm-chat-response\n" (llm-chat-prompt-to-text
prompt) "\n")))
- (or (when-let (f (llm-fake-chat-action-func provider))
- (let ((result (funcall f)))
- (pcase (type-of result)
- ('string (funcall response-callback result))
- ('cons (funcall error-callback (car result) (cdr result)))
- (_ (error "Incorrect type found in `chat-action-func': %s"
(type-of-result))))))
- (funcall response-callback "Sample response from
`llm-chat-response-async'")))
+ (if (llm-fake-chat-action-func provider)
+ (let* ((f (llm-fake-chat-action-func provider))
+ (result (funcall f)))
+ (pcase (type-of result)
+ ('string (funcall response-callback result))
+ ('cons (funcall error-callback (car result) (cdr result)))
+ (_ (error "Incorrect type found in `chat-action-func': %s"
(type-of-result)))))
+ (funcall response-callback "Sample response from
`llm-chat-response-async'")))
-(cl-defmethod llm-embedding-async ((provider llm-openai) string
vector-callback error-callback)
+(cl-defmethod llm-embedding-async ((provider llm-fake) string vector-callback
error-callback)
(when (llm-fake-output-to-buffer provider)
(with-current-buffer (get-buffer-create (llm-fake-output-to-buffer
provider))
(goto-char (point-max))
(insert "\nCall to llm-embedding with text: " string "\n")))
- (or (when-let (f (llm-fake-chat-action-func provider))
- (let ((result (funcall f)))
- (pcase (type-of result)
- ('vector (funcall vector-callback result))
- ('cons (funcall error-callback (car result) (cdr result)))
- (_ (error "Incorrect type found in `chat-embedding-func': %s"
(type-of-result))))))
- (funcall response-callback [0 0.1 0.2 0.3 0.4 0.5 0.6 0.7 0.8 0.9])))
+ (if (llm-fake-embedding-action-func provider)
+ (let* ((f (llm-fake-embedding-action-func provider))
+ (result (funcall f)))
+ (pcase (type-of result)
+ ('vector (funcall vector-callback result))
+ ('cons (funcall error-callback (car result) (cdr result)))
+ (_ (error "Incorrect type found in `chat-embedding-func': %s"
(type-of-result)))))
+ (funcall vector-callback [0 0.1 0.2 0.3 0.4 0.5 0.6 0.7 0.8 0.9])))
(provide 'llm-fake)
diff --git a/llm-test.el b/llm-test.el
new file mode 100644
index 0000000000..5470db0c8a
--- /dev/null
+++ b/llm-test.el
@@ -0,0 +1,57 @@
+;;; llm-test.el --- Unit tests for the llm module -*- lexical-binding: t -*-
+
+;; Copyright (c) 2023 Andrew Hyatt <ahyatt@gmail.com>
+
+;; Author: Andrew Hyatt <ahyatt@gmail.com>
+;; SPDX-License-Identifier: GPL-3.0-or-later
+;;
+;; 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 GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
+
+;;; Commentary:
+;; This tests just the code in the `llm' module, although it uses `llm-fake' as
+;; well to do so. All individual providers are probably best tested in the
+;; `llm-tester' module.
+
+;;; Code:
+
+(require 'llm)
+(require 'llm-fake)
+(require 'ert)
+
+(ert-deftest llm-test-embedding ()
+ (should-error (llm-embedding nil "Test"))
+ (should-error (llm-embedding-async nil "Test"))
+ ;; TODO: Test signals that are not errors, which ert doesn't seem to catch.
+ (should-error (llm-embedding (make-llm-fake
+ :embedding-action-func
+ (lambda () (cons 'error "my message")))
+ "Test"))
+ (should (equal
+ [0.1 0.2 0.3]
+ (llm-embedding (make-llm-fake :embedding-action-func (lambda ()
[0.1 0.2 0.3]))
+ "Test"))))
+
+(ert-deftest llm-test-chat ()
+ (should-error (llm-chat-response nil "Test"))
+ (should-error (llm-chat-response-async nil "Test"))
+ (should-error (llm-chat-response
+ (make-llm-fake
+ :chat-action-func (lambda () (cons 'error "my message")))
+ (make-llm-chat-prompt)))
+ (should (equal
+ "Response"
+ (llm-chat-response (make-llm-fake :chat-action-func (lambda ()
"Response"))
+ (make-llm-chat-prompt)))))
+
+;;; llm-test.el ends here
diff --git a/llm.el b/llm.el
index f83233eaaf..3c18ac8253 100644
--- a/llm.el
+++ b/llm.el
@@ -115,7 +115,7 @@ ERROR-CALLBACK receives the error response."
"Return a vector embedding of STRING from PROVIDER."
(llm--run-async-as-sync #'llm-embedding-async provider string))
-(cl-defmethod llm-chat-embedding ((_ (eql nil)) _)
+(cl-defmethod llm-embedding ((_ (eql nil)) _)
(error "LLM provider was nil. Please set the provider in the application
you are using."))
(cl-defgeneric llm-embedding-async (provider string vector-callback
error-callback)
- [elpa] branch externals/llm created (now 39ae6fc794), Andrew Hyatt, 2023/09/16
- [elpa] externals/llm 3b761aca25 02/34: Add README.org, Andrew Hyatt, 2023/09/16
- [elpa] externals/llm ad76cff80b 01/34: Initial checkin of the llm package for emacs., Andrew Hyatt, 2023/09/16
- [elpa] externals/llm cff5db8ad5 16/34: Add unit tests and fix all brokenness detected by them,
Andrew Hyatt <=
- [elpa] externals/llm 131a7ee5d3 12/34: Solve flaky errors when using sync llm commands, Andrew Hyatt, 2023/09/16
- [elpa] externals/llm 48ae59d149 14/34: Fix llm-chat-prompt-to-text, which was unusable, Andrew Hyatt, 2023/09/16
- [elpa] externals/llm 636014bf64 08/34: Make all remaining code async-friendly, Andrew Hyatt, 2023/09/16
- [elpa] externals/llm 9e3040bad2 20/34: Add warnings requested by GNU about nonfree software, Andrew Hyatt, 2023/09/16
- [elpa] externals/llm c8b14b4d9c 19/34: Fix fake provider embedding func and remove async unit tests, Andrew Hyatt, 2023/09/16
- [elpa] externals/llm 9057a50df4 11/34: Fix indenting in llm--run-async-as-sync, Andrew Hyatt, 2023/09/16
- [elpa] externals/llm c322577b9b 13/34: Test both sync and async commands, Andrew Hyatt, 2023/09/16
- [elpa] externals/llm cff9ab8f3c 22/34: Centralize nonfree llm warnings, and warn with a targeted type, Andrew Hyatt, 2023/09/16
- [elpa] externals/llm ad230d9d6b 10/34: Add methods for nil provider, to throw more meaningful errors, Andrew Hyatt, 2023/09/16
- [elpa] externals/llm 650bba65d5 25/34: Improve the docstring for llm--warn-on-nonfree, Andrew Hyatt, 2023/09/16