Skip to content

Commit fa13b66

Browse files
committed
Fix tests
1 parent 540cdcc commit fa13b66

File tree

13 files changed

+145
-169
lines changed

13 files changed

+145
-169
lines changed

integration-test/entrypoint.clj

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
[llm-mock.server :as llm-mock.server]))
66

77
(def namespaces
8-
'[;integration.initialize-test
8+
'[integration.initialize-test
99
integration.chat.openai-test
1010
integration.chat.anthropic-test
1111
integration.chat.github-copilot-test

integration-test/integration/chat/anthropic_test.clj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -140,6 +140,7 @@
140140
:lastMessageCost (m/pred string?)
141141
:sessionCost (m/pred string?)})
142142
(match-content chat-id "system" {:type "progress" :state "finished"})
143+
(match-content chat-id "system" {:type "metadata" :title "Some Cool Title"})
143144
(is (match?
144145
{:messages [{:role "user" :content [{:type "text" :text "hello!"}]}]
145146
:system (m/pred vector?)}

integration-test/integration/chat/github_copilot_test.clj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,7 @@
5959
(match-content chat-id "system" {:type "progress" :state "running" :text "Generating"})
6060
(match-content chat-id "assistant" {:type "text" :text "Foo"})
6161
(match-content chat-id "system" {:type "progress" :state "finished"})
62+
(match-content chat-id "system" {:type "metadata" :title "Some Cool Title"})
6263
(is (match?
6364
{:input [{:role "user" :content [{:type "input_text" :text "Tell me a joke!"}]}
6465
{:role "assistant" :content [{:type "output_text" :text "Knock knock!"}]}

integration-test/integration/chat/openai_test.clj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@
3939
:lastMessageCost (m/pred string?)
4040
:sessionCost (m/pred string?)})
4141
(match-content chat-id "system" {:type "progress" :state "finished"})
42+
(match-content chat-id "system" {:type "metadata" :title "Some Cool Title"})
4243
(is (match?
4344
{:input [{:role "user" :content [{:type "input_text" :text "Tell me a joke!"}]}]
4445
:instructions (m/pred string?)}

integration-test/llm_mock/anthropic.clj

Lines changed: 36 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
(ns llm-mock.anthropic
22
(:require
33
[cheshire.core :as json]
4+
[clojure.string :as string]
45
[integration.helper :as h]
56
[llm-mock.mocks :as llm.mocks]
67
[org.httpkit.server :as hk]))
@@ -205,21 +206,39 @@
205206
:output_tokens 30}})
206207
(hk/close ch)))))
207208

