summaryrefslogtreecommitdiffstats
path: root/test/cepis/health_test.exs
blob: 47a49c20501cd2e80814b611cdbf016662f7a20d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
defmodule Cepis.HealthTest do
  use Cepis.ServerCase

  test "reads the live database identifier", %{server: server, client: client} do
    body =
      json(%{
        "status" => "ok",
        "dne" => "26071",
        "ceps" => 1_596_168,
        "geo" => 894_489,
        "base_gerada_em" => "2026-07-24T03:48:28Z"
      })

    FakeServer.respond(server, "/health", {200, body, []})

    assert {:ok, health} = Cepis.health(client)
    assert Cepis.Health.ok?(health)
    assert health.dne == "26071"
    assert health.ceps == 1_596_168
    assert health.geo == 894_489
    assert health.base_gerada_em == "2026-07-24T03:48:28Z"
  end

  test "a degraded API is a diagnosis, not a failure", %{server: server, client: client} do
    body = json(%{"status" => "degradado", "dne" => "", "ceps" => 0, "geo" => 0})
    FakeServer.respond(server, "/health", {503, body, []})

    assert {:ok, health} = Cepis.health(client)
    refute Cepis.Health.ok?(health)
    assert health.status == "degradado"
  end

  test "a 503 on /health is not retried", %{server: server, client: client} do
    FakeServer.respond(server, "/health", {503, json(%{"status" => "degradado"}), []})

    assert {:ok, _health} = Cepis.health(client)
    assert FakeServer.count(server, "/health") == 1
  end

  test "the bang variant returns the struct", %{server: server, client: client} do
    FakeServer.respond(server, "/health", {200, json(%{"status" => "ok"}), []})

    assert %Cepis.Health{status: "ok"} = Cepis.health!(client)
  end
end