towerops/test/towerops/trace_test.exs
Graham McIntire cc3ca1c7de
feat: add site search to trace
Trace can now search sites by name, location, or address. Selecting
a site shows all devices at that site with status and recent alerts.
2026-03-06 15:37:23 -06:00

79 lines
2.3 KiB
Elixir

defmodule Towerops.TraceTest do
use Towerops.DataCase, async: true
import Towerops.AccountsFixtures
alias Towerops.Trace
setup do
user = user_fixture()
{:ok, organization} = Towerops.Organizations.create_organization(%{name: "Test ISP"}, user.id)
{:ok, site} =
Towerops.Sites.create_site(%{
name: "Tower Alpha",
organization_id: organization.id,
location: "Downtown"
})
{:ok, device} =
Towerops.Devices.create_device(%{
name: "Router-1",
ip_address: "10.0.0.1",
site_id: site.id,
organization_id: organization.id,
snmp_enabled: true
})
%{organization: organization, site: site, device: device}
end
describe "search/2" do
test "finds sites by name", %{organization: organization} do
results = Trace.search(organization.id, "Tower")
site_results = Enum.filter(results, &(&1.type == :site))
assert length(site_results) == 1
assert hd(site_results).label == "Tower Alpha"
end
test "finds sites by location", %{organization: organization} do
results = Trace.search(organization.id, "Downtown")
site_results = Enum.filter(results, &(&1.type == :site))
assert length(site_results) == 1
end
test "finds devices by name", %{organization: organization} do
results = Trace.search(organization.id, "Router")
device_results = Enum.filter(results, &(&1.type == :device))
assert length(device_results) == 1
assert hd(device_results).label == "Router-1"
end
test "returns empty list for short queries", %{organization: organization} do
assert Trace.search(organization.id, "a") == []
end
end
describe "assemble_trace/3" do
test "assembles trace from site with its devices", %{
organization: organization,
site: site,
device: device
} do
trace = Trace.assemble_trace(organization.id, :site, site.id)
assert trace
assert trace.site.id == site.id
assert trace.site.name == "Tower Alpha"
assert length(trace.devices) == 1
assert hd(trace.devices).id == device.id
end
test "returns nil for non-existent site", %{organization: organization} do
assert Trace.assemble_trace(organization.id, :site, Ecto.UUID.generate()) == nil
end
end
end