summaryrefslogtreecommitdiffstats
path: root/t/06-health.t
blob: fca28202d3dee1b37abf69fd77d6331d6cadd50e (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
46
47
48
49
50
51
52
53
54
55
56
57
58
use strict;
use warnings;

use FindBin ();
use lib "$FindBin::Bin/lib";

use Test::More;
use TestServer qw(json new_client);

use Cepis;

{
    my $body = json(
        {   status         => 'ok',
            dne            => '26071',
            ceps           => 1596168,
            geo            => 894489,
            base_gerada_em => '2026-07-24T03:48:28Z',
        }
    );
    my $server = TestServer->new( routes => { '/health' => [ 200, $body, {} ] } );
    my $client = new_client($server);

    my $health = $client->health;
    isa_ok( $health, 'Cepis::Health' );
    ok( $health->ok, 'the API reports itself healthy' );
    is( $health->dne,            '26071',                'dne identifies the database' );
    is( $health->ceps,           1596168,                'ceps' );
    is( $health->geo,            894489,                 'geo is a count here, not an object' );
    is( $health->base_gerada_em, '2026-07-24T03:48:28Z', 'base_gerada_em' );
    $server->stop;
}

{
    my $body = json(
        { status => 'degradado', dne => '', ceps => 0, geo => 0, base_gerada_em => '' } );
    my $server = TestServer->new( routes => { '/health' => [ 503, $body, {} ] } );
    my $client = new_client($server);

    my $health = $client->health;
    ok( !$health->ok, 'a degraded API does not throw' );
    is( $health->status, 'degradado', 'the diagnosis arrives in status' );
    is( $server->count('/health'), 1, 'and a 503 on /health is not retried' );
    $server->stop;
}

{
    my $server = TestServer->new( routes => { '/health' => [ 500, '{}', {} ] } );
    my $client = new_client($server);

    my $ok = eval { $client->health; 1 };
    ok( !$ok, 'a 500 on /health still throws' );
    isa_ok( $@, 'Cepis::Error::ServerError' );
    is( $server->count('/health'), 3, 'after retrying' );
    $server->stop;
}

done_testing();