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

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

package Whostmgr::360Monitoring::Agent::Plugins::Fake;

use cPstrict;
use Moo;

extends 'Whostmgr::360Monitoring::Agent::Plugins';

with qw(
  Whostmgr::360Monitoring::Agent::Plugins::Roles::Installable
);

sub is_installable {
    require Cpanel::FeatureFlags;
    return !Cpanel::FeatureFlags::is_feature_enabled('cpanel.agent360.fake_plugin.disabled');
}

sub install              { !!1 }
sub uninstall            { !!1 }
sub is_installed ($self) { !!1 }

package main;

use Test::More;

use experimental 'builtin';

use lib 't/lib';
use Whostmgr::360Monitoring::Agent;
use Whostmgr::360Monitoring::Agent::Test;

use constant {
    MUST_PROVIDE => [
        qw/
          install_and_enable
          uninstall_and_disable
          /
    ],
    PACKAGE_TO_TEST => test_to_package(__FILE__),
};

use_ok(PACKAGE_TO_TEST);
can_ok( PACKAGE_TO_TEST, MUST_PROVIDE->@* );
my $agent  = Whostmgr::360Monitoring::Agent->new();
my $plugin = new_ok( "Whostmgr::360Monitoring::Agent::Plugins::Fake", [ $agent, { enabled => 'yes' } ] );
ok( $plugin->install_and_enable,    "plugin executes install_and_enable()" );
ok( $plugin->uninstall_and_disable, "plugin executes uninstall_and_disable()" );

package Whostmgr::360Monitoring::Agent::Plugins::UnsupportedInstall {
    use cPstrict;
    use Whostmgr::360Monitoring::Moo;
    use Types::Common qw(Bool);
    use Moo;
    use Test::More;

    extends 'Whostmgr::360Monitoring::Agent::Plugins';
    with 'Whostmgr::360Monitoring::Agent::Plugins::Roles::Installable';

    sub is_installed   { !!0 }
    sub is_installable { !!0 }

    my $install   = 0;
    my $uninstall = 0;
    sub install   { $install++ }
    sub uninstall { $uninstall++ }

    my $agent  = Whostmgr::360Monitoring::Agent->new();
    my $plugin = new_ok( __PACKAGE__, [ $agent, { enabled => 'yes' } ] );
    $plugin->install;
    is( $install, 0, '$plugin->install not called' );
}

package Whostmgr::360Monitoring::Agent::Plugins::SupportedInstall {
    use cPstrict;
    use Whostmgr::360Monitoring::Moo;
    use Types::Common qw(Bool);
    use Moo;
    use Test::More;

    extends 'Whostmgr::360Monitoring::Agent::Plugins';
    with 'Whostmgr::360Monitoring::Agent::Plugins::Roles::Installable';

    sub is_installable { !!1 }
    sub is_installed   { !!0 }

    my $install;
    my $uninstall;
    sub install   { $install   = 1; }
    sub uninstall { $uninstall = 1 }

    subtest SupportedInstall => sub () {
        my $agent  = Whostmgr::360Monitoring::Agent->new();
        my $plugin = new_ok( __PACKAGE__, [ $agent, { enabled => 'yes' } ] );
        $plugin->install;
        is( $install, 1, '$plugin->install called' );
    }
}

package Whostmgr::360Monitoring::Agent::Plugins::AlreadyInstalled {
    use cPstrict;
    use Whostmgr::360Monitoring::Moo;
    use Types::Common qw(Bool);
    use Moo;
    use Test::More;

    extends 'Whostmgr::360Monitoring::Agent::Plugins';
    with 'Whostmgr::360Monitoring::Agent::Plugins::Roles::Installable';

    sub is_installed   { !!1 }
    sub is_installable { !!1 }

    my $install   = 0;
    my $uninstall = 0;
    sub install   { $install++ }
    sub uninstall { $uninstall++ }

    subtest AlreadyInstalled => sub () {
        my $agent  = Whostmgr::360Monitoring::Agent->new();
        my $plugin = new_ok( __PACKAGE__, [ $agent, { enabled => 'yes' } ] );
        $plugin->install;
        is( $install, 0, '[->install] not called' );
    };
}

package Whostmgr::360Monitoring::Agent::Plugins::NotInstalled {
    use cPstrict;
    use Whostmgr::360Monitoring::Moo;
    use Types::Common qw(Bool);
    use Moo;
    use Test::More;

    extends 'Whostmgr::360Monitoring::Agent::Plugins';
    with 'Whostmgr::360Monitoring::Agent::Plugins::Roles::Installable';

    sub is_installable { !!1 }
    sub is_installed   { !!0 }

    my $install;
    my $uninstall;
    sub install   { $install   = 1; }
    sub uninstall { $uninstall = 1 }

    subtest NotInstalled => sub () {
        my $agent  = Whostmgr::360Monitoring::Agent->new();
        my $plugin = new_ok( __PACKAGE__, [ $agent, { enabled => 'yes' } ] );
        $plugin->install;
        is( $install, 1, '$plugin->install called' );
    };
}

done_testing();
