Add test/hk_messaging_portal_tests.erl
Browse files
test/hk_messaging_portal_tests.erl
ADDED
|
@@ -0,0 +1,44 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
%%%-------------------------------------------------------------------
|
| 2 |
+
%%% @doc Covers "message delivery" from `<test>'.
|
| 3 |
+
%%% @end
|
| 4 |
+
%%%-------------------------------------------------------------------
|
| 5 |
+
-module(hk_messaging_portal_tests).
|
| 6 |
+
-include_lib("eunit/include/eunit.hrl").
|
| 7 |
+
|
| 8 |
+
setup() -> hk_test_helper:start_app().
|
| 9 |
+
teardown(Ok) -> hk_test_helper:stop_app(Ok).
|
| 10 |
+
|
| 11 |
+
messaging_fixture_test_() ->
|
| 12 |
+
{setup, fun setup/0, fun teardown/1, fun(_) -> [
|
| 13 |
+
{"a message to a subscribed recipient is delivered immediately",
|
| 14 |
+
fun immediate_delivery/0},
|
| 15 |
+
{"a message to an unsubscribed recipient stays queued but is stored",
|
| 16 |
+
fun queued_when_unsubscribed/0},
|
| 17 |
+
{"conversation history is returned in send order",
|
| 18 |
+
fun conversation_history_order/0}
|
| 19 |
+
] end}.
|
| 20 |
+
|
| 21 |
+
immediate_delivery() ->
|
| 22 |
+
ok = hk_messaging_portal:subscribe(<<"agent_1">>),
|
| 23 |
+
{ok, MessageId} = hk_messaging_portal:send(<<"user_1">>, <<"agent_1">>, <<"conv_1">>,
|
| 24 |
+
user_to_agent, #{text => <<"hi">>}),
|
| 25 |
+
receive
|
| 26 |
+
{hk_message, #{message_id := MessageId, status := delivered}} -> ok
|
| 27 |
+
after 1000 -> ?assert(false)
|
| 28 |
+
end,
|
| 29 |
+
{ok, Stored} = hk_messaging_portal:get(MessageId),
|
| 30 |
+
?assertEqual(delivered, maps:get(status, Stored)),
|
| 31 |
+
hk_messaging_portal:unsubscribe().
|
| 32 |
+
|
| 33 |
+
queued_when_unsubscribed() ->
|
| 34 |
+
{ok, MessageId} = hk_messaging_portal:send(<<"user_1">>, <<"agent_nobody_is_listening">>,
|
| 35 |
+
<<"conv_2">>, user_to_agent, #{text => <<"hello?">>}),
|
| 36 |
+
{ok, Stored} = hk_messaging_portal:get(MessageId),
|
| 37 |
+
?assertEqual(queued, maps:get(status, Stored)).
|
| 38 |
+
|
| 39 |
+
conversation_history_order() ->
|
| 40 |
+
Conv = <<"conv_3">>,
|
| 41 |
+
{ok, Id1} = hk_messaging_portal:send(<<"a">>, <<"b">>, Conv, user_to_agent, #{n => 1}),
|
| 42 |
+
{ok, Id2} = hk_messaging_portal:send(<<"b">>, <<"a">>, Conv, agent_to_user, #{n => 2}),
|
| 43 |
+
History = hk_messaging_portal:list_conversation(Conv),
|
| 44 |
+
?assertEqual([Id1, Id2], [maps:get(message_id, M) || M <- History]).
|