feat(server): hosts list/delete/rotate helpers + pubsub on metric insert

This commit is contained in:
Carsten 2026-04-21 22:50:33 +02:00
parent f3e7fab4d2
commit 3123743c1c
4 changed files with 77 additions and 2 deletions

View file

@ -52,4 +52,34 @@ defmodule Server.HostsTest do
assert offline.status == "offline"
end
end
describe "list_all/0" do
test "returns every host ordered by name" do
{:ok, {_, _}} = Hosts.create_host("pve-02")
{:ok, {_, _}} = Hosts.create_host("pve-01")
names = Hosts.list_all() |> Enum.map(& &1.name)
assert names == ["pve-01", "pve-02"]
end
end
describe "delete_host/1" do
test "deletes the host row" do
{:ok, {host, _}} = Hosts.create_host("pve-01")
assert {:ok, _} = Hosts.delete_host(host)
assert Server.Repo.get(Server.Schema.Host, host.id) == nil
end
end
describe "rotate_token/1" do
test "replaces token_hash and returns new plaintext token" do
{:ok, {host, old_token}} = Hosts.create_host("pve-01")
assert {:ok, {updated, new_token}} = Hosts.rotate_token(host)
assert updated.id == host.id
refute updated.token_hash == host.token_hash
assert is_binary(new_token)
refute new_token == old_token
assert {:error, :invalid_token} = Hosts.authenticate("pve-01", old_token)
assert {:ok, _} = Hosts.authenticate("pve-01", new_token)
end
end
end

View file

@ -32,6 +32,14 @@ defmodule Server.MetricsTest do
assert {:error, cs} = Metrics.record_sample(999_999, "fast", ts, %{})
assert %{host: ["does not exist"]} = errors_on(cs)
end
test "broadcasts {:metric_inserted, host_id, interval} on success", %{host: host} do
Phoenix.PubSub.subscribe(Server.PubSub, "metrics")
ts = DateTime.utc_now()
{:ok, _} = Metrics.record_sample(host.id, "fast", ts, %{"v" => 1})
assert_receive {:metric_inserted, host_id, "fast"}, 500
assert host_id == host.id
end
end
describe "latest_sample/2" do