Packages

This repo contains a testing library used in Carla end-to-end tests. It provides a library to describe and test conversations.

Current section

Files

Jump to
carla_test_helper lib carla.ex
Raw

lib/carla.ex

defmodule Carla do
@moduledoc """
Carla providing helper methods for querying/testing responses.
"""
use HTTPoison.Base
@expected_fields ~w(type speak data buttons uuid compareAttribute)
@doc """
TODO: Documentation
"""
def process_url(url) do
carla_path = case System.get_env("CARLA_PATH") do
nil -> "http://localhost:5000"
path -> path
end
carla_path <> url
end
def process_response_body(body) do
body
|> Poison.decode!
|> Map.take(@expected_fields)
|> Enum.map(fn({k,v}) -> {String.to_atom(k), v} end)
end
@doc """
Returns the intent (or type) for the specified response.
"""
def get_response_type(response) do
response.body[:type]
end
@doc """
Returns the uuid for the specified response.
"""
def get_uuid(response) do
response.body[:uuid]
end
@doc """
TODO: Documentation
"""
def get_quick_replies(response) do
response.body[:buttons] |> Enum.flat_map(fn(btn) -> btn end)
end
@doc """
TODO: Documentation
"""
def get_buttons(response) do
response.body[:speak]
|> Enum.flat_map(fn
%{"type" => "text_card", "buttons" => buttons} ->
buttons
%{"type" => "gallery_card", "gallery" => gallery} ->
Enum.flat_map(gallery, fn %{"buttons" => buttons} -> buttons end)
_ -> []
end)
end
@doc """
TODO: Documentation
"""
def get_gallery_cards(response) do
response.body[:speak]
|> Enum.filter(fn(card) -> card["type"] === "gallery_card" end)
end
@doc """
TODO: Documentation
"""
def get_messages(response) do
Enum.map(response.body[:speak], fn(msg) ->
Regex.replace(~r/\n\s+/, msg["message"], " ")
end)
end
@doc """
TODO: Documentation
"""
def send_message(message, uuid) do
Carla.post!(
"/carla",
Poison.encode!(%{ "message" => message }),
[{"Content-Type", "application/json"}],
hackney: [cookie: [~s(uid=#{uuid})]]
)
end
@doc """
TODO: Documentation
"""
def send_request(body, uuid) do
Carla.post!(
"/carla",
Poison.encode!(body),
[{"Content-Type", "application/json"}],
hackney: [cookie: [~s(uid=#{uuid})]]
)
end
@doc """
TODO: Documentation
"""
def send_message_async(message, uuid) do
t = Task.async(fn -> send_message(message, uuid) end)
Task.await(t, 10_000)
end
@doc """
TODO: Documentation
"""
def send_messages(messages, uuid) do
Enum.reduce(messages, %{}, fn(msg, _) -> send_message_async(msg, uuid) end)
end
end