-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathelm.test.el
206 lines (179 loc) · 8.48 KB
/
elm.test.el
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
;;; elm.test.el --- Description -*- lexical-binding: t; -*-
;;
;; Copyright (C) 2024 Hamza Hamud
;;
;; Author: Hamza Hamud <[email protected]>
;; Maintainer: Hamza Hamud <[email protected]>
;; Created: September 14, 2024
;; Modified: September 14, 2024
;; Version: 0.0.1
;; Keywords: abbrev bib c calendar comm convenience data docs emulations extensions faces files frames games hardware help hypermedia i18n internal languages lisp local maint mail matching mouse multimedia news outlines processes terminals tex tools unix vc wp
;; Homepage: https://github.com/user/elm.test
;; Package-Requires: ((emacs "24.3"))
;;
;; This file is not part of GNU Emacs.
;;
;;; Commentary:
;;
;; Description
;;
;;; Code:
(require 'ert)
(require 'elm)
;; Mocking helpers
(defun mock-read-auth (keys)
"Mocked version of `elm--read-auth` that returns fake API keys."
(pcase (plist-get keys :user)
("CLAUDE" "fake-claude-key")
("GROQ" "fake-groq-key")
(_ nil)))
(defun mock-auth-source-search (&rest _args)
"Return a fake auth source for testing purposes."
(list (list :secret (lambda () "fake-secret"))))
(defun mock-file-exists-p (file)
"Mock `file-exists-p` to simulate presence or absence of files."
(not (string= file "~/.elm/model.json")))
(defun mock-make-directory (&rest _args)
"Mock `make-directory` to simulate directory creation.")
(defun mock-insert-file (content)
"Simulate writing content to a file."
(with-temp-buffer
(insert content)
(buffer-string)))
(defun mock-json-encode (data)
"Mock JSON encoding to return pretty-printed JSON."
(format "%s" data))
(defun mock-request (&rest _args)
"Simulate the behavior of `request` function for API calls."
(message "Simulating request call")
;; Return success for testing
(funcall (plist-get _args :success)
:data '((choices . [((message . ((content . "Test response"))))]))))
;; ------------------------------------------
;; Unit Tests for Utility Functions
;; ------------------------------------------
(ert-deftest test-elm--read-auth ()
"Test `elm--read-auth` to ensure it fetches the correct API keys."
(cl-letf (((symbol-function 'auth-source-search) 'mock-auth-source-search))
(should (equal (elm--read-auth :host "elm" :user "CLAUDE") "fake-secret"))
(should-not (elm--read-auth :host "elm" :user "UNKNOWN"))))
(ert-deftest test-elm--create-dir ()
"Test `elm--create-dir` for correct directory and file creation."
(cl-letf (((symbol-function 'file-exists-p) 'mock-file-exists-p)
((symbol-function 'make-directory) 'mock-make-directory)
((symbol-function 'with-temp-file) 'mock-insert-file)
((symbol-function 'json-encode) 'mock-json-encode))
(elm--create-dir)
;; Ensure directory and file creation logic runs
(should (equal (mock-insert-file "test content") "test content"))))
(ert-deftest test-elm--create-url ()
"Test `elm--create-url` to ensure correct URL generation."
(cl-letf (((symbol-function 'elm--read-json)
(lambda ()
'((:claude . (:baseurl "https://api.anthropic.com/v1"
:chaturl "/messages"))
(:groq . (:baseurl "https://api.groq.com/v1"
:chaturl "/chat/completions"))))))
(should (equal (elm--create-url 'claude 'chaturl) "https://api.anthropic.com/v1/messages"))
(should-error (elm--create-url 'unknown 'chaturl))))
(ert-deftest test-elm--extract-urls ()
"Test `elm--extract-urls` to ensure proper URL extraction."
(let ((data '((:claude . (:baseurl "https://api.anthropic.com/v1"
:geturl "/models"))
(:groq . (:baseurl "https://api.groq.com/v1"
:geturl "/models")))))
(should (equal (elm--extract-urls data 'geturl)
'((:claude "https://api.anthropic.com/v1/models")
(:groq "https://api.groq.com/v1/models"))))))
(ert-deftest test-elm--get-api-key ()
"Test `elm--get-api-key` to ensure correct API key retrieval."
(let ((elm--api-keys (make-hash-table :test 'equal)))
(puthash "CLAUDE" "fake-claude-key" elm--api-keys)
(should (equal (elm--get-api-key "CLAUDE") "fake-claude-key"))
(should-error (elm--get-api-key "UNKNOWN"))))
(ert-deftest test-elm--construct-headers ()
"Test `elm--construct-headers` for proper API request headers."
(let ((elm--api-keys (make-hash-table :test 'equal)))
(puthash "CLAUDE" "fake-claude-key" elm--api-keys)
(puthash "GROQ" "fake-groq-key" elm--api-keys)
;; Test for Claude
(should (equal (elm--construct-headers "claude")
'(("Content-Type" . "application/json")
("x-api-key" . "fake-claude-key")
("anthropic-version" . "2023-06-01"))))
;; Test for GROQ
(should (equal (elm--construct-headers "groq")
'(("Content-Type" . "application/json")
("Authorization" . "Bearer fake-groq-key"))))))
(ert-deftest test-elm--construct-content ()
"Test `elm--construct-content` to verify content construction for API."
(setq elm--current-model "claude-3-haiku-20240307"
elm--current-provider "claude")
(let ((content (elm--construct-content "Hello, world")))
(should (equal content
'(("model" . "claude-3-haiku-20240307")
("messages" . ((("role" . "user")
("content" . "Hello, world"))))
("max_tokens" . 1024))))))
;; ------------------------------------------
;; Integration Tests for API Requests
;; ------------------------------------------
(ert-deftest test-elm--process-request ()
"Test `elm--process-request` to ensure correct API interaction."
(cl-letf (((symbol-function 'request) 'mock-request)
((symbol-function 'elm--construct-headers)
(lambda (_) '(("Authorization" . "Bearer fake-key")))))
;; Set up current model/provider for testing
(setq elm--current-provider "groq"
elm--current-model "groq-model-1")
;; Simulate request and success
(elm--process-request "Hello, world")
;; Check output (stdout should simulate request and response handling)
(should (string-match "Simulating request call" (current-message)))))
(ert-deftest test-elm--process-claude-response ()
"Test response parsing for Claude."
(let ((response '((content . [((text . "Response text"))]))))
(with-temp-buffer
(elm--process-claude-response "Test input" response)
(should (equal (buffer-string) "* Test input\n\nResponse text\n")))))
(ert-deftest test-elm--update-model-list ()
"Test `elm--update-model-list` for updating the models file."
(cl-letf (((symbol-function 'elm--read-json)
(lambda ()
'((claude . (:models ["claude-3-haiku"])))))
((symbol-function 'with-temp-file) 'mock-insert-file))
(let ((lisp-data '((models . ["claude-3-haiku" "claude-3-sonnet"]))))
(elm--update-model-list "~/.elm/model.json" 'claude lisp-data)
(should (equal (mock-insert-file "Updated content")
"Updated content")))))
;; ------------------------------------------
;; Interactive Tests
;; ------------------------------------------
(ert-deftest test-elm--select-model ()
"Test `elm--select-model` interactive command."
(cl-letf (((symbol-function 'completing-read)
(lambda (&rest _) "claude-3-haiku"))
((symbol-function 'elm--read-json)
(lambda ()
(let ((table (make-hash-table :test 'equal)))
(puthash "claude" (list :models '("claude-3-haiku" "claude-3-sonnet")) table)
table))))
(call-interactively 'elm--select-model)
(should (equal elm--current-model "claude-3-haiku"))
(should (equal elm--current-provider "claude"))))
(ert-deftest test-elm-rewrite ()
"Test `elm-rewrite` interactive command for sending code rewrite requests."
(cl-letf (((symbol-function 'elm--process-request) 'mock-request))
(with-temp-buffer
(insert "def foo(): pass")
(call-interactively 'elm-rewrite)
;; Check message for mock request
(should (string-match "Simulating request call" (current-message))))))
(ert-deftest test-elm-send-request ()
"Test `elm-send-request` interactive command."
(cl-letf (((symbol-function 'elm--process-request) 'mock-request))
(call-interactively 'elm-send-request)
;; Check the simulated API interaction
(should (string-match "Simulating request call" (current-message)))))
(provide 'elm.test)
;;; elm.test.el ends here