Skip to content

Commit 00989ed

Browse files
committed
Improve chat tests
1 parent 052b37b commit 00989ed

2 files changed

Lines changed: 61 additions & 23 deletions

File tree

src/eca/llm_api.clj

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -86,4 +86,4 @@
8686
:on-tool-called on-tool-called})
8787

8888
:else
89-
(on-error {:msg (str "ECA Unsupported model: " model)}))))
89+
(on-error-wrapper {:msg (str "ECA Unsupported model: " model)}))))

test/eca/features/chat_test.clj

Lines changed: 60 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -4,33 +4,37 @@
44
[eca.features.chat :as f.chat]
55
[eca.llm-api :as llm-api]
66
[eca.test-helper :as h]
7+
[matcher-combinators.matchers :as m]
78
[matcher-combinators.test :refer [match?]]))
89

910
(h/reset-components-before-test)
1011

11-
(deftest prompt-test
12+
(defn ^:private complete! [params api-mock-fn]
13+
(let [req-id (:request-id params)
14+
{:keys [chat-id] :as resp}
15+
(with-redefs [llm-api/complete! api-mock-fn]
16+
(f.chat/prompt params (h/db*) (h/messenger) {}))]
17+
(is (match? {:chat-id string? :status :success} resp))
18+
{:chat-id chat-id
19+
:req-id req-id}))
20+
21+
(deftest basic-prompt-test
1222
(testing "Simple hello"
13-
(let [req-id "1"
14-
{:keys [chat-id] :as resp}
15-
(with-redefs [llm-api/complete!
16-
(fn [{:keys [on-first-message-received
17-
on-message-received]}]
18-
(on-first-message-received {:type :text :text "Hey"})
19-
(on-message-received {:type :text :text "Hey"})
20-
(on-message-received {:type :text :text " you!"}))]
21-
(f.chat/prompt
22-
{:message "Hey!"
23-
:model "o4-mini"
24-
:request-id req-id}
25-
(h/db*)
26-
(h/messenger)
27-
{}))]
28-
(is (match?
29-
{:chat-id string?
30-
:status :success}
31-
resp))
23+
(h/reset-components!)
24+
(let [{:keys [chat-id req-id]}
25+
(complete!
26+
{:message "Hey!"
27+
:request-id "1"}
28+
(fn [{:keys [on-first-message-received
29+
on-message-received]}]
30+
(on-first-message-received {:type :text :text "Hey"})
31+
(on-message-received {:type :text :text "Hey"})
32+
(on-message-received {:type :text :text " you!"})
33+
(on-message-received {:type :finish})))]
3234
(is (match?
33-
{chat-id {:id chat-id}}
35+
{chat-id {:id chat-id
36+
:messages [{:role "user" :content "Hey!"}
37+
{:role "assistant" :content "Hey you!"}]}}
3438
(:chats (h/db))))
3539
(is (match?
3640
{:chat-content-received
@@ -53,5 +57,39 @@
5357
{:chat-id chat-id
5458
:request-id req-id
5559
:content {:type :text :text " you!"}
56-
:role :assistant}]}
60+
:role :assistant}
61+
{:chat-id chat-id
62+
:request-id req-id
63+
:content {:state :finished :type :progress}
64+
:role :system}]}
65+
(h/messages)))))
66+
(testing "LLM error"
67+
(h/reset-components!)
68+
(let [{:keys [chat-id req-id]}
69+
(complete!
70+
{:message "Hey!"
71+
:request-id "1"}
72+
(fn [{:keys [on-error]}]
73+
(on-error {:message "Error from mocked API"})))]
74+
(is (match?
75+
{chat-id {:id chat-id :messages m/absent}}
76+
(:chats (h/db))))
77+
(is (match?
78+
{:chat-content-received
79+
[{:chat-id chat-id
80+
:request-id req-id
81+
:content {:type :text :text "Hey!\n"}
82+
:role :user}
83+
{:chat-id chat-id
84+
:request-id req-id
85+
:content {:type :progress :state :running :text "Waiting model"}
86+
:role :system}
87+
{:chat-id chat-id
88+
:request-id req-id
89+
:content {:type :text :text "Error from mocked API\n"}
90+
:role :system}
91+
{:chat-id chat-id
92+
:request-id req-id
93+
:content {:state :finished :type :progress}
94+
:role :system}]}
5795
(h/messages))))))

0 commit comments

Comments
 (0)