#!/usr/local/cpanel/3rdparty/bin/perl -w

# cpanel - t/small/Whostmgr-360Monitoring-AgentManager.t
#                                                  Copyright 2024 cPanel, L.L.C.
#                                                           All rights reserved.
# copyright@cpanel.net                                         http://cpanel.net
# This code is subject to the cPanel license. Unauthorized copying is prohibited

package t::small::Whostmgr::360Monitoring::AgentManager;

use cPstrict;

use Test2::V0 -no_utf8 => 1;
use Test::Warn;
use Test::Cpanel::Policy;

use parent qw(
  Cpanel::TestObj
);

use Cpanel();

use Whostmgr::360Monitoring::AgentManager();

__PACKAGE__->new()->runtests() if !caller;

sub new ( $class, @args ) {
    my $self = $class->SUPER::new(@args);
    return $self;
}

sub runtests ( $self, @args ) {
    return $self->SUPER::runtests(@args);
}

sub test_stop : Tests(4) ($self) {
    my $case      = 0;
    my @status    = ( 'active', 'inactive' );
    my @mock_subs = (
        $self->mock_subs(
            'Cpanel::RestartSrv::Systemd::stop_via_systemd' => sub {
                if ( $case > 2 ) {
                    $@ = "Pretend something bad happened";
                    die;
                }

                # Intentionally allowing indexing beyond array bounds to test return undef.
                return $status[$case];
            },
        )
    );

    like(
        dies { Whostmgr::360Monitoring::AgentManager::stop() },
        qr/Failed to stop the service/,
        "test_stop: Dies as expected from failed stop."
    );
    $case++;

    my $result = Whostmgr::360Monitoring::AgentManager::stop();
    is( $result, $case, "test_stop: is $status[$case]." );
    $case++;

    like(
        dies { Whostmgr::360Monitoring::AgentManager::stop() },
        qr/Failed to stop the service/,
        "test_stop: Dies as expected from failed stop."
    );
    $case++;

    like(
        dies { Whostmgr::360Monitoring::AgentManager::stop() },
        qr/command failed because of an error/,
        "test_stop: Dies as expected from failed eval of stop."
    );
}

sub test_restart : Tests(4) ($self) {
    my $case      = 0;
    my @status    = ( 'active', 'inactive' );
    my @mock_subs = (
        $self->mock_subs(
            'Cpanel::RestartSrv::Systemd::restart_via_systemd' => sub {
                if ( $case > 2 ) {
                    $@ = "Pretend something bad happened";
                    die;
                }

                # Intentionally allowing indexing beyond array bounds to test return undef.
                return $status[$case];
            },
        )
    );

    my $result = Whostmgr::360Monitoring::AgentManager::restart();
    is( $result, 1, "test_restart: is $status[$case]." );
    $case++;

    like(
        dies { Whostmgr::360Monitoring::AgentManager::restart() },
        qr/service failed to start/,
        "test_restart: Dies as expected from failed restart."
    );
    $case++;

    like(
        dies { Whostmgr::360Monitoring::AgentManager::restart() },
        qr/service is not a known service/,
        "test_restart: Dies as expected from failed eval of restart."
    );
    $case++;

    like(
        dies { Whostmgr::360Monitoring::AgentManager::restart() },
        qr/command failed because of an error/,
        "test_restart: Dies as expected from failed eval of restart."
    );
}

sub test_start : Tests(4) ($self) {
    my $case      = 0;
    my @status    = ( 'active', 'inactive' );
    my @mock_subs = (
        $self->mock_subs(
            'Cpanel::RestartSrv::Systemd::restart_via_systemd' => sub {
                if ( $case > 2 ) {
                    $@ = "Pretend something bad happened";
                    die;
                }

                # Intentionally allowing indexing beyond array bounds to test return undef.
                return $status[$case];
            },
        )
    );

    my $result = Whostmgr::360Monitoring::AgentManager::start();
    is( $result, 1, "test_start: is $status[$case]." );
    $case++;

    like(
        dies { Whostmgr::360Monitoring::AgentManager::start() },
        qr/service failed to start/,
        "test_start: Dies as expected from failed start."
    );
    $case++;

    like(
        dies { Whostmgr::360Monitoring::AgentManager::start() },
        qr/service is not a known service/,
        "test_start: Dies as expected from failed eval of start."
    );
    $case++;

    like(
        dies { Whostmgr::360Monitoring::AgentManager::start() },
        qr/command failed because of an error/,
        "test_start: Dies as expected from failed eval of start."
    );
    $case++;
}

sub test_get_status : Tests(3) ($self) {
    my $case      = 0;
    my @status    = ( 'active', 'inactive' );
    my @mock_subs = (
        $self->mock_subs(
            'Cpanel::RestartSrv::Systemd::get_status_via_systemd' => sub {
                if ( $case > 2 ) {
                    return '';
                }

                # Intentionally allowing indexing beyond array bounds to test return undef.
                return $status[$case];
            },
        )
    );

    my $result = Whostmgr::360Monitoring::AgentManager::get_status();
    is( $result, $status[$case], "test_get_status: is $status[$case]." );
    $case++;

    $result = Whostmgr::360Monitoring::AgentManager::get_status();
    is( $result, $status[$case], "test_get_status: is $status[$case]." );
    $case++;

    $result = Whostmgr::360Monitoring::AgentManager::get_status();
    is( $result, '', "test_get_status: is '' when no agent is found." );
}

1;
