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

#                                      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 => 'Cpanel::Plugins::Components::Rules::PluginEnabled';
use Capture::Tiny;

use Cpanel::Plugins::Components::Rules::PluginEnabled;

my $mock_enabled;
my $mock = Test::MockModule->new('Cpanel::Plugins::State');
$mock->redefine(
    is_enabled => sub {
        return $mock_enabled;
    }
);

subtest 'When a plugin is installed and explicitly enabled' => sub {
    $mock_enabled = !!1;
    my $got = Cpanel::Plugins::Components::Rules::PluginEnabled->new('cpanel.helloworld.plugin')->is_allowed();
    is $got, !!1, "is_allowed() returns true for the plugin";
};

subtest 'When a plugin is installed and explicitly disable' => sub {
    $mock_enabled = !!0;
    my $got = Cpanel::Plugins::Components::Rules::PluginEnabled->new('cpanel.helloworld.plugin')->is_allowed();
    is $got, !!0, "is_allowed() returns true for the plugin";
};

subtest 'When a plugin is installed but we dont know if its company is enabled or disabled' => sub {
    $mock_enabled = '?';
    my $got = Cpanel::Plugins::Components::Rules::PluginEnabled->new('cpanel.helloworld.plugin')->is_allowed();
    is $got, !!1, "is_allowed() returns true since we assume there is something else doing the check to get here";
    note "NOTE: In the long term this is probably not a great assumption, we need a standardized API with the SaaS apps to request if the company is enabled or else we need to control the white/blacklist in cpanel and plesk.";
};

done_testing;
