#!/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::CompareVersion';

use Cpanel::Plugins::Components::Rules::CompareVersion ();

my $mock_version;
my $mock = Test::MockModule->new('Cpanel::Version');
$mock->redefine( 'getversionnumber' => sub { return $mock_version } );

subtest 'equal' => sub {

    $mock_version = '11.110';
    my $got = Cpanel::Plugins::Components::Rules::CompareVersion->new(operator => '==', version => '11.110')->is_allowed();
    is $got, !!1, 'When version is 110 and we are comparing for equality to 110 the check returned true.';

    $mock_version = '11.118';
    $got          = Cpanel::Plugins::Components::Rules::CompareVersion->new(operator => '==', version => '11.110')->is_allowed();
    is $got, !!0, 'When version is 118 and we are comparing for equality to 110 the check returned false.';

    $mock_version = '11.110';
    $got          = Cpanel::Plugins::Components::Rules::CompareVersion->new(operator => '==', version => '11.118')->is_allowed();
    is $got, !!0, 'When version is 110 and we are comparing for equality to 110 the check returned false.';
};

subtest 'lessthan' => sub {

    $mock_version = '11.108';
    my $got = Cpanel::Plugins::Components::Rules::CompareVersion->new(operator => '<', version => '11.110')->is_allowed();
    is $got, !!1, 'When version is 108 and we are comparing if the version is less than 110 the check returned true.';

    $mock_version = '11.118';
    $got          = Cpanel::Plugins::Components::Rules::CompareVersion->new(operator => '<', version => '11.110')->is_allowed();
    is $got, !!0, 'When version is 118 and we are comparing if the version is less than 110 the check returned false.';
};

subtest 'lessthan or equal' => sub {

    $mock_version = '11.108';
    my $got = Cpanel::Plugins::Components::Rules::CompareVersion->new(operator => '<=', version => '11.110')->is_allowed();
    is $got, !!1, 'When version is 108 and we are comparing if the version is less than or equal to 110 the check returned true.';

    $mock_version = '11.110';
    $got = Cpanel::Plugins::Components::Rules::CompareVersion->new(operator => '<=', version => '11.110')->is_allowed();
    is $got, !!1, 'When version is 110 and we are comparing if the version is less than or equal to 110 the check returned true.';

    $mock_version = '11.118';
    $got          = Cpanel::Plugins::Components::Rules::CompareVersion->new(operator => '<=', version => '11.110')->is_allowed();
    is $got, !!0, 'When version is 118 and we are comparing if the version is less than or equal to 110 the check returned false.';
};

subtest 'greaterthan' => sub {

    $mock_version = '11.126';
    my $got = Cpanel::Plugins::Components::Rules::CompareVersion->new(operator => '>', version => '11.110')->is_allowed();
    is $got, !!1, 'When version is 126 and we are comparing if the version is greater than 110 the check returned true.';

    $mock_version = '11.126';
    $got          = Cpanel::Plugins::Components::Rules::CompareVersion->new(operator => '>', version => '11.126')->is_allowed();
    is $got, !!0, 'When version is 126 and we are comparing if the version is greater than 126 the check returned false.';
};

subtest 'greaterthan or equal' => sub {

    $mock_version = '11.126';
    my $got = Cpanel::Plugins::Components::Rules::CompareVersion->new(operator => '>=', version => '11.110')->is_allowed();
    is $got, !!1, 'When version is 126 and we are comparing if the version is greater than or equal to 110 the check returned true.';

    $mock_version = '11.126';
    $got          = Cpanel::Plugins::Components::Rules::CompareVersion->new(operator => '>=', version => '11.126')->is_allowed();
    is $got, !!1, 'When version is 126 and we are comparing if the version is greater than or equal to 126 the check returned true.';

    $mock_version = '11.110';
    $got          = Cpanel::Plugins::Components::Rules::CompareVersion->new(operator => '>=', version => '11.126')->is_allowed();
    is $got, !!0, 'When version is 110 and we are comparing if the version is greater than or equal to 126 the check returned false.';
};

subtest 'not equal' => sub {

    $mock_version = '11.110';
    my $got = Cpanel::Plugins::Components::Rules::CompareVersion->new(operator => '!=', version => '11.118')->is_allowed();
    is $got, !!1, 'When version is 110 and we are comparing if the version is not equal to 118 the check returned true.';

    $mock_version = '11.110';
    $got          = Cpanel::Plugins::Components::Rules::CompareVersion->new(operator => '!=', version => '11.110')->is_allowed();
    is $got, !!0, 'When version is 110 and we are comparing if the version is not equal to 110 the check returned false.';
};

done_testing;