209+
(defn ^:private chat-title-text-0 [ch]
210+
(sse-send! ch "response.output_text.delta"
211+
{:type "response.output_text.delta" :delta "Some Cool"})
212+
(sse-send! ch "response.output_text.delta"
213+
{:type "response.output_text.delta" :delta " Title"})
214+
(sse-send! ch "response.completed"
215+
{:type "response.completed"
216+
:response {:output []
217+
:usage {:input_tokens 5
218+
:output_tokens 10}
219+
:status "completed"}})
220+
(hk/close ch))
221+
208222
(defn handle-anthropic-messages [req]
209-
(llm.mocks/set-last-req-body! (some-> (slurp (:body req))
210-
(json/parse-string true)))
211-
(hk/as-channel
212-
req
213-
{:on-open (fn [ch]
214-
(hk/send! ch {:status 200
215-
:headers {"Content-Type" "text/event-stream; charset=utf-8"
216-
"Cache-Control" "no-cache"
217-
"Connection" "keep-alive"}}
218-
false)
219-
(case llm.mocks/*case*
220-
:simple-text-0 (simple-text-0 ch)
221-
:simple-text-1 (simple-text-1 ch)
222-
:simple-text-2 (simple-text-2 ch)
223-
:reasoning-0 (reasoning-0 ch)
224-
:reasoning-1 (reasoning-1 ch)
225-
:tool-calling-0 (tool-calling-0 ch)))}))
223+
(let [body (some-> (slurp (:body req))
224+
(json/parse-string true))]
225+
(llm.mocks/set-last-req-body! body)
226+
(hk/as-channel
227+
req
228+
{:on-open (fn [ch]
229+
(hk/send! ch {:status 200
230+
:headers {"Content-Type" "text/event-stream; charset=utf-8"
231+
"Cache-Control" "no-cache"
232+
"Connection" "keep-alive"}}
233+
false)
234+
(if (string/includes? (:text (last (:system body))) llm.mocks/chat-title-generator-str)
235+
(do
236+
(Thread/sleep 2000) ;; avoid tests failing with mismatch order of contents
237+
(chat-title-text-0 ch))
238+
(case llm.mocks/*case*
239+
:simple-text-0 (simple-text-0 ch)
240+
:simple-text-1 (simple-text-1 ch)
241+
:simple-text-2 (simple-text-2 ch)
242+
:reasoning-0 (reasoning-0 ch)
243+
:reasoning-1 (reasoning-1 ch)
244+
:tool-calling-0 (tool-calling-0 ch))))})))

integration-test/llm_mock/copilot.clj

Lines changed: 0 additions & 88 deletions
This file was deleted.

integration-test/llm_mock/mocks.clj

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,3 +9,5 @@
99

1010
(defn set-last-req-body! [body]
1111
(alter-var-root #'*last-req-body* (constantly body)))
12+
13+
(def chat-title-generator-str "Title generator")

integration-test/llm_mock/ollama.clj

Lines changed: 30 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
(ns llm-mock.ollama
22
(:require
33
[cheshire.core :as json]
4+
[clojure.string :as string]
45
[integration.helper :as h]
56
[llm-mock.mocks :as llm.mocks]
67
[org.httpkit.server :as hk]))
@@ -62,24 +63,36 @@
6263
(sse-send! ch {:done_reason "stop"})
6364
(hk/close ch)))))
6465

66+
(defn ^:private chat-title-text-0 [ch]
67+
(sse-send! ch {:message {:thinking "Some Cool"}})
68+
(sse-send! ch {:message {:thinking " Title"}})
69+
(sse-send! ch {:done_reason "stop"})
70+
(hk/close ch))
71+
6572
(defn handle-ollama-chat [req]
66-
(llm.mocks/set-last-req-body! (some-> (slurp (:body req))
67-
(json/parse-string true)))
68-
(hk/as-channel
69-
req
70-
{:on-open (fn [ch]
71-
(hk/send! ch {:status 200
72-
:headers {"Content-Type" "text/event-stream; charset=utf-8"
73-
"Cache-Control" "no-cache"
74-
"Connection" "keep-alive"}}
75-
false)
76-
(case llm.mocks/*case*
77-
:simple-text-0 (simple-text-0 ch)
78-
:simple-text-1 (simple-text-1 ch)
79-
:simple-text-2 (simple-text-2 ch)
80-
:reasoning-0 (reasoning-0 ch)
81-
:reasoning-1 (reasoning-1 ch)
82-
:tool-calling-0 (tool-calling-0 ch)))}))
73+
(let [body-str (slurp (:body req))
74+
body (some-> body-str
75+
(json/parse-string true))]
76+
(llm.mocks/set-last-req-body! body)
77+
(hk/as-channel
78+
req
79+
{:on-open (fn [ch]
80+
(hk/send! ch {:status 200
81+
:headers {"Content-Type" "text/event-stream; charset=utf-8"
82+
"Cache-Control" "no-cache"
83+
"Connection" "keep-alive"}}
84+
false)
85+
(if (string/includes? (:content (first (:messages body))) llm.mocks/chat-title-generator-str)
86+
(do
87+
(Thread/sleep 2000) ;; avoid tests failing with mismatch order of contents
88+
(chat-title-text-0 ch))
89+
(case llm.mocks/*case*
90+
:simple-text-0 (simple-text-0 ch)
91+
:simple-text-1 (simple-text-1 ch)
92+
:simple-text-2 (simple-text-2 ch)
93+
:reasoning-0 (reasoning-0 ch)
94+
:reasoning-1 (reasoning-1 ch)
95+
:tool-calling-0 (tool-calling-0 ch))))})))
8396

8497
(defn handle-ollama-tags [_req]
8598
{:status 200

integration-test/llm_mock/openai.clj

Lines changed: 37 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
(ns llm-mock.openai
22
(:require
33
[cheshire.core :as json]
4+
[clojure.string :as string]
45
[integration.helper :as h]
56
[llm-mock.mocks :as llm.mocks]
67
[org.httpkit.server :as hk]))
@@ -179,22 +180,40 @@
179180
:status "completed"}})
180181
(hk/close ch)))))
181182

183+
(defn ^:private chat-title-text-0 [ch]
184+
(sse-send! ch "response.output_text.delta"
185+
{:type "response.output_text.delta" :delta "Some Cool"})
186+
(sse-send! ch "response.output_text.delta"
187+
{:type "response.output_text.delta" :delta " Title"})
188+
(sse-send! ch "response.completed"
189+
{:type "response.completed"
190+
:response {:output []
191+
:usage {:input_tokens 5
192+
:output_tokens 10}
193+
:status "completed"}})
194+
(hk/close ch))
195+
182196
(defn handle-openai-responses [req]
183-
(llm.mocks/set-last-req-body! (some-> (slurp (:body req))
184-
(json/parse-string true)))
185-
(hk/as-channel
186-
req
187-
{:on-open (fn [ch]
188-
;; initial SSE handshake
189-
(hk/send! ch {:status 200
190-
:headers {"Content-Type" "text/event-stream; charset=utf-8"
191-
"Cache-Control" "no-cache"
192-
"Connection" "keep-alive"}}
193-
false)
194-
(case llm.mocks/*case*
195-
:simple-text-0 (simple-text-0 ch)
196-
:simple-text-1 (simple-text-1 ch)
197-
:simple-text-2 (simple-text-2 ch)
198-
:reasoning-0 (reasoning-0 ch)
199-
:reasoning-1 (reasoning-1 ch)
200-
:tool-calling-0 (tool-calling-0 ch)))}))
197+
(let [body (some-> (slurp (:body req))
198+
(json/parse-string true))]
199+
(llm.mocks/set-last-req-body! body)
200+
(hk/as-channel
201+
req
202+
{:on-open (fn [ch]
203+
;; initial SSE handshake
204+
(hk/send! ch {:status 200
205+
:headers {"Content-Type" "text/event-stream; charset=utf-8"
206+
"Cache-Control" "no-cache"
207+
"Connection" "keep-alive"}}
208+
false)
209+
(if (string/includes? (:instructions body) llm.mocks/chat-title-generator-str)
210+
(do
211+
(Thread/sleep 2000) ;; avoid tests failing with mismatch order of contents
212+
(chat-title-text-0 ch))
213+
(case llm.mocks/*case*
214+
:simple-text-0 (simple-text-0 ch)
215+
:simple-text-1 (simple-text-1 ch)
216+
:simple-text-2 (simple-text-2 ch)
217+
:reasoning-0 (reasoning-0 ch)
218+
:reasoning-1 (reasoning-1 ch)
219+
:tool-calling-0 (tool-calling-0 ch))))})))

integration-test/llm_mock/openai_chat.clj

Lines changed: 33 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -62,27 +62,38 @@
6262
(send-sse! ch {:choices [{:delta {} :finish_reason "stop"}]})
6363
(hk/close ch))
6464

65+
(defn ^:private chat-title-text-0 [ch]
66+
(send-sse! ch {:choices [{:delta {:content "Some Cool"}}]})
67+
(send-sse! ch {:choices [{:delta {:content " Title"}}]})
68+
(send-sse! ch {:usage {:prompt_tokens 5 :completion_tokens 10}})
69+
(send-sse! ch {:choices [{:delta {} :finish_reason "stop"}]})
70+
(hk/close ch))
71+
6572
(defn handle-openai-chat [req]
6673
;; Capture and normalize the request body for assertions in tests
67-
(when-let [body (some-> (slurp (:body req)) (json/parse-string true))]
68-
(let [messages (:messages body)
69-
normalized (messages->normalized-input messages)]
70-
(llm.mocks/set-last-req-body! (merge normalized (select-keys body [:tools])))))
71-
(hk/as-channel
72-
req
73-
{:on-open (fn [ch]
74-
;; Send initial response headers for SSE
75-
(hk/send! ch {:status 200
76-
:headers {"Content-Type" "text/event-stream; charset=utf-8"
77-
"Cache-Control" "no-cache"
78-
"Connection" "keep-alive"}}
79-
false)
80-
(case llm.mocks/*case*
81-
:simple-text-0 (simple-text-0 ch)
82-
:simple-text-1 (simple-text-1 ch)
83-
:simple-text-2 (simple-text-2 ch)
84-
;; default fallback
85-
(do
86-
(send-sse! ch {:choices [{:delta {:content "hello"}}]})
87-
(send-sse! ch {:choices [{:delta {} :finish_reason "stop"}]})
88-
(hk/close ch))))}))
74+
(let [body (some-> (slurp (:body req)) (json/parse-string true))
75+
messages (:messages body)
76+
normalized (messages->normalized-input messages)]
77+
(llm.mocks/set-last-req-body! (merge normalized (select-keys body [:tools])))
78+
(hk/as-channel
79+
req
80+
{:on-open (fn [ch]
81+
;; Send initial response headers for SSE
82+
(hk/send! ch {:status 200
83+
:headers {"Content-Type" "text/event-stream; charset=utf-8"
84+
"Cache-Control" "no-cache"
85+
"Connection" "keep-alive"}}
86+
false)
87+
(if (string/includes? (:content (first (:messages body))) llm.mocks/chat-title-generator-str)
88+
(do
89+
(Thread/sleep 2000) ;; avoid tests failing with mismatch order of contents
90+
(chat-title-text-0 ch))
91+
(case llm.mocks/*case*
92+
:simple-text-0 (simple-text-0 ch)
93+
:simple-text-1 (simple-text-1 ch)
94+
:simple-text-2 (simple-text-2 ch)
95+
;; default fallback
96+
(do
97+
(send-sse! ch {:choices [{:delta {:content "hello"}}]})
98+
(send-sse! ch {:choices [{:delta {} :finish_reason "stop"}]})
99+
(hk/close ch)))))})))

0 commit comments

Comments
 (0)