#!/usr/local/cpanel/3rdparty/bin/perl -w
#
# cpanel - t/medium/Cpanel-Plugins-ServerId.t   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::MockAdmin;

use Test::Cpanel::Policy testing => 'Cpanel::Plugins::ServerId';
use Capture::Tiny;

use Cpanel::Plugins::ServerId;
use Cpanel::Admin::Modules::Cpanel::server_id;
use Common::Plugins::ServerInfo;

my $mock_ips = Test::MockModule->new('Cpanel::DIp::MainIP');
$mock_ips->redefine(
    getpublicmainserverip => sub {
        return "42.42.42.42";
    }
);

my $server_uuid    = '4242424242424242424242424242424242424242424242424242424242424242';
my $mock_uuid_file = Test::MockFile->file( Common::Plugins::ServerInfo::UUID_FILE(), $server_uuid );
my $expect         = '4242424242424242424242424242424242424242424242424242424242424242-42.42.42.42';

my $madmin = Test::MockAdmin->new();
my @mock   = $madmin->create_mock( 'server_id', 'GET_SERVER_UUID', sub { return $server_uuid } );

subtest 'get_server_id' => sub {
    local $> = 1;
    my $got = Cpanel::Plugins::ServerId::get_server_id();
    is( $got, $expect, "Got the expected server id" );
};

subtest 'get_server_id_as_root' => sub {
    local $> = 0;
    my $got = Cpanel::Plugins::ServerId::get_server_id();
    is( $got, $expect, "Got the expected server id" );
};
done_testing;
