From f95e791f87ca87b8494cc9ac893fa1b8999f5e23 Mon Sep 17 00:00:00 2001 From: Ismael Bejarano Date: Fri, 4 Oct 2024 13:55:40 -0300 Subject: [PATCH] Add unit test for the :paused status --- lib/ask/runtime/channel_status_server.ex | 8 ++++ .../runtime/channel_status_server_test.exs | 48 +++++++++++++++++++ 2 files changed, 56 insertions(+) diff --git a/lib/ask/runtime/channel_status_server.ex b/lib/ask/runtime/channel_status_server.ex index eba0493e9..708c04a65 100644 --- a/lib/ask/runtime/channel_status_server.ex +++ b/lib/ask/runtime/channel_status_server.ex @@ -71,6 +71,10 @@ defmodule Ask.Runtime.ChannelStatusServer do Logger.info("ChannelStatusServer: #{message}") end + defp process_channel_status_change({:down, _messages}, %{status: :paused}, _timestamp, _channel) do + nil + end + defp process_channel_status_change({:down, _messages}, %{status: :down}, _timestamp, _channel) do nil end @@ -86,6 +90,10 @@ defmodule Ask.Runtime.ChannelStatusServer do }) end + defp process_channel_status_change({:error, _code}, %{status: :paused}, _timestamp, _channel) do + nil + end + defp process_channel_status_change({:error, _code}, %{status: :error}, _timestamp, _channel) do nil end diff --git a/test/ask/runtime/channel_status_server_test.exs b/test/ask/runtime/channel_status_server_test.exs index a6068dcbf..3cf4dbb95 100644 --- a/test/ask/runtime/channel_status_server_test.exs +++ b/test/ask/runtime/channel_status_server_test.exs @@ -67,6 +67,20 @@ defmodule Ask.Runtime.ChannelStatusServerTest do assert t2 end + test "update channel status" do + {:ok, _pid} = ChannelStatusServer.start_link() + user = insert(:user) + + channel = + TestChannel.create_channel(user, "test", TestChannel.settings(TestChannel.new(), 1)) + + assert ChannelStatusServer.get_channel_status(channel.id) == :unknown + + ChannelStatusServer.update_channel_status(channel.id, %{status: :new_channel_status}) + + assert ChannelStatusServer.get_channel_status(channel.id) == %{status: :new_channel_status} + end + test "sends email when a channel is down and its status was previously :unknown" do {:ok, pid} = ChannelStatusServer.start_link() Process.register(self(), :mail_target) @@ -144,4 +158,38 @@ defmodule Ask.Runtime.ChannelStatusServerTest do ChannelStatusServer.poll(pid) refute_receive [:email, ^email] end + + test "doesn't send email when a channel is down but status was previously :paused" do + {:ok, pid} = ChannelStatusServer.start_link() + Process.register(self(), :mail_target) + user = insert(:user) + survey = insert(:survey, state: :running) + + channel = + TestChannel.create_channel(user, "test", TestChannel.settings(TestChannel.new(), 1, :down)) + + setup_surveys_with_channels([survey], [channel]) + + ChannelStatusServer.update_channel_status(channel.id, %{status: :paused}) + + ChannelStatusServer.poll(pid) + refute_receive [:email, _] + end + + test "doesn't send email when :error is received but status was previously :paused" do + {:ok, pid} = ChannelStatusServer.start_link() + Process.register(self(), :mail_target) + user = insert(:user) + survey = insert(:survey, state: :running) + + channel = + TestChannel.create_channel(user, "test", TestChannel.settings(TestChannel.new(), 1, :error)) + + setup_surveys_with_channels([survey], [channel]) + + ChannelStatusServer.update_channel_status(channel.id, %{status: :paused}) + + ChannelStatusServer.poll(pid) + refute_receive [:email, _] + end end