|
4 | 4 | [eca.features.chat :as f.chat] |
5 | 5 | [eca.llm-api :as llm-api] |
6 | 6 | [eca.test-helper :as h] |
| 7 | + [matcher-combinators.matchers :as m] |
7 | 8 | [matcher-combinators.test :refer [match?]])) |
8 | 9 |
|
9 | 10 | (h/reset-components-before-test) |
10 | 11 |
|
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 |
12 | 22 | (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})))] |
32 | 34 | (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!"}]}} |
34 | 38 | (:chats (h/db)))) |
35 | 39 | (is (match? |
36 | 40 | {:chat-content-received |
|
53 | 57 | {:chat-id chat-id |
54 | 58 | :request-id req-id |
55 | 59 | :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}]} |
57 | 95 | (h/messages)))))) |
0 commit comments