write tests for the session server

This commit is contained in:
Victor Martinez 2024-03-29 22:32:40 +01:00
parent 5b40c342f8
commit 0f3725940b
2 changed files with 74 additions and 7 deletions

View file

@ -0,0 +1,74 @@
defmodule Intisync.SessionServerTest do
use ExUnit.Case, async: true
alias Intisync.SessionServer
setup do
session_id = Intisync.Puid.generate()
pid = start_supervised!({SessionServer, {session_id}})
%{session_server_pid: pid, session_id: session_id}
end
test "A session starts with the proper initial state", %{
session_server_pid: pid,
session_id: session_id
} do
assert SessionServer.get_id(pid) == session_id
assert SessionServer.get_devices(pid) == %{}
assert SessionServer.full?(pid)
end
test "Calling device_connected adds the device to the state", %{
session_server_pid: pid
} do
index = 0
device = %{name: "Dummy", vibration: 0}
assert SessionServer.device_connected(pid, index, device) == :ok
assert SessionServer.get_devices(pid) == %{index => device}
end
test "Calling device_disconnected removes the device to the state", %{
session_server_pid: pid
} do
index = 0
device = %{name: "Dummy", vibration: 0}
assert SessionServer.device_connected(pid, index, device) == :ok
assert SessionServer.device_disconnected(pid, index) == :ok
assert SessionServer.get_devices(pid) == %{}
end
test "Calling remote_connected sets the session as full", %{
session_server_pid: pid
} do
SessionServer.remote_connected(pid)
assert SessionServer.full?(pid)
end
test "Calling remote_disconnected sets the session as not full", %{
session_server_pid: pid
} do
SessionServer.remote_connected(pid)
assert SessionServer.full?(pid)
SessionServer.remote_disconnected(pid)
refute SessionServer.full?(pid)
end
test "Calling empty_devices resets the devices state", %{
session_server_pid: pid
} do
assert SessionServer.device_connected(pid, 0, %{name: "Dummy", vibration: 0})
assert SessionServer.device_connected(pid, 1, %{name: "Dummy2", vibration: 0})
assert map_size(SessionServer.get_devices(pid)) == 2
assert SessionServer.empty_devices(pid) == :ok
assert SessionServer.get_devices(pid) == %{}
end
test "Calling vibrate device updates the vibration of that device", %{
session_server_pid: pid
} do
assert SessionServer.device_connected(pid, 0, %{name: "Dummy", vibration: 0})
assert SessionServer.device_connected(pid, 1, %{name: "Dummy2", vibration: 0})
assert SessionServer.vibrate_device(pid, 0, 45) == :ok
assert %{0 => %{vibration: 45}, 1 => %{vibration: 0}} = SessionServer.get_devices(pid)
end
end

View file

@ -6,13 +6,6 @@ defmodule IntisyncWeb.ConnCase do
Such tests rely on `Phoenix.ConnTest` and also Such tests rely on `Phoenix.ConnTest` and also
import other functionality to make it easier import other functionality to make it easier
to build common data structures and query the data layer. to build common data structures and query the data layer.
Finally, if the test case interacts with the database,
we enable the SQL sandbox, so changes done to the database
are reverted at the end of every test. If you are using
PostgreSQL, you can even run database tests asynchronously
by setting `use IntisyncWeb.ConnCase, async: true`, although
this option is not recommended for other databases.
""" """
use ExUnit.CaseTemplate use ExUnit.CaseTemplate