### ----------------------------------------------------
### -- Proc::Q
### -- Licenses: Artistic-2.0
### -- Authors: Zoffix Znet
### -- File: t/01-basic.rakutest
### ----------------------------------------------------
use Testo;
use Test::Notice;
use Proc::Q;

plan 29;

my @res;
my @l = 'a'..'z';
my $now = now;
my $first-response;
my $idx = 0;
react whenever proc-q
    @l.map({
        $*EXECUTABLE, '-e',
        "say '$_' ~ \$*IN.slurp; note '$_'; sleep {2*($idx/5).Int}; exit {$idx++}"
    }),
    :tags[@l.map: 'tag' ~ *],
    :in[@l».uc],
    :batch,
    :timeout(3),
    :merge
{
    once $first-response = now - $now;
    @res.push: $_;
}

is +~$first-response, 0..1, 'got first response ASAP';
is +@res, 26, 'got one res object per proc run';
is @res.all, Proc::Q::Res, 'all response objects are of type Proc::Q::Res';

@res .= sort: *.tag;
for @res {
    $_ = .Capture.Hash;
    $_ = .lines.sort.List with .<merged>;
}

my @exp =
    ${:err("a\n"), :exitcode(0), :!killed, :merged($(("a", "aA"))), :out("aA\n"), :tag("taga")},
    ${:err("b\n"), :exitcode(1), :!killed, :merged($(("b", "bB"))), :out("bB\n"), :tag("tagb")},
    ${:err("c\n"), :exitcode(2), :!killed, :merged($(("c", "cC"))), :out("cC\n"), :tag("tagc")},
    ${:err("d\n"), :exitcode(3), :!killed, :merged($(("d", "dD"))), :out("dD\n"), :tag("tagd")},
    ${:err("e\n"), :exitcode(4), :!killed, :merged($(("e", "eE"))), :out("eE\n"), :tag("tage")},
    ${:err("f\n"), :exitcode(5), :!killed, :merged($(("f", "fF"))), :out("fF\n"), :tag("tagf")},
    ${:err("g\n"), :exitcode(6), :!killed, :merged($(("g", "gG"))), :out("gG\n"), :tag("tagg")},
    ${:err("h\n"), :exitcode(7), :!killed, :merged($(("h", "hH"))), :out("hH\n"), :tag("tagh")},
    ${:err("i\n"), :exitcode(8), :!killed, :merged($(("i", "iI"))), :out("iI\n"), :tag("tagi")},
    ${:err("j\n"), :exitcode(9), :!killed, :merged($(("j", "jJ"))), :out("jJ\n"), :tag("tagj")},
    ${:err("k\n"), :exitcode(0), :killed, :merged($(("k", "kK"))), :out("kK\n"), :tag("tagk")},
    ${:err("l\n"), :exitcode(0), :killed, :merged($(("l", "lL"))), :out("lL\n"), :tag("tagl")},
    ${:err("m\n"), :exitcode(0), :killed, :merged($(("m", "mM"))), :out("mM\n"), :tag("tagm")},
    ${:err("n\n"), :exitcode(0), :killed, :merged($(("n", "nN"))), :out("nN\n"), :tag("tagn")},
    ${:err("o\n"), :exitcode(0), :killed, :merged($(("o", "oO"))), :out("oO\n"), :tag("tago")},
    ${:err("p\n"), :exitcode(0), :killed, :merged($(("p", "pP"))), :out("pP\n"), :tag("tagp")},
    ${:err("q\n"), :exitcode(0), :killed, :merged($(("q", "qQ"))), :out("qQ\n"), :tag("tagq")},
    ${:err("r\n"), :exitcode(0), :killed, :merged($(("r", "rR"))), :out("rR\n"), :tag("tagr")},
    ${:err("s\n"), :exitcode(0), :killed, :merged($(("s", "sS"))), :out("sS\n"), :tag("tags")},
    ${:err("t\n"), :exitcode(0), :killed, :merged($(("t", "tT"))), :out("tT\n"), :tag("tagt")},
    ${:err("u\n"), :exitcode(0), :killed, :merged($(("u", "uU"))), :out("uU\n"), :tag("tagu")},
    ${:err("v\n"), :exitcode(0), :killed, :merged($(("v", "vV"))), :out("vV\n"), :tag("tagv")},
    ${:err("w\n"), :exitcode(0), :killed, :merged($(("w", "wW"))), :out("wW\n"), :tag("tagw")},
    ${:err("x\n"), :exitcode(0), :killed, :merged($(("x", "xX"))), :out("xX\n"), :tag("tagx")},
    ${:err("y\n"), :exitcode(0), :killed, :merged($(("y", "yY"))), :out("yY\n"), :tag("tagy")},
    ${:err("z\n"), :exitcode(0), :killed, :merged($(("z", "zZ"))), :out("zZ\n"), :tag("tagz")};

notice 'These tests are a bit wobbly, especially on slow boxes. If testing fails, try to re-try.';
for @res.keys {
    # Possible bug in Rakudo; merged key sometimes is missing one of the streams
    @res[$_]<merged>:delete;
    @exp[$_]<merged>:delete;
    is-eqv @res[$_], @exp[$_], "result $_";
}

# vim: expandtab shiftwidth=4


