### ----------------------------------------------------
### -- Updown
### -- Licenses: Artistic-2.0
### -- Authors: Elizabeth Mattijsen
### -- File: xt/09-compare.rakutest
### ----------------------------------------------------
use Test;
use Updown;

my $ud := Updown.new(:api-key<foobar>);
my @compares = <booking duckduckgo facebook github google raku>;

plan @compares * 93;

sub test-metrics($metrics, $name) {
    isa-ok $metrics, Updown::Metrics;
    for <
      apdex    Rat
      requests Updown::Metrics::Requests
      timings  Updown::Metrics::Timings
    > -> $method, $type {
        ok $metrics."$method"() ~~ ::($type),
          ":$name: does $method return a $type";
    }

    my $requests := $metrics.requests;
    isa-ok $requests, Updown::Metrics::Requests;
    for <
      by_response_time Updown::Metrics::Requests::ByResponseTime
      failures         Int
      samples          Int
      satisfied        Int
      tolerated        Int
    > -> $method, $type {
        ok $requests."$method"() ~~ ::($type),
          ":$name: does $method return a $type";
    }

    my $brt := $requests.by_response_time;
    isa-ok $brt, Updown::Metrics::Requests::ByResponseTime;
    for <
      under125  Int
      under250  Int
      under500  Int
      under1000 Int
      under2000 Int
      under4000 Int
    > -> $method, $type {
        ok $brt."$method"() ~~ ::($type),
          ":$name: does $method return a $type";
    }

    my $timings := $metrics.timings;
    isa-ok $timings, Updown::Metrics::Timings;
    for <
      connection Int
      handshake  Int
      namelookup Int
      redirect   Int
      response   Int
      total      Int
    > -> $method, $type {
        ok $timings."$method"() ~~ ::($type),
          ":$name: does $method return a $type";
    }
}

sub test($name) is test-assertion {
    my %named = $name => True;
    my $check := $ud.check(|%named);
    isa-ok $check, Updown::Check;
    for <
      disabled_locations List
      alias              Str
      apdex_t            Rat
      custom_headers     Hash
      down               Bool
      enabled            Bool
      http_verb          Str
      last_check_at      DateTime
      last_status        Int
      next_check_at      DateTime
      period             Int
      published          Bool
      ssl                Updown::Check::SSL
      string_match       Str
      token              Str
      uptime             Rat
      url                Str
    > -> $method, $type {
        ok $check."$method"() ~~ ::($type),
          ":$name: does $method return a $type";
    }

    test-metrics($ud.overall_metrics(|%named), $name);

    my %hourly_metrics := $ud.hourly_metrics(|%named);
    test-metrics(%hourly_metrics.values.head, $name);

    my %node_metrics := $ud.node_metrics(|%named);
    test-metrics(%node_metrics.values.head, $name);

    if $ud.downtimes(|%named) -> @downtimes {
        my $downtime := @downtimes[0];
        isa-ok $downtime, Updown::Downtime;
        for <
          partial    Bool
          started_at DateTime
        > -> $method, $type {
            ok $downtime."$method"() ~~ ::($type),
              ":$name: does $method return a $type";
        }
    }
    else {
        pass ":$name: no downtimes" for ^3;
    }
}

test($_) for @compares;

# vim: expandtab shiftwidth=4


