#!/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::Cpanel::Policy testing => 'Cpanel::Plugins::Components::Rules::IsBeta';
use Capture::Tiny;

use Cpanel::Plugins::Components::Rules::IsBeta;
use Common::YAML;

my $mock_companyid = 7;
my $mock           = Test::MockModule->new("Cpanel::License::CompanyID");
$mock->redefine(
    get_company_id => sub {
        return $mock_companyid;
    }
);

my $mock_logger = Test::MockModule->new('Cpanel::Logger');

subtest 'Check is-beta rule for plugins with valid yaml and is-beta section' => sub {
    my $plugin        = 'sitejet';
    my $config_file   = "/var/cpanel/plugins/$plugin/config.yaml";
    my $mock_contents = <<~"_YAML_";
        is-beta:
        - company-id:
          - 7
          - 11
    _YAML_
    my @warn;
    $mock_logger->redefine( 'warn', sub { shift @_; push @warn, @_ } );
    my $mock_config = Test::MockFile->file( $config_file, $mock_contents );
    my $got         = Cpanel::Plugins::Components::Rules::IsBeta->new($plugin)->is_allowed();
    is $got, !!1, "got true value when passing custom config data for '$plugin' with valid yaml and is-beta section.";

    $mock_companyid = 17;
    $got            = Cpanel::Plugins::Components::Rules::IsBeta->new($plugin)->is_allowed();
    is $got, !!0, "got false value when 'is-beta' check fails to match with valid yaml and is-beta section.";
};

subtest 'Check is-beta rule for plugins with no is-beta section' => sub {
    my $plugin        = 'monitoring';
    my $config_file   = "/var/cpanel/plugins/$plugin/config.yaml";
    my $mock_contents = <<~"_YAML_";
        enable-plugin:
        - company-id:
          - 7
          - 11
    _YAML_
    Common::YAML::clear_cache();
    my @warn;
    $mock_logger->redefine( 'warn', sub { shift @_; push @warn, @_ } );
    my $mock_config = Test::MockFile->file( $config_file, $mock_contents );
    my $got         = Cpanel::Plugins::Components::Rules::IsBeta->new($plugin)->is_allowed();
    is $got, !!0, "got false value when '$plugin' did not have an is-beta section.";
    like( $warn[-1], qr/The \'is-beta\' section is missing in \'$config_file\' for/, 'expected warn log captured' );
};

subtest 'Check is-beta rule for plugins with no config file' => sub {
    my $plugin      = 'xovi';
    my $config_file = "/var/cpanel/plugins/$plugin/config.yaml";
    my $mock_config = Test::MockFile->file($config_file);

    my @warn = ();
    $mock_logger->redefine( 'warn', sub { shift @_; push @warn, @_ } );
    Common::YAML::clear_cache();
    my $got = Cpanel::Plugins::Components::Rules::IsBeta->new($plugin)->is_allowed();
    like( $warn[-1], qr/Could not find the \'$config_file\' for/, 'expected warn log captured' );
    is $got, !!0, "got false value when plugin '$plugin' did not have an yaml.";
};

done_testing;
