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

#                                      Copyright 2025 WebPros International, LLC
#                                                           All rights reserved.
# copyright@cpanel.net                                         http://cpanel.net
# This code is subject to the cPanel license. Unauthorized copying is prohibited.

use cPstrict;
use Test::Cpanel::Policy;

use Test2::V0;
use Test2::Tools::Explain;
use Test2::Tools::Compare;
use Test2::Tools::Exception;
use Test2::Plugin::NoWarnings;

use Test::MockModule;

BEGIN {
    $INC{'Cpanel/Systemd.pm'} = 1;
    package Cpanel::Systemd;
    our @EXPORT_OK = qw(systemctl);
    
    sub systemctl {
        my @args = @_;
        return bless { stdout_text => "ActiveState=active" }, 'MockRun';
    }
    
    package MockRun;
    sub stdout { return shift->{stdout_text}; }
}

use Whostmgr::CometBackup::AgentManager ();

subtest 'AgentManager - get_status success' => sub {
    my $mock_systemd = Test::MockModule->new('Cpanel::Systemd');
    $mock_systemd->redefine( 'systemctl' => sub {
        my @args = @_;
        if ( $args[0] eq 'show' ) {
            return bless { stdout_text => "ActiveState=active" }, 'MockRun';
        }
        return bless { stdout_text => "ActiveState=inactive" }, 'MockRun';
    } );

    my ( $status, $state ) = Whostmgr::CometBackup::AgentManager::get_status();
    
    is( $status, 1, 'Status is success' );
    is( $state, 'active', 'Service is active' );
};

subtest 'AgentManager - get_status inactive' => sub {
    my $mock_systemd = Test::MockModule->new('Cpanel::Systemd');
    $mock_systemd->redefine( 'systemctl' => sub {
        return bless { stdout_text => "ActiveState=inactive" }, 'MockRun';
    } );

    my ( $status, $state ) = Whostmgr::CometBackup::AgentManager::get_status();
    
    is( $status, 1, 'Status is success' );
    is( $state, 'inactive', 'Service is inactive' );
};

subtest 'AgentManager - get_status error' => sub {
    my $mock_systemd = Test::MockModule->new('Cpanel::Systemd');
    $mock_systemd->redefine( 'systemctl' => sub {
        die "systemctl failed";
    } );

    my ( $status, $message ) = Whostmgr::CometBackup::AgentManager::get_status();
    
    is( $status, 0, 'Status indicates failure' );
    like( $message, qr/systemctl command failed/, 'Error message about systemctl' );
};

subtest 'AgentManager - stop success' => sub {
    my $call_count = 0;
    my $mock_systemd = Test::MockModule->new('Cpanel::Systemd');
    $mock_systemd->redefine( 'systemctl' => sub {
        my @args = @_;
        $call_count++;
        if ( $args[0] eq 'show' ) {
            return bless { stdout_text => "ActiveState=inactive" }, 'MockRun';
        }
        return bless { stdout_text => "" }, 'MockRun';
    } );

    my ( $status, $message ) = Whostmgr::CometBackup::AgentManager::stop();
    
    is( $status, 1, 'Stop succeeded' );
    is( $message, 'stopped', 'Correct success message' );
};

subtest 'AgentManager - stop failure - service still running' => sub {
    my $mock_systemd = Test::MockModule->new('Cpanel::Systemd');
    $mock_systemd->redefine( 'systemctl' => sub {
        my @args = @_;
        if ( $args[0] eq 'show' ) {
            return bless { stdout_text => "ActiveState=active" }, 'MockRun';
        }
        return bless { stdout_text => "" }, 'MockRun';
    } );

    my ( $status, $message ) = Whostmgr::CometBackup::AgentManager::stop();
    
    is( $status, 0, 'Stop failed' );
    like( $message, qr/Failed to stop/, 'Error message about stop failure' );
};

subtest 'AgentManager - restart success' => sub {
    my $mock_systemd = Test::MockModule->new('Cpanel::Systemd');
    $mock_systemd->redefine( 'systemctl' => sub {
        my @args = @_;
        if ( $args[0] eq 'show' ) {
            return bless { stdout_text => "ActiveState=active" }, 'MockRun';
        }
        return bless { stdout_text => "" }, 'MockRun';
    } );

    my ( $status, $message ) = Whostmgr::CometBackup::AgentManager::restart();
    
    is( $status, 1, 'Restart succeeded' );
    is( $message, 'restarted', 'Correct success message' );
};

subtest 'AgentManager - restart failure - service not active' => sub {
    my $mock_systemd = Test::MockModule->new('Cpanel::Systemd');
    $mock_systemd->redefine( 'systemctl' => sub {
        my @args = @_;
        if ( $args[0] eq 'show' ) {
            return bless { stdout_text => "ActiveState=inactive" }, 'MockRun';
        }
        return bless { stdout_text => "" }, 'MockRun';
    } );

    my ( $status, $message ) = Whostmgr::CometBackup::AgentManager::restart();
    
    is( $status, 0, 'Restart failed' );
    like( $message, qr/failed to start/, 'Error message about start failure' );
};

subtest 'AgentManager - start calls restart' => sub {
    my $mock_systemd = Test::MockModule->new('Cpanel::Systemd');
    $mock_systemd->redefine( 'systemctl' => sub {
        my @args = @_;
        if ( $args[0] eq 'show' ) {
            return bless { stdout_text => "ActiveState=active" }, 'MockRun';
        }
        return bless { stdout_text => "" }, 'MockRun';
    } );

    my ( $status, $message ) = Whostmgr::CometBackup::AgentManager::start();
    
    is( $status, 1, 'Start succeeded' );
    is( $message, 'started', 'Correct success message' );
};

subtest 'AgentManager - disable success' => sub {
    my $mock_systemd = Test::MockModule->new('Cpanel::Systemd');
    $mock_systemd->redefine( 'systemctl' => sub {
        my @args = @_;
        if ( $args[0] eq 'show' ) {
            return bless { stdout_text => "ActiveState=inactive" }, 'MockRun';
        }
        return bless { stdout_text => "" }, 'MockRun';
    } );

    my ( $status, $state ) = Whostmgr::CometBackup::AgentManager::disable();
    
    is( $status, 1, 'Disable succeeded' );
    is( $state, 'inactive', 'Service is now inactive' );
};

done_testing();
