package scripts::setup_systemd_timer_for_plugins::test;

require 'scripts/setup_systemd_timer_for_plugins';

use Test::MockModule;
use Test::More tests => 5;
use Test::MockFile;
use cPstrict;

ok( scripts::setup_systemd_timer_for_plugins->isa("Cpanel::HelpfulScript"), "ISA HelpfulScript" );

foreach my $sub (qw/add remove/) {
    ok( scripts::setup_systemd_timer_for_plugins->can("$sub"), "Implements $sub" )
}

subtest add => sub () {
    plan tests => 4;

    my $mock = Test::MockModule->new("Cpanel::SafeRun::Object");
    $mock->redefine(
        "new",
        sub ($class, %args) {
            is( $args{program}, '/usr/bin/systemctl', 'executable called' );
            is( $args{args}[0], 'enable', "enable" );
            is( $args{args}[1], 'foobar.timer', "timer name" );
            is( $args{args}[2], '--now', "now" );

            my $obj = +{ exec_failed => 0 };
            bless $obj, 'Cpanel::SafeRun::Object';
            return $obj;
        }
    );
    scripts::setup_systemd_timer_for_plugins::add( undef, 'foobar');
};

subtest remove => sub () {
    plan tests => 8;

    my $mock = Test::MockModule->new("Cpanel::SafeRun::Object");
    $mock->redefine(
        "new",
        sub ($class, %args) {
            is( $args{program}, '/usr/bin/systemctl', 'executable called' );
            is( $args{args}[0], 'disable', "disable" );
            is( $args{args}[1], 'foobar.timer', "timer name" );
            is( $args{args}[2], '--now', "now" );

            # Second call
            $mock->redefine(
                "new",
                sub ($class, %args) {
                    is( $args{program}, '/usr/bin/systemctl', 'executable called' );
                    is( $args{args}[0], 'disable', "disable" );
                    is( $args{args}[1], 'foobar.service', "service name" );
                    is( $args{args}[2], undef, "now" );

                    my $obj = +{ exec_failed => 0 };
                    bless $obj, 'Cpanel::SafeRun::Object';
                    return $obj;
                }
            );

            my $obj = +{ exec_failed => 0 };
            bless $obj, 'Cpanel::SafeRun::Object';
            return $obj;
        }
    );
    scripts::setup_systemd_timer_for_plugins::remove( undef, 'foobar');
};

# Test run/add/remove

