|
1 | 1 | (ns eca.features.tools.custom-test |
2 | 2 | (:require |
3 | | - [clojure.string :as string] |
| 3 | + [babashka.process :as p] |
4 | 4 | [clojure.test :refer [deftest is testing]] |
5 | | - [babashka.process :as process] |
6 | 5 | [eca.features.tools.custom :as f.tools.custom])) |
7 | 6 |
|
8 | 7 | (deftest definitions-test |
9 | 8 | (testing "when a valid tool is configured" |
10 | 9 | (let [mock-custom-tools {"file-search" |
11 | 10 | {:description "Finds files." |
12 | | - :command ["find" "{{directory}}" "-name" "{{pattern}}"] |
13 | | - :schema {:properties {:directory {:type "string"} |
14 | | - :pattern {:type "string"}} |
| 11 | + :command "find {{directory}} -name {{pattern}}" |
| 12 | + :schema {:properties {"directory" {:type "string"} |
| 13 | + "pattern" {:type "string"}} |
15 | 14 | :required ["directory" "pattern"]}}}] |
16 | 15 | (testing "and the command executes successfully" |
17 | | - (with-redefs [process/sh (fn [command-vec & _] |
18 | | - (is (= ["find" "/tmp" "-name" "*.clj"] command-vec)) |
19 | | - {:out "mocked-output" :exit 0})] |
| 16 | + (with-redefs [p/shell (fn [_opts _bash command] |
| 17 | + (is (= "find /tmp -name *.clj" command)) |
| 18 | + {:out "mocked-output" :exit 0})] |
20 | 19 | (let [config {:customTools mock-custom-tools} |
21 | 20 | custom-defs (f.tools.custom/definitions config) |
22 | 21 | custom-tool-def (get custom-defs "file-search")] |
23 | 22 | (is (some? custom-tool-def) "The custom tool should be loaded.") |
24 | | - (let [result ((:handler custom-tool-def) {:directory "/tmp" :pattern "*.clj"} {})] |
25 | | - (is (= "mocked-output" result) "The tool should return the mocked shell output."))))))) |
| 23 | + (let [result ((:handler custom-tool-def) {"directory" "/tmp" "pattern" "*.clj"} {})] |
| 24 | + (is (= {:contents [{:text "mocked-output", :type :text}], :error false} result) "The tool should return the mocked shell output."))))))) |
26 | 25 |
|
27 | 26 | (testing "when multiple tools are configured" |
28 | 27 | (let [mock-custom-tools {"git-status" |
29 | 28 | {:description "Gets git status" |
30 | | - :command ["git" "status"]} |
| 29 | + :command "git status"} |
31 | 30 | "echo-message" |
32 | 31 | {:description "Echoes a message" |
33 | | - :command ["echo" "{{message}}"] |
34 | | - :schema {:properties {:message {:type "string"}} :required ["message"]}}}] |
35 | | - (with-redefs [process/sh (fn [command-vec & _] |
36 | | - (condp = command-vec |
37 | | - ["git" "status"] {:out "On branch main" :exit 0} |
38 | | - ["echo" "Hello World"] {:out "Hello World" :exit 0} |
39 | | - (is false "Unexpected command received by mock p/sh")))] |
| 32 | + :command "echo {{message}}" |
| 33 | + :schema {:properties {"message" {:type "string"}} :required ["message"]}}}] |
| 34 | + (with-redefs [p/shell (fn [_opts _bash command] |
| 35 | + (condp = command |
| 36 | + "git status" {:out "On branch main" :exit 0} |
| 37 | + "echo Hello World" {:out "Hello World" :exit 0} |
| 38 | + (is false "Unexpected command received by mock p/sh")))] |
40 | 39 | (let [config {:customTools mock-custom-tools} |
41 | 40 | custom-defs (f.tools.custom/definitions config) |
42 | 41 | git-status-handler (get-in custom-defs ["git-status" :handler]) |
43 | 42 | echo-handler (get-in custom-defs ["echo-message" :handler])] |
44 | 43 | (is (some? git-status-handler) "Git status tool should be loaded.") |
45 | 44 | (is (some? echo-handler) "Echo message tool should be loaded.") |
46 | | - (is (= "On branch main" (git-status-handler {} {}))) |
47 | | - (is (= "Hello World" (echo-handler {:message "Hello World"} {}))))))) |
| 45 | + (is (= {:contents [{:text "On branch main", :type :text}], :error false} (git-status-handler {} {}))) |
| 46 | + (is (= {:contents [{:text "Hello World", :type :text}], :error false} (echo-handler {"message" "Hello World"} {}))))))) |
48 | 47 |
|
49 | 48 | (testing "when the custom tools config is empty or missing" |
50 | 49 | (testing "with an empty map" |
|
0 commit comments