### ----------------------------------------------------
### -- Collection
### -- Licenses: Artistic-2.0
### -- Authors: Richard Hainsworth, aka finanalyst
### -- File: xt/50-report-completion.rakutest
### ----------------------------------------------------
use Test;
use Test::Deeply::Relaxed;
use File::Directory::Tree;

use Collection;
constant TMP = 'xt/test-dir';
constant MODE = 'test-mode';
constant PLUGS = "{MODE}/plugins";
constant CONFS = "{MODE}/configs";
constant TEMPLATES = "{MODE}/templates";
constant OUTPUT = "{MODE}/html";
constant REPORT = "{MODE}/reports";
constant ASSETS = "{TMP}/assets";

plan 9;
my $rv;

bail-out "{TMP} and {TMP}/{ MODE } and {TMP}/{ PLUGS } must exist. Have source tests been run?"
unless TMP.IO.d and "{TMP}/{ MODE }".IO.d and "{TMP}/{PLUGS}".IO.d;

#my @pls = <setup1 setup_rend rend2 reporter compiler completer unused>;

chdir TMP;

# rewrite the plugins for reporter and completer

(PLUGS ~ '/reporter/config.raku').IO.spurt( q:to/CONF/);
    %(
        :report<link-test.raku>
    )
    CONF
(PLUGS ~ '/reporter/link-test.raku').IO.spurt( q:to/CODE/);
    sub (%processed, @plugins-used, $pp, %options --> Array ) {
        ## format is %.links{$entry}<target location>
        my @report = 'Link report', ;
        for %processed.kv -> $fn, $podf {
            next unless $podf.links and +$podf.links.keys;
            @report.append: "$fn contains links";
            for $podf.links.kv -> $entry, (:$target, :$place, :$link-label, :$type) {
                @report.append: "\t$link-label of type $type points to $place in target $target"
            }
        }
        my @plugs = "Plugin report", ;
        for @plugins-used {
            @plugs.append: "Plugins used at ｢{ .key }｣ milestone:";
            for .value.kv -> $plug, %params {
                @plugs.append: "\t｢$plug｣ called with: ", %params.gist;
            }
        }
        my @templates = "Templates report", ;
        for %processed.kv -> $fn, $podf {
            next unless $podf.templates-used;
            @templates.append("$fn used\n" ~ $podf.templates-used.raku);
        }
        [ 'link-report.txt' => @report.join("\n"),
        'plugin-report.txt' => @plugs.join("\n"),
        'template-report.txt' => @templates.join("\n")
        ]
    }
    CODE

(PLUGS ~ '/completer/config.raku').IO.spurt( q:to/CONF/ );
    %(
        :completion<run-app.raku>
    )
    CONF
(PLUGS ~ '/completer/run-app.raku').IO.spurt( q:to/CODE/);
    use Test;
    sub ($destination, $landing, $ext, %plugin-options, %options) {
        ok $destination.IO.d, 'destination is a directory';
        ok "$destination/$landing\.$ext".IO.f, 'landing is a file';
        is %plugin-options<completer><host>, 'localhost', 'got plugin option';
    }
    CODE
$rv = collect(MODE, :no-status,
        :after<report>, :full-render, :no-refresh, :no-preserve-state, :without-report );

my $rpt = REPORT ~ '/link-report.txt';
nok $rpt.IO.f, 'a report file is not generated';
#empty-directory REPORT;

$rv = collect(MODE, :no-status,
        :after<report>, :full-render, :no-preserve-state, :no-refresh, :!without-report );
ok $rpt.IO.f, 'a report file is generated';
like $rpt.IO.slurp,
        /
        'Link report'
        .+ setup_rend .+ 'external' .+ 'nowhere.net.nul'
        /, 'link report ok';
like (REPORT ~ '/plugin-report.txt').IO.slurp,
       / 'Plugin report'
        .+ '｢Compilation｣ milestone'
        .+ 'compiler'
       /, 'plugin report ok';
like (REPORT ~ '/template-report.txt').IO.slurp,
        / 'Templates report'
        .+ 'file-c'
        /, 'template report contains expected info';

# should call three tests
collect(MODE, :no-status,
        :after<completion>, :full-render, :no-preserve-state, :no-refresh );

# now try a reporter with a Nil return
(PLUGS ~ '/reporter/link-test.raku').IO.spurt( q:to/CODE/);
    sub (%processed, @plugins-used, $pp, %options, --> Array ) {
        #only side effects
        []
        }
    CODE
lives-ok { collect(MODE, :no-status, :no-preserve-state, :after<report>, :full-render, :no-refresh) },
        'allows a Nil return';

done-testing;


