Add tests

This commit is contained in:
Mike Cifelli 2024-05-07 11:16:22 -04:00
parent 68fb573d01
commit a07ce6e410
Signed by: mike
GPG Key ID: 6B08C6BE47D08E4C
2 changed files with 103 additions and 8 deletions

View File

@ -0,0 +1,92 @@
defmodule Chronoscope.GeminiTest do
use Chronoscope.Case, async: true
alias Chronoscope.DynamicSupervisorMock
alias Chronoscope.GenServerMock
alias Chronoscope.Gemini
alias Chronoscope.RegistryMock
import Mox
setup :verify_on_exit!
describe "Chronoscope.Gemini.healthy?()" do
test "is healthy" do
assert Gemini.healthy?() == true
end
end
describe "Chronoscope.Gemini.list()" do
test "shows empty client list" do
DynamicSupervisorMock
|> expect(:which_children, fn _ -> [] end)
assert Gemini.list() == []
end
test "lists all children" do
DynamicSupervisorMock
|> expect(:which_children, fn _ -> [{1, 2, 3, 4}, {5, 6, 7, 8}] end)
GenServerMock
|> expect(:call, fn 2, :list -> :one end)
|> expect(:call, fn 6, :list -> :two end)
assert Gemini.list() == [:one, :two]
end
end
describe "Chronoscope.Gemini.remove()" do
test "does nothing if the client doesn't exist" do
RegistryMock
|> expect(:lookup, fn _, _ -> [] end)
assert Gemini.remove("localhost", 1965, "/") == {:error, :not_found}
end
test "removes a client" do
RegistryMock
|> expect(:lookup, fn _, _ -> [{1, 2}] end)
GenServerMock
|> expect(:call, fn 1, :terminate -> :terminating end)
assert Gemini.remove("localhost", 1965, "/") == {:ok, :terminating}
end
end
describe "Chronoscope.Gemini.connect()" do
test "creates a new client" do
RegistryMock
|> expect(:lookup, fn _, _ -> [] end)
DynamicSupervisorMock
|> expect(
:start_child,
fn Chronoscope.Gemini.DynamicSupervisor,
{Chronoscope.Gemini.Client,
[
resource: %{host: "localhost", port: 1965, path: "/"},
name: {:via, RegistryMock, {Chronoscope.Gemini.Registry, "localhost:1965/"}}
]} ->
{:ok, 1}
end
)
GenServerMock
|> expect(:call, fn 1, :connect -> :result end)
assert Gemini.connect("localhost", 1965, "/") == :result
end
test "reuses an existing client" do
RegistryMock
|> expect(:lookup, fn _, _ -> [{1, 2}] end)
GenServerMock
|> expect(:call, fn 1, :connect -> :result end)
assert Gemini.connect("localhost", 1965, "/") == :result
end
end
end

View File

@ -61,14 +61,17 @@ defmodule Chronoscope.NTSTest do
|> expect(:lookup, fn _, _ -> [] end) |> expect(:lookup, fn _, _ -> [] end)
DynamicSupervisorMock DynamicSupervisorMock
|> expect(:start_child, fn Chronoscope.NTS.DynamicSupervisor, |> expect(
{Chronoscope.NTS.Client, :start_child,
[ fn Chronoscope.NTS.DynamicSupervisor,
server: %{host: "localhost", port: 1111}, {Chronoscope.NTS.Client,
name: {:via, RegistryMock, {Chronoscope.NTS.Registry, "localhost:1111"}} [
]} -> server: %{host: "localhost", port: 1111},
{:ok, 1} name: {:via, RegistryMock, {Chronoscope.NTS.Registry, "localhost:1111"}}
end) ]} ->
{:ok, 1}
end
)
GenServerMock GenServerMock
|> expect(:call, fn 1, :key_establishment -> :result end) |> expect(:call, fn 1, :key_establishment -> :result end)