#!/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::MockFile   qw/strict/;

use Test::Cpanel::Policy testing => 'Cpanel::Xovi::LifeCycle';

use Cpanel::Plugin::Install ();
use Cpanel::Xovi::LifeCycle ();

my $mockfile = Test::MockFile->file( Cpanel::Plugin::Install::_addon_feature_directory() . '/xovi' );

subtest 'install' => sub {
    subtest 'when install steps succeed' => sub {
        my $mock = Test::MockModule->new('Common::Plugins::Setup::Steps');
        $mock->redefine( 'run_steps' => sub { return !!1 } );

        my $got = Cpanel::Xovi::LifeCycle::install();
        is $got, 1, 'install() should return 1 when run_steps succeeds';
    };

    subtest 'when install steps fail' => sub {
        my $mock = Test::MockModule->new('Common::Plugins::Setup::Steps');
        $mock->redefine( 'run_steps' => sub { return ( !!0, ['uhoh'] ) } );

        my $got_exception = dies { Cpanel::Xovi::LifeCycle::install() };
        like $got_exception,
          qr/Failed to install SEO \(by XOVI\): uhoh/,
          'install() should die when run_steps fail in some way.';
    };
};

subtest 'uninstall' => sub {
    subtest 'when uninstall steps succeed' => sub {
        my $mock = Test::MockModule->new('Common::Plugins::Setup::Steps');
        $mock->redefine( 'run_steps' => sub { return !!1 } );

        my $got = Cpanel::Xovi::LifeCycle::uninstall();
        is $got, 1, 'uninstall() should return 1 when run_steps succeeds';
    };

    subtest 'when uninstall steps fail' => sub {
        my $mock = Test::MockModule->new('Common::Plugins::Setup::Steps');
        $mock->redefine( 'run_steps' => sub { return ( !!0,, ['uhoh'] ) } );

        my $got_exception = dies { Cpanel::Xovi::LifeCycle::uninstall() };
        like $got_exception,
          qr/Failed to uninstall SEO \(by XOVI\): uhoh/,
          'uninstall() should die when run_steps fail in some way.';
    };
};

done_testing;
