### ----------------------------------------------------
### -- Collection
### -- Licenses: Artistic-2.0
### -- Authors: Richard Hainsworth, aka finanalyst
### -- File: xt/40-compilation.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 HTML = "{ MODE }/html";

plan 8;
my $rv;

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

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

chdir TMP;
bail-out "{ TEMPLATES } must exit. Have the render tests been run?"
    unless TEMPLATES.IO.d;

# test single file run
# output directory at this point should be empty, so make sure
empty-directory HTML;
$rv = collect(MODE, :no-status, :no-preserve-state, :after<compilation>, :full-render, :with-only<file-c>);
ok "{HTML}/file-c.html".IO.f, 'got file-c';
is HTML.IO.dir(test => / '.html' /).elems, 1, 'only one output file';

# test debug-when, together with the OUTPUT tests
use Test::Output;
output-like {
    $rv = collect(MODE, :no-status, :no-preserve-state, :after<compilation>, :full-render, :debug-when<file-c>)
    },
    / 'Processing first pod declaration in file sources/file-c.rakudoc' /,
    'outputs info about file-c';

#rewrite the config for compiler plugin
(PLUGS ~ '/compiler/frontmatter.raku').IO.spurt( q:to/CODE/);
    sub ($pr, %processed, %options) {
        for <toc footnotes glossary meta> -> $component {
            #%data contains keys for each source and sub-keys for each page component
            # what is required is to make the structure pointed to by raw-component available
            # to a collection structure with sub-keys of filenames
            $pr.add-data("collection-$component",
                    %( |gather for %processed.keys {
                        take $_ => %processed{$_}{"raw-$component"} }) )
        }
    }
    CODE

stdout-like  {
    $rv = collect(MODE, :collection-info, :no-status, :no-preserve-state, :before<compilation>, :full-render, :no-refresh) ;
},
    / 'Starting ｢Render｣ milestone' .+ 'is listed for milestone' .+ 'compilation'/,
    'got collection info';

is-deeply-relaxed $rv[0][1].keys.Set,
        <extra_0_setup1 extra_1_setup_rend file-a file-b file-c file-d>.Set,
        'got all the files in cache';
is-deeply-relaxed $rv[0][0].plugin-datakeys.Set,
        <generation-data image-manager image-mover rend2 setup_rend collection-footnotes collection-toc collection-glossary collection-meta mode-name gather-css asset-mover>.Set,
        'name-spaces set up';
is-deeply-relaxed "{MODE}/html".IO.dir(test => /'.html'/)>>.relative(MODE ~ '/html').Set,
        (<extra_0_setup1 extra_1_setup_rend file-a file-b file-c file-d> >>~>> '.html').Set,
        'got all the files in cache';
isa-ok $rv[0][0].get-data('collection-toc'), Associative , 'plugin data key has content';

done-testing;


