#!/usr/local/cpanel/3rdparty/bin/perl

#                                      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 Test::Cpanel::Policy;

use Test2::V0;
use Test2::Tools::Explain;
use Test2::Tools::Compare;
use Test2::Tools::Exception;
use Test2::Plugin::NoWarnings;

use Test::MockModule;
use Test::MockFile;

BEGIN {
    $INC{'YAML/XS.pm'} = 1;
    package YAML::XS;
    our $LOADED_FILE;
    sub LoadFile {
        my ($file) = @_;
        $LOADED_FILE = $file;
        return { test_key => 'test_value', loaded_file => $file };
    }
}

use Whostmgr::CometBackup::Config ();

subtest 'Config - load returns hash' => sub {
    Whostmgr::CometBackup::Config::clear_cache();
    
    my $config = Whostmgr::CometBackup::Config::load();
    
    ok( defined $config, 'Config load returns defined value' );
    is( ref $config, 'HASH', 'Config is a hash reference' );
};

subtest 'Config - load caches configuration' => sub {
    Whostmgr::CometBackup::Config::clear_cache();
    
    my $config1 = Whostmgr::CometBackup::Config::load();
    my $config2 = Whostmgr::CometBackup::Config::load();
    
    is( $config1, $config2, 'Second load returns cached configuration' );
};

subtest 'Config - clear_cache resets configuration' => sub {
    my $mock_yaml = Test::MockModule->new('YAML::XS');
    my $call_count = 0;
    $mock_yaml->redefine( 'LoadFile' => sub {
        $call_count++;
        return { call_number => $call_count };
    } );
    
    Whostmgr::CometBackup::Config::clear_cache();
    my $config1 = Whostmgr::CometBackup::Config::load();
    is( $config1->{call_number}, 1, 'First load has call_number 1' );
    
    Whostmgr::CometBackup::Config::clear_cache();
    my $config2 = Whostmgr::CometBackup::Config::load();
    is( $config2->{call_number}, 2, 'After clear_cache, second load has call_number 2' );
};

subtest 'Config - load with mock YAML file' => sub {
    my $mock_yaml = Test::MockModule->new('YAML::XS');
    $mock_yaml->redefine( 'LoadFile' => sub {
        return {
            backup_enabled => 1,
            max_concurrent_jobs => 5,
        };
    } );
    
    Whostmgr::CometBackup::Config::clear_cache();
    my $config = Whostmgr::CometBackup::Config::load();
    
    is( $config->{backup_enabled}, 1, 'Config contains expected key' );
    is( $config->{max_concurrent_jobs}, 5, 'Config contains expected value' );
};

subtest 'Config - load file path' => sub {
    my $loaded_file = undef;
    my $mock_yaml = Test::MockModule->new('YAML::XS');
    $mock_yaml->redefine( 'LoadFile' => sub {
        $loaded_file = $_[0];
        return {};
    } );
    
    Whostmgr::CometBackup::Config::clear_cache();
    Whostmgr::CometBackup::Config::load();
    
    is( $loaded_file, '/var/cpanel/plugins/comet-backup/config.yaml', 'Loads from correct path' );
};

done_testing();
