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

use Cpanel::Plugins::Components::Rules::Tiers;

my $mock_tier   = 'edge';
my $mock_config = Test::MockModule->new("Cpanel::Update::Config");

$mock_config->redefine(
    get_tier => sub {
        return $mock_tier;
    }
);

my $mock_data = {
    'edge'       => '11.128.0.5',
    'current'    => '11.128.0.5',
    'release'    => '11.126.0.16',
    'stable'     => '11.126.0.16',
    '11.128'     => '11.128.0.5',
    '11.128.0'   => '11.128.0.5',
    '11.128.0.5' => '11.128.0.5',
};
my $mock_tiers = Test::MockModule->new("Cpanel::Update::Tiers");
$mock_tiers->redefine(
    get_remote_version_for_tier => sub {
        my ( $self, $version ) = @_;
        return $mock_data->{$version} // '';
    }
);

subtest 'Compare all major tiers when passing one tier.' => sub {
    my $tier = 'release';

    my $got = Cpanel::Plugins::Components::Rules::Tiers->new($tier)->is_allowed();
    is $got, !!0, "got false value when '$tier' is not matching with '$mock_tier'.";

    $tier = $mock_tier;
    $got  = Cpanel::Plugins::Components::Rules::Tiers->new($tier)->is_allowed();
    is $got, !!1, "got true value when '$tier' is matching with '$mock_tier'.";

};
subtest 'Compare all major tiers when passing list of tiers' => sub {
    my $tiers = [ 'edge', 'current', 'release' ];

    my $got = Cpanel::Plugins::Components::Rules::Tiers->new($tiers)->is_allowed();
    is $got, !!1, "got true value when tiers list, $tiers->@*, is having the '$mock_tier'.";

    $tiers = [ 'EDGE', 'CURRENT' ];
    $got   = Cpanel::Plugins::Components::Rules::Tiers->new($tiers)->is_allowed();
    is $got, !!1, "got true value even when upper case tiers list, $tiers->@*, is having the '$mock_tier'.";

    $tiers = [ 'release', 'lts' ];
    $got   = Cpanel::Plugins::Components::Rules::Tiers->new($tiers)->is_allowed();
    is $got, !!0, "got true value when tiers list, $tiers->@*, is not having the '$mock_tier'.";
};

subtest "Run check when tier list is ''" => sub {
    $mock_tier = '';

    my $tiers = [ 'edge', 'release' ];

    my $got = Cpanel::Plugins::Components::Rules::Tiers->new($tiers)->is_allowed();
    is $got, !!0, "got false value when tier list is ''.";
};

subtest 'Compare tiers check when cpupdate.conf has versions in it' => sub {

    # TODO: DUCKS-3060
    # handle cpanel versions check
    # for now any versions check against tier will result in false
    my $tiers = [ 'edge', 'current', 'release' ];

    # currently edge points to 11.128.0.5
    $mock_tier = '11.128.0.5';

    my $got = Cpanel::Plugins::Components::Rules::Tiers->new($tiers)->is_allowed();
    is $got, !!0, "got false value when tiers list, $tiers->@*, is having the matching version '$mock_tier'.";

    $mock_tier = '11.128.0';
    $got       = Cpanel::Plugins::Components::Rules::Tiers->new($tiers)->is_allowed();
    is $got, !!0, "got false value when tiers list, $tiers->@*, is having the matching version '$mock_tier'. ";

    $mock_tier = '11.128';
    $got       = Cpanel::Plugins::Components::Rules::Tiers->new($tiers)->is_allowed();
    is $got, !!0, "got false value when tiers list, $tiers->@*, is having the matching version '$mock_tier'.";

    # making tiers rules with case senstivity
    $tiers     = ['EDGE'];
    $mock_tier = '11.128.0.5';
    $got       = Cpanel::Plugins::Components::Rules::Tiers->new($tiers)->is_allowed();
    is $got, !!0, "got false value with upper case tiers list, $tiers->@*, is having the matching version '$mock_tier'.";

    $tiers = [ 'release', 'lts' ];
    $got   = Cpanel::Plugins::Components::Rules::Tiers->new($tiers)->is_allowed();
    is $got, !!0, "got false value when tiers list, $tiers->@*, is not having the matching version '$mock_tier'.";
};

subtest 'Compare tiers check when cpupdate.conf has unknown tiers in it' => sub {
    my $tiers = [ 'edge', 'current', 'release' ];

    $mock_tier = 'dev';

    my $got = Cpanel::Plugins::Components::Rules::Tiers->new($tiers)->is_allowed();
    is $got, !!0, "got false value when comparing against unknown tiers '$mock_tier' in cpupdate.conf against the tiers list, $tiers->@* .";

    $tiers     = [ 'release', 'lts' ];
    $mock_tier = '11.140.0';
    $got       = Cpanel::Plugins::Components::Rules::Tiers->new($tiers)->is_allowed();
    is $got, !!0, "got false value when comparing against unknown  '$mock_tier' in cpupdate.conf against the tiers list, $tiers->@* .";
};

subtest 'Compare tiers check when rules have incorrect tiers listed' => sub {
    my $tiers = [ 'incorrect', 'wrong', 'tier' ];

    $mock_tier = 'edge';

    my $got = Cpanel::Plugins::Components::Rules::Tiers->new($tiers)->is_allowed();
    is $got, !!0, "got false value when '$mock_tier' is compared against incorrect tiers list, $tiers->@* ";

};
done_testing;
