### ----------------------------------------------------
### -- Pakku
### -- Licenses: Artistic-2.0
### -- Authors: Haytham Elganiny
### -- File: t/config.rakutest
### ----------------------------------------------------
#!/usr/bin/env raku

use Test;

use Pakku::Log;
use Pakku::Command::Config;

my $config-file = $*TMPDIR.add( 'pakku-config.json.' ~ now.Num );

my class TestConfig does Pakku::Command::Config {
  has %!cnf;
	method !cnf ( ) { %!cnf }

	submethod TWEAK ( ) { %!cnf<pakku><config> = $config-file } 
} 

Pakku::Log.new: verbose => 'nothing';

my $config = TestConfig.new;

lives-ok { $config.fly( 'config', :operation<new> ) }, 'create new config';

my Pair @option = :async, :dont;
lives-ok { $config.fly( 'config', :module<pakku>, :@option ) }, 'enable option';
is-deeply from-json(slurp $config-file )<pakku><async dont>, (True, True), 'confirm enable option in config file';

@option = :!test;
lives-ok { $config.fly( 'config', :module<add>, :@option ) }, 'disable option';
ok from-json(slurp $config-file )<add><test> === False, 'confirm disable option in config file';

@option = :!deps;
lives-ok { $config.fly( 'config', :module<add>, :@option ) }, 'disable Bool & Str option';
ok from-json(slurp $config-file )<add><deps> === False , 'confirm disable Bool & Str option in config file';

@option = deps => 'test';
lives-ok { $config.fly( 'config', :module<add>, :@option ) }, 'set Bool & Str option';
ok from-json(slurp $config-file )<add><deps> ~~ 'test' , 'confirm set Bool & Str option in config file';

@option = to => 'home';
lives-ok { $config.fly( 'config', :module<add>, :@option ) }, 'set option';
ok from-json(slurp $config-file )<add><to> ~~ 'home', 'confirm set option in config file';

@option = to => Nil;
lives-ok { $config.fly( 'config', :module<add>, :@option ) }, 'unset option';
is-deeply from-json(slurp $config-file )<add><to>, Any, 'confirm unset option in config file';

@option = count => '42';
lives-ok { $config.fly( 'config', :module<search>, :@option ) }, 'set Int option';
ok from-json(slurp $config-file )<search><count> ~~ 42, 'confirm set Int option in config file';

@option = exclude => ["Dep1", "Dep2"];
lives-ok { $config.fly( 'config', :module<add>, :@option ) }, 'set Array option';
is-deeply from-json(slurp $config-file )<add><exclude>, ["Dep1", "Dep2"], 'confirm set Array option in config file';

my $recman-name = 'my-recman';
@option = priority => 7, location => 'http://my-recman.com';
lives-ok { $config.fly( 'config', :module<recman>, :$recman-name, :@option ) }, 'set new recman';

lives-ok { $config.fly( 'config', :module<recman>, :operation<disable>, :$recman-name ) }, 'recman disable';
lives-ok { $config.fly( 'config', :module<recman>, :operation<enable>,  :$recman-name ) }, 'recman enable';
is-deeply from-json(slurp $config-file )<recman>.first( *.<name> eq $recman-name )<active>, True, 'confirm recman active in config file';

@option = priority => 42;
lives-ok { $config.fly( 'config', :module<recman>, :$recman-name, :@option ) }, 'set recman option';
is-deeply from-json(slurp $config-file )<recman>.first( *.<name> eq $recman-name )<priority>, 42, 'confirm set recman option in config file';
@option = priority => Nil;
lives-ok { $config.fly( 'config', :module<recman>, :$recman-name, :@option ) }, 'unset recman option';
is-deeply from-json(slurp $config-file )<recman>.first( *.<name> eq $recman-name )<priority>, Any, 'confirm unset recman option in config file';

lives-ok { $config.fly( 'config', :module<recman>, :operation<unset>, :$recman-name ) }, 'unset recman name';
quietly is-deeply from-json(slurp $config-file )<recman>.first(  *.<name> eq $recman-name ), Nil, 'confirm unset recman name in config file';

lives-ok { $config.fly( 'config', :module<recman>, :operation<unset> ) }, 'unset recman';
is-deeply from-json(slurp $config-file )<recman>, Any, 'confirm unset recman in config file';

my $log-level = 'debug';
@option = prefix => 'DEBUG: ', color => 'blue';
lives-ok { $config.fly( 'config', :module<log>, :$log-level, :@option ) }, 'set new log level';

@option = color => 'yellow';
lives-ok { $config.fly( 'config', :module<log>, :$log-level, :@option ) }, 'set log level option';
is-deeply from-json(slurp $config-file )<log>{ $log-level }<color>, 'yellow', 'confirm set log level option in config file';

@option = color => Nil;
lives-ok { $config.fly( 'config', :module<log>, :$log-level, :@option ) }, 'unset log level option';
is-deeply from-json(slurp $config-file )<log>{ $log-level }<color>, Any, 'confirm unset log level option in config file';

lives-ok { $config.fly( 'config', :module<log>, :operation<unset>, :$log-level ) }, 'unset log level';
quietly is-deeply from-json(slurp $config-file )<log>{ $log-level }, Any, 'confirm unset log level in config file';

lives-ok { $config.fly( 'config', :module<pakku>, :operation<reset> ) }, 'reset config module';

lives-ok { $config.fly( 'config', :operation<reset> ) }, 'reset config';

unlink $config-file;

done-testing;

sub from-json ( Str:D  $json --> Hash:D ) { Rakudo::Internals::JSON.from-json: $json; }
sub to-json ( \obj --> Str:D ) { Rakudo::Internals::JSON.to-json: obj; }


