Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 7 additions & 1 deletion src/yetibot/core/webapp/routes/api.clj
Original file line number Diff line number Diff line change
Expand Up @@ -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))))
Expand Down
20 changes: 19 additions & 1 deletion test/yetibot/core/test/webapp/routes/api.clj
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -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))))