#                                      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 Test2::V0;
use Test2::Tools::Explain;
use Test2::Plugin::NoWarnings;
use Test2::Tools::Subtest;
use Test2::Tools::Exception;

use Test::MockModule qw/strict/;
use Test::MockFile   qw/strict/;

use Test::Cpanel::Policy testing => 'Whostmgr::360Monitoring::LifeCycle';

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

subtest 'install' => sub {
    subtest 'when install steps succeed' => sub {
        my $mock = Test::MockModule->new('Common::Plugins::Setup::Steps');
        $mock->redefine( 'run_steps' => sub { return !!1 } );

        my $mockLifeCycle = Test::MockModule->new('Whostmgr::360Monitoring::LifeCycle');
        $mockLifeCycle->redefine('is_setup_already' => sub { return !!0 } );

        my $got = Whostmgr::360Monitoring::LifeCycle::install();
        is $got, 1, 'install() should return 1 when run_steps succeeds';
    };

    subtest 'when install steps fail' => sub {
        my $mock = Test::MockModule->new('Common::Plugins::Setup::Steps');
        $mock->redefine( 'run_steps' => sub { return ( !!0,, ['uhoh'] ) } );

        my $mockLifeCycle = Test::MockModule->new('Whostmgr::360Monitoring::LifeCycle');
        $mockLifeCycle->redefine('is_setup_already' => sub { return !!0 } );

        my $got_exception = dies { Whostmgr::360Monitoring::LifeCycle::install() };
        like $got_exception, qr/Failed to install Server Monitoring \(Powered by 360 Monitoring\): uhoh/, 'install() should die when run_steps fail in some way.';
    };
};

subtest 'uninstall' => sub {
    subtest 'when uninstall steps succeed' => sub {
        my $mock = Test::MockModule->new('Common::Plugins::Setup::Steps');
        $mock->redefine( 'run_steps' => sub { return !!1 } );

        my $mockLifeCycle = Test::MockModule->new('Whostmgr::360Monitoring::LifeCycle');
        $mockLifeCycle->redefine('is_setup_already' => sub { return !!0 } );

        my $got = Whostmgr::360Monitoring::LifeCycle::uninstall();
        is $got, 1, 'uninstall() should return 1 when run_steps succeeds';
    };

    subtest 'when uninstall steps fail' => sub {
        my $mock = Test::MockModule->new('Common::Plugins::Setup::Steps');
        $mock->redefine( 'run_steps' => sub { return ( !!0,, ['uhoh'] ) } );

        my $mockLifeCycle = Test::MockModule->new('Whostmgr::360Monitoring::LifeCycle');
        $mockLifeCycle->redefine('is_setup_already' => sub { return !!0 } );

        my $got_exception = dies { Whostmgr::360Monitoring::LifeCycle::uninstall() };
        like $got_exception, qr/Some of the steps used to uninstall Server Monitoring \(Powered by 360 Monitoring\) failed: uhoh/, 'uninstall() should die when run_steps fail in some way.';
    };
};

done_testing;
