#!/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.

package t::medium::Cpanel::Plugins::Cache;

use cPstrict;
use Test::Cpanel::Policy;

use Test2::V0;
use Test2::Plugin::NoWarnings;

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

# This test needs to use a module using the cache,
# our unit tests can't load inline Moo roles.
use Cpanel::Plugins::Promotions::koality ();

use base qw(Test::Class);

my $mock_promotions = Test::MockModule->new('Cpanel::Plugins::Promotions::koality');
$mock_promotions->redefine( 'can_show_promotions' => sub { return int rand(10000) } );

my $mock_id = Test::MockModule->new('Cpanel::License::CompanyID');
$mock_id->redefine( 'get_company_id' => sub { return 1 } );

my $mock_stage_file = Test::MockFile->file( '/var/cpanel/use_koality_stage',               '' );
my $cache_file      = Test::MockFile->file( '/var/cpanel/plugins/common/methodcache.json', '{}' );

__PACKAGE__->new()->runtests() if !caller;

sub test_setup_cache : Tests(5) {

    my $obj             = Cpanel::Plugins::Promotions::koality->new();
    my $expected_result = $obj->can_show_promotions();

    for ( 1 .. 5 ) {
        is( $obj->can_show_promotions(), $expected_result, "A method that returns a random value returned a cached result." );
    }

    return;
}

1;
