|
| 1 | +(ns eca.features.tools.custom-test |
| 2 | + (:require |
| 3 | + [clojure.string :as string] |
| 4 | + [clojure.test :refer [deftest is testing]] |
| 5 | + [babashka.process :as process] |
| 6 | + [eca.features.tools.custom :as f.tools.custom])) |
| 7 | + |
| 8 | +(deftest definitions-test |
| 9 | + (testing "when a valid tool is configured" |
| 10 | + (let [mock-custom-tools {"file-search" |
| 11 | + {:description "Finds files." |
| 12 | + :command ["find" "{{directory}}" "-name" "{{pattern}}"] |
| 13 | + :schema {:properties {:directory {:type "string"} |
| 14 | + :pattern {:type "string"}} |
| 15 | + :required ["directory" "pattern"]}}}] |
| 16 | + (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})] |
| 20 | + (let [config {:custom-tools mock-custom-tools} |
| 21 | + custom-defs (f.tools.custom/definitions config) |
| 22 | + custom-tool-def (get custom-defs "file-search")] |
| 23 | + (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."))))))) |
| 26 | + |
| 27 | + (testing "when multiple tools are configured" |
| 28 | + (let [mock-custom-tools {"git-status" |
| 29 | + {:description "Gets git status" |
| 30 | + :command ["git" "status"]} |
| 31 | + "echo-message" |
| 32 | + {: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")))] |
| 40 | + (let [config {:custom-tools mock-custom-tools} |
| 41 | + custom-defs (f.tools.custom/definitions config) |
| 42 | + git-status-handler (get-in custom-defs ["git-status" :handler]) |
| 43 | + echo-handler (get-in custom-defs ["echo-message" :handler])] |
| 44 | + (is (some? git-status-handler) "Git status tool should be loaded.") |
| 45 | + (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"} {}))))))) |
| 48 | + |
| 49 | + (testing "when the custom tools config is empty or missing" |
| 50 | + (testing "with an empty map" |
| 51 | + (let [config {:custom-tools {}} |
| 52 | + custom-defs (f.tools.custom/definitions config)] |
| 53 | + (is (empty? custom-defs) "No custom tools should be loaded."))) |
| 54 | + (testing "with the key missing from the config" |
| 55 | + (let [config {} |
| 56 | + custom-defs (f.tools.custom/definitions config)] |
| 57 | + (is (empty? custom-defs) "No custom tools should be loaded."))))) |
0 commit comments