Skip to content

Commit 6c39475

Browse files
committed
Add tests for :twilio/handle-event for twilio-transport-in
1 parent e22f0a8 commit 6c39475

File tree

2 files changed

+53
-15
lines changed

2 files changed

+53
-15
lines changed

core/src/voice_fn/transport.clj

Lines changed: 19 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -111,6 +111,24 @@
111111

112112
:else [state]))}))
113113

114+
(defn twilio-transport-in-transform
115+
[{:twilio/keys [handle-event] :as state} _ input]
116+
(let [data (u/parse-if-json input)
117+
output (if (fn? handle-event) (handle-event data) nil)
118+
out-frames (partial merge-with into output)]
119+
(case (:event data)
120+
"start" [state (if-let [stream-sid (:streamSid data)]
121+
(out-frames {:sys-out [(frame/system-config-change {:twilio/stream-sid stream-sid
122+
:transport/serializer (make-twilio-serializer stream-sid)})]})
123+
(out-frames {}))]
124+
"media"
125+
[state (out-frames {:out [(frame/audio-input-raw
126+
(u/decode-base64 (get-in data [:media :payload])))]})]
127+
128+
"close"
129+
[state (out-frames {:sys-out [(frame/system-stop true)]})]
130+
nil)))
131+
114132
(def twilio-transport-in
115133
(flow/process
116134
{:describe (fn [] {:outs {:sys-out "Channel for system messages that have priority"
@@ -123,18 +141,4 @@
123141
{::flow/in-ports {:twilio-in in-ch}
124142
:twilio/handle-event handle-event})
125143

126-
:transform (fn [{:twilio/keys [handle-event] :as state} _ input]
127-
(let [data (u/parse-if-json input)
128-
output (if (fn? handle-event) (handle-event data) nil)
129-
out-frames (partial merge-with into output)]
130-
(case (:event data)
131-
"start" (when-let [stream-sid (:streamSid data)]
132-
[state (out-frames {:sys-out [(frame/system-config-change {:twilio/stream-sid stream-sid
133-
:transport/serializer (make-twilio-serializer stream-sid)})]})])
134-
"media"
135-
[state (out-frames {:out [(frame/audio-input-raw
136-
(u/decode-base64 (get-in data [:media :payload])))]})]
137-
138-
"close"
139-
[state (out-frames {:sys-out [(frame/system-stop true)]})]
140-
nil)))}))
144+
:transform twilio-transport-in-transform}))
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
(ns voice-fn.transport-test
2+
(:require
3+
[midje.sweet :as midje :refer [fact facts]]
4+
[voice-fn.frame :as frame]
5+
[voice-fn.transport :as sut]
6+
[voice-fn.utils.core :as u]))
7+
8+
(def test-config-change-frame (frame/system-config-change {:llm/context {:messages [{:role :system :content "You are a cool llm"}]}}))
9+
(defn twilio-start-handle-event [event]
10+
(cond
11+
(= (:event event) "start")
12+
{:sys-out [test-config-change-frame]}
13+
:else {}))
14+
15+
(facts
16+
"about twilio transport in"
17+
(fact
18+
"Transport in calls twilio/handle-event if it is provided"
19+
(let [state {:twilio/handle-event twilio-start-handle-event}]
20+
(sut/twilio-transport-in-transform
21+
state
22+
:twilio-in
23+
(u/json-str {:event "start"})) => [state {:sys-out [test-config-change-frame]}]
24+
(fact
25+
"Merges frames in the same array if more are generated"
26+
(let [[new-state {:keys [sys-out]}] (sut/twilio-transport-in-transform
27+
state
28+
:twilio-in
29+
(u/json-str {:event "start"
30+
:streamSid "hello"}))]
31+
new-state => state
32+
(first sys-out) => test-config-change-frame
33+
(frame/system-config-change? (last sys-out)) => true
34+
(:twilio/stream-sid (:frame/data (last sys-out))) => "hello")))))

0 commit comments

Comments
 (0)