towerops/test/towerops/workers/alert_digest_worker_test.exs
Graham McIntire 3ca0834ef0 tests: raise coverage to 70% via helper promotion + new unit/property tests
Promoted pure presentation and utility helpers from `defp` to `def @doc false`
across ~20 LiveViews, Oban workers, and sync modules so they're reachable from
unit tests. Refactored several `cond` blocks into idiomatic function heads with
guards. Added ~250 new test cases in new files under test/towerops and
test/towerops_web, including DB-backed tests for CnMaestro.Sync and
AlertNotificationWorker, and removed dead LiveView tab components and
CapacityLive (no callers anywhere in lib/test).

Configured mix.exs test_coverage.ignore_modules to exclude vendored third-party
code (SnmpKit, protobuf-generated Towerops.Agent.*, Absinthe GraphQL types,
Phoenix HTML modules, Inspect protocol impls) from coverage calculations —
these are not our project code.

Coverage: 66.93% → 70.09%. Full suite: 10,127 tests, 0 failures.
2026-04-24 09:49:06 -05:00

70 lines
2.1 KiB
Elixir

defmodule Towerops.Workers.AlertDigestWorkerTest do
use Towerops.DataCase
use Oban.Testing, repo: Towerops.Repo
import Towerops.AccountsFixtures
alias Towerops.Alerts.NotificationDigest
alias Towerops.Workers.AlertDigestWorker
defp insert_digest!(opts) do
user_id = Keyword.fetch!(opts, :user_id)
digest_sent = Keyword.get(opts, :digest_sent, false)
alert_ids = Keyword.get(opts, :alert_ids, [Ecto.UUID.generate()])
{:ok, org} =
Towerops.Organizations.create_organization(
%{name: "Org #{System.unique_integer([:positive])}"},
user_id
)
{:ok, digest} =
Towerops.Repo.insert(%NotificationDigest{
user_id: user_id,
organization_id: org.id,
suppressed_alert_ids: alert_ids,
digest_sent: digest_sent,
window_start: DateTime.truncate(DateTime.utc_now(), :second)
})
digest
end
describe "perform/1 cron mode" do
test "enqueues delivery jobs per user with pending digest" do
user = user_fixture()
_ = insert_digest!(user_id: user.id)
assert :ok = perform_job(AlertDigestWorker, %{"user_id" => "__cron__"})
assert [_j] = all_enqueued(worker: AlertDigestWorker)
end
test "doesn't enqueue when no pending digests exist" do
assert :ok = perform_job(AlertDigestWorker, %{"user_id" => "__cron__"})
assert [] = all_enqueued(worker: AlertDigestWorker)
end
test "skips users whose digests are already sent" do
user = user_fixture()
_ = insert_digest!(user_id: user.id, digest_sent: true)
assert :ok = perform_job(AlertDigestWorker, %{"user_id" => "__cron__"})
assert [] = all_enqueued(worker: AlertDigestWorker)
end
end
describe "perform/1 delivery mode" do
test "no-op when there's no pending digest for user" do
user = user_fixture()
assert :ok = perform_job(AlertDigestWorker, %{"user_id" => user.id})
end
end
describe "enqueue/1" do
test "schedules a delivery job 5 minutes out" do
user = user_fixture()
assert {:ok, _job} = AlertDigestWorker.enqueue(user.id)
assert [_j] = all_enqueued(worker: AlertDigestWorker)
end
end
end