|
330 | 330 | {:type "tool_result" :tool_use_id "c2" :content "content-b\n"}]}] |
331 | 331 | result))))) |
332 | 332 |
|
| 333 | +(deftest cache-control-value-test |
| 334 | + (let [cache-control #'llm-providers.anthropic/cache-control-value] |
| 335 | + (testing "default 5-min TTL when no cache-retention set" |
| 336 | + (is (= {:type "ephemeral"} (cache-control "https://api.anthropic.com" nil))) |
| 337 | + (is (= {:type "ephemeral"} (cache-control nil nil)))) |
| 338 | + (testing "default 5-min TTL for short retention" |
| 339 | + (is (= {:type "ephemeral"} (cache-control "https://api.anthropic.com" "short")))) |
| 340 | + (testing "1-hour TTL for long retention on direct Anthropic API" |
| 341 | + (is (= {:type "ephemeral" :ttl "1h"} (cache-control "https://api.anthropic.com" "long"))) |
| 342 | + (is (= {:type "ephemeral" :ttl "1h"} (cache-control "https://api.anthropic.com/v1" "long")))) |
| 343 | + (testing "1-hour TTL when api-url is nil (default direct API)" |
| 344 | + (is (= {:type "ephemeral" :ttl "1h"} (cache-control nil "long")))) |
| 345 | + (testing "falls back to 5-min when using a proxy" |
| 346 | + (is (= {:type "ephemeral"} (cache-control "https://my-proxy.example.com" "long")))))) |
| 347 | + |
333 | 348 | (deftest add-cache-to-last-message-test |
334 | | - (is (match? |
335 | | - [] |
336 | | - (#'llm-providers.anthropic/add-cache-to-last-message []))) |
337 | | - (testing "when message content is a vector" |
338 | | - (is (match? |
339 | | - [{:role "user" :content [{:type :text :text "Hey" :cache_control {:type "ephemeral"}}]}] |
340 | | - (#'llm-providers.anthropic/add-cache-to-last-message |
341 | | - [{:role "user" :content [{:type :text :text "Hey"}]}]))) |
342 | | - (is (match? |
343 | | - [{:role "user" :content [{:type :text :text "Hey"}]} |
344 | | - {:role "user" :content [{:type :text :text "Ho" :cache_control {:type "ephemeral"}}]}] |
345 | | - (#'llm-providers.anthropic/add-cache-to-last-message |
346 | | - [{:role "user" :content [{:type :text :text "Hey"}]} |
347 | | - {:role "user" :content [{:type :text :text "Ho"}]}])))) |
348 | | - (testing "when message content is string" |
349 | | - (is (match? |
350 | | - [{:role "user" :content [{:type :text :text "Hey" :cache_control {:type "ephemeral"}}]}] |
351 | | - (#'llm-providers.anthropic/add-cache-to-last-message |
352 | | - [{:role "user" :content "Hey"}]))) |
| 349 | + (let [default-cache {:type "ephemeral"}] |
353 | 350 | (is (match? |
354 | | - [{:role "user" :content "Hey"} |
355 | | - {:role "user" :content [{:type :text :text "Ho" :cache_control {:type "ephemeral"}}]}] |
356 | | - (#'llm-providers.anthropic/add-cache-to-last-message |
357 | | - [{:role "user" :content "Hey"} |
358 | | - {:role "user" :content "Ho"}]))))) |
| 351 | + [] |
| 352 | + (#'llm-providers.anthropic/add-cache-to-last-message [] default-cache))) |
| 353 | + (testing "when message content is a vector" |
| 354 | + (is (match? |
| 355 | + [{:role "user" :content [{:type :text :text "Hey" :cache_control {:type "ephemeral"}}]}] |
| 356 | + (#'llm-providers.anthropic/add-cache-to-last-message |
| 357 | + [{:role "user" :content [{:type :text :text "Hey"}]}] default-cache))) |
| 358 | + (is (match? |
| 359 | + [{:role "user" :content [{:type :text :text "Hey"}]} |
| 360 | + {:role "user" :content [{:type :text :text "Ho" :cache_control {:type "ephemeral"}}]}] |
| 361 | + (#'llm-providers.anthropic/add-cache-to-last-message |
| 362 | + [{:role "user" :content [{:type :text :text "Hey"}]} |
| 363 | + {:role "user" :content [{:type :text :text "Ho"}]}] default-cache)))) |
| 364 | + (testing "when message content is string" |
| 365 | + (is (match? |
| 366 | + [{:role "user" :content [{:type :text :text "Hey" :cache_control {:type "ephemeral"}}]}] |
| 367 | + (#'llm-providers.anthropic/add-cache-to-last-message |
| 368 | + [{:role "user" :content "Hey"}] default-cache))) |
| 369 | + (is (match? |
| 370 | + [{:role "user" :content "Hey"} |
| 371 | + {:role "user" :content [{:type :text :text "Ho" :cache_control {:type "ephemeral"}}]}] |
| 372 | + (#'llm-providers.anthropic/add-cache-to-last-message |
| 373 | + [{:role "user" :content "Hey"} |
| 374 | + {:role "user" :content "Ho"}] default-cache)))) |
| 375 | + (testing "with 1-hour TTL" |
| 376 | + (let [long-cache {:type "ephemeral" :ttl "1h"}] |
| 377 | + (is (match? |
| 378 | + [{:role "user" :content [{:type :text :text "Hey" :cache_control {:type "ephemeral" :ttl "1h"}}]}] |
| 379 | + (#'llm-providers.anthropic/add-cache-to-last-message |
| 380 | + [{:role "user" :content [{:type :text :text "Hey"}]}] long-cache))))))) |
359 | 381 |
|
360 | 382 | (deftest add-cache-to-last-tool-test |
361 | | - (let [add-cache #'llm-providers.anthropic/add-cache-to-last-tool] |
| 383 | + (let [default-cache {:type "ephemeral"} |
| 384 | + add-cache (fn [tools] (#'llm-providers.anthropic/add-cache-to-last-tool tools default-cache))] |
362 | 385 | (testing "empty tools returns empty" |
363 | 386 | (is (match? [] (add-cache []))) |
364 | 387 | (is (match? nil (add-cache nil)))) |
|
0 commit comments