diff --git a/src/yetibot/core/webapp/routes/api.clj b/src/yetibot/core/webapp/routes/api.clj index 2046689b..82fc3153 100644 --- a/src/yetibot/core/webapp/routes/api.clj +++ b/src/yetibot/core/webapp/routes/api.clj @@ -18,7 +18,13 @@ *target* (:room chat-source)] (info "chat-source" chat-source) (let [user {:username "api"} - res (or text (handle-unparsed-expr chat-source user command))] + raw-res (or text (handle-unparsed-expr chat-source user command)) + res (if (map? raw-res) + (cond + (contains? raw-res :value) (:value raw-res) + (contains? raw-res :error) (:error raw-res) + :else raw-res) + raw-res)] (chat-data-structure res) res)) (str "invalid chat-source:" chat-source)))) diff --git a/test/yetibot/core/test/webapp/routes/api.clj b/test/yetibot/core/test/webapp/routes/api.clj index 642fc2ca..41da0292 100644 --- a/test/yetibot/core/test/webapp/routes/api.clj +++ b/test/yetibot/core/test/webapp/routes/api.clj @@ -1,6 +1,7 @@ (ns yetibot.core.test.webapp.routes.api (:require [yetibot.core.webapp.routes.api :refer [api]] [yetibot.core.chat :refer [chat-data-structure]] + [yetibot.core.handler :refer [handle-unparsed-expr]] [midje.sweet :refer [=> fact facts contains provided anything]])) (facts @@ -21,4 +22,21 @@ "will return :text when :chat-source is legit, which is almost always as long as it is not empty/nil and not malformed" (api good-cs req) => (:text good-cs) - (provided (chat-data-structure anything) => nil)))) + (provided (chat-data-structure anything) => nil))) + + (let [command-cs {:chat-source "{:uuid \"C123\" :room \"#mychan\"}" + :command "echo hello"} + req "/api"] + (fact + "will evaluate command and return its extracted :value" + (api command-cs req) => "hello from command" + (provided + (handle-unparsed-expr anything anything "echo hello") => {:settings {} :skip-next-n 0 :value "hello from command" :data nil} + (chat-data-structure "hello from command") => nil)) + + (fact + "will evaluate command and return its extracted :error on failure" + (api command-cs req) => "error occurred" + (provided + (handle-unparsed-expr anything anything "echo hello") => {:error "error occurred"} + (chat-data-structure "error occurred") => nil))))