Skip to content

Commit e1543d1

Browse files
committed
Improve integration tests
1 parent 9fd1c2c commit e1543d1

File tree

5 files changed

+57
-29
lines changed

5 files changed

+57
-29
lines changed

integration-test/integration/chat_openai_test.clj

Lines changed: 42 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -27,16 +27,16 @@
2727
(llm.mocks/set-case! :simple-text-0)
2828
(let [req-id 0
2929
resp (eca/request! (fixture/chat-prompt-request
30-
{:request-id req-id
31-
:model "gpt-5"
32-
:message "Tell me a joke!"}))
30+
{:request-id req-id
31+
:model "gpt-5"
32+
:message "Tell me a joke!"}))
3333
chat-id (reset! chat-id* (:chatId resp))]
3434

3535
(is (match?
36-
{:chatId (m/pred string?)
37-
:model "gpt-5"
38-
:status "success"}
39-
resp))
36+
{:chatId (m/pred string?)
37+
:model "gpt-5"
38+
:status "success"}
39+
resp))
4040

4141
(match-content chat-id req-id "user" {:type "text" :text "Tell me a joke!\n"})
4242
(match-content chat-id req-id "system" {:type "progress" :state "running" :text "Waiting model"})
@@ -49,23 +49,27 @@
4949
:sessionTokens 30
5050
:messageCost (m/pred string?)
5151
:sessionCost (m/pred string?)})
52-
(match-content chat-id req-id "system" {:type "progress" :state "finished"})))
52+
(match-content chat-id req-id "system" {:type "progress" :state "finished"})
53+
(is (match?
54+
{:input [{:role "user" :content [{:type "input_text" :text "Tell me a joke!"}]}]
55+
:instructions (m/pred string?)}
56+
llm.mocks/*last-req-body*))))
5357

5458
(testing "We reply"
5559
(llm.mocks/set-case! :simple-text-1)
5660
(let [req-id 1
5761
resp (eca/request! (fixture/chat-prompt-request
58-
{:chat-id @chat-id*
59-
:request-id req-id
60-
:model "gpt-5"
61-
:message "Who's there?"}))
62+
{:chat-id @chat-id*
63+
:request-id req-id
64+
:model "gpt-5"
65+
:message "Who's there?"}))
6266
chat-id @chat-id*]
6367

6468
(is (match?
65-
{:chatId (m/pred string?)
66-
:model "gpt-5"
67-
:status "success"}
68-
resp))
69+
{:chatId (m/pred string?)
70+
:model "gpt-5"
71+
:status "success"}
72+
resp))
6973

7074
(match-content chat-id req-id "user" {:type "text" :text "Who's there?\n"})
7175
(match-content chat-id req-id "system" {:type "progress" :state "running" :text "Waiting model"})
@@ -77,23 +81,28 @@
7781
:sessionTokens 45
7882
:messageCost (m/pred string?)
7983
:sessionCost (m/pred string?)})
80-
(match-content chat-id req-id "system" {:type "progress" :state "finished"})))
84+
(match-content chat-id req-id "system" {:type "progress" :state "finished"})
85+
(is (match?
86+
{:input [{:role "user" :content [{:type "input_text" :text "Tell me a joke!"}]}
87+
{:role "assistant" :content [{:type "output_text" :text "Knock knock!"}]}
88+
{:role "user" :content [{:type "input_text" :text "Who's there?"}]}]}
89+
llm.mocks/*last-req-body*))))
8190

8291
(testing "model reply again keeping context"
8392
(llm.mocks/set-case! :simple-text-2)
8493
(let [req-id 2
8594
resp (eca/request! (fixture/chat-prompt-request
86-
{:chat-id @chat-id*
87-
:request-id req-id
88-
:model "gpt-5"
89-
:message "What foo?"}))
95+
{:chat-id @chat-id*
96+
:request-id req-id
97+
:model "gpt-5"
98+
:message "What foo?"}))
9099
chat-id @chat-id*]
91100

92101
(is (match?
93-
{:chatId (m/pred string?)
94-
:model "gpt-5"
95-
:status "success"}
96-
resp))
102+
{:chatId (m/pred string?)
103+
:model "gpt-5"
104+
:status "success"}
105+
resp))
97106

98107
(match-content chat-id req-id "user" {:type "text" :text "What foo?\n"})
99108
(match-content chat-id req-id "system" {:type "progress" :state "running" :text "Waiting model"})
@@ -108,4 +117,11 @@
108117
:sessionTokens 65
109118
:messageCost (m/pred string?)
110119
:sessionCost (m/pred string?)})
111-
(match-content chat-id req-id "system" {:type "progress" :state "finished"})))))
120+
(match-content chat-id req-id "system" {:type "progress" :state "finished"})
121+
(is (match?
122+
{:input [{:role "user" :content [{:type "input_text" :text "Tell me a joke!"}]}
123+
{:role "assistant" :content [{:type "output_text" :text "Knock knock!"}]}
124+
{:role "user" :content [{:type "input_text" :text "Who's there?"}]}
125+
{:role "assistant" :content [{:type "output_text" :text "Foo"}]}
126+
{:role "user" :content [{:type "input_text" :text "What foo?"}]}]}
127+
llm.mocks/*last-req-body*))))))

integration-test/integration/eca.clj

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,8 @@
44
[clojure.core.async :as async]
55
[clojure.java.io :as io]
66
[clojure.test :refer [use-fixtures]]
7-
[integration.client :as client]))
7+
[integration.client :as client]
8+
[llm-mock.mocks :as llm.mocks]))
89

910
(def ^:dynamic *eca-binary-path* nil)
1011
(def ^:dynamic *eca-process* nil)
@@ -33,7 +34,9 @@
3334
(some-> *mock-client* client/shutdown)
3435
(some-> *eca-process* deref) ;; wait for shutdown of client to shutdown server
3536
(alter-var-root #'*eca-process* (constantly nil))
36-
(alter-var-root #'*mock-client* (constantly nil)))
37+
(alter-var-root #'*mock-client* (constantly nil))
38+
(llm.mocks/set-case! nil)
39+
(llm.mocks/set-last-req-body! nil))
3740

3841
(defn clean-after-test []
3942
(use-fixtures :each (fn [f] (clean!) (f)))

integration-test/integration/fixture.clj

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,9 @@
99

1010
(def default-init-options {:pureConfig true
1111
:openaiApiUrl (str base-llm-mock-url "/openai")
12-
:anthropicApiUrl (str base-llm-mock-url "/anthropic")})
12+
:openaiApiKey "foo"
13+
:anthropicApiUrl (str base-llm-mock-url "/anthropic")
14+
:anthropicApiKey "foo"})
1315

1416
(defn initialize-request
1517
([]

integration-test/llm_mock/mocks.clj

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,3 +4,8 @@
44

55
(defn set-case! [case]
66
(alter-var-root #'*case* (constantly case)))
7+
8+
(def ^:dynamic *last-req-body* nil)
9+
10+
(defn set-last-req-body! [body]
11+
(alter-var-root #'*last-req-body* (constantly body)))

integration-test/llm_mock/openai.clj

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,8 @@
5353
(hk/close ch))
5454

5555
(defn handle-openai-responses [req]
56+
(llm.mocks/set-last-req-body! (some-> (slurp (:body req))
57+
(json/parse-string true)))
5658
(hk/as-channel
5759
req
5860
{:on-open (fn [ch]

0 commit comments

Comments
 (0)