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

use Cpanel::Plugins::Components::Rules::Distributions;

my $mock_distro = 'cloudlinux';
my $mock_major  = 7;
my $mock_minor  = 9;
my $mock        = Test::MockModule->new("Cpanel::OS");
$mock->redefine(
    distro => sub {
        return $mock_distro;
    },
    major => sub {
        return $mock_major;
    },
    minor => sub {
        return $mock_minor;
    },
);

subtest 'Check distro when passing one distribution data' => sub {
    my $phrase = 'centos';
    my $got    = Cpanel::Plugins::Components::Rules::Distributions->new($phrase)->is_allowed();
    is $got, !!0, "got false value when distro $phrase is the NOT the current distro $mock_distro.";

    $phrase = 'cloudlinux';
    $got    = Cpanel::Plugins::Components::Rules::Distributions->new($phrase)->is_allowed();
    is $got, !!1, "got true value when distro $phrase is the current distro  $mock_distro.";

    $phrase = 'cloudlinux-7';
    $got    = Cpanel::Plugins::Components::Rules::Distributions->new($phrase)->is_allowed();
    is $got, !!1, "got true value when distro $mock_distro & $mock_major alone matches $phrase.";

    $phrase = 'cloudlinux-7.9';
    $got    = Cpanel::Plugins::Components::Rules::Distributions->new($phrase)->is_allowed();
    is $got, !!1, "got true value when distro $phrase matches $mock_distro, $mock_major & $mock_minor.";

    $phrase = 'cloudlinux-6.9';
    $got    = Cpanel::Plugins::Components::Rules::Distributions->new($phrase)->is_allowed();
    is $got, !!0, "got false value when distro $phrase does not match $mock_distro, $mock_major & $mock_minor.";
};

subtest 'Check distro  when passing list of distro data' => sub {
    my $phrase = [ 'almalinux-8.1', 'ubuntu-2.1', 'centos-5' ];
    my $got    = Cpanel::Plugins::Components::Rules::Distributions->new($phrase)->is_allowed();
    is $got, !!0, "got false value when distro list, $phrase->@*, is the NOT the expected $mock_distro, $mock_major & $mock_minor.";

    $phrase = [ 'centos-7', 'cloudlinux-6.1', 'cloudlinux-7.9' ];
    $got    = Cpanel::Plugins::Components::Rules::Distributions->new($phrase)->is_allowed();
    is $got, !!1, "got true value when distro list, $phrase->@*, is the expected $mock_distro, $mock_major & $mock_minor.";
};

subtest "Run check when disro is ''" => sub {
    $mock_distro = '';

    my $phrase = [ 'cloudlinux-7', 'cloudlinux-6', 'centos-7' ];

    my $got = Cpanel::Plugins::Components::Rules::Distributions->new($phrase)->is_allowed();
    is $got, !!0, "got false value when distro data is ''.";
};

done_testing;
