diff --git a/test/chronoscope/gemini_test.exs b/test/chronoscope/gemini_test.exs new file mode 100644 index 0000000..9cb0a79 --- /dev/null +++ b/test/chronoscope/gemini_test.exs @@ -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 diff --git a/test/chronoscope/nts_test.exs b/test/chronoscope/nts_test.exs index 427bee3..67c75b7 100644 --- a/test/chronoscope/nts_test.exs +++ b/test/chronoscope/nts_test.exs @@ -61,14 +61,17 @@ defmodule Chronoscope.NTSTest do |> expect(:lookup, fn _, _ -> [] end) DynamicSupervisorMock - |> expect(:start_child, fn Chronoscope.NTS.DynamicSupervisor, - {Chronoscope.NTS.Client, - [ - server: %{host: "localhost", port: 1111}, - name: {:via, RegistryMock, {Chronoscope.NTS.Registry, "localhost:1111"}} - ]} -> - {:ok, 1} - end) + |> expect( + :start_child, + fn Chronoscope.NTS.DynamicSupervisor, + {Chronoscope.NTS.Client, + [ + server: %{host: "localhost", port: 1111}, + name: {:via, RegistryMock, {Chronoscope.NTS.Registry, "localhost:1111"}} + ]} -> + {:ok, 1} + end + ) GenServerMock |> expect(:call, fn 1, :key_establishment -> :result end)