Skip to content

Commit 3f26dec

Browse files
fix: extract value or error from api response map to avoid printing accumulator (#265)
1 parent a3d9c4d commit 3f26dec

2 files changed

Lines changed: 26 additions & 2 deletions

File tree

  • src/yetibot/core/webapp/routes
  • test/yetibot/core/test/webapp/routes

src/yetibot/core/webapp/routes/api.clj

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,13 @@
1818
*target* (:room chat-source)]
1919
(info "chat-source" chat-source)
2020
(let [user {:username "api"}
21-
res (or text (handle-unparsed-expr chat-source user command))]
21+
raw-res (or text (handle-unparsed-expr chat-source user command))
22+
res (if (map? raw-res)
23+
(cond
24+
(contains? raw-res :value) (:value raw-res)
25+
(contains? raw-res :error) (:error raw-res)
26+
:else raw-res)
27+
raw-res)]
2228
(chat-data-structure res)
2329
res))
2430
(str "invalid chat-source:" chat-source))))

test/yetibot/core/test/webapp/routes/api.clj

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
(ns yetibot.core.test.webapp.routes.api
22
(:require [yetibot.core.webapp.routes.api :refer [api]]
33
[yetibot.core.chat :refer [chat-data-structure]]
4+
[yetibot.core.handler :refer [handle-unparsed-expr]]
45
[midje.sweet :refer [=> fact facts contains provided anything]]))
56

67
(facts
@@ -21,4 +22,21 @@
2122
"will return :text when :chat-source is legit, which is almost always
2223
as long as it is not empty/nil and not malformed"
2324
(api good-cs req) => (:text good-cs)
24-
(provided (chat-data-structure anything) => nil))))
25+
(provided (chat-data-structure anything) => nil)))
26+
27+
(let [command-cs {:chat-source "{:uuid \"C123\" :room \"#mychan\"}"
28+
:command "echo hello"}
29+
req "/api"]
30+
(fact
31+
"will evaluate command and return its extracted :value"
32+
(api command-cs req) => "hello from command"
33+
(provided
34+
(handle-unparsed-expr anything anything "echo hello") => {:settings {} :skip-next-n 0 :value "hello from command" :data nil}
35+
(chat-data-structure "hello from command") => nil))
36+
37+
(fact
38+
"will evaluate command and return its extracted :error on failure"
39+
(api command-cs req) => "error occurred"
40+
(provided
41+
(handle-unparsed-expr anything anything "echo hello") => {:error "error occurred"}
42+
(chat-data-structure "error occurred") => nil))))

0 commit comments

Comments
 (0)