### ----------------------------------------------------
### -- Log::Async
### -- Licenses: Artistic-1.0-Perl
### -- Authors: Brian Duggan
### -- File: t/04-filter.rakutest
### ----------------------------------------------------
use Test;
use lib 'lib';
use Log::Async;

plan 10;

my @last = "my";
my $channel = Channel.new;
sub wait-for-out {
  my $p1 = start $channel.receive;
  my $p2 = Promise.in(5);
  await Promise.anyof($p1,$p2);
  if $p1 {
    @last.push: $p1.result;
  } else {
    diag "timed out waiting for message";
    diag "last contains " ~ @last.raku;
  }
}

logger.add-tap({ $channel.send: $^message<msg> }, :level(TRACE));

trace "name";
wait-for-out;
is @last.tail, 'name', 'got trace message';

debug 'is';
trace "trace1";
wait-for-out;
is @last.tail, 'trace1', 'debug message not sent to trace log';

warning 'Inigo';
trace "trace2";
wait-for-out;
is @last.tail, 'trace2', 'warning message not sent to trace log';

error 'Montoya';
trace "trace3";
wait-for-out;
is @last.tail, 'trace3', 'error message not sent to trace log';

my $debug-or-error = Channel.new;
my $severe = Channel.new;
my $not-severe = Channel.new;
logger.add-tap({ $debug-or-error.send: $^m<msg> }, level => (DEBUG | ERROR) );
logger.add-tap({ $severe        .send: $^m<msg> }, :level(* >= ERROR) );
logger.add-tap({ $not-severe    .send: $^m<msg> }, :level(TRACE..INFO) );
info '1';
trace '2';
debug '3';
error '4';
fatal '5';
error '6';

wait-for-out;
is @last.tail, '2', 'trace messages are still sent';

sub wait-for-channel($channel) {
  my @out;
  react {
    my $count = 0;
    whenever $channel { @out.push: $_; done if ++$count == 3 }
    whenever Promise.in(20) { done }
  }
  @out;
}
is (wait-for-channel $debug-or-error), <3 4 6>, 'filter with junction';
is (wait-for-channel $severe        ), <4 5 6>, 'filter with whatever';
is (wait-for-channel $not-severe    ), <1 2 3>, 'not severe';

logger.add-tap({ $channel.send: $^message<msg> }, :msg(rx/cat/));
debug 'cat alert1';
debug 'dog alog';
error 'cat alert2';
wait-for-out;
is @last.tail, 'cat alert1', 'filtered by msg';
wait-for-out;
is @last.tail, 'cat alert2', 'filtered by msg';



