#!/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::FeatureEnabled';
use Capture::Tiny;

use Cpanel::Plugins::Components::Rules::FeatureEnabled;

my $mock_enabled;
my $mock = Test::MockModule->new('Cpanel::Features::Check');
$mock->redefine(
    check_feature_for_user => sub {
        return $mock_enabled;
    }
);

my $mock_feature_file = Test::MockFile->file( '/usr/local/cpanel/whostmgr/addonfeatures/xovi', '' );

subtest 'When a feature is enabled' => sub {
    $mock_enabled = 1;
    my $got = Cpanel::Plugins::Components::Rules::FeatureEnabled->new('xovi')->is_allowed();
    is $got, 1, "is_allowed() returns true for the plugin";
};

subtest 'When a feature is disabled' => sub {
    $mock_enabled = 0;
    my $got = Cpanel::Plugins::Components::Rules::FeatureEnabled->new('xovi')->is_allowed();
    is $got, 0, "is_allowed() returns false for the plugin";
};

subtest 'When the plugin is uninstalled' => sub {
    $mock_feature_file = '';
    $mock_feature_file = Test::MockFile->file('/usr/local/cpanel/whostmgr/addonfeatures/xovi');
    $mock_enabled      = 1;
    my $got = Cpanel::Plugins::Components::Rules::FeatureEnabled->new('xovi')->is_allowed();
    is $got, 0, "is_allowed() returns false when the plugin is uninstalled";
};

done_testing;
