|
979 | 979 | :role "assistant"}]} |
980 | 980 | (h/messages))))))) |
981 | 981 |
|
| 982 | +(deftest prompt-cache-agent-switch-test |
| 983 | + (testing "Static prompt cache is rebuilt when switching agents within the same chat" |
| 984 | + (h/reset-components!) |
| 985 | + (let [build-calls* (atom 0) |
| 986 | + real-build f.prompt/build-chat-instructions] |
| 987 | + (with-redefs [f.prompt/build-chat-instructions |
| 988 | + (fn [& args] |
| 989 | + (swap! build-calls* inc) |
| 990 | + (apply real-build args)) |
| 991 | + ;; Test config doesn't populate :agent, so validate-agent-name |
| 992 | + ;; would fall back to "code" for every input. Short-circuit it |
| 993 | + ;; so the test can actually exercise an agent switch. |
| 994 | + config/validate-agent-name (fn [agent-name _config] agent-name)] |
| 995 | + (let [mocks {:all-tools-mock (constantly []) |
| 996 | + :api-mock (fn [{:keys [on-message-received]}] |
| 997 | + (on-message-received {:type :finish}))} |
| 998 | + {:keys [chat-id]} (prompt! {:message "Hi" :agent "code"} mocks)] |
| 999 | + (is (= 1 @build-calls*) |
| 1000 | + "First prompt should build the static instructions once") |
| 1001 | + (h/reset-messenger!) |
| 1002 | + (prompt! {:message "Still code" :chat-id chat-id :agent "code"} mocks) |
| 1003 | + (is (= 1 @build-calls*) |
| 1004 | + "Second prompt with the same agent should reuse cached instructions") |
| 1005 | + (h/reset-messenger!) |
| 1006 | + (prompt! {:message "Switch to plan" :chat-id chat-id :agent "plan"} mocks) |
| 1007 | + (is (= 2 @build-calls*) |
| 1008 | + "Switching agent should invalidate the cache and rebuild instructions") |
| 1009 | + (h/reset-messenger!) |
| 1010 | + (prompt! {:message "Back to code" :chat-id chat-id :agent "code"} mocks) |
| 1011 | + (is (= 3 @build-calls*) |
| 1012 | + "Switching back to the first agent should also trigger a rebuild")))))) |
| 1013 | + |
| 1014 | +(deftest prompt-cache-key-includes-agent-test |
| 1015 | + (testing "sync-or-async-prompt! receives prompt-cache-key scoped by active agent" |
| 1016 | + (h/reset-components!) |
| 1017 | + (with-redefs [config/validate-agent-name (fn [agent-name _config] agent-name)] |
| 1018 | + (let [captured* (atom []) |
| 1019 | + mocks {:all-tools-mock (constantly []) |
| 1020 | + :api-mock (fn [{:keys [on-message-received] :as params}] |
| 1021 | + (swap! captured* conj (:prompt-cache-key params)) |
| 1022 | + (on-message-received {:type :finish}))} |
| 1023 | + {:keys [chat-id]} (prompt! {:message "hi" :agent "code"} mocks)] |
| 1024 | + (h/reset-messenger!) |
| 1025 | + (prompt! {:message "hello" :chat-id chat-id :agent "plan"} mocks) |
| 1026 | + (is (= 2 (count @captured*))) |
| 1027 | + (is (every? some? @captured*)) |
| 1028 | + (is (string/ends-with? (first @captured*) "/code") |
| 1029 | + "First prompt's cache key should be suffixed by /code") |
| 1030 | + (is (string/ends-with? (second @captured*) "/plan") |
| 1031 | + "Second prompt's cache key should be suffixed by /plan"))))) |
| 1032 | + |
982 | 1033 |
|
0 commit comments