aprs.me/test/aprsme/deployment_notifier_test.exs

50 lines
1.6 KiB
Elixir

defmodule Aprsme.DeploymentNotifierTest do
use ExUnit.Case, async: false
alias Aprsme.DeploymentNotifier
describe "notify_deployment/1" do
test "broadcasts :new_deployment over Aprsme.PubSub" do
:ok = Phoenix.PubSub.subscribe(Aprsme.PubSub, "deployment_events")
now = DateTime.utc_now()
assert :ok == DeploymentNotifier.notify_deployment(now)
assert_receive {:new_deployment, %{deployed_at: ^now}}, 500
end
end
describe "handle_info :check_deployment" do
test "no-op when deployed_at is unchanged" do
deployed = DateTime.utc_now()
Application.put_env(:aprsme, :deployed_at, deployed)
state = %{deployed_at: deployed}
assert {:noreply, ^state} = DeploymentNotifier.handle_info(:check_deployment, state)
end
test "broadcasts and updates state when deployed_at has changed" do
:ok = Phoenix.PubSub.subscribe(Aprsme.PubSub, "deployment_events")
new_deploy = DateTime.utc_now()
old_deploy = DateTime.add(new_deploy, -60, :second)
# Simulate an updated deploy timestamp in app env.
Application.put_env(:aprsme, :deployed_at, new_deploy)
state = %{deployed_at: old_deploy}
assert {:noreply, %{deployed_at: ^new_deploy}} =
DeploymentNotifier.handle_info(:check_deployment, state)
assert_receive {:new_deployment, %{deployed_at: ^new_deploy}}, 500
end
end
describe "init/1" do
test "schedules a check and stores the current deployed_at in state" do
assert {:ok, %{deployed_at: %DateTime{}}} = DeploymentNotifier.init([])
# The 30s check is scheduled — we don't wait for it.
end
end
end